simplify some code.
[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     /// EmitAlignment - Emit an alignment directive to the specified power of
194     /// two boundary.  For example, if you pass in 3 here, you will get an 8
195     /// byte alignment.  If a global value is specified, and if that global has
196     /// an explicit alignment requested, it will unconditionally override the
197     /// alignment request.  However, if ForcedAlignBits is specified, this value
198     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
199     /// and the alignment computed with NumBits and the global.  If UseFillExpr
200     /// is true, it also emits an optional second value FillValue which the
201     /// assembler uses to fill gaps to match alignment for text sections if the
202     /// has specified a non-zero fill value.
203     ///
204     /// The algorithm is:
205     ///     Align = NumBits;
206     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
207     ///     Align = std::max(Align, ForcedAlignBits);
208     ///
209     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
210                        unsigned ForcedAlignBits = 0,
211                        bool UseFillExpr = true) const;
212     
213     /// EmitBasicBlockStart - This method prints the label for the specified
214     /// MachineBasicBlock, an alignment (if present) and a comment describing
215     /// it if appropriate.
216     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
217     
218     
219     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
220     void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
221     
222     
223     //===------------------------------------------------------------------===//
224     // Overridable Hooks
225     //===------------------------------------------------------------------===//
226     
227     // Targets can, or in the case of EmitInstruction, must implement these to
228     // customize output.
229     
230     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
231     /// that want to emit something at the start of their file.
232     virtual void EmitStartOfAsmFile(Module &) {}
233     
234     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
235     /// want to emit something at the end of their file.
236     virtual void EmitEndOfAsmFile(Module &) {}
237     
238     /// EmitFunctionBodyStart - Targets can override this to emit stuff before
239     /// the first basic block in the function.
240     virtual void EmitFunctionBodyStart() {}
241     
242     /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
243     /// the last basic block in the function.
244     virtual void EmitFunctionBodyEnd() {}
245     
246     /// EmitInstruction - Targets should implement this to emit instructions.
247     virtual void EmitInstruction(const MachineInstr *) {
248       assert(0 && "EmitInstruction not implemented");
249     }
250     
251     virtual void EmitFunctionEntryLabel();
252     
253     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
254     
255     /// isBlockOnlyReachableByFallthough - Return true if the basic block has
256     /// exactly one predecessor and the control transfer mechanism between
257     /// the predecessor and this block is a fall-through.
258     virtual bool
259     isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
260     
261     //===------------------------------------------------------------------===//
262     // Symbol Lowering Routines.
263     //===------------------------------------------------------------------===//
264   public:
265
266     /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
267     /// temporary label with the specified stem and unique ID.
268     MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
269     
270     /// GetTempSymbol - Return an assembler temporary label with the specified
271     /// stem.
272     MCSymbol *GetTempSymbol(StringRef Name) const;
273     
274     
275     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
276     /// global value name as its base, with the specified suffix, and where the
277     /// symbol is forced to have private linkage if ForcePrivate is true.
278     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
279                                            StringRef Suffix,
280                                            bool ForcePrivate = true) const;
281     
282     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
283     /// ExternalSymbol.
284     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
285     
286     /// GetCPISymbol - Return the symbol for the specified constant pool entry.
287     MCSymbol *GetCPISymbol(unsigned CPID) const;
288
289     /// GetJTISymbol - Return the symbol for the specified jump table entry.
290     MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
291
292     /// GetJTSetSymbol - Return the symbol for the specified jump table .set
293     /// FIXME: privatize to AsmPrinter.
294     MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
295
296     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
297     /// uses of the specified basic block.
298     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
299     MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
300
301      //===------------------------------------------------------------------===//
302     // Emission Helper Routines.
303     //===------------------------------------------------------------------===//
304   public:
305     /// printOffset - This is just convenient handler for printing offsets.
306     void printOffset(int64_t Offset, raw_ostream &OS) const;
307     
308     /// EmitInt8 - Emit a byte directive and value.
309     ///
310     void EmitInt8(int Value) const;
311     
312     /// EmitInt16 - Emit a short directive and value.
313     ///
314     void EmitInt16(int Value) const;
315     
316     /// EmitInt32 - Emit a long directive and value.
317     ///
318     void EmitInt32(int Value) const;
319     
320     /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
321     /// in bytes of the directive is specified by Size and Hi/Lo specify the
322     /// labels.  This implicitly uses .set if it is available.
323     void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
324                              unsigned Size) const;
325     
326     //===------------------------------------------------------------------===//
327     // Dwarf Emission Helper Routines
328     //===------------------------------------------------------------------===//
329     
330     /// EmitSLEB128 - emit the specified signed leb128 value.
331     void EmitSLEB128(int Value, const char *Desc = 0) const;
332     
333     /// EmitULEB128 - emit the specified unsigned leb128 value.
334     void EmitULEB128(unsigned Value, const char *Desc = 0,
335                      unsigned PadTo = 0) const;
336     
337     /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
338     void EmitCFAByte(unsigned Val) const;
339
340     /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
341     /// encoding.  If verbose assembly output is enabled, we output comments
342     /// describing the encoding.  Desc is a string saying what the encoding is
343     /// specifying (e.g. "LSDA").
344     void EmitEncodingByte(unsigned Val, const char *Desc = 0);
345     
346     //===------------------------------------------------------------------===//
347     // Inline Asm Support
348     //===------------------------------------------------------------------===//
349   public:
350     // These are hooks that targets can override to implement inline asm
351     // support.  These should probably be moved out of AsmPrinter someday.
352     
353     /// PrintSpecial - Print information related to the specified machine instr
354     /// that is independent of the operand, and may be independent of the instr
355     /// itself.  This can be useful for portably encoding the comment character
356     /// or other bits of target-specific knowledge into the asmstrings.  The
357     /// syntax used is ${:comment}.  Targets can override this to add support
358     /// for their own strange codes.
359     virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
360                               const char *Code) const;
361     
362     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
363     /// instruction, using the specified assembler variant.  Targets should
364     /// override this to format as appropriate.  This method can return true if
365     /// the operand is erroneous.
366     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
367                                  unsigned AsmVariant, const char *ExtraCode,
368                                  raw_ostream &OS);
369     
370     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
371     /// instruction, using the specified assembler variant as an address.
372     /// Targets should override this to format as appropriate.  This method can
373     /// return true if the operand is erroneous.
374     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
375                                        unsigned AsmVariant, 
376                                        const char *ExtraCode,
377                                        raw_ostream &OS);
378     
379   private:
380     /// Private state for PrintSpecial()
381     // Assign a unique ID to this machine instruction.
382     mutable const MachineInstr *LastMI;
383     mutable unsigned LastFn;
384     mutable unsigned Counter;
385     mutable unsigned SetCounter;
386
387     /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
388     void EmitInlineAsm(StringRef Str) const;
389     
390     /// EmitInlineAsm - This method formats and emits the specified machine
391     /// instruction that is an inline asm.
392     void EmitInlineAsm(const MachineInstr *MI) const;
393
394     //===------------------------------------------------------------------===//
395     // Internal Implementation Details
396     //===------------------------------------------------------------------===//
397     
398     /// EmitVisibility - This emits visibility information about symbol, if
399     /// this is suported by the target.
400     void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
401     
402     void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
403     
404     void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
405                             const MachineBasicBlock *MBB,
406                             unsigned uid) const;
407     void EmitLLVMUsedList(Constant *List);
408     void EmitXXStructorList(Constant *List);
409     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
410   };
411 }
412
413 #endif