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;
47 // Necessary for external weak linkage support
48 std::set<const GlobalValue*> ExtWeakSymbols;
51 /// Output stream on which we're printing assembly code.
55 /// Target machine description.
59 /// Target Asm Printer information.
61 const TargetAsmInfo *TAI;
63 /// Name-mangler for global names.
67 /// Cache of mangled name for current function. This is recalculated at the
68 /// beginning of each call to runOnMachineFunction().
70 std::string CurrentFnName;
72 /// CurrentSection - The current section we are emitting to. This is
73 /// controlled and used by the SwitchSection method.
74 std::string CurrentSection;
77 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
80 /// SwitchToTextSection - Switch to the specified section of the executable
81 /// if we are not already in it! If GV is non-null and if the global has an
82 /// explicitly requested section, we switch to the section indicated for the
83 /// global instead of NewSection.
85 /// If the new section is an empty string, this method forgets what the
86 /// current section is, but does not emit a .section directive.
88 /// This method is used when about to emit executable code.
90 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
92 /// SwitchToDataSection - Switch to the specified section of the executable
93 /// if we are not already in it! If GV is non-null and if the global has an
94 /// explicitly requested section, we switch to the section indicated for the
95 /// global instead of NewSection.
97 /// If the new section is an empty string, this method forgets what the
98 /// current section is, but does not emit a .section directive.
100 /// This method is used when about to emit data. For most assemblers, this
101 /// is the same as the SwitchToTextSection method, but not all assemblers
104 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
106 /// getGlobalLinkName - Returns the asm/link name of of the specified
107 /// global variable. Should be overridden by each target asm printer to
108 /// generate the appropriate value.
109 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
111 /// EmitExternalGlobal - Emit the external reference to a global variable.
112 /// Should be overridden if an indirect reference should be used.
113 virtual void EmitExternalGlobal(const GlobalVariable *GV);
115 /// getCurrentFunctionEHName - Called to return (and cache) the
118 std::string getCurrentFunctionEHName(const MachineFunction *MF);
121 /// doInitialization - Set up the AsmPrinter when we are working on a new
122 /// module. If your pass overrides this, it must make sure to explicitly
123 /// call this implementation.
124 bool doInitialization(Module &M);
126 /// doFinalization - Shut down the asmprinter. If you override this in your
127 /// pass, you must make sure to call it explicitly.
128 bool doFinalization(Module &M);
130 /// PrintSpecial - Print information related to the specified machine instr
131 /// that is independent of the operand, and may be independent of the instr
132 /// itself. This can be useful for portably encoding the comment character
133 /// or other bits of target-specific knowledge into the asmstrings. The
134 /// syntax used is ${:comment}. Targets can override this to add support
135 /// for their own strange codes.
136 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
138 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
139 /// instruction, using the specified assembler variant. Targets should
140 /// override this to format as appropriate. This method can return true if
141 /// the operand is erroneous.
142 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
143 unsigned AsmVariant, const char *ExtraCode);
145 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
146 /// instruction, using the specified assembler variant as an address.
147 /// Targets should override this to format as appropriate. This method can
148 /// return true if the operand is erroneous.
149 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
151 const char *ExtraCode);
153 /// getSectionForFunction - Return the section that we should emit the
154 /// specified function body into. This defaults to 'TextSection'. This
155 /// should most likely be overridden by the target to put linkonce/weak
156 /// functions into special sections.
157 virtual std::string getSectionForFunction(const Function &F) const;
159 /// SetupMachineFunction - This should be called when a new MachineFunction
160 /// is being processed from runOnMachineFunction.
161 void SetupMachineFunction(MachineFunction &MF);
163 /// getFunctionNumber - Return a unique ID for the current function.
165 unsigned getFunctionNumber() const { return FunctionNumber; }
167 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
168 /// not normally call this, as the counter is automatically bumped by
169 /// SetupMachineFunction.
170 void IncrementFunctionNumber() { FunctionNumber++; }
172 /// EmitConstantPool - Print to the current output stream assembly
173 /// representations of the constants in the constant pool MCP. This is
174 /// used to print out constants which have been "spilled to memory" by
175 /// the code generator.
177 void EmitConstantPool(MachineConstantPool *MCP);
179 /// EmitJumpTableInfo - Print assembly representations of the jump tables
180 /// used by the current function to the current output stream.
182 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
184 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
185 /// special global used by LLVM. If so, emit it and return true, otherwise
186 /// do nothing and return false.
187 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
190 //===------------------------------------------------------------------===//
191 /// LEB 128 number encoding.
193 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
194 /// representing an unsigned leb128 value.
195 void PrintULEB128(unsigned Value) const;
197 /// SizeULEB128 - Compute the number of bytes required for an unsigned
199 static unsigned SizeULEB128(unsigned Value);
201 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
202 /// representing a signed leb128 value.
203 void PrintSLEB128(int Value) const;
205 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
207 static unsigned SizeSLEB128(int Value);
209 //===------------------------------------------------------------------===//
210 // Emission and print routines
213 /// PrintHex - Print a value as a hexidecimal value.
215 void PrintHex(int Value) const;
217 /// EOL - Print a newline character to asm stream. If a comment is present
218 /// then it will be printed first. Comments should not contain '\n'.
220 void EOL(const std::string &Comment) const;
222 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
223 /// unsigned leb128 value.
224 void EmitULEB128Bytes(unsigned Value) const;
226 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
227 /// signed leb128 value.
228 void EmitSLEB128Bytes(int Value) const;
230 /// EmitInt8 - Emit a byte directive and value.
232 void EmitInt8(int Value) const;
234 /// EmitInt16 - Emit a short directive and value.
236 void EmitInt16(int Value) const;
238 /// EmitInt32 - Emit a long directive and value.
240 void EmitInt32(int Value) const;
242 /// EmitInt64 - Emit a long long directive and value.
244 void EmitInt64(uint64_t Value) const;
246 /// EmitString - Emit a string with quotes and a null terminator.
247 /// Special characters are emitted properly.
248 /// @verbatim (Eg. '\t') @endverbatim
249 void EmitString(const std::string &String) const;
251 //===------------------------------------------------------------------===//
253 /// EmitAlignment - Emit an alignment directive to the specified power of
254 /// two boundary. For example, if you pass in 3 here, you will get an 8
255 /// byte alignment. If a global value is specified, and if that global has
256 /// an explicit alignment requested, it will unconditionally override the
257 /// alignment request. However, if ForcedAlignBits is specified, this value
258 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
259 /// and the alignment computed with NumBits and the global. If UseFillExpr
260 /// is true, it also emits an optional second value FillValue which the
261 /// assembler uses to fill gaps to match alignment.
263 /// The algorithm is:
265 /// if (GV && GV->hasalignment) Align = GV->getalignment();
266 /// Align = std::max(Align, ForcedAlignBits);
268 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
269 unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
270 unsigned FillValue = 0) const;
273 /// EmitZeros - Emit a block of zeros.
275 void EmitZeros(uint64_t NumZeros) const;
277 /// EmitString - Emit a zero-byte-terminated string constant.
279 virtual void EmitString(const ConstantArray *CVA) const;
281 /// EmitConstantValueOnly - Print out the specified constant, without a
282 /// storage class. Only constants of first-class type are allowed here.
283 void EmitConstantValueOnly(const Constant *CV);
285 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
287 void EmitGlobalConstant(const Constant* CV);
289 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
291 /// printInlineAsm - This method formats and prints the specified machine
292 /// instruction that is an inline asm.
293 void printInlineAsm(const MachineInstr *MI) const;
295 /// printLabel - This method prints a local label used by debug and
296 /// exception handling tables.
297 void printLabel(const MachineInstr *MI) const;
299 /// printBasicBlockLabel - This method prints the label for the specified
300 /// MachineBasicBlock
301 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
302 bool printColon = false,
303 bool printComment = true) const;
305 /// printSetLabel - This method prints a set label for the specified
306 /// MachineBasicBlock
307 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
308 void printSetLabel(unsigned uid, unsigned uid2,
309 const MachineBasicBlock *MBB) const;
311 /// printDataDirective - This method prints the asm directive for the
313 void printDataDirective(const Type *type);
316 void EmitLLVMUsedList(Constant *List);
317 void EmitXXStructorList(Constant *List);
318 void EmitConstantPool(unsigned Alignment, const char *Section,
319 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);