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 /// JumpTableSection - This is the section that we SwitchToSection right
160 /// before emitting the jump tables for a function.
161 const char *JumpTableSection; // Defaults to "\t.section .rodata\n"
163 /// StaticCtorsSection - This is the directive that is emitted to switch to
164 /// a section to emit the static constructor list.
165 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
166 const char *StaticCtorsSection;
168 /// StaticDtorsSection - This is the directive that is emitted to switch to
169 /// a section to emit the static destructor list.
170 /// Defaults to "\t.section .dtors,\"aw\",@progbits".
171 const char *StaticDtorsSection;
173 /// FourByteConstantSection, EightByteConstantSection,
174 /// SixteenByteConstantSection - These are special sections where we place
175 /// 4-, 8-, and 16- byte constant literals.
176 const char *FourByteConstantSection;
177 const char *EightByteConstantSection;
178 const char *SixteenByteConstantSection;
180 //===--- Global Variable Emission Directives --------------------------===//
182 /// LCOMMDirective - This is the name of a directive (if supported) that can
183 /// be used to efficiently declare a local (internal) block of zero
184 /// initialized data in the .bss/.data section. The syntax expected is:
185 /// \literal <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
187 const char *LCOMMDirective; // Defaults to null.
189 const char *COMMDirective; // Defaults to "\t.comm\t".
191 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
192 /// argument that specifies the alignment of the declaration.
193 bool COMMDirectiveTakesAlignment; // Defaults to true.
195 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
196 /// directives, this is true for most ELF targets.
197 bool HasDotTypeDotSizeDirective; // Defaults to true.
200 AsmPrinter(std::ostream &o, TargetMachine &TM);
203 /// SwitchToTextSection - Switch to the specified section of the executable
204 /// if we are not already in it! If GV is non-null and if the global has an
205 /// explicitly requested section, we switch to the section indicated for the
206 /// global instead of NewSection.
208 /// If the new section is an empty string, this method forgets what the
209 /// current section is, but does not emit a .section directive.
211 /// This method is used when about to emit executable code.
213 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
215 /// SwitchToDataSection - Switch to the specified section of the executable
216 /// if we are not already in it! If GV is non-null and if the global has an
217 /// explicitly requested section, we switch to the section indicated for the
218 /// global instead of NewSection.
220 /// If the new section is an empty string, this method forgets what the
221 /// current section is, but does not emit a .section directive.
223 /// This method is used when about to emit data. For most assemblers, this
224 /// is the same as the SwitchToTextSection method, but not all assemblers
227 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
229 /// getPreferredAlignmentLog - Return the preferred alignment of the
230 /// specified global, returned in log form. This includes an explicitly
231 /// requested alignment (if the global has one).
232 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
234 /// doInitialization - Set up the AsmPrinter when we are working on a new
235 /// module. If your pass overrides this, it must make sure to explicitly
236 /// call this implementation.
237 bool doInitialization(Module &M);
239 /// doFinalization - Shut down the asmprinter. If you override this in your
240 /// pass, you must make sure to call it explicitly.
241 bool doFinalization(Module &M);
243 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
244 /// instruction, using the specified assembler variant. Targets should
245 /// override this to format as appropriate. This method can return true if
246 /// the operand is erroneous.
247 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
248 unsigned AsmVariant, const char *ExtraCode);
250 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
251 /// instruction, using the specified assembler variant as an address.
252 /// Targets should override this to format as appropriate. This method can
253 /// return true if the operand is erroneous.
254 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
256 const char *ExtraCode);
258 /// SetupMachineFunction - This should be called when a new MachineFunction
259 /// is being processed from runOnMachineFunction.
260 void SetupMachineFunction(MachineFunction &MF);
262 /// getFunctionNumber - Return a unique ID for the current function.
264 unsigned getFunctionNumber() const { return FunctionNumber; }
266 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
267 /// not normally call this, as the counter is automatically bumped by
268 /// SetupMachineFunction.
269 void IncrementFunctionNumber() { FunctionNumber++; }
271 /// EmitConstantPool - Print to the current output stream assembly
272 /// representations of the constants in the constant pool MCP. This is
273 /// used to print out constants which have been "spilled to memory" by
274 /// the code generator.
276 void EmitConstantPool(MachineConstantPool *MCP);
278 /// EmitJumpTableInfo - Print assembly representations of the jump tables
279 /// used by the current function to the current output stream.
281 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
283 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
284 /// special global used by LLVM. If so, emit it and return true, otherwise
285 /// do nothing and return false.
286 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
288 /// EmitAlignment - Emit an alignment directive to the specified power of
289 /// two boundary. For example, if you pass in 3 here, you will get an 8
290 /// byte alignment. If a global value is specified, and if that global has
291 /// an explicit alignment requested, it will override the alignment request.
292 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
294 /// EmitZeros - Emit a block of zeros.
296 void EmitZeros(uint64_t NumZeros) const;
298 /// EmitString - Emit a zero-byte-terminated string constant.
300 virtual void EmitString(const ConstantArray *CVA) const;
302 /// EmitConstantValueOnly - Print out the specified constant, without a
303 /// storage class. Only constants of first-class type are allowed here.
304 void EmitConstantValueOnly(const Constant *CV);
306 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
308 void EmitGlobalConstant(const Constant* CV);
310 /// printInlineAsm - This method formats and prints the specified machine
311 /// instruction that is an inline asm.
312 void printInlineAsm(const MachineInstr *MI) const;
314 /// printBasicBlockLabel - This method prints the label for the specified
315 /// MachineBasicBlock
316 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
317 bool printColon = false,
318 bool printComment = true) const;
321 void EmitXXStructorList(Constant *List);
322 void EmitConstantPool(unsigned Alignment, const char *Section,
323 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);