Rename some GC classes so that their roll will hopefully be clearer.
[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 TargetAsmInfo;
36   class Type;
37
38   /// AsmPrinter - This class is intended to be used as a driving class for all
39   /// asm writers.
40   class AsmPrinter : public MachineFunctionPass {
41     static char ID;
42
43     /// FunctionNumber - This provides a unique ID for each function emitted in
44     /// this translation unit.  It is autoincremented by SetupMachineFunction,
45     /// and can be accessed with getFunctionNumber() and 
46     /// IncrementFunctionNumber().
47     ///
48     unsigned FunctionNumber;
49
50     /// MachineModuleInfo - This is needed because printDeclare() has to insert
51     /// DebugVariable entries into the dwarf table. This is a short term hack
52     /// that ought be fixed soon.
53     MachineModuleInfo *MMI;
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     // Necessary for external weak linkage support
62     std::set<const GlobalValue*> ExtWeakSymbols;
63
64   public:
65     /// Output stream on which we're printing assembly code.
66     ///
67     std::ostream &O;
68
69     /// Target machine description.
70     ///
71     TargetMachine &TM;
72     
73     /// Target Asm Printer information.
74     ///
75     const TargetAsmInfo *TAI;
76
77     /// Target Register Information.
78     ///
79     const TargetRegisterInfo *TRI;
80
81     /// Name-mangler for global names.
82     ///
83     Mangler *Mang;
84
85     /// Cache of mangled name for current function. This is recalculated at the
86     /// beginning of each call to runOnMachineFunction().
87     ///
88     std::string CurrentFnName;
89     
90     /// CurrentSection - The current section we are emitting to.  This is
91     /// controlled and used by the SwitchSection method.
92     std::string CurrentSection;
93
94     /// IsInTextSection - True if the current section we are emitting to is a
95     /// text section.
96     bool IsInTextSection;
97   
98   protected:
99     AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
100     
101   public:
102     virtual ~AsmPrinter();
103     
104     /// SwitchToTextSection - Switch to the specified section of the executable
105     /// if we are not already in it!  If GV is non-null and if the global has an
106     /// explicitly requested section, we switch to the section indicated for the
107     /// global instead of NewSection.
108     ///
109     /// If the new section is an empty string, this method forgets what the
110     /// current section is, but does not emit a .section directive.
111     ///
112     /// This method is used when about to emit executable code.
113     ///
114     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
115
116     /// SwitchToDataSection - Switch to the specified section of the executable
117     /// if we are not already in it!  If GV is non-null and if the global has an
118     /// explicitly requested section, we switch to the section indicated for the
119     /// global instead of NewSection.
120     ///
121     /// If the new section is an empty string, this method forgets what the
122     /// current section is, but does not emit a .section directive.
123     ///
124     /// This method is used when about to emit data.  For most assemblers, this
125     /// is the same as the SwitchToTextSection method, but not all assemblers
126     /// are the same.
127     ///
128     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
129     
130     /// getGlobalLinkName - Returns the asm/link name of of the specified
131     /// global variable.  Should be overridden by each target asm printer to
132     /// generate the appropriate value.
133     virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
134
135     /// EmitExternalGlobal - Emit the external reference to a global variable.
136     /// Should be overridden if an indirect reference should be used.
137     virtual void EmitExternalGlobal(const GlobalVariable *GV);
138
139     /// getCurrentFunctionEHName - Called to return (and cache) the
140     /// CurrentFnEHName.
141     /// 
142     std::string getCurrentFunctionEHName(const MachineFunction *MF);
143
144   protected:
145     /// getAnalysisUsage - Record analysis usage.
146     /// 
147     void getAnalysisUsage(AnalysisUsage &AU) const;
148     
149     /// doInitialization - Set up the AsmPrinter when we are working on a new
150     /// module.  If your pass overrides this, it must make sure to explicitly
151     /// call this implementation.
152     bool doInitialization(Module &M);
153
154     /// doFinalization - Shut down the asmprinter.  If you override this in your
155     /// pass, you must make sure to call it explicitly.
156     bool doFinalization(Module &M);
157     
158     /// PrintSpecial - Print information related to the specified machine instr
159     /// that is independent of the operand, and may be independent of the instr
160     /// itself.  This can be useful for portably encoding the comment character
161     /// or other bits of target-specific knowledge into the asmstrings.  The
162     /// syntax used is ${:comment}.  Targets can override this to add support
163     /// for their own strange codes.
164     virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
165
166     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
167     /// instruction, using the specified assembler variant.  Targets should
168     /// override this to format as appropriate.  This method can return true if
169     /// the operand is erroneous.
170     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
171                                  unsigned AsmVariant, const char *ExtraCode);
172     
173     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
174     /// instruction, using the specified assembler variant as an address.
175     /// Targets should override this to format as appropriate.  This method can
176     /// return true if the operand is erroneous.
177     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
178                                        unsigned AsmVariant, 
179                                        const char *ExtraCode);
180     
181     /// getSectionForFunction - Return the section that we should emit the
182     /// specified function body into.  This defaults to 'TextSection'.  This
183     /// should most likely be overridden by the target to put linkonce/weak
184     /// functions into special sections.
185     virtual std::string getSectionForFunction(const Function &F) const;
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     void EmitLLVMUsedList(Constant *List);
366     void EmitXXStructorList(Constant *List);
367     void EmitConstantPool(unsigned Alignment, const char *Section,
368                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
369     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
370   };
371 }
372
373 #endif