Added hook hasReservedCallFrame(). It returns true if the call frame is
[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     /// FunctionNumber - This provides a unique ID for each function emitted in
38     /// this translation unit.  It is autoincremented by SetupMachineFunction,
39     /// and can be accessed with getFunctionNumber() and 
40     /// IncrementFunctionNumber().
41     ///
42     unsigned FunctionNumber;
43
44   protected:
45     // Necessary for external weak linkage support
46     std::set<const GlobalValue*> ExtWeakSymbols;
47
48   public:
49     /// Output stream on which we're printing assembly code.
50     ///
51     std::ostream &O;
52
53     /// Target machine description.
54     ///
55     TargetMachine &TM;
56     
57     /// Target Asm Printer information.
58     ///
59     const TargetAsmInfo *TAI;
60
61     /// Name-mangler for global names.
62     ///
63     Mangler *Mang;
64
65     /// Cache of mangled name for current function. This is recalculated at the
66     /// beginning of each call to runOnMachineFunction().
67     ///
68     std::string CurrentFnName;
69     
70     /// CurrentSection - The current section we are emitting to.  This is
71     /// controlled and used by the SwitchSection method.
72     std::string CurrentSection;
73   
74   protected:
75     AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
76     
77   public:
78     /// SwitchToTextSection - Switch to the specified section of the executable
79     /// if we are not already in it!  If GV is non-null and if the global has an
80     /// explicitly requested section, we switch to the section indicated for the
81     /// global instead of NewSection.
82     ///
83     /// If the new section is an empty string, this method forgets what the
84     /// current section is, but does not emit a .section directive.
85     ///
86     /// This method is used when about to emit executable code.
87     ///
88     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
89
90     /// SwitchToDataSection - Switch to the specified section of the executable
91     /// if we are not already in it!  If GV is non-null and if the global has an
92     /// explicitly requested section, we switch to the section indicated for the
93     /// global instead of NewSection.
94     ///
95     /// If the new section is an empty string, this method forgets what the
96     /// current section is, but does not emit a .section directive.
97     ///
98     /// This method is used when about to emit data.  For most assemblers, this
99     /// is the same as the SwitchToTextSection method, but not all assemblers
100     /// are the same.
101     ///
102     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
103     
104     /// getGlobalLinkName - Returns the asm/link name of of the specified
105     /// global variable.  Should be overridden by each target asm printer to
106     /// generate the appropriate value.
107     virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
108
109     /// EmitExternalGlobal - Emit the external reference to a global variable.
110     /// Should be overridden if an indirect reference should be used.
111     virtual void EmitExternalGlobal(const GlobalVariable *GV);
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     /// getFunctionNumber - Return a unique ID for the current function.
157     ///
158     unsigned getFunctionNumber() const { return FunctionNumber; }
159     
160     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
161     /// not normally call this, as the counter is automatically bumped by
162     /// SetupMachineFunction.
163     void IncrementFunctionNumber() { FunctionNumber++; }
164     
165     /// EmitConstantPool - Print to the current output stream assembly
166     /// representations of the constants in the constant pool MCP. This is
167     /// used to print out constants which have been "spilled to memory" by
168     /// the code generator.
169     ///
170     void EmitConstantPool(MachineConstantPool *MCP);
171
172     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
173     /// used by the current function to the current output stream.  
174     ///
175     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
176     
177     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
178     /// special global used by LLVM.  If so, emit it and return true, otherwise
179     /// do nothing and return false.
180     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
181     
182   public:
183     //===------------------------------------------------------------------===//
184     /// LEB 128 number encoding.
185
186     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
187     /// representing an unsigned leb128 value.
188     void PrintULEB128(unsigned Value) const;
189
190     /// SizeULEB128 - Compute the number of bytes required for an unsigned
191     /// leb128 value.
192     static unsigned SizeULEB128(unsigned Value);
193
194     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
195     /// representing a signed leb128 value.
196     void PrintSLEB128(int Value) const;
197
198     /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
199     /// value.
200     static unsigned SizeSLEB128(int Value);
201     
202     //===------------------------------------------------------------------===//
203     // Emission and print routines
204     //
205
206     /// PrintHex - Print a value as a hexidecimal value.
207     ///
208     void PrintHex(int Value) const;
209
210     /// EOL - Print a newline character to asm stream.  If a comment is present
211     /// then it will be printed first.  Comments should not contain '\n'.
212     void EOL() const;
213     void EOL(const std::string &Comment) const;
214     
215     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
216     /// unsigned leb128 value.
217     void EmitULEB128Bytes(unsigned Value) const;
218     
219     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
220     /// signed leb128 value.
221     void EmitSLEB128Bytes(int Value) const;
222     
223     /// EmitInt8 - Emit a byte directive and value.
224     ///
225     void EmitInt8(int Value) const;
226
227     /// EmitInt16 - Emit a short directive and value.
228     ///
229     void EmitInt16(int Value) const;
230
231     /// EmitInt32 - Emit a long directive and value.
232     ///
233     void EmitInt32(int Value) const;
234
235     /// EmitInt64 - Emit a long long directive and value.
236     ///
237     void EmitInt64(uint64_t Value) const;
238
239     /// EmitString - Emit a string with quotes and a null terminator.
240     /// Special characters are emitted properly.
241     /// \literal (Eg. '\t') \endliteral
242     void EmitString(const std::string &String) const;
243     
244     //===------------------------------------------------------------------===//
245
246     /// EmitAlignment - Emit an alignment directive to the specified power of
247     /// two boundary.  For example, if you pass in 3 here, you will get an 8
248     /// byte alignment.  If a global value is specified, and if that global has
249     /// an explicit alignment requested, it will override the alignment request.
250     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
251
252   protected:
253     /// EmitZeros - Emit a block of zeros.
254     ///
255     void EmitZeros(uint64_t NumZeros) const;
256
257     /// EmitString - Emit a zero-byte-terminated string constant.
258     ///
259     virtual void EmitString(const ConstantArray *CVA) const;
260
261     /// EmitConstantValueOnly - Print out the specified constant, without a
262     /// storage class.  Only constants of first-class type are allowed here.
263     void EmitConstantValueOnly(const Constant *CV);
264
265     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
266     ///
267     void EmitGlobalConstant(const Constant* CV);
268
269     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
270     
271     /// printInlineAsm - This method formats and prints the specified machine
272     /// instruction that is an inline asm.
273     void printInlineAsm(const MachineInstr *MI) const;
274     
275     /// printLabel - This method prints a local label used by debug and
276     /// exception handling tables.
277     void printLabel(const MachineInstr *MI) const;
278
279     /// printBasicBlockLabel - This method prints the label for the specified
280     /// MachineBasicBlock
281     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
282                                       bool printColon = false,
283                                       bool printComment = true) const;
284                                       
285     /// printSetLabel - This method prints a set label for the specified
286     /// MachineBasicBlock
287     void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
288     void printSetLabel(unsigned uid, unsigned uid2,
289                        const MachineBasicBlock *MBB) const;
290
291     /// printDataDirective - This method prints the asm directive for the
292     /// specified type.
293     void printDataDirective(const Type *type);
294
295   private:
296     void EmitLLVMUsedList(Constant *List);
297     void EmitXXStructorList(Constant *List);
298     void EmitConstantPool(unsigned Alignment, const char *Section,
299                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
300
301   };
302 }
303
304 #endif