AsmPrinter.h revision e736ed19478f05811f08a56706e07fa4ab865401
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
22namespace 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    //===--- Data Emission Directives -------------------------------------===//
90
91    /// ZeroDirective - this should be set to the directive used to get some
92    /// number of zero bytes emitted to the current section.  Common cases are
93    /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
94    /// Data*bitsDirective's will be used to emit zero bytes.
95    const char *ZeroDirective;   // Defaults to "\t.zero\t"
96
97    /// AsciiDirective - This directive allows emission of an ascii string with
98    /// the standard C escape characters embedded into it.
99    const char *AsciiDirective;  // Defaults to "\t.ascii\t"
100
101    /// AscizDirective - If not null, this allows for special handling of
102    /// zero terminated strings on this target.  This is commonly supported as
103    /// ".asciz".  If a target doesn't support this, it can be set to null.
104    const char *AscizDirective;  // Defaults to "\t.asciz\t"
105
106    /// DataDirectives - These directives are used to output some unit of
107    /// integer data to the current section.  If a data directive is set to
108    /// null, smaller data directives will be used to emit the large sizes.
109    const char *Data8bitsDirective;   // Defaults to "\t.byte\t"
110    const char *Data16bitsDirective;  // Defaults to "\t.short\t"
111    const char *Data32bitsDirective;  // Defaults to "\t.long\t"
112    const char *Data64bitsDirective;  // Defaults to "\t.quad\t"
113
114    //===--- Alignment Information ----------------------------------------===//
115
116    /// AlignDirective - The directive used to emit round up to an alignment
117    /// boundary.
118    ///
119    const char *AlignDirective;       // Defaults to "\t.align\t"
120
121    /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
122    /// emits ".align N" directives, where N is the number of bytes to align to.
123    /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
124    /// boundary.
125    bool AlignmentIsInBytes;          // Defaults to true
126
127    //===--- Section Switching Directives ---------------------------------===//
128
129    /// SwitchToSectionDirective - This is the directive used when we want to
130    /// emit a global to an arbitrary section.  The section name is emited after
131    /// this.
132    const char *SwitchToSectionDirective;  // Defaults to "\t.section\t"
133
134    /// ConstantPoolSection - This is the section that we SwitchToSection right
135    /// before emitting the constant pool for a function.
136    const char *ConstantPoolSection;     // Defaults to "\t.section .rodata\n"
137
138    /// StaticCtorsSection - This is the directive that is emitted to switch to
139    /// a section to emit the static constructor list.
140    /// Defaults to "\t.section .ctors,\"aw\",@progbits".
141    const char *StaticCtorsSection;
142
143    /// StaticDtorsSection - This is the directive that is emitted to switch to
144    /// a section to emit the static destructor list.
145    /// Defaults to "\t.section .dtors,\"aw\",@progbits".
146    const char *StaticDtorsSection;
147
148    //===--- Global Variable Emission Directives --------------------------===//
149
150    /// LCOMMDirective - This is the name of a directive (if supported) that can
151    /// be used to efficiently declare a local (internal) block of zero
152    /// initialized data in the .bss/.data section.  The syntax expected is:
153    ///    <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
154    const char *LCOMMDirective;          // Defaults to null.
155
156    const char *COMMDirective;           // Defaults to "\t.comm\t".
157
158    /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
159    /// argument that specifies the alignment of the declaration.
160    bool COMMDirectiveTakesAlignment;    // Defaults to true.
161
162    /// HasDotTypeDotSizeDirective - True if the target has .type and .size
163    /// directives, this is true for most ELF targets.
164    bool HasDotTypeDotSizeDirective;     // Defaults to true.
165
166  protected:
167    AsmPrinter(std::ostream &o, TargetMachine &TM);
168
169  public:
170    /// SwitchSection - Switch to the specified section of the executable if we
171    /// are not already in it!  If GV is non-null and if the global has an
172    /// explicitly requested section, we switch to the section indicated for the
173    /// global instead of NewSection.
174    ///
175    /// If the new section is an empty string, this method forgets what the
176    /// current section is, but does not emit a .section directive.
177    ///
178    void SwitchSection(const char *NewSection, const GlobalValue *GV);
179
180  protected:
181    /// getFunctionNumber - Return a unique ID for the current function.
182    ///
183    unsigned getFunctionNumber() const { return FunctionNumber; }
184
185    /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
186    /// not normally call this, as the counter is automatically bumped by
187    /// SetupMachineFunction.
188    void IncrementFunctionNumber() { FunctionNumber++; }
189
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    /// SetupMachineFunction - This should be called when a new MachineFunction
200    /// is being processed from runOnMachineFunction.
201    void SetupMachineFunction(MachineFunction &MF);
202
203    /// EmitConstantPool - Print to the current output stream assembly
204    /// representations of the constants in the constant pool MCP. This is
205    /// used to print out constants which have been "spilled to memory" by
206    /// the code generator.
207    ///
208    void EmitConstantPool(MachineConstantPool *MCP);
209
210    /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
211    /// special global used by LLVM.  If so, emit it and return true, otherwise
212    /// do nothing and return false.
213    bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
214
215    /// EmitAlignment - Emit an alignment directive to the specified power of
216    /// two boundary.  For example, if you pass in 3 here, you will get an 8
217    /// byte alignment.  If a global value is specified, and if that global has
218    /// an explicit alignment requested, it will override the alignment request.
219    void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
220
221    /// EmitZeros - Emit a block of zeros.
222    ///
223    void EmitZeros(uint64_t NumZeros) const;
224
225    /// EmitConstantValueOnly - Print out the specified constant, without a
226    /// storage class.  Only constants of first-class type are allowed here.
227    void EmitConstantValueOnly(const Constant *CV);
228
229    /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
230    ///
231    void EmitGlobalConstant(const Constant* CV);
232
233    /// printInlineAsm - This method formats and prints the specified machine
234    /// instruction that is an inline asm.
235    void printInlineAsm(const MachineInstr *MI) const;
236  private:
237    void EmitXXStructorList(Constant *List);
238
239  };
240}
241
242#endif
243