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"
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;
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().
38 unsigned FunctionNumber;
41 /// Output stream on which we're printing assembly code.
45 /// Target machine description.
49 /// Name-mangler for global names.
53 /// Cache of mangled name for current function. This is recalculated at the
54 /// beginning of each call to runOnMachineFunction().
56 std::string CurrentFnName;
58 //===------------------------------------------------------------------===//
59 // Properties to be set by the derived class ctor, used to configure the
62 /// CommentString - This indicates the comment character used by the
64 const char *CommentString; // Defaults to "#"
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 ""
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 "."
75 /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
76 /// will enclose any GlobalVariable (that isn't a function)
78 const char *GlobalVarAddrPrefix; // Defaults to ""
79 const char *GlobalVarAddrSuffix; // Defaults to ""
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.
87 const char *FunctionAddrPrefix; // Defaults to ""
88 const char *FunctionAddrSuffix; // Defaults to ""
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"
95 //===--- Data Emission Directives -------------------------------------===//
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 ""
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"
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"
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"
121 //===--- Alignment Information ----------------------------------------===//
123 /// AlignDirective - The directive used to emit round up to an alignment
126 const char *AlignDirective; // Defaults to "\t.align\t"
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
132 bool AlignmentIsInBytes; // Defaults to true
134 //===--- Section Switching Directives ---------------------------------===//
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
139 const char *SwitchToSectionDirective; // Defaults to "\t.section\t"
140 bool MLSections; // True if Microsoft ML assembler is targetted
142 /// ConstantPoolSection - This is the section that we SwitchToSection right
143 /// before emitting the constant pool for a function.
144 const char *ConstantPoolSection; // Defaults to "\t.section .rodata\n"
146 /// JumpTableSection - This is the section that we SwitchToSection right
147 /// before emitting the jump tables for a function.
148 const char *JumpTableSection; // Defaults to "\t.section .rodata\n"
150 /// StaticCtorsSection - This is the directive that is emitted to switch to
151 /// a section to emit the static constructor list.
152 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
153 const char *StaticCtorsSection;
155 /// StaticDtorsSection - This is the directive that is emitted to switch to
156 /// a section to emit the static destructor list.
157 /// Defaults to "\t.section .dtors,\"aw\",@progbits".
158 const char *StaticDtorsSection;
160 //===--- Global Variable Emission Directives --------------------------===//
162 /// LCOMMDirective - This is the name of a directive (if supported) that can
163 /// be used to efficiently declare a local (internal) block of zero
164 /// initialized data in the .bss/.data section. The syntax expected is:
165 /// <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
166 const char *LCOMMDirective; // Defaults to null.
168 const char *COMMDirective; // Defaults to "\t.comm\t".
170 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
171 /// argument that specifies the alignment of the declaration.
172 bool COMMDirectiveTakesAlignment; // Defaults to true.
174 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
175 /// directives, this is true for most ELF targets.
176 bool HasDotTypeDotSizeDirective; // Defaults to true.
179 AsmPrinter(std::ostream &o, TargetMachine &TM);
182 /// SwitchToTextSection - Switch to the specified section of the executable
183 /// if we are not already in it! If GV is non-null and if the global has an
184 /// explicitly requested section, we switch to the section indicated for the
185 /// global instead of NewSection.
187 /// If the new section is an empty string, this method forgets what the
188 /// current section is, but does not emit a .section directive.
190 /// This method is used when about to emit executable code.
192 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
194 /// SwitchToDataSection - Switch to the specified section of the executable
195 /// if we are not already in it! If GV is non-null and if the global has an
196 /// explicitly requested section, we switch to the section indicated for the
197 /// global instead of NewSection.
199 /// If the new section is an empty string, this method forgets what the
200 /// current section is, but does not emit a .section directive.
202 /// This method is used when about to emit data. For most assemblers, this
203 /// is the same as the SwitchToTextSection method, but not all assemblers
206 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
208 /// getPreferredAlignmentLog - Return the preferred alignment of the
209 /// specified global, returned in log form. This includes an explicitly
210 /// requested alignment (if the global has one).
211 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
213 /// doInitialization - Set up the AsmPrinter when we are working on a new
214 /// module. If your pass overrides this, it must make sure to explicitly
215 /// call this implementation.
216 bool doInitialization(Module &M);
218 /// doFinalization - Shut down the asmprinter. If you override this in your
219 /// pass, you must make sure to call it explicitly.
220 bool doFinalization(Module &M);
222 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
223 /// instruction, using the specified assembler variant. Targets should
224 /// override this to format as appropriate. This method can return true if
225 /// the operand is erroneous.
226 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
227 unsigned AsmVariant, const char *ExtraCode);
229 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
230 /// instruction, using the specified assembler variant as an address.
231 /// Targets should override this to format as appropriate. This method can
232 /// return true if the operand is erroneous.
233 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
235 const char *ExtraCode);
237 /// SetupMachineFunction - This should be called when a new MachineFunction
238 /// is being processed from runOnMachineFunction.
239 void SetupMachineFunction(MachineFunction &MF);
241 /// getFunctionNumber - Return a unique ID for the current function.
243 unsigned getFunctionNumber() const { return FunctionNumber; }
245 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
246 /// not normally call this, as the counter is automatically bumped by
247 /// SetupMachineFunction.
248 void IncrementFunctionNumber() { FunctionNumber++; }
250 /// EmitConstantPool - Print to the current output stream assembly
251 /// representations of the constants in the constant pool MCP. This is
252 /// used to print out constants which have been "spilled to memory" by
253 /// the code generator.
255 void EmitConstantPool(MachineConstantPool *MCP);
257 /// EmitJumpTableInfo - Print assembly representations of the jump tables
258 /// used by the current function to the current output stream.
260 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
262 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
263 /// special global used by LLVM. If so, emit it and return true, otherwise
264 /// do nothing and return false.
265 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
267 /// EmitAlignment - Emit an alignment directive to the specified power of
268 /// two boundary. For example, if you pass in 3 here, you will get an 8
269 /// byte alignment. If a global value is specified, and if that global has
270 /// an explicit alignment requested, it will override the alignment request.
271 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
273 /// EmitZeros - Emit a block of zeros.
275 void EmitZeros(uint64_t NumZeros) const;
277 /// EmitString - Emit a zero-byte-terminated string constant.
279 virtual void EmitString(const ConstantArray *CVA) const;
281 /// EmitConstantValueOnly - Print out the specified constant, without a
282 /// storage class. Only constants of first-class type are allowed here.
283 void EmitConstantValueOnly(const Constant *CV);
285 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
287 void EmitGlobalConstant(const Constant* CV);
289 /// printInlineAsm - This method formats and prints the specified machine
290 /// instruction that is an inline asm.
291 void printInlineAsm(const MachineInstr *MI) const;
293 /// printBasicBlockLabel - This method prints the label for the specified
294 /// MachineBasicBlock
295 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
296 bool printColon = false,
297 bool printComment = true) const;
300 void EmitXXStructorList(Constant *List);