Add a predicate
[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     /// StaticCtorsSection - This is the directive that is emitted to switch to
144     /// a section to emit the static constructor list.
145     /// Defaults to "\t.section .ctors,\"aw\",@progbits".
146     const char *StaticCtorsSection;
147
148     /// StaticDtorsSection - This is the directive that is emitted to switch to
149     /// a section to emit the static destructor list.
150     /// Defaults to "\t.section .dtors,\"aw\",@progbits".
151     const char *StaticDtorsSection;
152     
153     //===--- Global Variable Emission Directives --------------------------===//
154     
155     /// LCOMMDirective - This is the name of a directive (if supported) that can
156     /// be used to efficiently declare a local (internal) block of zero
157     /// initialized data in the .bss/.data section.  The syntax expected is:
158     ///    <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
159     const char *LCOMMDirective;          // Defaults to null.
160     
161     const char *COMMDirective;           // Defaults to "\t.comm\t".
162     
163     /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
164     /// argument that specifies the alignment of the declaration.
165     bool COMMDirectiveTakesAlignment;    // Defaults to true.
166     
167     /// HasDotTypeDotSizeDirective - True if the target has .type and .size
168     /// directives, this is true for most ELF targets.
169     bool HasDotTypeDotSizeDirective;     // Defaults to true.
170   
171   protected:
172     AsmPrinter(std::ostream &o, TargetMachine &TM);
173     
174   public:
175     /// SwitchSection - Switch to the specified section of the executable if we
176     /// are not already in it!  If GV is non-null and if the global has an
177     /// explicitly requested section, we switch to the section indicated for the
178     /// global instead of NewSection.
179     ///
180     /// If the new section is an empty string, this method forgets what the
181     /// current section is, but does not emit a .section directive.
182     ///
183     void SwitchSection(const char *NewSection, const GlobalValue *GV);
184
185     /// getPreferredAlignmentLog - Return the preferred alignment of the
186     /// specified global, returned in log form.  This includes an explicitly
187     /// requested alignment (if the global has one).
188     unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
189   protected:
190     /// doInitialization - Set up the AsmPrinter when we are working on a new
191     /// module.  If your pass overrides this, it must make sure to explicitly
192     /// call this implementation.
193     bool doInitialization(Module &M);
194
195     /// doFinalization - Shut down the asmprinter.  If you override this in your
196     /// pass, you must make sure to call it explicitly.
197     bool doFinalization(Module &M);
198
199     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
200     /// instruction, using the specified assembler variant.  Targets should
201     /// override this to format as appropriate.  This method can return true if
202     /// the operand is erroneous.
203     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
204                                  unsigned AsmVariant, const char *ExtraCode);
205     
206     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
207     /// instruction, using the specified assembler variant as an address.
208     /// Targets should override this to format as appropriate.  This method can
209     /// return true if the operand is erroneous.
210     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
211                                        unsigned AsmVariant, 
212                                        const char *ExtraCode);
213     
214     /// SetupMachineFunction - This should be called when a new MachineFunction
215     /// is being processed from runOnMachineFunction.
216     void SetupMachineFunction(MachineFunction &MF);
217     
218     /// getFunctionNumber - Return a unique ID for the current function.
219     ///
220     unsigned getFunctionNumber() const { return FunctionNumber; }
221     
222     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
223     /// not normally call this, as the counter is automatically bumped by
224     /// SetupMachineFunction.
225     void IncrementFunctionNumber() { FunctionNumber++; }
226     
227     /// EmitConstantPool - Print to the current output stream assembly
228     /// representations of the constants in the constant pool MCP. This is
229     /// used to print out constants which have been "spilled to memory" by
230     /// the code generator.
231     ///
232     void EmitConstantPool(MachineConstantPool *MCP);
233
234     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
235     /// special global used by LLVM.  If so, emit it and return true, otherwise
236     /// do nothing and return false.
237     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
238
239     /// EmitAlignment - Emit an alignment directive to the specified power of
240     /// two boundary.  For example, if you pass in 3 here, you will get an 8
241     /// byte alignment.  If a global value is specified, and if that global has
242     /// an explicit alignment requested, it will override the alignment request.
243     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
244
245     /// EmitZeros - Emit a block of zeros.
246     ///
247     void EmitZeros(uint64_t NumZeros) const;
248
249     /// EmitConstantValueOnly - Print out the specified constant, without a
250     /// storage class.  Only constants of first-class type are allowed here.
251     void EmitConstantValueOnly(const Constant *CV);
252
253     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
254     ///
255     void EmitGlobalConstant(const Constant* CV);
256     
257     /// printInlineAsm - This method formats and prints the specified machine
258     /// instruction that is an inline asm.
259     void printInlineAsm(const MachineInstr *MI) const;
260   private:
261     void EmitXXStructorList(Constant *List);
262
263   };
264 }
265
266 #endif