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