Drop obsolete hook and change all usage to new interface
[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     /// SetupMachineFunction - This should be called when a new MachineFunction
188     /// is being processed from runOnMachineFunction.
189     void SetupMachineFunction(MachineFunction &MF);
190     
191     /// getFunctionNumber - Return a unique ID for the current function.
192     ///
193     unsigned getFunctionNumber() const { return FunctionNumber; }
194     
195     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
196     /// not normally call this, as the counter is automatically bumped by
197     /// SetupMachineFunction.
198     void IncrementFunctionNumber() { FunctionNumber++; }
199     
200     /// EmitConstantPool - Print to the current output stream assembly
201     /// representations of the constants in the constant pool MCP. This is
202     /// used to print out constants which have been "spilled to memory" by
203     /// the code generator.
204     ///
205     void EmitConstantPool(MachineConstantPool *MCP);
206
207     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
208     /// used by the current function to the current output stream.  
209     ///
210     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
211     
212     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
213     /// special global used by LLVM.  If so, emit it and return true, otherwise
214     /// do nothing and return false.
215     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
216     
217   public:
218     //===------------------------------------------------------------------===//
219     /// LEB 128 number encoding.
220
221     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
222     /// representing an unsigned leb128 value.
223     void PrintULEB128(unsigned Value) const;
224
225     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
226     /// representing a signed leb128 value.
227     void PrintSLEB128(int Value) const;
228
229     //===------------------------------------------------------------------===//
230     // Emission and print routines
231     //
232
233     /// PrintHex - Print a value as a hexidecimal value.
234     ///
235     void PrintHex(int Value) const;
236
237     /// EOL - Print a newline character to asm stream.  If a comment is present
238     /// then it will be printed first.  Comments should not contain '\n'.
239     void EOL() const;
240     void EOL(const std::string &Comment) const;
241     void EOL(const char* Comment) const;
242     
243     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
244     /// unsigned leb128 value.
245     void EmitULEB128Bytes(unsigned Value) const;
246     
247     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
248     /// signed leb128 value.
249     void EmitSLEB128Bytes(int Value) const;
250     
251     /// EmitInt8 - Emit a byte directive and value.
252     ///
253     void EmitInt8(int Value) const;
254
255     /// EmitInt16 - Emit a short directive and value.
256     ///
257     void EmitInt16(int Value) const;
258
259     /// EmitInt32 - Emit a long directive and value.
260     ///
261     void EmitInt32(int Value) const;
262
263     /// EmitInt64 - Emit a long long directive and value.
264     ///
265     void EmitInt64(uint64_t Value) const;
266
267     /// EmitString - Emit a string with quotes and a null terminator.
268     /// Special characters are emitted properly.
269     /// @verbatim (Eg. '\t') @endverbatim
270     void EmitString(const std::string &String) const;
271     
272     /// EmitFile - Emit a .file directive.
273     void EmitFile(unsigned Number, const std::string &Name) const;
274
275     //===------------------------------------------------------------------===//
276
277     /// EmitAlignment - Emit an alignment directive to the specified power of
278     /// two boundary.  For example, if you pass in 3 here, you will get an 8
279     /// byte alignment.  If a global value is specified, and if that global has
280     /// an explicit alignment requested, it will unconditionally override the
281     /// alignment request.  However, if ForcedAlignBits is specified, this value
282     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
283     /// and the alignment computed with NumBits and the global.  If UseFillExpr
284     /// is true, it also emits an optional second value FillValue which the
285     /// assembler uses to fill gaps to match alignment for text sections if the
286     /// has specified a non-zero fill value.
287     ///
288     /// The algorithm is:
289     ///     Align = NumBits;
290     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
291     ///     Align = std::max(Align, ForcedAlignBits);
292     ///
293     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
294                        unsigned ForcedAlignBits = 0,
295                        bool UseFillExpr = true) const;
296
297     /// printLabel - This method prints a local label used by debug and
298     /// exception handling tables.
299     void printLabel(const MachineInstr *MI) const;
300     void printLabel(unsigned Id) const;
301
302     /// printDeclare - This method prints a local variable declaration used by
303     /// debug tables.
304     void printDeclare(const MachineInstr *MI) const;
305
306   protected:
307     /// EmitZeros - Emit a block of zeros.
308     ///
309     void EmitZeros(uint64_t NumZeros) const;
310
311     /// EmitString - Emit a zero-byte-terminated string constant.
312     ///
313     virtual void EmitString(const ConstantArray *CVA) const;
314
315     /// EmitConstantValueOnly - Print out the specified constant, without a
316     /// storage class.  Only constants of first-class type are allowed here.
317     void EmitConstantValueOnly(const Constant *CV);
318
319     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
320     void EmitGlobalConstant(const Constant* CV);
321
322     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
323     
324     /// printInlineAsm - This method formats and prints the specified machine
325     /// instruction that is an inline asm.
326     void printInlineAsm(const MachineInstr *MI) const;
327
328     /// printImplicitDef - This method prints the specified machine instruction
329     /// that is an implicit def.
330     virtual void printImplicitDef(const MachineInstr *MI) const;
331     
332     /// printBasicBlockLabel - This method prints the label for the specified
333     /// MachineBasicBlock
334     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
335                                       bool printAlign = false,
336                                       bool printColon = false,
337                                       bool printComment = true) const;
338                                       
339     /// printPICJumpTableSetLabel - This method prints a set label for the
340     /// specified MachineBasicBlock for a jumptable entry.
341     virtual void printPICJumpTableSetLabel(unsigned uid,
342                                            const MachineBasicBlock *MBB) const;
343     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
344                                            const MachineBasicBlock *MBB) const;
345     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
346                                         const MachineBasicBlock *MBB,
347                                         unsigned uid) const;
348     
349     /// printDataDirective - This method prints the asm directive for the
350     /// specified type.
351     void printDataDirective(const Type *type);
352
353     /// printSuffixedName - This prints a name with preceding 
354     /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
355     /// correctly.
356     void printSuffixedName(const char *Name, const char *Suffix,
357                            const char *Prefix = 0);
358     void printSuffixedName(const std::string &Name, const char* Suffix);
359
360     /// printVisibility - This prints visibility information about symbol, if
361     /// this is suported by the target.
362     void printVisibility(const std::string& Name, unsigned Visibility) const;
363
364   private:
365     const GlobalValue *findGlobalValue(const Constant* CV);
366     void EmitLLVMUsedList(Constant *List);
367     void EmitXXStructorList(Constant *List);
368     void EmitConstantPool(unsigned Alignment, const char *Section,
369                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
370     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
371   };
372 }
373
374 #endif