rename GetPrivateGlobalValueSymbolStub -> GetSymbolWithGlobalValueBase,
[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/Analysis/DebugInfo.h"
21 #include "llvm/Support/DebugLoc.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/ADT/DenseMap.h"
24
25 namespace llvm {
26   class BlockAddress;
27   class GCStrategy;
28   class Constant;
29   class ConstantArray;
30   class ConstantFP;
31   class ConstantInt;
32   class ConstantStruct;
33   class ConstantVector;
34   class GCMetadataPrinter;
35   class GlobalValue;
36   class GlobalVariable;
37   class MachineBasicBlock;
38   class MachineFunction;
39   class MachineInstr;
40   class MachineLoopInfo;
41   class MachineLoop;
42   class MachineConstantPool;
43   class MachineConstantPoolEntry;
44   class MachineConstantPoolValue;
45   class MachineJumpTableInfo;
46   class MachineModuleInfo;
47   class MCInst;
48   class MCContext;
49   class MCSection;
50   class MCStreamer;
51   class MCSymbol;
52   class DwarfWriter;
53   class Mangler;
54   class MCAsmInfo;
55   class TargetLoweringObjectFile;
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     /// FunctionNumber - This provides a unique ID for each function emitted in
65     /// this translation unit.  It is autoincremented by SetupMachineFunction,
66     /// and can be accessed with getFunctionNumber() and 
67     /// IncrementFunctionNumber().
68     ///
69     unsigned FunctionNumber;
70
71     // GCMetadataPrinters - The garbage collection metadata printer table.
72     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
73     typedef gcp_map_type::iterator gcp_iterator;
74     gcp_map_type GCMetadataPrinters;
75
76     /// If VerboseAsm is set, a pointer to the loop info for this
77     /// function.
78     ///
79     MachineLoopInfo *LI;
80
81   public:
82     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
83     MachineModuleInfo *MMI;
84     
85   protected:
86     /// DW - If available, this is a pointer to the current dwarf writer.
87     DwarfWriter *DW;
88
89   public:
90     /// Flags to specify different kinds of comments to output in
91     /// assembly code.  These flags carry semantic information not
92     /// otherwise easily derivable from the IR text.
93     ///
94     enum CommentFlag {
95       ReloadReuse = 0x1
96     };
97
98     /// Output stream on which we're printing assembly code.
99     ///
100     formatted_raw_ostream &O;
101
102     /// Target machine description.
103     ///
104     TargetMachine &TM;
105     
106     /// getObjFileLowering - Return information about object file lowering.
107     TargetLoweringObjectFile &getObjFileLowering() const;
108     
109     /// Target Asm Printer information.
110     ///
111     const MCAsmInfo *MAI;
112
113     /// Target Register Information.
114     ///
115     const TargetRegisterInfo *TRI;
116
117     /// OutContext - This is the context for the output file that we are
118     /// streaming.  This owns all of the global MC-related objects for the
119     /// generated translation unit.
120     MCContext &OutContext;
121     
122     /// OutStreamer - This is the MCStreamer object for the file we are
123     /// generating.  This contains the transient state for the current
124     /// translation unit that we are generating (such as the current section
125     /// etc).
126     MCStreamer &OutStreamer;
127     
128     /// The current machine function.
129     const MachineFunction *MF;
130
131     /// Name-mangler for global names.
132     ///
133     Mangler *Mang;
134
135     /// The symbol for the current function. This is recalculated at the
136     /// beginning of each call to runOnMachineFunction().
137     ///
138     const MCSymbol *CurrentFnSym;
139     
140     /// getCurrentSection() - Return the current section we are emitting to.
141     const MCSection *getCurrentSection() const;
142     
143
144     /// VerboseAsm - Emit comments in assembly output if this is true.
145     ///
146     bool VerboseAsm;
147
148     /// Private state for PrintSpecial()
149     // Assign a unique ID to this machine instruction.
150     mutable const MachineInstr *LastMI;
151     mutable const Function *LastFn;
152     mutable unsigned Counter;
153     
154     // Private state for processDebugLoc()
155     mutable DILocation PrevDLT;
156
157   protected:
158     explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
159                         const MCAsmInfo *T, bool V);
160     
161   public:
162     virtual ~AsmPrinter();
163
164     /// isVerbose - Return true if assembly output should contain comments.
165     ///
166     bool isVerbose() const { return VerboseAsm; }
167
168     /// getFunctionNumber - Return a unique ID for the current function.
169     ///
170     unsigned getFunctionNumber() const { return FunctionNumber; }
171     
172   protected:
173     /// getAnalysisUsage - Record analysis usage.
174     /// 
175     void getAnalysisUsage(AnalysisUsage &AU) const;
176     
177     /// doInitialization - Set up the AsmPrinter when we are working on a new
178     /// module.  If your pass overrides this, it must make sure to explicitly
179     /// call this implementation.
180     bool doInitialization(Module &M);
181
182     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
183     /// that want to emit something at the start of their file.
184     virtual void EmitStartOfAsmFile(Module &) {}
185     
186     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
187     /// want to emit something at the end of their file.
188     virtual void EmitEndOfAsmFile(Module &) {}
189     
190     /// doFinalization - Shut down the asmprinter.  If you override this in your
191     /// pass, you must make sure to call it explicitly.
192     bool doFinalization(Module &M);
193     
194     /// PrintSpecial - Print information related to the specified machine instr
195     /// that is independent of the operand, and may be independent of the instr
196     /// itself.  This can be useful for portably encoding the comment character
197     /// or other bits of target-specific knowledge into the asmstrings.  The
198     /// syntax used is ${:comment}.  Targets can override this to add support
199     /// for their own strange codes.
200     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
201
202     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
203     /// instruction, using the specified assembler variant.  Targets should
204     /// override this to format as appropriate.  This method can return true if
205     /// the operand is erroneous.
206     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
207                                  unsigned AsmVariant, const char *ExtraCode);
208     
209     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
210     /// instruction, using the specified assembler variant as an address.
211     /// Targets should override this to format as appropriate.  This method can
212     /// return true if the operand is erroneous.
213     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
214                                        unsigned AsmVariant, 
215                                        const char *ExtraCode);
216     
217     /// PrintGlobalVariable - Emit the specified global variable and its
218     /// initializer to the output stream.
219     virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
220
221     /// SetupMachineFunction - This should be called when a new MachineFunction
222     /// is being processed from runOnMachineFunction.
223     void SetupMachineFunction(MachineFunction &MF);
224     
225     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
226     /// not normally call this, as the counter is automatically bumped by
227     /// SetupMachineFunction.
228     void IncrementFunctionNumber() { FunctionNumber++; }
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     void EmitConstantPool(MachineConstantPool *MCP);
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(MachineJumpTableInfo *MJTI, MachineFunction &MF);
241     
242     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
243     /// special global used by LLVM.  If so, emit it and return true, otherwise
244     /// do nothing and return false.
245     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
246
247   public:
248     //===------------------------------------------------------------------===//
249     /// LEB 128 number encoding.
250
251     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
252     /// representing an unsigned leb128 value.
253     void PrintULEB128(unsigned Value) const;
254
255     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
256     /// representing a signed leb128 value.
257     void PrintSLEB128(int Value) const;
258
259     //===------------------------------------------------------------------===//
260     // Emission and print routines
261     //
262
263     /// PrintHex - Print a value as a hexidecimal value.
264     ///
265     void PrintHex(int Value) const;
266
267     /// EOL - Print a newline character to asm stream.  If a comment is present
268     /// then it will be printed first.  Comments should not contain '\n'.
269     void EOL() const;
270     void EOL(const std::string &Comment) const;
271     void EOL(const char* Comment) const;
272     void EOL(const char *Comment, unsigned Encoding) const;
273
274     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
275     /// unsigned leb128 value.
276     void EmitULEB128Bytes(unsigned Value) const;
277     
278     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
279     /// signed leb128 value.
280     void EmitSLEB128Bytes(int Value) const;
281     
282     /// EmitInt8 - Emit a byte directive and value.
283     ///
284     void EmitInt8(int Value) const;
285
286     /// EmitInt16 - Emit a short directive and value.
287     ///
288     void EmitInt16(int Value) const;
289
290     /// EmitInt32 - Emit a long directive and value.
291     ///
292     void EmitInt32(int Value) const;
293
294     /// EmitInt64 - Emit a long long directive and value.
295     ///
296     void EmitInt64(uint64_t Value) const;
297
298     /// EmitString - Emit a string with quotes and a null terminator.
299     /// Special characters are emitted properly.
300     /// @verbatim (Eg. '\t') @endverbatim
301     void EmitString(const StringRef String) const;
302     void EmitString(const char *String, unsigned Size) const;
303
304     /// EmitFile - Emit a .file directive.
305     void EmitFile(unsigned Number, const std::string &Name) const;
306
307     //===------------------------------------------------------------------===//
308
309     /// EmitAlignment - Emit an alignment directive to the specified power of
310     /// two boundary.  For example, if you pass in 3 here, you will get an 8
311     /// byte alignment.  If a global value is specified, and if that global has
312     /// an explicit alignment requested, it will unconditionally override the
313     /// alignment request.  However, if ForcedAlignBits is specified, this value
314     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
315     /// and the alignment computed with NumBits and the global.  If UseFillExpr
316     /// is true, it also emits an optional second value FillValue which the
317     /// assembler uses to fill gaps to match alignment for text sections if the
318     /// has specified a non-zero fill value.
319     ///
320     /// The algorithm is:
321     ///     Align = NumBits;
322     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
323     ///     Align = std::max(Align, ForcedAlignBits);
324     ///
325     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
326                        unsigned ForcedAlignBits = 0,
327                        bool UseFillExpr = true) const;
328
329     /// printLabel - This method prints a local label used by debug and
330     /// exception handling tables.
331     void printLabel(const MachineInstr *MI) const;
332     void printLabel(unsigned Id) const;
333
334     /// printDeclare - This method prints a local variable declaration used by
335     /// debug tables.
336     void printDeclare(const MachineInstr *MI) const;
337
338     /// EmitComments - Pretty-print comments for instructions
339     void EmitComments(const MachineInstr &MI) const;
340     /// EmitComments - Pretty-print comments for basic blocks
341     void EmitComments(const MachineBasicBlock &MBB) const;
342
343     /// GetGlobalValueSymbol - Return the MCSymbol for the specified global
344     /// value.
345     MCSymbol *GetGlobalValueSymbol(const GlobalValue *GV) const;
346
347     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
348     /// global value name as its base, with the specified suffix, and where the
349     /// symbol is forced to have private linkage if ForcePrivate is true.
350     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
351                                            StringRef Suffix,
352                                            bool ForcePrivate = true) const;
353     
354     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
355     /// ExternalSymbol.
356     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
357     
358     /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
359     /// block label.
360     MCSymbol *GetMBBSymbol(unsigned MBBID) const;
361     
362     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
363     /// uses of the specified basic block.
364     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA,
365                                     const char *Suffix = "") const;
366     MCSymbol *GetBlockAddressSymbol(const Function *F,
367                                     const BasicBlock *BB,
368                                     const char *Suffix = "") const;
369
370     /// EmitBasicBlockStart - This method prints the label for the specified
371     /// MachineBasicBlock, an alignment (if present) and a comment describing
372     /// it if appropriate.
373     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
374   protected:
375     /// EmitZeros - Emit a block of zeros.
376     ///
377     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
378
379     /// EmitString - Emit a zero-byte-terminated string constant.
380     ///
381     virtual void EmitString(const ConstantArray *CVA) const;
382
383     /// EmitConstantValueOnly - Print out the specified constant, without a
384     /// storage class.  Only constants of first-class type are allowed here.
385     void EmitConstantValueOnly(const Constant *CV);
386
387     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
388     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
389
390     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
391
392     /// processDebugLoc - Processes the debug information of each machine
393     /// instruction's DebugLoc. 
394     void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
395     
396     /// printInlineAsm - This method formats and prints the specified machine
397     /// instruction that is an inline asm.
398     void printInlineAsm(const MachineInstr *MI) const;
399
400     /// printImplicitDef - This method prints the specified machine instruction
401     /// that is an implicit def.
402     void printImplicitDef(const MachineInstr *MI) const;
403
404     /// printKill - This method prints the specified kill machine instruction.
405     void printKill(const MachineInstr *MI) const;
406
407     /// printPICJumpTableSetLabel - This method prints a set label for the
408     /// specified MachineBasicBlock for a jumptable entry.
409     virtual void printPICJumpTableSetLabel(unsigned uid,
410                                            const MachineBasicBlock *MBB) const;
411     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
412                                            const MachineBasicBlock *MBB) const;
413     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
414                                         const MachineBasicBlock *MBB,
415                                         unsigned uid) const;
416     
417     /// printDataDirective - This method prints the asm directive for the
418     /// specified type.
419     void printDataDirective(const Type *type, unsigned AddrSpace = 0);
420
421     /// printVisibility - This prints visibility information about symbol, if
422     /// this is suported by the target.
423     void printVisibility(const MCSymbol *Sym, unsigned Visibility) const;
424     
425     /// printOffset - This is just convenient handler for printing offsets.
426     void printOffset(int64_t Offset) const;
427  
428   private:
429     void EmitLLVMUsedList(Constant *List);
430     void EmitXXStructorList(Constant *List);
431     void EmitGlobalConstantStruct(const ConstantStruct* CVS,
432                                   unsigned AddrSpace);
433     void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
434     void EmitGlobalConstantVector(const ConstantVector* CP);
435     void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
436     void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
437     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
438   };
439 }
440
441 #endif