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