105d7730407f20f97005a6eee62bfa9751bfc278
[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/ADT/DenseMap.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include <set>
24
25 namespace llvm {
26   class GCStrategy;
27   class Constant;
28   class ConstantArray;
29   class ConstantInt;
30   class ConstantStruct;
31   class ConstantVector;
32   class GCMetadataPrinter;
33   class GlobalVariable;
34   class MachineConstantPoolEntry;
35   class MachineConstantPoolValue;
36   class MachineModuleInfo;
37   class DwarfWriter;
38   class Mangler;
39   class Section;
40   class TargetAsmInfo;
41   class Type;
42   class raw_ostream;
43
44   /// AsmPrinter - This class is intended to be used as a driving class for all
45   /// asm writers.
46   class AsmPrinter : public MachineFunctionPass {
47     static char ID;
48
49     /// FunctionNumber - This provides a unique ID for each function emitted in
50     /// this translation unit.  It is autoincremented by SetupMachineFunction,
51     /// and can be accessed with getFunctionNumber() and 
52     /// IncrementFunctionNumber().
53     ///
54     unsigned FunctionNumber;
55
56     // GCMetadataPrinters - The garbage collection metadata printer table.
57     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
58     typedef gcp_map_type::iterator gcp_iterator;
59     gcp_map_type GCMetadataPrinters;
60     
61   protected:
62     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
63     MachineModuleInfo *MMI;
64     
65     /// DW - If available, this is a pointer to the current dwarf writer.
66     DwarfWriter *DW;
67     
68     /// OptLevel - Generating code at a specific optimization level.
69     CodeGenOpt::Level OptLevel;
70   public:
71     /// Output stream on which we're printing assembly code.
72     ///
73     raw_ostream &O;
74
75     /// Target machine description.
76     ///
77     TargetMachine &TM;
78     
79     /// Target Asm Printer information.
80     ///
81     const TargetAsmInfo *TAI;
82
83     /// Target Register Information.
84     ///
85     const TargetRegisterInfo *TRI;
86
87     /// The current machine function.
88     const MachineFunction *MF;
89
90     /// Name-mangler for global names.
91     ///
92     Mangler *Mang;
93
94     /// Cache of mangled name for current function. This is recalculated at the
95     /// beginning of each call to runOnMachineFunction().
96     ///
97     std::string CurrentFnName;
98     
99     /// CurrentSection - The current section we are emitting to.  This is
100     /// controlled and used by the SwitchSection method.
101     std::string CurrentSection;
102     const Section* CurrentSection_;
103
104     /// IsInTextSection - True if the current section we are emitting to is a
105     /// text section.
106     bool IsInTextSection;
107
108     /// VerboseAsm - Emit comments in assembly output if this is true.
109     ///
110     bool VerboseAsm;
111
112     /// Private state for PrintSpecial()
113     // Assign a unique ID to this machine instruction.
114     mutable const MachineInstr *LastMI;
115     mutable const Function *LastFn;
116     mutable unsigned Counter;
117
118   protected:
119     explicit AsmPrinter(raw_ostream &o, TargetMachine &TM,
120                         const TargetAsmInfo *T, CodeGenOpt::Level OL, bool V);
121     
122   public:
123     virtual ~AsmPrinter();
124
125     /// isVerbose - Return true if assembly output should contain comments.
126     ///
127     bool isVerbose() const { return VerboseAsm; }
128
129     /// SwitchToTextSection - Switch to the specified section of the executable
130     /// if we are not already in it!  If GV is non-null and if the global has an
131     /// explicitly requested section, we switch to the section indicated for the
132     /// global instead of NewSection.
133     ///
134     /// If the new section is an empty string, this method forgets what the
135     /// current section is, but does not emit a .section directive.
136     ///
137     /// This method is used when about to emit executable code.
138     ///
139     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
140
141     /// SwitchToDataSection - Switch to the specified section of the executable
142     /// if we are not already in it!  If GV is non-null and if the global has an
143     /// explicitly requested section, we switch to the section indicated for the
144     /// global instead of NewSection.
145     ///
146     /// If the new section is an empty string, this method forgets what the
147     /// current section is, but does not emit a .section directive.
148     ///
149     /// This method is used when about to emit data.  For most assemblers, this
150     /// is the same as the SwitchToTextSection method, but not all assemblers
151     /// are the same.
152     ///
153     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
154
155     /// SwitchToSection - Switch to the specified section of the executable if
156     /// we are not already in it!
157     void SwitchToSection(const Section* NS);
158
159     /// getGlobalLinkName - Returns the asm/link name of of the specified
160     /// global variable.  Should be overridden by each target asm printer to
161     /// generate the appropriate value.
162     virtual const std::string &getGlobalLinkName(const GlobalVariable *GV,
163                                                  std::string &LinkName) const;
164
165     /// EmitExternalGlobal - Emit the external reference to a global variable.
166     /// Should be overridden if an indirect reference should be used.
167     virtual void EmitExternalGlobal(const GlobalVariable *GV);
168
169     /// getCurrentFunctionEHName - Called to return (and cache) the
170     /// CurrentFnEHName.
171     /// 
172     const std::string &getCurrentFunctionEHName(const MachineFunction *MF,
173                                                 std::string &FuncEHName) const;
174
175   protected:
176     /// getAnalysisUsage - Record analysis usage.
177     /// 
178     void getAnalysisUsage(AnalysisUsage &AU) const;
179     
180     /// doInitialization - Set up the AsmPrinter when we are working on a new
181     /// module.  If your pass overrides this, it must make sure to explicitly
182     /// call this implementation.
183     bool doInitialization(Module &M);
184
185     /// doFinalization - Shut down the asmprinter.  If you override this in your
186     /// pass, you must make sure to call it explicitly.
187     bool doFinalization(Module &M);
188     
189     /// PrintSpecial - Print information related to the specified machine instr
190     /// that is independent of the operand, and may be independent of the instr
191     /// itself.  This can be useful for portably encoding the comment character
192     /// or other bits of target-specific knowledge into the asmstrings.  The
193     /// syntax used is ${:comment}.  Targets can override this to add support
194     /// for their own strange codes.
195     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
196
197     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
198     /// instruction, using the specified assembler variant.  Targets should
199     /// override this to format as appropriate.  This method can return true if
200     /// the operand is erroneous.
201     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
202                                  unsigned AsmVariant, const char *ExtraCode);
203     
204     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
205     /// instruction, using the specified assembler variant as an address.
206     /// Targets should override this to format as appropriate.  This method can
207     /// return true if the operand is erroneous.
208     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
209                                        unsigned AsmVariant, 
210                                        const char *ExtraCode);
211     
212     /// SetupMachineFunction - This should be called when a new MachineFunction
213     /// is being processed from runOnMachineFunction.
214     void SetupMachineFunction(MachineFunction &MF);
215     
216     /// getFunctionNumber - Return a unique ID for the current function.
217     ///
218     unsigned getFunctionNumber() const { return FunctionNumber; }
219     
220     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
221     /// not normally call this, as the counter is automatically bumped by
222     /// SetupMachineFunction.
223     void IncrementFunctionNumber() { FunctionNumber++; }
224     
225     /// EmitConstantPool - Print to the current output stream assembly
226     /// representations of the constants in the constant pool MCP. This is
227     /// used to print out constants which have been "spilled to memory" by
228     /// the code generator.
229     ///
230     void EmitConstantPool(MachineConstantPool *MCP);
231
232     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
233     /// used by the current function to the current output stream.  
234     ///
235     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
236     
237     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
238     /// special global used by LLVM.  If so, emit it and return true, otherwise
239     /// do nothing and return false.
240     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
241     
242   public:
243     //===------------------------------------------------------------------===//
244     /// LEB 128 number encoding.
245
246     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
247     /// representing an unsigned leb128 value.
248     void PrintULEB128(unsigned Value) const;
249
250     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
251     /// representing a signed leb128 value.
252     void PrintSLEB128(int Value) const;
253
254     //===------------------------------------------------------------------===//
255     // Emission and print routines
256     //
257
258     /// PrintHex - Print a value as a hexidecimal value.
259     ///
260     void PrintHex(int Value) const;
261
262     /// EOL - Print a newline character to asm stream.  If a comment is present
263     /// then it will be printed first.  Comments should not contain '\n'.
264     void EOL() const;
265     void EOL(const std::string &Comment) const;
266     void EOL(const char* Comment) 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   protected:
333     /// EmitZeros - Emit a block of zeros.
334     ///
335     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
336
337     /// EmitString - Emit a zero-byte-terminated string constant.
338     ///
339     virtual void EmitString(const ConstantArray *CVA) const;
340
341     /// EmitConstantValueOnly - Print out the specified constant, without a
342     /// storage class.  Only constants of first-class type are allowed here.
343     void EmitConstantValueOnly(const Constant *CV);
344
345     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
346     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
347
348     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
349
350     /// processDebugLoc - Processes the debug information of each machine
351     /// instruction's DebugLoc.
352     void processDebugLoc(DebugLoc DL);
353     
354     /// printInlineAsm - This method formats and prints the specified machine
355     /// instruction that is an inline asm.
356     void printInlineAsm(const MachineInstr *MI) const;
357
358     /// printImplicitDef - This method prints the specified machine instruction
359     /// that is an implicit def.
360     virtual void printImplicitDef(const MachineInstr *MI) const;
361     
362     /// printBasicBlockLabel - This method prints the label for the specified
363     /// MachineBasicBlock
364     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
365                                       bool printAlign = false,
366                                       bool printColon = false,
367                                       bool printComment = true) const;
368                                       
369     /// printPICJumpTableSetLabel - This method prints a set label for the
370     /// specified MachineBasicBlock for a jumptable entry.
371     virtual void printPICJumpTableSetLabel(unsigned uid,
372                                            const MachineBasicBlock *MBB) const;
373     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
374                                            const MachineBasicBlock *MBB) const;
375     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
376                                         const MachineBasicBlock *MBB,
377                                         unsigned uid) const;
378     
379     /// printDataDirective - This method prints the asm directive for the
380     /// specified type.
381     void printDataDirective(const Type *type, unsigned AddrSpace = 0);
382
383     /// printSuffixedName - This prints a name with preceding 
384     /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
385     /// correctly.
386     void printSuffixedName(const char *Name, const char *Suffix,
387                            const char *Prefix = 0);
388     void printSuffixedName(const std::string &Name, const char* Suffix);
389
390     /// printVisibility - This prints visibility information about symbol, if
391     /// this is suported by the target.
392     void printVisibility(const std::string& Name, unsigned Visibility) const;
393
394     /// printOffset - This is just convenient handler for printing offsets.
395     void printOffset(int64_t Offset) const;
396
397   private:
398     const GlobalValue *findGlobalValue(const Constant* CV);
399     void EmitLLVMUsedList(Constant *List);
400     void EmitXXStructorList(Constant *List);
401     void EmitGlobalConstantStruct(const ConstantStruct* CVS,
402                                   unsigned AddrSpace);
403     void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
404     void EmitGlobalConstantVector(const ConstantVector* CP);
405     void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
406     void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
407     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
408   };
409 }
410
411 #endif