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