Handle quoted names when constructing $stub's,
[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     /// SizeULEB128 - Compute the number of bytes required for an unsigned
216     /// leb128 value.
217     static unsigned SizeULEB128(unsigned Value);
218
219     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
220     /// representing a signed leb128 value.
221     void PrintSLEB128(int Value) const;
222
223     /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
224     /// value.
225     static unsigned SizeSLEB128(int Value);
226     
227     //===------------------------------------------------------------------===//
228     // Emission and print routines
229     //
230
231     /// PrintHex - Print a value as a hexidecimal value.
232     ///
233     void PrintHex(int Value) const;
234
235     /// EOL - Print a newline character to asm stream.  If a comment is present
236     /// then it will be printed first.  Comments should not contain '\n'.
237     void EOL() const;
238     void EOL(const std::string &Comment) const;
239     
240     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
241     /// unsigned leb128 value.
242     void EmitULEB128Bytes(unsigned Value) const;
243     
244     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
245     /// signed leb128 value.
246     void EmitSLEB128Bytes(int Value) const;
247     
248     /// EmitInt8 - Emit a byte directive and value.
249     ///
250     void EmitInt8(int Value) const;
251
252     /// EmitInt16 - Emit a short directive and value.
253     ///
254     void EmitInt16(int Value) const;
255
256     /// EmitInt32 - Emit a long directive and value.
257     ///
258     void EmitInt32(int Value) const;
259
260     /// EmitInt64 - Emit a long long directive and value.
261     ///
262     void EmitInt64(uint64_t Value) const;
263
264     /// EmitString - Emit a string with quotes and a null terminator.
265     /// Special characters are emitted properly.
266     /// @verbatim (Eg. '\t') @endverbatim
267     void EmitString(const std::string &String) const;
268     
269     /// EmitFile - Emit a .file directive.
270     void EmitFile(unsigned Number, const std::string &Name) const;
271
272     //===------------------------------------------------------------------===//
273
274     /// EmitAlignment - Emit an alignment directive to the specified power of
275     /// two boundary.  For example, if you pass in 3 here, you will get an 8
276     /// byte alignment.  If a global value is specified, and if that global has
277     /// an explicit alignment requested, it will unconditionally override the
278     /// alignment request.  However, if ForcedAlignBits is specified, this value
279     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
280     /// and the alignment computed with NumBits and the global.  If UseFillExpr
281     /// is true, it also emits an optional second value FillValue which the
282     /// assembler uses to fill gaps to match alignment for text sections if the
283     /// has specified a non-zero fill value.
284     ///
285     /// The algorithm is:
286     ///     Align = NumBits;
287     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
288     ///     Align = std::max(Align, ForcedAlignBits);
289     ///
290     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
291                        unsigned ForcedAlignBits = 0,
292                        bool UseFillExpr = true) const;
293
294     /// printLabel - This method prints a local label used by debug and
295     /// exception handling tables.
296     void printLabel(const MachineInstr *MI) const;
297     void printLabel(unsigned Id) const;
298
299     /// printDeclare - This method prints a local variable declaration used by
300     /// debug tables.
301     void printDeclare(const MachineInstr *MI) const;
302
303   protected:
304     /// EmitZeros - Emit a block of zeros.
305     ///
306     void EmitZeros(uint64_t NumZeros) const;
307
308     /// EmitString - Emit a zero-byte-terminated string constant.
309     ///
310     virtual void EmitString(const ConstantArray *CVA) const;
311
312     /// EmitConstantValueOnly - Print out the specified constant, without a
313     /// storage class.  Only constants of first-class type are allowed here.
314     void EmitConstantValueOnly(const Constant *CV);
315
316     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
317     /// If Packed is false, pad to the ABI size.
318     void EmitGlobalConstant(const Constant* CV, bool Packed = false);
319
320     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
321     
322     /// printInlineAsm - This method formats and prints the specified machine
323     /// instruction that is an inline asm.
324     void printInlineAsm(const MachineInstr *MI) const;
325
326     /// printImplicitDef - This method prints the specified machine instruction
327     /// that is an implicit def.
328     virtual void printImplicitDef(const MachineInstr *MI) const;
329     
330     /// printBasicBlockLabel - This method prints the label for the specified
331     /// MachineBasicBlock
332     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
333                                       bool printAlign = false,
334                                       bool printColon = false,
335                                       bool printComment = true) const;
336                                       
337     /// printPICJumpTableSetLabel - This method prints a set label for the
338     /// specified MachineBasicBlock for a jumptable entry.
339     virtual void printPICJumpTableSetLabel(unsigned uid,
340                                            const MachineBasicBlock *MBB) const;
341     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
342                                            const MachineBasicBlock *MBB) const;
343     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
344                                         const MachineBasicBlock *MBB,
345                                         unsigned uid) const;
346     
347     /// printDataDirective - This method prints the asm directive for the
348     /// specified type.
349     void printDataDirective(const Type *type);
350
351     /// printSuffixedName - This prints a name with preceding 
352     /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
353     /// correctly.
354     void printSuffixedName(std::string &Name, const char* Suffix);
355
356   private:
357     void EmitLLVMUsedList(Constant *List);
358     void EmitXXStructorList(Constant *List);
359     void EmitConstantPool(unsigned Alignment, const char *Section,
360                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
361
362   };
363 }
364
365 #endif