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 /// FunctionNumber - This provides a unique ID for each function emitted in
30 /// this translation unit. It is autoincremented by SetupMachineFunction,
31 /// and can be accessed with getFunctionNumber() and
32 /// IncrementFunctionNumber().
34 unsigned FunctionNumber;
37 /// Output stream on which we're printing assembly code.
41 /// Target machine description.
45 /// Name-mangler for global names.
49 /// Cache of mangled name for current function. This is recalculated at the
50 /// beginning of each call to runOnMachineFunction().
52 std::string CurrentFnName;
54 //===------------------------------------------------------------------===//
55 // Properties to be set by the derived class ctor, used to configure the
58 /// CommentString - This indicates the comment character used by the
60 const char *CommentString; // Defaults to "#"
62 /// GlobalPrefix - If this is set to a non-empty string, it is prepended
63 /// onto all global symbols. This is often used for "_" or ".".
64 const char *GlobalPrefix; // Defaults to ""
66 /// PrivateGlobalPrefix - This prefix is used for globals like constant
67 /// pool entries that are completely private to the .o file and should not
68 /// have names in the .o file. This is often "." or "L".
69 const char *PrivateGlobalPrefix; // Defaults to "."
71 /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
72 /// will enclose any GlobalVariable (that isn't a function)
74 const char *GlobalVarAddrPrefix; // Defaults to ""
75 const char *GlobalVarAddrSuffix; // Defaults to ""
77 /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
78 /// will enclose any GlobalVariable that points to a function.
79 /// For example, this is used by the IA64 backend to materialize
80 /// function descriptors, by decorating the ".data8" object with the
81 /// \literal @fptr( ) \endliteral
82 /// link-relocation operator.
84 const char *FunctionAddrPrefix; // Defaults to ""
85 const char *FunctionAddrSuffix; // Defaults to ""
87 /// InlineAsmStart/End - If these are nonempty, they contain a directive to
88 /// emit before and after an inline assmebly statement.
89 const char *InlineAsmStart; // Defaults to "#APP\n"
90 const char *InlineAsmEnd; // Defaults to "#NO_APP\n"
92 //===--- Data Emission Directives -------------------------------------===//
94 /// ZeroDirective - this should be set to the directive used to get some
95 /// number of zero bytes emitted to the current section. Common cases are
96 /// "\t.zero\t" and "\t.space\t". If this is set to null, the
97 /// Data*bitsDirective's will be used to emit zero bytes.
98 const char *ZeroDirective; // Defaults to "\t.zero\t"
99 const char *ZeroDirectiveSuffix; // Defaults to ""
101 /// AsciiDirective - This directive allows emission of an ascii string with
102 /// the standard C escape characters embedded into it.
103 const char *AsciiDirective; // Defaults to "\t.ascii\t"
105 /// AscizDirective - If not null, this allows for special handling of
106 /// zero terminated strings on this target. This is commonly supported as
107 /// ".asciz". If a target doesn't support this, it can be set to null.
108 const char *AscizDirective; // Defaults to "\t.asciz\t"
110 /// DataDirectives - These directives are used to output some unit of
111 /// integer data to the current section. If a data directive is set to
112 /// null, smaller data directives will be used to emit the large sizes.
113 const char *Data8bitsDirective; // Defaults to "\t.byte\t"
114 const char *Data16bitsDirective; // Defaults to "\t.short\t"
115 const char *Data32bitsDirective; // Defaults to "\t.long\t"
116 const char *Data64bitsDirective; // Defaults to "\t.quad\t"
118 //===--- Alignment Information ----------------------------------------===//
120 /// AlignDirective - The directive used to emit round up to an alignment
123 const char *AlignDirective; // Defaults to "\t.align\t"
125 /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
126 /// emits ".align N" directives, where N is the number of bytes to align to.
127 /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
129 bool AlignmentIsInBytes; // Defaults to true
131 //===--- Section Switching Directives ---------------------------------===//
133 /// CurrentSection - The current section we are emitting to. This is
134 /// controlled and used by the SwitchSection method.
135 std::string CurrentSection;
137 /// SwitchToSectionDirective - This is the directive used when we want to
138 /// emit a global to an arbitrary section. The section name is emited after
140 const char *SwitchToSectionDirective; // Defaults to "\t.section\t"
142 /// TextSectionStartSuffix - This is printed after each start of section
143 /// directive for text sections.
144 const char *TextSectionStartSuffix; // Defaults to "".
146 /// DataSectionStartSuffix - This is printed after each start of section
147 /// directive for data sections.
148 const char *DataSectionStartSuffix; // Defaults to "".
150 /// SectionEndDirectiveSuffix - If non-null, the asm printer will close each
151 /// section with the section name and this suffix printed.
152 const char *SectionEndDirectiveSuffix; // Defaults to null.
154 /// ConstantPoolSection - This is the section that we SwitchToSection right
155 /// before emitting the constant pool for a function.
156 const char *ConstantPoolSection; // Defaults to "\t.section .rodata\n"
158 /// JumpTableSection - This is the section that we SwitchToSection right
159 /// before emitting the jump tables for a function.
160 const char *JumpTableSection; // Defaults to "\t.section .rodata\n"
162 /// StaticCtorsSection - This is the directive that is emitted to switch to
163 /// a section to emit the static constructor list.
164 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
165 const char *StaticCtorsSection;
167 /// StaticDtorsSection - This is the directive that is emitted to switch to
168 /// a section to emit the static destructor list.
169 /// Defaults to "\t.section .dtors,\"aw\",@progbits".
170 const char *StaticDtorsSection;
172 //===--- Global Variable Emission Directives --------------------------===//
174 /// LCOMMDirective - This is the name of a directive (if supported) that can
175 /// be used to efficiently declare a local (internal) block of zero
176 /// initialized data in the .bss/.data section. The syntax expected is:
177 /// \literal <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
179 const char *LCOMMDirective; // Defaults to null.
181 const char *COMMDirective; // Defaults to "\t.comm\t".
183 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
184 /// argument that specifies the alignment of the declaration.
185 bool COMMDirectiveTakesAlignment; // Defaults to true.
187 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
188 /// directives, this is true for most ELF targets.
189 bool HasDotTypeDotSizeDirective; // Defaults to true.
192 AsmPrinter(std::ostream &o, TargetMachine &TM);
195 /// SwitchToTextSection - Switch to the specified section of the executable
196 /// if we are not already in it! If GV is non-null and if the global has an
197 /// explicitly requested section, we switch to the section indicated for the
198 /// global instead of NewSection.
200 /// If the new section is an empty string, this method forgets what the
201 /// current section is, but does not emit a .section directive.
203 /// This method is used when about to emit executable code.
205 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
207 /// SwitchToDataSection - Switch to the specified section of the executable
208 /// if we are not already in it! If GV is non-null and if the global has an
209 /// explicitly requested section, we switch to the section indicated for the
210 /// global instead of NewSection.
212 /// If the new section is an empty string, this method forgets what the
213 /// current section is, but does not emit a .section directive.
215 /// This method is used when about to emit data. For most assemblers, this
216 /// is the same as the SwitchToTextSection method, but not all assemblers
219 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
221 /// getPreferredAlignmentLog - Return the preferred alignment of the
222 /// specified global, returned in log form. This includes an explicitly
223 /// requested alignment (if the global has one).
224 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
226 /// doInitialization - Set up the AsmPrinter when we are working on a new
227 /// module. If your pass overrides this, it must make sure to explicitly
228 /// call this implementation.
229 bool doInitialization(Module &M);
231 /// doFinalization - Shut down the asmprinter. If you override this in your
232 /// pass, you must make sure to call it explicitly.
233 bool doFinalization(Module &M);
235 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
236 /// instruction, using the specified assembler variant. Targets should
237 /// override this to format as appropriate. This method can return true if
238 /// the operand is erroneous.
239 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
240 unsigned AsmVariant, const char *ExtraCode);
242 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
243 /// instruction, using the specified assembler variant as an address.
244 /// Targets should override this to format as appropriate. This method can
245 /// return true if the operand is erroneous.
246 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
248 const char *ExtraCode);
250 /// SetupMachineFunction - This should be called when a new MachineFunction
251 /// is being processed from runOnMachineFunction.
252 void SetupMachineFunction(MachineFunction &MF);
254 /// getFunctionNumber - Return a unique ID for the current function.
256 unsigned getFunctionNumber() const { return FunctionNumber; }
258 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
259 /// not normally call this, as the counter is automatically bumped by
260 /// SetupMachineFunction.
261 void IncrementFunctionNumber() { FunctionNumber++; }
263 /// EmitConstantPool - Print to the current output stream assembly
264 /// representations of the constants in the constant pool MCP. This is
265 /// used to print out constants which have been "spilled to memory" by
266 /// the code generator.
268 void EmitConstantPool(MachineConstantPool *MCP);
270 /// EmitJumpTableInfo - Print assembly representations of the jump tables
271 /// used by the current function to the current output stream.
273 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
275 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
276 /// special global used by LLVM. If so, emit it and return true, otherwise
277 /// do nothing and return false.
278 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
280 /// EmitAlignment - Emit an alignment directive to the specified power of
281 /// two boundary. For example, if you pass in 3 here, you will get an 8
282 /// byte alignment. If a global value is specified, and if that global has
283 /// an explicit alignment requested, it will override the alignment request.
284 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
286 /// EmitZeros - Emit a block of zeros.
288 void EmitZeros(uint64_t NumZeros) const;
290 /// EmitString - Emit a zero-byte-terminated string constant.
292 virtual void EmitString(const ConstantArray *CVA) const;
294 /// EmitConstantValueOnly - Print out the specified constant, without a
295 /// storage class. Only constants of first-class type are allowed here.
296 void EmitConstantValueOnly(const Constant *CV);
298 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
300 void EmitGlobalConstant(const Constant* CV);
302 /// printInlineAsm - This method formats and prints the specified machine
303 /// instruction that is an inline asm.
304 void printInlineAsm(const MachineInstr *MI) const;
306 /// printBasicBlockLabel - This method prints the label for the specified
307 /// MachineBasicBlock
308 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
309 bool printColon = false,
310 bool printComment = true) const;
313 void EmitXXStructorList(Constant *List);