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