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