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 AsmPrinter : public MachineFunctionPass {
28 /// CurrentSection - The current section we are emitting to. This is
29 /// controlled and used by the SwitchSection method.
30 std::string CurrentSection;
32 /// FunctionNumber - This provides a unique ID for each function emitted in
33 /// this translation unit. It is autoincremented by SetupMachineFunction,
34 /// and can be accessed with getFunctionNumber() and
35 /// IncrementFunctionNumber().
37 unsigned FunctionNumber;
40 /// Output stream on which we're printing assembly code.
44 /// Target machine description.
48 /// Name-mangler for global names.
52 /// Cache of mangled name for current function. This is recalculated at the
53 /// beginning of each call to runOnMachineFunction().
55 std::string CurrentFnName;
57 //===------------------------------------------------------------------===//
58 // Properties to be set by the derived class ctor, used to configure the
61 /// CommentString - This indicates the comment character used by the
63 const char *CommentString; // Defaults to "#"
65 /// GlobalPrefix - If this is set to a non-empty string, it is prepended
66 /// onto all global symbols. This is often used for "_" or ".".
67 const char *GlobalPrefix; // Defaults to ""
69 /// PrivateGlobalPrefix - This prefix is used for globals like constant
70 /// pool entries that are completely private to the .o file and should not
71 /// have names in the .o file. This is often "." or "L".
72 const char *PrivateGlobalPrefix; // Defaults to "."
74 /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
75 /// will enclose any GlobalVariable (that isn't a function)
77 const char *GlobalVarAddrPrefix; // Defaults to ""
78 const char *GlobalVarAddrSuffix; // Defaults to ""
80 /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
81 /// will enclose any GlobalVariable that points to a function.
82 /// For example, this is used by the IA64 backend to materialize
83 /// function descriptors, by decorating the ".data8" object with the
84 /// @fptr( ) link-relocation operator.
86 const char *FunctionAddrPrefix; // Defaults to ""
87 const char *FunctionAddrSuffix; // Defaults to ""
89 /// InlineAsmStart/End - If these are nonempty, they contain a directive to
90 /// emit before and after an inline assmebly statement.
91 const char *InlineAsmStart; // Defaults to "#APP\n"
92 const char *InlineAsmEnd; // Defaults to "#NO_APP\n"
94 //===--- Data Emission Directives -------------------------------------===//
96 /// ZeroDirective - this should be set to the directive used to get some
97 /// number of zero bytes emitted to the current section. Common cases are
98 /// "\t.zero\t" and "\t.space\t". If this is set to null, the
99 /// Data*bitsDirective's will be used to emit zero bytes.
100 const char *ZeroDirective; // Defaults to "\t.zero\t"
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 /// SwitchToSectionDirective - This is the directive used when we want to
135 /// emit a global to an arbitrary section. The section name is emited after
137 const char *SwitchToSectionDirective; // Defaults to "\t.section\t"
139 /// ConstantPoolSection - This is the section that we SwitchToSection right
140 /// before emitting the constant pool for a function.
141 const char *ConstantPoolSection; // Defaults to "\t.section .rodata\n"
143 /// StaticCtorsSection - This is the directive that is emitted to switch to
144 /// a section to emit the static constructor list.
145 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
146 const char *StaticCtorsSection;
148 /// StaticDtorsSection - This is the directive that is emitted to switch to
149 /// a section to emit the static destructor list.
150 /// Defaults to "\t.section .dtors,\"aw\",@progbits".
151 const char *StaticDtorsSection;
153 //===--- Global Variable Emission Directives --------------------------===//
155 /// LCOMMDirective - This is the name of a directive (if supported) that can
156 /// be used to efficiently declare a local (internal) block of zero
157 /// initialized data in the .bss/.data section. The syntax expected is:
158 /// <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
159 const char *LCOMMDirective; // Defaults to null.
161 const char *COMMDirective; // Defaults to "\t.comm\t".
163 /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
164 /// argument that specifies the alignment of the declaration.
165 bool COMMDirectiveTakesAlignment; // Defaults to true.
167 /// HasDotTypeDotSizeDirective - True if the target has .type and .size
168 /// directives, this is true for most ELF targets.
169 bool HasDotTypeDotSizeDirective; // Defaults to true.
172 AsmPrinter(std::ostream &o, TargetMachine &TM);
175 /// SwitchSection - Switch to the specified section of the executable if we
176 /// are not already in it! If GV is non-null and if the global has an
177 /// explicitly requested section, we switch to the section indicated for the
178 /// global instead of NewSection.
180 /// If the new section is an empty string, this method forgets what the
181 /// current section is, but does not emit a .section directive.
183 void SwitchSection(const char *NewSection, const GlobalValue *GV);
185 /// getPreferredAlignmentLog - Return the preferred alignment of the
186 /// specified global, returned in log form. This includes an explicitly
187 /// requested alignment (if the global has one).
188 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
190 /// doInitialization - Set up the AsmPrinter when we are working on a new
191 /// module. If your pass overrides this, it must make sure to explicitly
192 /// call this implementation.
193 bool doInitialization(Module &M);
195 /// doFinalization - Shut down the asmprinter. If you override this in your
196 /// pass, you must make sure to call it explicitly.
197 bool doFinalization(Module &M);
199 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
200 /// instruction, using the specified assembler variant. Targets should
201 /// overried this to format as appropriate. This method can return true if
202 /// the operand is erroneous.
203 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
204 unsigned AsmVariant, const char *ExtraCode);
206 /// SetupMachineFunction - This should be called when a new MachineFunction
207 /// is being processed from runOnMachineFunction.
208 void SetupMachineFunction(MachineFunction &MF);
210 /// getFunctionNumber - Return a unique ID for the current function.
212 unsigned getFunctionNumber() const { return FunctionNumber; }
214 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
215 /// not normally call this, as the counter is automatically bumped by
216 /// SetupMachineFunction.
217 void IncrementFunctionNumber() { FunctionNumber++; }
219 /// EmitConstantPool - Print to the current output stream assembly
220 /// representations of the constants in the constant pool MCP. This is
221 /// used to print out constants which have been "spilled to memory" by
222 /// the code generator.
224 void EmitConstantPool(MachineConstantPool *MCP);
226 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
227 /// special global used by LLVM. If so, emit it and return true, otherwise
228 /// do nothing and return false.
229 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
231 /// EmitAlignment - Emit an alignment directive to the specified power of
232 /// two boundary. For example, if you pass in 3 here, you will get an 8
233 /// byte alignment. If a global value is specified, and if that global has
234 /// an explicit alignment requested, it will override the alignment request.
235 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
237 /// EmitZeros - Emit a block of zeros.
239 void EmitZeros(uint64_t NumZeros) const;
241 /// EmitConstantValueOnly - Print out the specified constant, without a
242 /// storage class. Only constants of first-class type are allowed here.
243 void EmitConstantValueOnly(const Constant *CV);
245 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
247 void EmitGlobalConstant(const Constant* CV);
249 /// printInlineAsm - This method formats and prints the specified machine
250 /// instruction that is an inline asm.
251 void printInlineAsm(const MachineInstr *MI) const;
253 void EmitXXStructorList(Constant *List);