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