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