JumpTable support! What this represents is working asm and jit support for
[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 class is intended to be used as a base class for target-specific
11 // asmwriters.  This class primarily takes care of printing global constants,
12 // which are printed in a very similar way across all targets.
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
22 namespace llvm {
23   class Constant;
24   class Mangler;
25   class GlobalVariable;
26
27   class AsmPrinter : public MachineFunctionPass {
28     /// CurrentSection - The current section we are emitting to.  This is
29     /// controlled and used by the SwitchSection method.
30     std::string CurrentSection;
31     
32     /// FunctionNumber - This provides a unique ID for each function emitted in
33     /// this translation unit.  It is autoincremented by SetupMachineFunction,
34     /// and can be accessed with getFunctionNumber() and 
35     /// IncrementFunctionNumber().
36     ///
37     unsigned FunctionNumber;
38
39   public:
40     /// Output stream on which we're printing assembly code.
41     ///
42     std::ostream &O;
43
44     /// Target machine description.
45     ///
46     TargetMachine &TM;
47
48     /// Name-mangler for global names.
49     ///
50     Mangler *Mang;
51
52     /// Cache of mangled name for current function. This is recalculated at the
53     /// beginning of each call to runOnMachineFunction().
54     ///
55     std::string CurrentFnName;
56
57     //===------------------------------------------------------------------===//
58     // Properties to be set by the derived class ctor, used to configure the
59     // asmwriter.
60
61     /// CommentString - This indicates the comment character used by the
62     /// assembler.
63     const char *CommentString;     // Defaults to "#"
64
65     /// GlobalPrefix - If this is set to a non-empty string, it is prepended
66     /// onto all global symbols.  This is often used for "_" or ".".
67     const char *GlobalPrefix;    // Defaults to ""
68
69     /// PrivateGlobalPrefix - This prefix is used for globals like constant
70     /// pool entries that are completely private to the .o file and should not
71     /// have names in the .o file.  This is often "." or "L".
72     const char *PrivateGlobalPrefix;   // Defaults to "."
73     
74     /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
75     /// will enclose any GlobalVariable (that isn't a function)
76     ///
77     const char *GlobalVarAddrPrefix;       // Defaults to ""
78     const char *GlobalVarAddrSuffix;       // Defaults to ""
79
80     /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
81     /// will enclose any GlobalVariable that points to a function.
82     /// For example, this is used by the IA64 backend to materialize
83     /// function descriptors, by decorating the ".data8" object with the
84     /// @fptr( ) link-relocation operator.
85     ///
86     const char *FunctionAddrPrefix;       // Defaults to ""
87     const char *FunctionAddrSuffix;       // Defaults to ""
88
89     /// InlineAsmStart/End - If these are nonempty, they contain a directive to
90     /// emit before and after an inline assmebly statement.
91     const char *InlineAsmStart;           // Defaults to "#APP\n"
92     const char *InlineAsmEnd;             // Defaults to "#NO_APP\n"
93     
94     //===--- Data Emission Directives -------------------------------------===//
95
96     /// ZeroDirective - this should be set to the directive used to get some
97     /// number of zero bytes emitted to the current section.  Common cases are
98     /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
99     /// Data*bitsDirective's will be used to emit zero bytes.
100     const char *ZeroDirective;   // Defaults to "\t.zero\t"
101
102     /// AsciiDirective - This directive allows emission of an ascii string with
103     /// the standard C escape characters embedded into it.
104     const char *AsciiDirective;  // Defaults to "\t.ascii\t"
105     
106     /// AscizDirective - If not null, this allows for special handling of
107     /// zero terminated strings on this target.  This is commonly supported as
108     /// ".asciz".  If a target doesn't support this, it can be set to null.
109     const char *AscizDirective;  // Defaults to "\t.asciz\t"
110
111     /// DataDirectives - These directives are used to output some unit of
112     /// integer data to the current section.  If a data directive is set to
113     /// null, smaller data directives will be used to emit the large sizes.
114     const char *Data8bitsDirective;   // Defaults to "\t.byte\t"
115     const char *Data16bitsDirective;  // Defaults to "\t.short\t"
116     const char *Data32bitsDirective;  // Defaults to "\t.long\t"
117     const char *Data64bitsDirective;  // Defaults to "\t.quad\t"
118
119     //===--- Alignment Information ----------------------------------------===//
120
121     /// AlignDirective - The directive used to emit round up to an alignment
122     /// boundary.
123     ///
124     const char *AlignDirective;       // Defaults to "\t.align\t"
125
126     /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
127     /// emits ".align N" directives, where N is the number of bytes to align to.
128     /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
129     /// boundary.
130     bool AlignmentIsInBytes;          // Defaults to true
131     
132     //===--- Section Switching Directives ---------------------------------===//
133     
134     /// SwitchToSectionDirective - This is the directive used when we want to
135     /// emit a global to an arbitrary section.  The section name is emited after
136     /// this.
137     const char *SwitchToSectionDirective;  // Defaults to "\t.section\t"
138     
139     /// ConstantPoolSection - This is the section that we SwitchToSection right
140     /// before emitting the constant pool for a function.
141     const char *ConstantPoolSection;     // Defaults to "\t.section .rodata\n"
142
143     /// JumpTableSection - This is the section that we SwitchToSection right
144     /// before emitting the jump tables for a function.
145     const char *JumpTableSection;     // Defaults to "\t.section .rodata\n"
146     
147     /// StaticCtorsSection - This is the directive that is emitted to switch to
148     /// a section to emit the static constructor list.
149     /// Defaults to "\t.section .ctors,\"aw\",@progbits".
150     const char *StaticCtorsSection;
151
152     /// StaticDtorsSection - This is the directive that is emitted to switch to
153     /// a section to emit the static destructor list.
154     /// Defaults to "\t.section .dtors,\"aw\",@progbits".
155     const char *StaticDtorsSection;
156     
157     //===--- Global Variable Emission Directives --------------------------===//
158     
159     /// LCOMMDirective - This is the name of a directive (if supported) that can
160     /// be used to efficiently declare a local (internal) block of zero
161     /// initialized data in the .bss/.data section.  The syntax expected is:
162     ///    <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
163     const char *LCOMMDirective;          // Defaults to null.
164     
165     const char *COMMDirective;           // Defaults to "\t.comm\t".
166     
167     /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
168     /// argument that specifies the alignment of the declaration.
169     bool COMMDirectiveTakesAlignment;    // Defaults to true.
170     
171     /// HasDotTypeDotSizeDirective - True if the target has .type and .size
172     /// directives, this is true for most ELF targets.
173     bool HasDotTypeDotSizeDirective;     // Defaults to true.
174   
175   protected:
176     AsmPrinter(std::ostream &o, TargetMachine &TM);
177     
178   public:
179     /// SwitchSection - Switch to the specified section of the executable if we
180     /// are not already in it!  If GV is non-null and if the global has an
181     /// explicitly requested section, we switch to the section indicated for the
182     /// global instead of NewSection.
183     ///
184     /// If the new section is an empty string, this method forgets what the
185     /// current section is, but does not emit a .section directive.
186     ///
187     void SwitchSection(const char *NewSection, const GlobalValue *GV);
188
189     /// getPreferredAlignmentLog - Return the preferred alignment of the
190     /// specified global, returned in log form.  This includes an explicitly
191     /// requested alignment (if the global has one).
192     unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
193   protected:
194     /// doInitialization - Set up the AsmPrinter when we are working on a new
195     /// module.  If your pass overrides this, it must make sure to explicitly
196     /// call this implementation.
197     bool doInitialization(Module &M);
198
199     /// doFinalization - Shut down the asmprinter.  If you override this in your
200     /// pass, you must make sure to call it explicitly.
201     bool doFinalization(Module &M);
202
203     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
204     /// instruction, using the specified assembler variant.  Targets should
205     /// override this to format as appropriate.  This method can return true if
206     /// the operand is erroneous.
207     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
208                                  unsigned AsmVariant, const char *ExtraCode);
209     
210     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
211     /// instruction, using the specified assembler variant as an address.
212     /// Targets should override this to format as appropriate.  This method can
213     /// return true if the operand is erroneous.
214     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
215                                        unsigned AsmVariant, 
216                                        const char *ExtraCode);
217     
218     /// SetupMachineFunction - This should be called when a new MachineFunction
219     /// is being processed from runOnMachineFunction.
220     void SetupMachineFunction(MachineFunction &MF);
221     
222     /// getFunctionNumber - Return a unique ID for the current function.
223     ///
224     unsigned getFunctionNumber() const { return FunctionNumber; }
225     
226     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
227     /// not normally call this, as the counter is automatically bumped by
228     /// SetupMachineFunction.
229     void IncrementFunctionNumber() { FunctionNumber++; }
230     
231     /// EmitConstantPool - Print to the current output stream assembly
232     /// representations of the constants in the constant pool MCP. This is
233     /// used to print out constants which have been "spilled to memory" by
234     /// the code generator.
235     ///
236     void EmitConstantPool(MachineConstantPool *MCP);
237
238     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
239     /// used by the current function to the current output stream.  
240     ///
241     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
242     
243     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
244     /// special global used by LLVM.  If so, emit it and return true, otherwise
245     /// do nothing and return false.
246     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
247
248     /// EmitAlignment - Emit an alignment directive to the specified power of
249     /// two boundary.  For example, if you pass in 3 here, you will get an 8
250     /// byte alignment.  If a global value is specified, and if that global has
251     /// an explicit alignment requested, it will override the alignment request.
252     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
253
254     /// EmitZeros - Emit a block of zeros.
255     ///
256     void EmitZeros(uint64_t NumZeros) const;
257
258     /// EmitConstantValueOnly - Print out the specified constant, without a
259     /// storage class.  Only constants of first-class type are allowed here.
260     void EmitConstantValueOnly(const Constant *CV);
261
262     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
263     ///
264     void EmitGlobalConstant(const Constant* CV);
265     
266     /// printInlineAsm - This method formats and prints the specified machine
267     /// instruction that is an inline asm.
268     void printInlineAsm(const MachineInstr *MI) const;
269     
270     /// printBasicBlockLabel - This method prints the label for the specified
271     /// MachineBasicBlock
272     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB) const;
273     
274   private:
275     void EmitXXStructorList(Constant *List);
276
277   };
278 }
279
280 #endif