Change the names used for internal labels to use the current
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <set>
22
23 namespace llvm {
24   class Constant;
25   class ConstantArray;
26   class GlobalVariable;
27   class GlobalAlias;
28   class MachineConstantPoolEntry;
29   class MachineConstantPoolValue;
30   class Mangler;
31   class TargetAsmInfo;
32   
33
34   /// AsmPrinter - This class is intended to be used as a driving class for all
35   /// asm writers.
36   class AsmPrinter : public MachineFunctionPass {
37     static char ID;
38
39   protected:
40     // Necessary for external weak linkage support
41     std::set<const GlobalValue*> ExtWeakSymbols;
42
43   public:
44     /// Output stream on which we're printing assembly code.
45     ///
46     std::ostream &O;
47
48     /// Target machine description.
49     ///
50     TargetMachine &TM;
51     
52     /// Target Asm Printer information.
53     ///
54     const TargetAsmInfo *TAI;
55
56     /// Name-mangler for global names.
57     ///
58     Mangler *Mang;
59
60     /// Cache of mangled name for current function. This is recalculated at the
61     /// beginning of each call to runOnMachineFunction().
62     ///
63     std::string CurrentFnName;
64     
65     /// CurrentSection - The current section we are emitting to.  This is
66     /// controlled and used by the SwitchSection method.
67     std::string CurrentSection;
68   
69   protected:
70     AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
71     
72   public:
73     /// SwitchToTextSection - Switch to the specified section of the executable
74     /// if we are not already in it!  If GV is non-null and if the global has an
75     /// explicitly requested section, we switch to the section indicated for the
76     /// global instead of NewSection.
77     ///
78     /// If the new section is an empty string, this method forgets what the
79     /// current section is, but does not emit a .section directive.
80     ///
81     /// This method is used when about to emit executable code.
82     ///
83     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
84
85     /// SwitchToDataSection - Switch to the specified section of the executable
86     /// if we are not already in it!  If GV is non-null and if the global has an
87     /// explicitly requested section, we switch to the section indicated for the
88     /// global instead of NewSection.
89     ///
90     /// If the new section is an empty string, this method forgets what the
91     /// current section is, but does not emit a .section directive.
92     ///
93     /// This method is used when about to emit data.  For most assemblers, this
94     /// is the same as the SwitchToTextSection method, but not all assemblers
95     /// are the same.
96     ///
97     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
98     
99     /// getGlobalLinkName - Returns the asm/link name of of the specified
100     /// global variable.  Should be overridden by each target asm printer to
101     /// generate the appropriate value.
102     virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
103
104     /// EmitExternalGlobal - Emit the external reference to a global variable.
105     /// Should be overridden if an indirect reference should be used.
106     virtual void EmitExternalGlobal(const GlobalVariable *GV);
107
108     /// getCurrentFunctionEHName - Called to return (and cache) the
109     /// CurrentFnEHName.
110     /// 
111     std::string getCurrentFunctionEHName(const MachineFunction *MF);
112
113   protected:
114     /// doInitialization - Set up the AsmPrinter when we are working on a new
115     /// module.  If your pass overrides this, it must make sure to explicitly
116     /// call this implementation.
117     bool doInitialization(Module &M);
118
119     /// doFinalization - Shut down the asmprinter.  If you override this in your
120     /// pass, you must make sure to call it explicitly.
121     bool doFinalization(Module &M);
122     
123     /// PrintSpecial - Print information related to the specified machine instr
124     /// that is independent of the operand, and may be independent of the instr
125     /// itself.  This can be useful for portably encoding the comment character
126     /// or other bits of target-specific knowledge into the asmstrings.  The
127     /// syntax used is ${:comment}.  Targets can override this to add support
128     /// for their own strange codes.
129     virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
130
131     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
132     /// instruction, using the specified assembler variant.  Targets should
133     /// override this to format as appropriate.  This method can return true if
134     /// the operand is erroneous.
135     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
136                                  unsigned AsmVariant, const char *ExtraCode);
137     
138     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
139     /// instruction, using the specified assembler variant as an address.
140     /// Targets should override this to format as appropriate.  This method can
141     /// return true if the operand is erroneous.
142     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
143                                        unsigned AsmVariant, 
144                                        const char *ExtraCode);
145     
146     /// getSectionForFunction - Return the section that we should emit the
147     /// specified function body into.  This defaults to 'TextSection'.  This
148     /// should most likely be overridden by the target to put linkonce/weak
149     /// functions into special sections.
150     virtual std::string getSectionForFunction(const Function &F) const;
151     
152     /// SetupMachineFunction - This should be called when a new MachineFunction
153     /// is being processed from runOnMachineFunction.
154     void SetupMachineFunction(MachineFunction &MF);
155     
156     /// EmitConstantPool - Print to the current output stream assembly
157     /// representations of the constants in the constant pool MCP. This is
158     /// used to print out constants which have been "spilled to memory" by
159     /// the code generator.
160     ///
161     void EmitConstantPool(MachineConstantPool *MCP);
162
163     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
164     /// used by the current function to the current output stream.  
165     ///
166     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
167     
168     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
169     /// special global used by LLVM.  If so, emit it and return true, otherwise
170     /// do nothing and return false.
171     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
172     
173   public:
174     //===------------------------------------------------------------------===//
175     /// LEB 128 number encoding.
176
177     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
178     /// representing an unsigned leb128 value.
179     void PrintULEB128(unsigned Value) const;
180
181     /// SizeULEB128 - Compute the number of bytes required for an unsigned
182     /// leb128 value.
183     static unsigned SizeULEB128(unsigned Value);
184
185     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
186     /// representing a signed leb128 value.
187     void PrintSLEB128(int Value) const;
188
189     /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
190     /// value.
191     static unsigned SizeSLEB128(int Value);
192     
193     //===------------------------------------------------------------------===//
194     // Emission and print routines
195     //
196
197     /// PrintHex - Print a value as a hexidecimal value.
198     ///
199     void PrintHex(int Value) const;
200
201     /// EOL - Print a newline character to asm stream.  If a comment is present
202     /// then it will be printed first.  Comments should not contain '\n'.
203     void EOL() const;
204     void EOL(const std::string &Comment) const;
205     
206     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
207     /// unsigned leb128 value.
208     void EmitULEB128Bytes(unsigned Value) const;
209     
210     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
211     /// signed leb128 value.
212     void EmitSLEB128Bytes(int Value) const;
213     
214     /// EmitInt8 - Emit a byte directive and value.
215     ///
216     void EmitInt8(int Value) const;
217
218     /// EmitInt16 - Emit a short directive and value.
219     ///
220     void EmitInt16(int Value) const;
221
222     /// EmitInt32 - Emit a long directive and value.
223     ///
224     void EmitInt32(int Value) const;
225
226     /// EmitInt64 - Emit a long long directive and value.
227     ///
228     void EmitInt64(uint64_t Value) const;
229
230     /// EmitString - Emit a string with quotes and a null terminator.
231     /// Special characters are emitted properly.
232     /// @verbatim (Eg. '\t') @endverbatim
233     void EmitString(const std::string &String) const;
234     
235     /// EmitFile - Emit a .file directive.
236     void EmitFile(unsigned Number, const std::string &Name) const;
237
238     //===------------------------------------------------------------------===//
239
240     /// EmitAlignment - Emit an alignment directive to the specified power of
241     /// two boundary.  For example, if you pass in 3 here, you will get an 8
242     /// byte alignment.  If a global value is specified, and if that global has
243     /// an explicit alignment requested, it will unconditionally override the
244     /// alignment request.  However, if ForcedAlignBits is specified, this value
245     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
246     /// and the alignment computed with NumBits and the global. If UseFillExpr
247     /// is true, it also emits an optional second value FillValue which the
248     /// assembler uses to fill gaps to match alignment.
249     ///
250     /// The algorithm is:
251     ///     Align = NumBits;
252     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
253     ///     Align = std::max(Align, ForcedAlignBits);
254     ///
255     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
256                        unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
257                        unsigned FillValue = 0) const;
258
259   protected:
260     /// EmitZeros - Emit a block of zeros.
261     ///
262     void EmitZeros(uint64_t NumZeros) const;
263
264     /// EmitString - Emit a zero-byte-terminated string constant.
265     ///
266     virtual void EmitString(const ConstantArray *CVA) const;
267
268     /// EmitConstantValueOnly - Print out the specified constant, without a
269     /// storage class.  Only constants of first-class type are allowed here.
270     void EmitConstantValueOnly(const Constant *CV);
271
272     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
273     ///
274     void EmitGlobalConstant(const Constant* CV);
275
276     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
277     
278     /// printInlineAsm - This method formats and prints the specified machine
279     /// instruction that is an inline asm.
280     void printInlineAsm(const MachineInstr *MI) const;
281     
282     /// printLabel - This method prints a local label used by debug and
283     /// exception handling tables.
284     void printLabel(const MachineInstr *MI) const;
285
286     /// printBasicBlockLabel - This method prints the label for the specified
287     /// MachineBasicBlock
288     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
289                                       bool printColon = false,
290                                       bool printComment = true) const;
291                                       
292     /// printSetLabel - This method prints a set label for the specified
293     /// MachineBasicBlock
294     void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
295     void printSetLabel(unsigned uid, unsigned uid2,
296                        const MachineBasicBlock *MBB) const;
297
298     /// printDataDirective - This method prints the asm directive for the
299     /// specified type.
300     void printDataDirective(const Type *type);
301
302   private:
303     void EmitLLVMUsedList(Constant *List);
304     void EmitXXStructorList(Constant *List);
305     void EmitConstantPool(unsigned Alignment, const char *Section,
306                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
307
308   };
309 }
310
311 #endif