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 file contains a class to be used as the base class for target specific
11 // asm writers. This class primarily handles common functionality used by
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 MachineConstantPoolEntry;
29 class MachineConstantPoolValue;
34 /// AsmPrinter - This class is intended to be used as a driving class for all
36 class AsmPrinter : public MachineFunctionPass {
39 /// FunctionNumber - This provides a unique ID for each function emitted in
40 /// this translation unit. It is autoincremented by SetupMachineFunction,
41 /// and can be accessed with getFunctionNumber() and
42 /// IncrementFunctionNumber().
44 unsigned FunctionNumber;
46 /// Cache of mangled exception handling name for current function. This is
47 /// recalculated at the beginning of each call to runOnMachineFunction().
49 std::string CurrentFnEHName;
52 // Necessary for external weak linkage support
53 std::set<const GlobalValue*> ExtWeakSymbols;
56 /// Output stream on which we're printing assembly code.
60 /// Target machine description.
64 /// Target Asm Printer information.
66 const TargetAsmInfo *TAI;
68 /// Name-mangler for global names.
72 /// Cache of mangled name for current function. This is recalculated at the
73 /// beginning of each call to runOnMachineFunction().
75 std::string CurrentFnName;
77 /// CurrentSection - The current section we are emitting to. This is
78 /// controlled and used by the SwitchSection method.
79 std::string CurrentSection;
82 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
85 /// SwitchToTextSection - Switch to the specified section of the executable
86 /// if we are not already in it! If GV is non-null and if the global has an
87 /// explicitly requested section, we switch to the section indicated for the
88 /// global instead of NewSection.
90 /// If the new section is an empty string, this method forgets what the
91 /// current section is, but does not emit a .section directive.
93 /// This method is used when about to emit executable code.
95 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
97 /// SwitchToDataSection - Switch to the specified section of the executable
98 /// if we are not already in it! If GV is non-null and if the global has an
99 /// explicitly requested section, we switch to the section indicated for the
100 /// global instead of NewSection.
102 /// If the new section is an empty string, this method forgets what the
103 /// current section is, but does not emit a .section directive.
105 /// This method is used when about to emit data. For most assemblers, this
106 /// is the same as the SwitchToTextSection method, but not all assemblers
109 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
111 /// getGlobalLinkName - Returns the asm/link name of of the specified
112 /// global variable. Should be overridden by each target asm printer to
113 /// generate the appropriate value.
114 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
116 /// EmitExternalGlobal - Emit the external reference to a global variable.
117 /// Should be overridden if an indirect reference should be used.
118 virtual void EmitExternalGlobal(const GlobalVariable *GV);
120 /// getCurrentFunctionEHName - Called to return (and cache) the
123 const std::string &getCurrentFunctionEHName(const MachineFunction *MF);
126 /// doInitialization - Set up the AsmPrinter when we are working on a new
127 /// module. If your pass overrides this, it must make sure to explicitly
128 /// call this implementation.
129 bool doInitialization(Module &M);
131 /// doFinalization - Shut down the asmprinter. If you override this in your
132 /// pass, you must make sure to call it explicitly.
133 bool doFinalization(Module &M);
135 /// PrintSpecial - Print information related to the specified machine instr
136 /// that is independent of the operand, and may be independent of the instr
137 /// itself. This can be useful for portably encoding the comment character
138 /// or other bits of target-specific knowledge into the asmstrings. The
139 /// syntax used is ${:comment}. Targets can override this to add support
140 /// for their own strange codes.
141 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
143 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
144 /// instruction, using the specified assembler variant. Targets should
145 /// override this to format as appropriate. This method can return true if
146 /// the operand is erroneous.
147 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
148 unsigned AsmVariant, const char *ExtraCode);
150 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
151 /// instruction, using the specified assembler variant as an address.
152 /// Targets should override this to format as appropriate. This method can
153 /// return true if the operand is erroneous.
154 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
156 const char *ExtraCode);
158 /// getSectionForFunction - Return the section that we should emit the
159 /// specified function body into. This defaults to 'TextSection'. This
160 /// should most likely be overridden by the target to put linkonce/weak
161 /// functions into special sections.
162 virtual std::string getSectionForFunction(const Function &F) const;
164 /// SetupMachineFunction - This should be called when a new MachineFunction
165 /// is being processed from runOnMachineFunction.
166 void SetupMachineFunction(MachineFunction &MF);
168 /// getFunctionNumber - Return a unique ID for the current function.
170 unsigned getFunctionNumber() const { return FunctionNumber; }
172 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
173 /// not normally call this, as the counter is automatically bumped by
174 /// SetupMachineFunction.
175 void IncrementFunctionNumber() { FunctionNumber++; }
177 /// EmitConstantPool - Print to the current output stream assembly
178 /// representations of the constants in the constant pool MCP. This is
179 /// used to print out constants which have been "spilled to memory" by
180 /// the code generator.
182 void EmitConstantPool(MachineConstantPool *MCP);
184 /// EmitJumpTableInfo - Print assembly representations of the jump tables
185 /// used by the current function to the current output stream.
187 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
189 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
190 /// special global used by LLVM. If so, emit it and return true, otherwise
191 /// do nothing and return false.
192 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
195 //===------------------------------------------------------------------===//
196 /// LEB 128 number encoding.
198 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
199 /// representing an unsigned leb128 value.
200 void PrintULEB128(unsigned Value) const;
202 /// SizeULEB128 - Compute the number of bytes required for an unsigned
204 static unsigned SizeULEB128(unsigned Value);
206 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
207 /// representing a signed leb128 value.
208 void PrintSLEB128(int Value) const;
210 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
212 static unsigned SizeSLEB128(int Value);
214 //===------------------------------------------------------------------===//
215 // Emission and print routines
218 /// PrintHex - Print a value as a hexidecimal value.
220 void PrintHex(int Value) const;
222 /// EOL - Print a newline character to asm stream. If a comment is present
223 /// then it will be printed first. Comments should not contain '\n'.
225 void EOL(const std::string &Comment) const;
227 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
228 /// unsigned leb128 value.
229 void EmitULEB128Bytes(unsigned Value) const;
231 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
232 /// signed leb128 value.
233 void EmitSLEB128Bytes(int Value) const;
235 /// EmitInt8 - Emit a byte directive and value.
237 void EmitInt8(int Value) const;
239 /// EmitInt16 - Emit a short directive and value.
241 void EmitInt16(int Value) const;
243 /// EmitInt32 - Emit a long directive and value.
245 void EmitInt32(int Value) const;
247 /// EmitInt64 - Emit a long long directive and value.
249 void EmitInt64(uint64_t Value) const;
251 /// EmitString - Emit a string with quotes and a null terminator.
252 /// Special characters are emitted properly.
253 /// @verbatim (Eg. '\t') @endverbatim
254 void EmitString(const std::string &String) const;
256 //===------------------------------------------------------------------===//
258 /// EmitAlignment - Emit an alignment directive to the specified power of
259 /// two boundary. For example, if you pass in 3 here, you will get an 8
260 /// byte alignment. If a global value is specified, and if that global has
261 /// an explicit alignment requested, it will unconditionally override the
262 /// alignment request. However, if ForcedAlignBits is specified, this value
263 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
264 /// and the alignment computed with NumBits and the global. If UseFillExpr
265 /// is true, it also emits an optional second value FillValue which the
266 /// assembler uses to fill gaps to match alignment.
268 /// The algorithm is:
270 /// if (GV && GV->hasalignment) Align = GV->getalignment();
271 /// Align = std::max(Align, ForcedAlignBits);
273 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
274 unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
275 unsigned FillValue = 0) const;
278 /// EmitZeros - Emit a block of zeros.
280 void EmitZeros(uint64_t NumZeros) const;
282 /// EmitString - Emit a zero-byte-terminated string constant.
284 virtual void EmitString(const ConstantArray *CVA) const;
286 /// EmitConstantValueOnly - Print out the specified constant, without a
287 /// storage class. Only constants of first-class type are allowed here.
288 void EmitConstantValueOnly(const Constant *CV);
290 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
292 void EmitGlobalConstant(const Constant* CV);
294 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
296 /// printInlineAsm - This method formats and prints the specified machine
297 /// instruction that is an inline asm.
298 void printInlineAsm(const MachineInstr *MI) const;
300 /// printLabel - This method prints a local label used by debug and
301 /// exception handling tables.
302 void printLabel(const MachineInstr *MI) const;
304 /// printBasicBlockLabel - This method prints the label for the specified
305 /// MachineBasicBlock
306 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
307 bool printColon = false,
308 bool printComment = true) const;
310 /// printSetLabel - This method prints a set label for the specified
311 /// MachineBasicBlock
312 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
313 void printSetLabel(unsigned uid, unsigned uid2,
314 const MachineBasicBlock *MBB) const;
316 /// printDataDirective - This method prints the asm directive for the
318 void printDataDirective(const Type *type);
321 void EmitLLVMUsedList(Constant *List);
322 void EmitXXStructorList(Constant *List);
323 void EmitConstantPool(unsigned Alignment, const char *Section,
324 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);