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