b6ed3a5254c6e19fe5ababfe818c3909469a56ab
[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     AsmPrinter(std::ostream &o, TargetMachine &tm)
138       : FunctionNumber(0), O(o), TM(tm),
139         CommentString("#"),
140         GlobalPrefix(""),
141         PrivateGlobalPrefix("."),
142         GlobalVarAddrPrefix(""),
143         GlobalVarAddrSuffix(""),
144         FunctionAddrPrefix(""),
145         FunctionAddrSuffix(""),
146         ZeroDirective("\t.zero\t"),
147         AsciiDirective("\t.ascii\t"),
148         AscizDirective("\t.asciz\t"),
149         Data8bitsDirective("\t.byte\t"),
150         Data16bitsDirective("\t.short\t"),
151         Data32bitsDirective("\t.long\t"),
152         Data64bitsDirective("\t.quad\t"),
153         AlignDirective("\t.align\t"),
154         AlignmentIsInBytes(true),
155         SwitchToSectionDirective("\t.section\t"),
156         ConstantPoolSection("\t.section .rodata\n") {
157     }
158
159     /// SwitchSection - Switch to the specified section of the executable if we
160     /// are not already in it!  If GV is non-null and if the global has an
161     /// explicitly requested section, we switch to the section indicated for the
162     /// global instead of NewSection.
163     ///
164     /// If the new section is an empty string, this method forgets what the
165     /// current section is, but does not emit a .section directive.
166     ///
167     void SwitchSection(const char *NewSection, const GlobalValue *GV);
168
169     /// getFunctionNumber - Return a unique ID for the current function.
170     ///
171     unsigned getFunctionNumber() const { return FunctionNumber; }
172     
173     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
174     /// not normally call this, as the counter is automatically bumped by
175     /// SetupMachineFunction.
176     void IncrementFunctionNumber() { FunctionNumber++; }
177     
178     /// doInitialization - Set up the AsmPrinter when we are working on a new
179     /// module.  If your pass overrides this, it must make sure to explicitly
180     /// call this implementation.
181     bool doInitialization(Module &M);
182
183     /// doFinalization - Shut down the asmprinter.  If you override this in your
184     /// pass, you must make sure to call it explicitly.
185     bool doFinalization(Module &M);
186
187     /// SetupMachineFunction - This should be called when a new MachineFunction
188     /// is being processed from runOnMachineFunction.
189     void SetupMachineFunction(MachineFunction &MF);
190     
191     void EmitConstantPool(MachineConstantPool *MCP);
192
193     /// EmitAlignment - Emit an alignment directive to the specified power of
194     /// two boundary.  For example, if you pass in 3 here, you will get an 8
195     /// byte alignment.  If a global value is specified, and if that global has
196     /// an explicit alignment requested, it will override the alignment request.
197     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
198
199     /// EmitZeros - Emit a block of zeros.
200     ///
201     void EmitZeros(uint64_t NumZeros) const;
202
203     /// EmitConstantValueOnly - Print out the specified constant, without a
204     /// storage class.  Only constants of first-class type are allowed here.
205     void EmitConstantValueOnly(const Constant *CV);
206
207     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
208     ///
209     void EmitGlobalConstant(const Constant* CV);
210   };
211 }
212
213 #endif