1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Support/DataTypes.h"
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;
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().
36 unsigned FunctionNumber;
39 /// Output stream on which we're printing assembly code.
43 /// Target machine description.
47 /// Name-mangler for global names.
51 /// Cache of mangled name for current function. This is recalculated at the
52 /// beginning of each call to runOnMachineFunction().
54 std::string CurrentFnName;
56 //===------------------------------------------------------------------===//
57 // Properties to be set by the derived class ctor, used to configure the
60 /// CommentString - This indicates the comment character used by the
62 const char *CommentString; // Defaults to "#"
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 ""
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 "."
73 /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
74 /// will enclose any GlobalVariable (that isn't a function)
76 const char *GlobalVarAddrPrefix; // Defaults to ""
77 const char *GlobalVarAddrSuffix; // Defaults to ""
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.
85 const char *FunctionAddrPrefix; // Defaults to ""
86 const char *FunctionAddrSuffix; // Defaults to ""
88 //===--- Data Emission Directives -------------------------------------===//
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"
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"
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"
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"
113 //===--- Alignment Information ----------------------------------------===//
115 /// AlignDirective - The directive used to emit round up to an alignment
118 const char *AlignDirective; // Defaults to "\t.align\t"
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
124 bool AlignmentIsInBytes; // Defaults to true
126 //===--- Section Switching Directives ---------------------------------===//
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
131 const char *SwitchToSectionDirective; // Defaults to "\t.section\t"
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"
137 //===--- Global Variable Emission Directives --------------------------===//
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.
145 const char *COMMDirective; // Defaults to "\t.comm\t".
147 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
148 /// argument that specifies the alignment of the declaration.
149 bool COMMDirectiveTakesAlignment; // Defaults to true.
151 AsmPrinter(std::ostream &o, TargetMachine &tm)
152 : FunctionNumber(0), O(o), TM(tm),
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"),
172 COMMDirective("\t.comm\t"),
173 COMMDirectiveTakesAlignment(true) {
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.
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.
184 void SwitchSection(const char *NewSection, const GlobalValue *GV);
186 /// getFunctionNumber - Return a unique ID for the current function.
188 unsigned getFunctionNumber() const { return FunctionNumber; }
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++; }
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);
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);
204 /// SetupMachineFunction - This should be called when a new MachineFunction
205 /// is being processed from runOnMachineFunction.
206 void SetupMachineFunction(MachineFunction &MF);
208 void EmitConstantPool(MachineConstantPool *MCP);
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;
216 /// EmitZeros - Emit a block of zeros.
218 void EmitZeros(uint64_t NumZeros) const;
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);
224 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
226 void EmitGlobalConstant(const Constant* CV);