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