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 /// SetDirective - This is the name of a directive that can be used to tell
189 /// the assembler to set the value of a variable to some expression.
190 const char *SetDirective; // Defaults to null.
192 /// LCOMMDirective - This is the name of a directive (if supported) that can
193 /// be used to efficiently declare a local (internal) block of zero
194 /// initialized data in the .bss/.data section. The syntax expected is:
195 /// \literal <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
197 const char *LCOMMDirective; // Defaults to null.
199 const char *COMMDirective; // Defaults to "\t.comm\t".
201 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
202 /// argument that specifies the alignment of the declaration.
203 bool COMMDirectiveTakesAlignment; // Defaults to true.
205 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
206 /// directives, this is true for most ELF targets.
207 bool HasDotTypeDotSizeDirective; // Defaults to true.
210 AsmPrinter(std::ostream &o, TargetMachine &TM);
213 /// SwitchToTextSection - Switch to the specified section of the executable
214 /// if we are not already in it! If GV is non-null and if the global has an
215 /// explicitly requested section, we switch to the section indicated for the
216 /// global instead of NewSection.
218 /// If the new section is an empty string, this method forgets what the
219 /// current section is, but does not emit a .section directive.
221 /// This method is used when about to emit executable code.
223 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
225 /// SwitchToDataSection - Switch to the specified section of the executable
226 /// if we are not already in it! If GV is non-null and if the global has an
227 /// explicitly requested section, we switch to the section indicated for the
228 /// global instead of NewSection.
230 /// If the new section is an empty string, this method forgets what the
231 /// current section is, but does not emit a .section directive.
233 /// This method is used when about to emit data. For most assemblers, this
234 /// is the same as the SwitchToTextSection method, but not all assemblers
237 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
239 /// getPreferredAlignmentLog - Return the preferred alignment of the
240 /// specified global, returned in log form. This includes an explicitly
241 /// requested alignment (if the global has one).
242 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
244 /// doInitialization - Set up the AsmPrinter when we are working on a new
245 /// module. If your pass overrides this, it must make sure to explicitly
246 /// call this implementation.
247 bool doInitialization(Module &M);
249 /// doFinalization - Shut down the asmprinter. If you override this in your
250 /// pass, you must make sure to call it explicitly.
251 bool doFinalization(Module &M);
253 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
254 /// instruction, using the specified assembler variant. Targets should
255 /// override this to format as appropriate. This method can return true if
256 /// the operand is erroneous.
257 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
258 unsigned AsmVariant, const char *ExtraCode);
260 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
261 /// instruction, using the specified assembler variant as an address.
262 /// Targets should override this to format as appropriate. This method can
263 /// return true if the operand is erroneous.
264 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
266 const char *ExtraCode);
268 /// SetupMachineFunction - This should be called when a new MachineFunction
269 /// is being processed from runOnMachineFunction.
270 void SetupMachineFunction(MachineFunction &MF);
272 /// getFunctionNumber - Return a unique ID for the current function.
274 unsigned getFunctionNumber() const { return FunctionNumber; }
276 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
277 /// not normally call this, as the counter is automatically bumped by
278 /// SetupMachineFunction.
279 void IncrementFunctionNumber() { FunctionNumber++; }
281 /// EmitConstantPool - Print to the current output stream assembly
282 /// representations of the constants in the constant pool MCP. This is
283 /// used to print out constants which have been "spilled to memory" by
284 /// the code generator.
286 void EmitConstantPool(MachineConstantPool *MCP);
288 /// EmitJumpTableInfo - Print assembly representations of the jump tables
289 /// used by the current function to the current output stream.
291 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
293 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
294 /// special global used by LLVM. If so, emit it and return true, otherwise
295 /// do nothing and return false.
296 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
298 /// EmitAlignment - Emit an alignment directive to the specified power of
299 /// two boundary. For example, if you pass in 3 here, you will get an 8
300 /// byte alignment. If a global value is specified, and if that global has
301 /// an explicit alignment requested, it will override the alignment request.
302 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
304 /// EmitZeros - Emit a block of zeros.
306 void EmitZeros(uint64_t NumZeros) const;
308 /// EmitString - Emit a zero-byte-terminated string constant.
310 virtual void EmitString(const ConstantArray *CVA) const;
312 /// EmitConstantValueOnly - Print out the specified constant, without a
313 /// storage class. Only constants of first-class type are allowed here.
314 void EmitConstantValueOnly(const Constant *CV);
316 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
318 void EmitGlobalConstant(const Constant* CV);
320 /// printInlineAsm - This method formats and prints the specified machine
321 /// instruction that is an inline asm.
322 void printInlineAsm(const MachineInstr *MI) const;
324 /// printBasicBlockLabel - This method prints the label for the specified
325 /// MachineBasicBlock
326 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
327 bool printColon = false,
328 bool printComment = true) const;
330 /// printSetLabel - This method prints a set label for the specified
331 /// MachineBasicBlock
332 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
335 void EmitXXStructorList(Constant *List);
336 void EmitConstantPool(unsigned Alignment, const char *Section,
337 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);