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