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