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