Refactor code to push DILocation prcessing into DwarfDebug.cpp from AsmPrinter.cpp.
[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/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Support/DebugLoc.h"
21 #include "llvm/Target/TargetMachine.h"
22
23 namespace llvm {
24   class BlockAddress;
25   class GCStrategy;
26   class Constant;
27   class ConstantArray;
28   class ConstantFP;
29   class ConstantInt;
30   class ConstantStruct;
31   class ConstantVector;
32   class GCMetadataPrinter;
33   class GlobalValue;
34   class GlobalVariable;
35   class MachineBasicBlock;
36   class MachineFunction;
37   class MachineInstr;
38   class MachineLoopInfo;
39   class MachineLoop;
40   class MachineConstantPool;
41   class MachineConstantPoolEntry;
42   class MachineConstantPoolValue;
43   class MachineJumpTableInfo;
44   class MachineModuleInfo;
45   class MCInst;
46   class MCContext;
47   class MCSection;
48   class MCStreamer;
49   class MCSymbol;
50   class DwarfWriter;
51   class Mangler;
52   class MCAsmInfo;
53   class TargetLoweringObjectFile;
54   class Twine;
55   class Type;
56   class formatted_raw_ostream;
57
58   /// AsmPrinter - This class is intended to be used as a driving class for all
59   /// asm writers.
60   class AsmPrinter : public MachineFunctionPass {
61     static char ID;
62
63     // GCMetadataPrinters - The garbage collection metadata printer table.
64     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
65     typedef gcp_map_type::iterator gcp_iterator;
66     gcp_map_type GCMetadataPrinters;
67
68     /// If VerboseAsm is set, a pointer to the loop info for this
69     /// function.
70     ///
71     MachineLoopInfo *LI;
72
73   public:
74     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
75     MachineModuleInfo *MMI;
76     
77   protected:
78     /// DW - If available, this is a pointer to the current dwarf writer.
79     DwarfWriter *DW;
80
81   public:
82
83     /// Output stream on which we're printing assembly code.
84     ///
85     formatted_raw_ostream &O;
86
87     /// Target machine description.
88     ///
89     TargetMachine &TM;
90     
91     /// getObjFileLowering - Return information about object file lowering.
92     TargetLoweringObjectFile &getObjFileLowering() const;
93     
94     /// Target Asm Printer information.
95     ///
96     const MCAsmInfo *MAI;
97
98     /// Target Register Information.
99     ///
100     const TargetRegisterInfo *TRI;
101
102     /// OutContext - This is the context for the output file that we are
103     /// streaming.  This owns all of the global MC-related objects for the
104     /// generated translation unit.
105     MCContext &OutContext;
106     
107     /// OutStreamer - This is the MCStreamer object for the file we are
108     /// generating.  This contains the transient state for the current
109     /// translation unit that we are generating (such as the current section
110     /// etc).
111     MCStreamer &OutStreamer;
112     
113     /// The current machine function.
114     const MachineFunction *MF;
115
116     /// Name-mangler for global names.
117     ///
118     Mangler *Mang;
119
120     /// The symbol for the current function. This is recalculated at the
121     /// beginning of each call to runOnMachineFunction().
122     ///
123     MCSymbol *CurrentFnSym;
124     
125     /// getCurrentSection() - Return the current section we are emitting to.
126     const MCSection *getCurrentSection() const;
127     
128
129     /// VerboseAsm - Emit comments in assembly output if this is true.
130     ///
131     bool VerboseAsm;
132
133     /// Private state for PrintSpecial()
134     // Assign a unique ID to this machine instruction.
135     mutable const MachineInstr *LastMI;
136     mutable const Function *LastFn;
137     mutable unsigned Counter;
138     mutable unsigned SetCounter;
139     
140   protected:
141     explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
142                         MCStreamer &Streamer);
143     
144   public:
145     virtual ~AsmPrinter();
146
147     /// isVerbose - Return true if assembly output should contain comments.
148     ///
149     bool isVerbose() const { return VerboseAsm; }
150
151     /// getFunctionNumber - Return a unique ID for the current function.
152     ///
153     unsigned getFunctionNumber() const;
154     
155   protected:
156     /// getAnalysisUsage - Record analysis usage.
157     /// 
158     void getAnalysisUsage(AnalysisUsage &AU) const;
159     
160     /// doInitialization - Set up the AsmPrinter when we are working on a new
161     /// module.  If your pass overrides this, it must make sure to explicitly
162     /// call this implementation.
163     bool doInitialization(Module &M);
164
165     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
166     /// that want to emit something at the start of their file.
167     virtual void EmitStartOfAsmFile(Module &) {}
168     
169     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
170     /// want to emit something at the end of their file.
171     virtual void EmitEndOfAsmFile(Module &) {}
172     
173     /// doFinalization - Shut down the asmprinter.  If you override this in your
174     /// pass, you must make sure to call it explicitly.
175     bool doFinalization(Module &M);
176     
177     /// PrintSpecial - Print information related to the specified machine instr
178     /// that is independent of the operand, and may be independent of the instr
179     /// itself.  This can be useful for portably encoding the comment character
180     /// or other bits of target-specific knowledge into the asmstrings.  The
181     /// syntax used is ${:comment}.  Targets can override this to add support
182     /// for their own strange codes.
183     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
184
185     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
186     /// instruction, using the specified assembler variant.  Targets should
187     /// override this to format as appropriate.  This method can return true if
188     /// the operand is erroneous.
189     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
190                                  unsigned AsmVariant, const char *ExtraCode);
191     
192     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
193     /// instruction, using the specified assembler variant as an address.
194     /// Targets should override this to format as appropriate.  This method can
195     /// return true if the operand is erroneous.
196     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
197                                        unsigned AsmVariant, 
198                                        const char *ExtraCode);
199     
200     /// runOnMachineFunction - Emit the specified function out to the
201     /// OutStreamer.
202     virtual bool runOnMachineFunction(MachineFunction &MF) {
203       SetupMachineFunction(MF);
204       EmitFunctionHeader();
205       EmitFunctionBody();
206       return false;
207     }      
208     
209     /// SetupMachineFunction - This should be called when a new MachineFunction
210     /// is being processed from runOnMachineFunction.
211     void SetupMachineFunction(MachineFunction &MF);
212     
213     /// EmitFunctionHeader - This method emits the header for the current
214     /// function.
215     void EmitFunctionHeader();
216     
217     /// EmitFunctionBody - This method emits the body and trailer for a
218     /// function.
219     void EmitFunctionBody();
220
221     /// EmitInstruction - Targets should implement this to emit instructions.
222     virtual void EmitInstruction(const MachineInstr *) {
223       assert(0 && "EmitInstruction not implemented");
224     }
225     
226     /// EmitFunctionBodyStart - Targets can override this to emit stuff before
227     /// the first basic block in the function.
228     virtual void EmitFunctionBodyStart() {}
229
230     /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
231     /// the last basic block in the function.
232     virtual void EmitFunctionBodyEnd() {}
233     
234     /// EmitConstantPool - Print to the current output stream assembly
235     /// representations of the constants in the constant pool MCP. This is
236     /// used to print out constants which have been "spilled to memory" by
237     /// the code generator.
238     ///
239     virtual void EmitConstantPool();
240     
241     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
242     /// used by the current function to the current output stream.  
243     ///
244     void EmitJumpTableInfo();
245     
246     /// EmitGlobalVariable - Emit the specified global variable to the .s file.
247     virtual void EmitGlobalVariable(const GlobalVariable *GV);
248     
249     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
250     /// special global used by LLVM.  If so, emit it and return true, otherwise
251     /// do nothing and return false.
252     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
253
254   public:
255     //===------------------------------------------------------------------===//
256     // Emission and print routines
257     //
258
259     /// EmitInt8 - Emit a byte directive and value.
260     ///
261     void EmitInt8(int Value) const;
262
263     /// EmitInt16 - Emit a short directive and value.
264     ///
265     void EmitInt16(int Value) const;
266
267     /// EmitInt32 - Emit a long directive and value.
268     ///
269     void EmitInt32(int Value) const;
270
271     /// EmitInt64 - Emit a long long directive and value.
272     ///
273     void EmitInt64(uint64_t Value) const;
274     
275     
276     /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
277     /// in bytes of the directive is specified by Size and Hi/Lo specify the
278     /// labels.  This implicitly uses .set if it is available.
279     void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
280                              unsigned Size) const;
281
282     //===------------------------------------------------------------------===//
283
284     /// EmitAlignment - Emit an alignment directive to the specified power of
285     /// two boundary.  For example, if you pass in 3 here, you will get an 8
286     /// byte alignment.  If a global value is specified, and if that global has
287     /// an explicit alignment requested, it will unconditionally override the
288     /// alignment request.  However, if ForcedAlignBits is specified, this value
289     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
290     /// and the alignment computed with NumBits and the global.  If UseFillExpr
291     /// is true, it also emits an optional second value FillValue which the
292     /// assembler uses to fill gaps to match alignment for text sections if the
293     /// has specified a non-zero fill value.
294     ///
295     /// The algorithm is:
296     ///     Align = NumBits;
297     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
298     ///     Align = std::max(Align, ForcedAlignBits);
299     ///
300     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
301                        unsigned ForcedAlignBits = 0,
302                        bool UseFillExpr = true) const;
303
304     /// printDeclare - This method prints a local variable declaration used by
305     /// debug tables.
306     void printDeclare(const MachineInstr *MI) const;
307
308     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
309     /// global value name as its base, with the specified suffix, and where the
310     /// symbol is forced to have private linkage if ForcePrivate is true.
311     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
312                                            StringRef Suffix,
313                                            bool ForcePrivate = true) const;
314     
315     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
316     /// ExternalSymbol.
317     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
318     
319     /// GetCPISymbol - Return the symbol for the specified constant pool entry.
320     MCSymbol *GetCPISymbol(unsigned CPID) const;
321
322     /// GetJTISymbol - Return the symbol for the specified jump table entry.
323     MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
324
325     /// GetJTSetSymbol - Return the symbol for the specified jump table .set
326     /// FIXME: privatize to AsmPrinter.
327     MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
328
329     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
330     /// uses of the specified basic block.
331     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
332     MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
333
334     /// EmitBasicBlockStart - This method prints the label for the specified
335     /// MachineBasicBlock, an alignment (if present) and a comment describing
336     /// it if appropriate.
337     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
338     
339     
340     // Data emission.
341     
342     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
343     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
344     
345   protected:
346     virtual void EmitFunctionEntryLabel();
347     
348     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
349
350     /// printOffset - This is just convenient handler for printing offsets.
351     void printOffset(int64_t Offset) const;
352
353     /// isBlockOnlyReachableByFallthough - Return true if the basic block has
354     /// exactly one predecessor and the control transfer mechanism between
355     /// the predecessor and this block is a fall-through.
356     virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
357
358   private:
359
360     /// processDebugLoc - Processes the debug information of each machine
361     /// instruction's DebugLoc. 
362     void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
363     
364     void printLabelInst(const MachineInstr *MI) const;
365
366     /// printInlineAsm - This method formats and prints the specified machine
367     /// instruction that is an inline asm.
368     void printInlineAsm(const MachineInstr *MI) const;
369
370     /// printImplicitDef - This method prints the specified machine instruction
371     /// that is an implicit def.
372     void printImplicitDef(const MachineInstr *MI) const;
373
374     /// printKill - This method prints the specified kill machine instruction.
375     void printKill(const MachineInstr *MI) const;
376
377     /// EmitVisibility - This emits visibility information about symbol, if
378     /// this is suported by the target.
379     void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
380     
381     void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
382     
383     void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
384                             const MachineBasicBlock *MBB,
385                             unsigned uid) const;
386     void EmitLLVMUsedList(Constant *List);
387     void EmitXXStructorList(Constant *List);
388     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
389   };
390 }
391
392 #endif