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