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