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"
27 class MachineConstantPoolEntry;
29 class AsmPrinter : public MachineFunctionPass {
30 /// FunctionNumber - This provides a unique ID for each function emitted in
31 /// this translation unit. It is autoincremented by SetupMachineFunction,
32 /// and can be accessed with getFunctionNumber() and
33 /// IncrementFunctionNumber().
35 unsigned FunctionNumber;
38 /// Output stream on which we're printing assembly code.
42 /// Target machine description.
46 /// Name-mangler for global names.
50 /// Cache of mangled name for current function. This is recalculated at the
51 /// beginning of each call to runOnMachineFunction().
53 std::string CurrentFnName;
55 //===------------------------------------------------------------------===//
56 // Properties to be set by the derived class ctor, used to configure the
59 /// CommentString - This indicates the comment character used by the
61 const char *CommentString; // Defaults to "#"
63 /// GlobalPrefix - If this is set to a non-empty string, it is prepended
64 /// onto all global symbols. This is often used for "_" or ".".
65 const char *GlobalPrefix; // Defaults to ""
67 /// PrivateGlobalPrefix - This prefix is used for globals like constant
68 /// pool entries that are completely private to the .o file and should not
69 /// have names in the .o file. This is often "." or "L".
70 const char *PrivateGlobalPrefix; // Defaults to "."
72 /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
73 /// will enclose any GlobalVariable (that isn't a function)
75 const char *GlobalVarAddrPrefix; // Defaults to ""
76 const char *GlobalVarAddrSuffix; // Defaults to ""
78 /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
79 /// will enclose any GlobalVariable that points to a function.
80 /// For example, this is used by the IA64 backend to materialize
81 /// function descriptors, by decorating the ".data8" object with the
82 /// \literal @fptr( ) \endliteral
83 /// link-relocation operator.
85 const char *FunctionAddrPrefix; // Defaults to ""
86 const char *FunctionAddrSuffix; // Defaults to ""
88 /// InlineAsmStart/End - If these are nonempty, they contain a directive to
89 /// emit before and after an inline assmebly statement.
90 const char *InlineAsmStart; // Defaults to "#APP\n"
91 const char *InlineAsmEnd; // Defaults to "#NO_APP\n"
93 //===--- Data Emission Directives -------------------------------------===//
95 /// ZeroDirective - this should be set to the directive used to get some
96 /// number of zero bytes emitted to the current section. Common cases are
97 /// "\t.zero\t" and "\t.space\t". If this is set to null, the
98 /// Data*bitsDirective's will be used to emit zero bytes.
99 const char *ZeroDirective; // Defaults to "\t.zero\t"
100 const char *ZeroDirectiveSuffix; // Defaults to ""
102 /// AsciiDirective - This directive allows emission of an ascii string with
103 /// the standard C escape characters embedded into it.
104 const char *AsciiDirective; // Defaults to "\t.ascii\t"
106 /// AscizDirective - If not null, this allows for special handling of
107 /// zero terminated strings on this target. This is commonly supported as
108 /// ".asciz". If a target doesn't support this, it can be set to null.
109 const char *AscizDirective; // Defaults to "\t.asciz\t"
111 /// DataDirectives - These directives are used to output some unit of
112 /// integer data to the current section. If a data directive is set to
113 /// null, smaller data directives will be used to emit the large sizes.
114 const char *Data8bitsDirective; // Defaults to "\t.byte\t"
115 const char *Data16bitsDirective; // Defaults to "\t.short\t"
116 const char *Data32bitsDirective; // Defaults to "\t.long\t"
117 const char *Data64bitsDirective; // Defaults to "\t.quad\t"
119 //===--- Alignment Information ----------------------------------------===//
121 /// AlignDirective - The directive used to emit round up to an alignment
124 const char *AlignDirective; // Defaults to "\t.align\t"
126 /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
127 /// emits ".align N" directives, where N is the number of bytes to align to.
128 /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
130 bool AlignmentIsInBytes; // Defaults to true
132 //===--- Section Switching Directives ---------------------------------===//
134 /// CurrentSection - The current section we are emitting to. This is
135 /// controlled and used by the SwitchSection method.
136 std::string CurrentSection;
138 /// SwitchToSectionDirective - This is the directive used when we want to
139 /// emit a global to an arbitrary section. The section name is emited after
141 const char *SwitchToSectionDirective; // Defaults to "\t.section\t"
143 /// TextSectionStartSuffix - This is printed after each start of section
144 /// directive for text sections.
145 const char *TextSectionStartSuffix; // Defaults to "".
147 /// DataSectionStartSuffix - This is printed after each start of section
148 /// directive for data sections.
149 const char *DataSectionStartSuffix; // Defaults to "".
151 /// SectionEndDirectiveSuffix - If non-null, the asm printer will close each
152 /// section with the section name and this suffix printed.
153 const char *SectionEndDirectiveSuffix; // Defaults to null.
155 /// ConstantPoolSection - This is the section that we SwitchToSection right
156 /// before emitting the constant pool for a function.
157 const char *ConstantPoolSection; // Defaults to "\t.section .rodata\n"
159 /// JumpTableDataSection - This is the section that we SwitchToSection right
160 /// before emitting the jump tables for a function when the relocation model
162 const char *JumpTableDataSection; // Defaults to "\t.section .rodata\n"
164 /// JumpTableTextSection - This is the section that we SwitchToSection right
165 /// before emitting the jump tables for a function when the relocation model
167 const char *JumpTableTextSection; // Defaults to "\t.text\n"
169 /// StaticCtorsSection - This is the directive that is emitted to switch to
170 /// a section to emit the static constructor list.
171 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
172 const char *StaticCtorsSection;
174 /// StaticDtorsSection - This is the directive that is emitted to switch to
175 /// a section to emit the static destructor list.
176 /// Defaults to "\t.section .dtors,\"aw\",@progbits".
177 const char *StaticDtorsSection;
179 /// FourByteConstantSection, EightByteConstantSection,
180 /// SixteenByteConstantSection - These are special sections where we place
181 /// 4-, 8-, and 16- byte constant literals.
182 const char *FourByteConstantSection;
183 const char *EightByteConstantSection;
184 const char *SixteenByteConstantSection;
186 //===--- Global Variable Emission Directives --------------------------===//
188 /// LCOMMDirective - This is the name of a directive (if supported) that can
189 /// be used to efficiently declare a local (internal) block of zero
190 /// initialized data in the .bss/.data section. The syntax expected is:
191 /// \literal <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
193 const char *LCOMMDirective; // Defaults to null.
195 const char *COMMDirective; // Defaults to "\t.comm\t".
197 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
198 /// argument that specifies the alignment of the declaration.
199 bool COMMDirectiveTakesAlignment; // Defaults to true.
201 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
202 /// directives, this is true for most ELF targets.
203 bool HasDotTypeDotSizeDirective; // Defaults to true.
206 AsmPrinter(std::ostream &o, TargetMachine &TM);
209 /// SwitchToTextSection - Switch to the specified section of the executable
210 /// if we are not already in it! If GV is non-null and if the global has an
211 /// explicitly requested section, we switch to the section indicated for the
212 /// global instead of NewSection.
214 /// If the new section is an empty string, this method forgets what the
215 /// current section is, but does not emit a .section directive.
217 /// This method is used when about to emit executable code.
219 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
221 /// SwitchToDataSection - Switch to the specified section of the executable
222 /// if we are not already in it! If GV is non-null and if the global has an
223 /// explicitly requested section, we switch to the section indicated for the
224 /// global instead of NewSection.
226 /// If the new section is an empty string, this method forgets what the
227 /// current section is, but does not emit a .section directive.
229 /// This method is used when about to emit data. For most assemblers, this
230 /// is the same as the SwitchToTextSection method, but not all assemblers
233 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
235 /// getPreferredAlignmentLog - Return the preferred alignment of the
236 /// specified global, returned in log form. This includes an explicitly
237 /// requested alignment (if the global has one).
238 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
240 /// doInitialization - Set up the AsmPrinter when we are working on a new
241 /// module. If your pass overrides this, it must make sure to explicitly
242 /// call this implementation.
243 bool doInitialization(Module &M);
245 /// doFinalization - Shut down the asmprinter. If you override this in your
246 /// pass, you must make sure to call it explicitly.
247 bool doFinalization(Module &M);
249 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
250 /// instruction, using the specified assembler variant. Targets should
251 /// override this to format as appropriate. This method can return true if
252 /// the operand is erroneous.
253 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
254 unsigned AsmVariant, const char *ExtraCode);
256 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
257 /// instruction, using the specified assembler variant as an address.
258 /// Targets should override this to format as appropriate. This method can
259 /// return true if the operand is erroneous.
260 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
262 const char *ExtraCode);
264 /// SetupMachineFunction - This should be called when a new MachineFunction
265 /// is being processed from runOnMachineFunction.
266 void SetupMachineFunction(MachineFunction &MF);
268 /// getFunctionNumber - Return a unique ID for the current function.
270 unsigned getFunctionNumber() const { return FunctionNumber; }
272 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
273 /// not normally call this, as the counter is automatically bumped by
274 /// SetupMachineFunction.
275 void IncrementFunctionNumber() { FunctionNumber++; }
277 /// EmitConstantPool - Print to the current output stream assembly
278 /// representations of the constants in the constant pool MCP. This is
279 /// used to print out constants which have been "spilled to memory" by
280 /// the code generator.
282 void EmitConstantPool(MachineConstantPool *MCP);
284 /// EmitJumpTableInfo - Print assembly representations of the jump tables
285 /// used by the current function to the current output stream.
287 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
289 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
290 /// special global used by LLVM. If so, emit it and return true, otherwise
291 /// do nothing and return false.
292 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
294 /// EmitAlignment - Emit an alignment directive to the specified power of
295 /// two boundary. For example, if you pass in 3 here, you will get an 8
296 /// byte alignment. If a global value is specified, and if that global has
297 /// an explicit alignment requested, it will override the alignment request.
298 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
300 /// EmitZeros - Emit a block of zeros.
302 void EmitZeros(uint64_t NumZeros) const;
304 /// EmitString - Emit a zero-byte-terminated string constant.
306 virtual void EmitString(const ConstantArray *CVA) const;
308 /// EmitConstantValueOnly - Print out the specified constant, without a
309 /// storage class. Only constants of first-class type are allowed here.
310 void EmitConstantValueOnly(const Constant *CV);
312 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
314 void EmitGlobalConstant(const Constant* CV);
316 /// printInlineAsm - This method formats and prints the specified machine
317 /// instruction that is an inline asm.
318 void printInlineAsm(const MachineInstr *MI) const;
320 /// printBasicBlockLabel - This method prints the label for the specified
321 /// MachineBasicBlock
322 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
323 bool printColon = false,
324 bool printComment = true) const;
327 void EmitXXStructorList(Constant *List);
328 void EmitConstantPool(unsigned Alignment, const char *Section,
329 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);