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