9f9b47e156e4a081425c2f320fa2a8ca11e255da
[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
26   class AsmPrinter : public MachineFunctionPass {
27     /// CurrentSection - The current section we are emitting to.  This is
28     /// controlled and used by the SwitchSection method.
29     std::string CurrentSection;
30     
31     /// FunctionNumber - This provides a unique ID for each function emitted in
32     /// this translation unit.  It is autoincremented by SetupMachineFunction,
33     /// and can be accessed with getFunctionNumber() and 
34     /// IncrementFunctionNumber().
35     ///
36     unsigned FunctionNumber;
37     
38   protected:
39     /// Output stream on which we're printing assembly code.
40     ///
41     std::ostream &O;
42
43     /// Target machine description.
44     ///
45     TargetMachine &TM;
46
47     /// Name-mangler for global names.
48     ///
49     Mangler *Mang;
50
51     /// Cache of mangled name for current function. This is recalculated at the
52     /// beginning of each call to runOnMachineFunction().
53     ///
54     std::string CurrentFnName;
55
56     //===------------------------------------------------------------------===//
57     // Properties to be set by the derived class ctor, used to configure the
58     // asmwriter.
59
60     /// CommentString - This indicates the comment character used by the
61     /// assembler.
62     const char *CommentString;     // Defaults to "#"
63
64     /// GlobalPrefix - If this is set to a non-empty string, it is prepended
65     /// onto all global symbols.  This is often used for "_" or ".".
66     const char *GlobalPrefix;    // Defaults to ""
67
68     /// PrivateGlobalPrefix - This prefix is used for globals like constant
69     /// pool entries that are completely private to the .o file and should not
70     /// have names in the .o file.  This is often "." or "L".
71     const char *PrivateGlobalPrefix;   // Defaults to "."
72     
73     /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
74     /// will enclose any GlobalVariable (that isn't a function)
75     ///
76     const char *GlobalVarAddrPrefix;       // Defaults to ""
77     const char *GlobalVarAddrSuffix;       // Defaults to ""
78
79     /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
80     /// will enclose any GlobalVariable that points to a function.
81     /// For example, this is used by the IA64 backend to materialize
82     /// function descriptors, by decorating the ".data8" object with the
83     /// @fptr( ) link-relocation operator.
84     ///
85     const char *FunctionAddrPrefix;       // Defaults to ""
86     const char *FunctionAddrSuffix;       // Defaults to ""
87
88     //===--- Data Emission Directives -------------------------------------===//
89
90     /// ZeroDirective - this should be set to the directive used to get some
91     /// number of zero bytes emitted to the current section.  Common cases are
92     /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
93     /// Data*bitsDirective's will be used to emit zero bytes.
94     const char *ZeroDirective;   // Defaults to "\t.zero\t"
95
96     /// AsciiDirective - This directive allows emission of an ascii string with
97     /// the standard C escape characters embedded into it.
98     const char *AsciiDirective;  // Defaults to "\t.ascii\t"
99     
100     /// AscizDirective - If not null, this allows for special handling of
101     /// zero terminated strings on this target.  This is commonly supported as
102     /// ".asciz".  If a target doesn't support this, it can be set to null.
103     const char *AscizDirective;  // Defaults to "\t.asciz\t"
104
105     /// DataDirectives - These directives are used to output some unit of
106     /// integer data to the current section.  If a data directive is set to
107     /// null, smaller data directives will be used to emit the large sizes.
108     const char *Data8bitsDirective;   // Defaults to "\t.byte\t"
109     const char *Data16bitsDirective;  // Defaults to "\t.short\t"
110     const char *Data32bitsDirective;  // Defaults to "\t.long\t"
111     const char *Data64bitsDirective;  // Defaults to "\t.quad\t"
112
113     //===--- Alignment Information ----------------------------------------===//
114
115     /// AlignDirective - The directive used to emit round up to an alignment
116     /// boundary.
117     ///
118     const char *AlignDirective;       // Defaults to "\t.align\t"
119
120     /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
121     /// emits ".align N" directives, where N is the number of bytes to align to.
122     /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
123     /// boundary.
124     bool AlignmentIsInBytes;          // Defaults to true
125     
126     //===--- Section Switching Directives ---------------------------------===//
127     
128     /// SwitchToSectionDirective - This is the directive used when we want to
129     /// emit a global to an arbitrary section.  The section name is emited after
130     /// this.
131     const char *SwitchToSectionDirective;  // Defaults to "\t.section\t"
132     
133     /// ConstantPoolSection - This is the section that we SwitchToSection right
134     /// before emitting the constant pool for a function.
135     const char *ConstantPoolSection;     // Defaults to "\t.section .rodata\n"
136
137     //===--- Global Variable Emission Directives --------------------------===//
138     
139     /// LCOMMDirective - This is the name of a directive (if supported) that can
140     /// be used to efficiently declare a local (internal) block of zero
141     /// initialized data in the .bss/.data section.  The syntax expected is:
142     ///    <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
143     const char *LCOMMDirective;          // Defaults to null.
144     
145     const char *COMMDirective;           // Defaults to "\t.comm\t".
146     
147     /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
148     /// argument that specifies the alignment of the declaration.
149     bool COMMDirectiveTakesAlignment;    // Defaults to true.
150
151     AsmPrinter(std::ostream &o, TargetMachine &tm)
152       : FunctionNumber(0), O(o), TM(tm),
153         CommentString("#"),
154         GlobalPrefix(""),
155         PrivateGlobalPrefix("."),
156         GlobalVarAddrPrefix(""),
157         GlobalVarAddrSuffix(""),
158         FunctionAddrPrefix(""),
159         FunctionAddrSuffix(""),
160         ZeroDirective("\t.zero\t"),
161         AsciiDirective("\t.ascii\t"),
162         AscizDirective("\t.asciz\t"),
163         Data8bitsDirective("\t.byte\t"),
164         Data16bitsDirective("\t.short\t"),
165         Data32bitsDirective("\t.long\t"),
166         Data64bitsDirective("\t.quad\t"),
167         AlignDirective("\t.align\t"),
168         AlignmentIsInBytes(true),
169         SwitchToSectionDirective("\t.section\t"),
170         ConstantPoolSection("\t.section .rodata\n"),
171         LCOMMDirective(0),
172         COMMDirective("\t.comm\t"),
173         COMMDirectiveTakesAlignment(true) {
174     }
175
176     /// SwitchSection - Switch to the specified section of the executable if we
177     /// are not already in it!  If GV is non-null and if the global has an
178     /// explicitly requested section, we switch to the section indicated for the
179     /// global instead of NewSection.
180     ///
181     /// If the new section is an empty string, this method forgets what the
182     /// current section is, but does not emit a .section directive.
183     ///
184     void SwitchSection(const char *NewSection, const GlobalValue *GV);
185
186     /// getFunctionNumber - Return a unique ID for the current function.
187     ///
188     unsigned getFunctionNumber() const { return FunctionNumber; }
189     
190     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
191     /// not normally call this, as the counter is automatically bumped by
192     /// SetupMachineFunction.
193     void IncrementFunctionNumber() { FunctionNumber++; }
194     
195     /// doInitialization - Set up the AsmPrinter when we are working on a new
196     /// module.  If your pass overrides this, it must make sure to explicitly
197     /// call this implementation.
198     bool doInitialization(Module &M);
199
200     /// doFinalization - Shut down the asmprinter.  If you override this in your
201     /// pass, you must make sure to call it explicitly.
202     bool doFinalization(Module &M);
203
204     /// SetupMachineFunction - This should be called when a new MachineFunction
205     /// is being processed from runOnMachineFunction.
206     void SetupMachineFunction(MachineFunction &MF);
207     
208     void EmitConstantPool(MachineConstantPool *MCP);
209
210     /// EmitAlignment - Emit an alignment directive to the specified power of
211     /// two boundary.  For example, if you pass in 3 here, you will get an 8
212     /// byte alignment.  If a global value is specified, and if that global has
213     /// an explicit alignment requested, it will override the alignment request.
214     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
215
216     /// EmitZeros - Emit a block of zeros.
217     ///
218     void EmitZeros(uint64_t NumZeros) const;
219
220     /// EmitConstantValueOnly - Print out the specified constant, without a
221     /// storage class.  Only constants of first-class type are allowed here.
222     void EmitConstantValueOnly(const Constant *CV);
223
224     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
225     ///
226     void EmitGlobalConstant(const Constant* CV);
227   };
228 }
229
230 #endif