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