Move the tablegen-produced DebugLoc handling into a AsmWriter::processDebugLoc function.
[oota-llvm.git] / include / llvm / CodeGen / AsmPrinter.h
1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a class to be used as the base class for target specific
11 // asm writers.  This class primarily handles common functionality used by
12 // all asm writers.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include <set>
24
25 namespace llvm {
26   class GCStrategy;
27   class Constant;
28   class ConstantArray;
29   class ConstantInt;
30   class ConstantStruct;
31   class ConstantVector;
32   class GCMetadataPrinter;
33   class GlobalVariable;
34   class MachineConstantPoolEntry;
35   class MachineConstantPoolValue;
36   class DwarfWriter;
37   class Mangler;
38   class Section;
39   class TargetAsmInfo;
40   class Type;
41   class raw_ostream;
42
43   /// AsmPrinter - This class is intended to be used as a driving class for all
44   /// asm writers.
45   class AsmPrinter : public MachineFunctionPass {
46     static char ID;
47
48     /// FunctionNumber - This provides a unique ID for each function emitted in
49     /// this translation unit.  It is autoincremented by SetupMachineFunction,
50     /// and can be accessed with getFunctionNumber() and 
51     /// IncrementFunctionNumber().
52     ///
53     unsigned FunctionNumber;
54
55     // GCMetadataPrinters - The garbage collection metadata printer table.
56     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
57     typedef gcp_map_type::iterator gcp_iterator;
58     gcp_map_type GCMetadataPrinters;
59     
60   protected:
61     /// DW -This is needed because printDeclare() has to insert
62     /// DbgVariable entries into the dwarf table. This is a short term hack
63     /// that ought be fixed soon.
64     DwarfWriter *DW;
65     
66     // Necessary for external weak linkage support
67     std::set<const GlobalValue*> ExtWeakSymbols;
68
69     /// OptLevel - Generating code at a specific optimization level.
70     CodeGenOpt::Level OptLevel;
71   public:
72     /// Output stream on which we're printing assembly code.
73     ///
74     raw_ostream &O;
75
76     /// Target machine description.
77     ///
78     TargetMachine &TM;
79     
80     /// Target Asm Printer information.
81     ///
82     const TargetAsmInfo *TAI;
83
84     /// Target Register Information.
85     ///
86     const TargetRegisterInfo *TRI;
87
88     /// The current machine function.
89     const MachineFunction *MF;
90
91     /// Name-mangler for global names.
92     ///
93     Mangler *Mang;
94
95     /// Cache of mangled name for current function. This is recalculated at the
96     /// beginning of each call to runOnMachineFunction().
97     ///
98     std::string CurrentFnName;
99     
100     /// CurrentSection - The current section we are emitting to.  This is
101     /// controlled and used by the SwitchSection method.
102     std::string CurrentSection;
103     const Section* CurrentSection_;
104
105     /// IsInTextSection - True if the current section we are emitting to is a
106     /// text section.
107     bool IsInTextSection;
108
109     /// VerboseAsm - Emit comments in assembly output if this is true.
110     ///
111     bool VerboseAsm;
112
113   protected:
114     explicit AsmPrinter(raw_ostream &o, TargetMachine &TM,
115                         const TargetAsmInfo *T, CodeGenOpt::Level OL, bool V);
116     
117   public:
118     virtual ~AsmPrinter();
119
120     /// isVerbose - Return true if assembly output should contain comments.
121     ///
122     bool isVerbose() const { return VerboseAsm; }
123
124     /// SwitchToTextSection - Switch to the specified section of the executable
125     /// if we are not already in it!  If GV is non-null and if the global has an
126     /// explicitly requested section, we switch to the section indicated for the
127     /// global instead of NewSection.
128     ///
129     /// If the new section is an empty string, this method forgets what the
130     /// current section is, but does not emit a .section directive.
131     ///
132     /// This method is used when about to emit executable code.
133     ///
134     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
135
136     /// SwitchToDataSection - Switch to the specified section of the executable
137     /// if we are not already in it!  If GV is non-null and if the global has an
138     /// explicitly requested section, we switch to the section indicated for the
139     /// global instead of NewSection.
140     ///
141     /// If the new section is an empty string, this method forgets what the
142     /// current section is, but does not emit a .section directive.
143     ///
144     /// This method is used when about to emit data.  For most assemblers, this
145     /// is the same as the SwitchToTextSection method, but not all assemblers
146     /// are the same.
147     ///
148     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
149
150     /// SwitchToSection - Switch to the specified section of the executable if
151     /// we are not already in it!
152     void SwitchToSection(const Section* NS);
153
154     /// getGlobalLinkName - Returns the asm/link name of of the specified
155     /// global variable.  Should be overridden by each target asm printer to
156     /// generate the appropriate value.
157     virtual const std::string &getGlobalLinkName(const GlobalVariable *GV,
158                                                  std::string &LinkName) const;
159
160     /// EmitExternalGlobal - Emit the external reference to a global variable.
161     /// Should be overridden if an indirect reference should be used.
162     virtual void EmitExternalGlobal(const GlobalVariable *GV);
163
164     /// getCurrentFunctionEHName - Called to return (and cache) the
165     /// CurrentFnEHName.
166     /// 
167     const std::string &getCurrentFunctionEHName(const MachineFunction *MF,
168                                                 std::string &FuncEHName) const;
169
170   protected:
171     /// getAnalysisUsage - Record analysis usage.
172     /// 
173     void getAnalysisUsage(AnalysisUsage &AU) const;
174     
175     /// doInitialization - Set up the AsmPrinter when we are working on a new
176     /// module.  If your pass overrides this, it must make sure to explicitly
177     /// call this implementation.
178     bool doInitialization(Module &M);
179
180     /// doFinalization - Shut down the asmprinter.  If you override this in your
181     /// pass, you must make sure to call it explicitly.
182     bool doFinalization(Module &M);
183     
184     /// PrintSpecial - Print information related to the specified machine instr
185     /// that is independent of the operand, and may be independent of the instr
186     /// itself.  This can be useful for portably encoding the comment character
187     /// or other bits of target-specific knowledge into the asmstrings.  The
188     /// syntax used is ${:comment}.  Targets can override this to add support
189     /// for their own strange codes.
190     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
191
192     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
193     /// instruction, using the specified assembler variant.  Targets should
194     /// override this to format as appropriate.  This method can return true if
195     /// the operand is erroneous.
196     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
197                                  unsigned AsmVariant, const char *ExtraCode);
198     
199     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
200     /// instruction, using the specified assembler variant as an address.
201     /// Targets should override this to format as appropriate.  This method can
202     /// return true if the operand is erroneous.
203     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
204                                        unsigned AsmVariant, 
205                                        const char *ExtraCode);
206     
207     /// SetupMachineFunction - This should be called when a new MachineFunction
208     /// is being processed from runOnMachineFunction.
209     void SetupMachineFunction(MachineFunction &MF);
210     
211     /// getFunctionNumber - Return a unique ID for the current function.
212     ///
213     unsigned getFunctionNumber() const { return FunctionNumber; }
214     
215     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
216     /// not normally call this, as the counter is automatically bumped by
217     /// SetupMachineFunction.
218     void IncrementFunctionNumber() { FunctionNumber++; }
219     
220     /// EmitConstantPool - Print to the current output stream assembly
221     /// representations of the constants in the constant pool MCP. This is
222     /// used to print out constants which have been "spilled to memory" by
223     /// the code generator.
224     ///
225     void EmitConstantPool(MachineConstantPool *MCP);
226
227     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
228     /// used by the current function to the current output stream.  
229     ///
230     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
231     
232     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
233     /// special global used by LLVM.  If so, emit it and return true, otherwise
234     /// do nothing and return false.
235     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
236     
237   public:
238     //===------------------------------------------------------------------===//
239     /// LEB 128 number encoding.
240
241     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
242     /// representing an unsigned leb128 value.
243     void PrintULEB128(unsigned Value) const;
244
245     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
246     /// representing a signed leb128 value.
247     void PrintSLEB128(int Value) const;
248
249     //===------------------------------------------------------------------===//
250     // Emission and print routines
251     //
252
253     /// PrintHex - Print a value as a hexidecimal value.
254     ///
255     void PrintHex(int Value) const;
256
257     /// EOL - Print a newline character to asm stream.  If a comment is present
258     /// then it will be printed first.  Comments should not contain '\n'.
259     void EOL() const;
260     void EOL(const std::string &Comment) const;
261     void EOL(const char* Comment) const;
262     
263     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
264     /// unsigned leb128 value.
265     void EmitULEB128Bytes(unsigned Value) const;
266     
267     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
268     /// signed leb128 value.
269     void EmitSLEB128Bytes(int Value) const;
270     
271     /// EmitInt8 - Emit a byte directive and value.
272     ///
273     void EmitInt8(int Value) const;
274
275     /// EmitInt16 - Emit a short directive and value.
276     ///
277     void EmitInt16(int Value) const;
278
279     /// EmitInt32 - Emit a long directive and value.
280     ///
281     void EmitInt32(int Value) const;
282
283     /// EmitInt64 - Emit a long long directive and value.
284     ///
285     void EmitInt64(uint64_t Value) const;
286
287     /// EmitString - Emit a string with quotes and a null terminator.
288     /// Special characters are emitted properly.
289     /// @verbatim (Eg. '\t') @endverbatim
290     void EmitString(const std::string &String) const;
291     void EmitString(const char *String, unsigned Size) const;
292
293     /// EmitFile - Emit a .file directive.
294     void EmitFile(unsigned Number, const std::string &Name) const;
295
296     //===------------------------------------------------------------------===//
297
298     /// EmitAlignment - Emit an alignment directive to the specified power of
299     /// two boundary.  For example, if you pass in 3 here, you will get an 8
300     /// byte alignment.  If a global value is specified, and if that global has
301     /// an explicit alignment requested, it will unconditionally override the
302     /// alignment request.  However, if ForcedAlignBits is specified, this value
303     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
304     /// and the alignment computed with NumBits and the global.  If UseFillExpr
305     /// is true, it also emits an optional second value FillValue which the
306     /// assembler uses to fill gaps to match alignment for text sections if the
307     /// has specified a non-zero fill value.
308     ///
309     /// The algorithm is:
310     ///     Align = NumBits;
311     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
312     ///     Align = std::max(Align, ForcedAlignBits);
313     ///
314     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
315                        unsigned ForcedAlignBits = 0,
316                        bool UseFillExpr = true) const;
317
318     /// printLabel - This method prints a local label used by debug and
319     /// exception handling tables.
320     void printLabel(const MachineInstr *MI) const;
321     void printLabel(unsigned Id) const;
322
323     /// printDeclare - This method prints a local variable declaration used by
324     /// debug tables.
325     void printDeclare(const MachineInstr *MI) const;
326
327   protected:
328     /// EmitZeros - Emit a block of zeros.
329     ///
330     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
331
332     /// EmitString - Emit a zero-byte-terminated string constant.
333     ///
334     virtual void EmitString(const ConstantArray *CVA) const;
335
336     /// EmitConstantValueOnly - Print out the specified constant, without a
337     /// storage class.  Only constants of first-class type are allowed here.
338     void EmitConstantValueOnly(const Constant *CV);
339
340     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
341     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
342
343     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
344
345     /// processDebugLoc - Processes the debug information of each machine
346     /// instruction's DebugLoc.
347     void processDebugLoc(DebugLoc DL);
348     
349     /// printInlineAsm - This method formats and prints the specified machine
350     /// instruction that is an inline asm.
351     void printInlineAsm(const MachineInstr *MI) const;
352
353     /// printImplicitDef - This method prints the specified machine instruction
354     /// that is an implicit def.
355     virtual void printImplicitDef(const MachineInstr *MI) const;
356     
357     /// printBasicBlockLabel - This method prints the label for the specified
358     /// MachineBasicBlock
359     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
360                                       bool printAlign = false,
361                                       bool printColon = false,
362                                       bool printComment = true) const;
363                                       
364     /// printPICJumpTableSetLabel - This method prints a set label for the
365     /// specified MachineBasicBlock for a jumptable entry.
366     virtual void printPICJumpTableSetLabel(unsigned uid,
367                                            const MachineBasicBlock *MBB) const;
368     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
369                                            const MachineBasicBlock *MBB) const;
370     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
371                                         const MachineBasicBlock *MBB,
372                                         unsigned uid) const;
373     
374     /// printDataDirective - This method prints the asm directive for the
375     /// specified type.
376     void printDataDirective(const Type *type, unsigned AddrSpace = 0);
377
378     /// printSuffixedName - This prints a name with preceding 
379     /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
380     /// correctly.
381     void printSuffixedName(const char *Name, const char *Suffix,
382                            const char *Prefix = 0);
383     void printSuffixedName(const std::string &Name, const char* Suffix);
384
385     /// printVisibility - This prints visibility information about symbol, if
386     /// this is suported by the target.
387     void printVisibility(const std::string& Name, unsigned Visibility) const;
388
389     /// printOffset - This is just convenient handler for printing offsets.
390     void printOffset(int64_t Offset) const;
391
392   private:
393     const GlobalValue *findGlobalValue(const Constant* CV);
394     void EmitLLVMUsedList(Constant *List);
395     void EmitXXStructorList(Constant *List);
396     void EmitGlobalConstantStruct(const ConstantStruct* CVS,
397                                   unsigned AddrSpace);
398     void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
399     void EmitGlobalConstantVector(const ConstantVector* CP);
400     void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
401     void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
402     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
403   };
404 }
405
406 #endif