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 {
40 // Necessary for external weak linkage support
41 std::set<const GlobalValue*> ExtWeakSymbols;
44 /// Output stream on which we're printing assembly code.
48 /// Target machine description.
52 /// Target Asm Printer information.
54 const TargetAsmInfo *TAI;
56 /// Name-mangler for global names.
60 /// Cache of mangled name for current function. This is recalculated at the
61 /// beginning of each call to runOnMachineFunction().
63 std::string CurrentFnName;
65 /// CurrentSection - The current section we are emitting to. This is
66 /// controlled and used by the SwitchSection method.
67 std::string CurrentSection;
70 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
73 /// SwitchToTextSection - Switch to the specified section of the executable
74 /// if we are not already in it! If GV is non-null and if the global has an
75 /// explicitly requested section, we switch to the section indicated for the
76 /// global instead of NewSection.
78 /// If the new section is an empty string, this method forgets what the
79 /// current section is, but does not emit a .section directive.
81 /// This method is used when about to emit executable code.
83 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
85 /// SwitchToDataSection - 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 data. For most assemblers, this
94 /// is the same as the SwitchToTextSection method, but not all assemblers
97 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
99 /// getGlobalLinkName - Returns the asm/link name of of the specified
100 /// global variable. Should be overridden by each target asm printer to
101 /// generate the appropriate value.
102 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
104 /// EmitExternalGlobal - Emit the external reference to a global variable.
105 /// Should be overridden if an indirect reference should be used.
106 virtual void EmitExternalGlobal(const GlobalVariable *GV);
108 /// getCurrentFunctionEHName - Called to return (and cache) the
111 std::string getCurrentFunctionEHName(const MachineFunction *MF);
114 /// doInitialization - Set up the AsmPrinter when we are working on a new
115 /// module. If your pass overrides this, it must make sure to explicitly
116 /// call this implementation.
117 bool doInitialization(Module &M);
119 /// doFinalization - Shut down the asmprinter. If you override this in your
120 /// pass, you must make sure to call it explicitly.
121 bool doFinalization(Module &M);
123 /// PrintSpecial - Print information related to the specified machine instr
124 /// that is independent of the operand, and may be independent of the instr
125 /// itself. This can be useful for portably encoding the comment character
126 /// or other bits of target-specific knowledge into the asmstrings. The
127 /// syntax used is ${:comment}. Targets can override this to add support
128 /// for their own strange codes.
129 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
131 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
132 /// instruction, using the specified assembler variant. Targets should
133 /// override this to format as appropriate. This method can return true if
134 /// the operand is erroneous.
135 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
136 unsigned AsmVariant, const char *ExtraCode);
138 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
139 /// instruction, using the specified assembler variant as an address.
140 /// Targets should override this to format as appropriate. This method can
141 /// return true if the operand is erroneous.
142 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
144 const char *ExtraCode);
146 /// getSectionForFunction - Return the section that we should emit the
147 /// specified function body into. This defaults to 'TextSection'. This
148 /// should most likely be overridden by the target to put linkonce/weak
149 /// functions into special sections.
150 virtual std::string getSectionForFunction(const Function &F) const;
152 /// SetupMachineFunction - This should be called when a new MachineFunction
153 /// is being processed from runOnMachineFunction.
154 void SetupMachineFunction(MachineFunction &MF);
156 /// EmitConstantPool - Print to the current output stream assembly
157 /// representations of the constants in the constant pool MCP. This is
158 /// used to print out constants which have been "spilled to memory" by
159 /// the code generator.
161 void EmitConstantPool(MachineConstantPool *MCP);
163 /// EmitJumpTableInfo - Print assembly representations of the jump tables
164 /// used by the current function to the current output stream.
166 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
168 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
169 /// special global used by LLVM. If so, emit it and return true, otherwise
170 /// do nothing and return false.
171 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
174 //===------------------------------------------------------------------===//
175 /// LEB 128 number encoding.
177 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
178 /// representing an unsigned leb128 value.
179 void PrintULEB128(unsigned Value) const;
181 /// SizeULEB128 - Compute the number of bytes required for an unsigned
183 static unsigned SizeULEB128(unsigned Value);
185 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
186 /// representing a signed leb128 value.
187 void PrintSLEB128(int Value) const;
189 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
191 static unsigned SizeSLEB128(int Value);
193 //===------------------------------------------------------------------===//
194 // Emission and print routines
197 /// PrintHex - Print a value as a hexidecimal value.
199 void PrintHex(int Value) const;
201 /// EOL - Print a newline character to asm stream. If a comment is present
202 /// then it will be printed first. Comments should not contain '\n'.
204 void EOL(const std::string &Comment) const;
206 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
207 /// unsigned leb128 value.
208 void EmitULEB128Bytes(unsigned Value) const;
210 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
211 /// signed leb128 value.
212 void EmitSLEB128Bytes(int Value) const;
214 /// EmitInt8 - Emit a byte directive and value.
216 void EmitInt8(int Value) const;
218 /// EmitInt16 - Emit a short directive and value.
220 void EmitInt16(int Value) const;
222 /// EmitInt32 - Emit a long directive and value.
224 void EmitInt32(int Value) const;
226 /// EmitInt64 - Emit a long long directive and value.
228 void EmitInt64(uint64_t Value) const;
230 /// EmitString - Emit a string with quotes and a null terminator.
231 /// Special characters are emitted properly.
232 /// @verbatim (Eg. '\t') @endverbatim
233 void EmitString(const std::string &String) const;
235 /// EmitFile - Emit a .file directive.
236 void EmitFile(unsigned Number, const std::string &Name) const;
238 //===------------------------------------------------------------------===//
240 /// EmitAlignment - Emit an alignment directive to the specified power of
241 /// two boundary. For example, if you pass in 3 here, you will get an 8
242 /// byte alignment. If a global value is specified, and if that global has
243 /// an explicit alignment requested, it will unconditionally override the
244 /// alignment request. However, if ForcedAlignBits is specified, this value
245 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
246 /// and the alignment computed with NumBits and the global. If UseFillExpr
247 /// is true, it also emits an optional second value FillValue which the
248 /// assembler uses to fill gaps to match alignment.
250 /// The algorithm is:
252 /// if (GV && GV->hasalignment) Align = GV->getalignment();
253 /// Align = std::max(Align, ForcedAlignBits);
255 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
256 unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
257 unsigned FillValue = 0) const;
260 /// EmitZeros - Emit a block of zeros.
262 void EmitZeros(uint64_t NumZeros) const;
264 /// EmitString - Emit a zero-byte-terminated string constant.
266 virtual void EmitString(const ConstantArray *CVA) const;
268 /// EmitConstantValueOnly - Print out the specified constant, without a
269 /// storage class. Only constants of first-class type are allowed here.
270 void EmitConstantValueOnly(const Constant *CV);
272 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
274 void EmitGlobalConstant(const Constant* CV);
276 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
278 /// printInlineAsm - This method formats and prints the specified machine
279 /// instruction that is an inline asm.
280 void printInlineAsm(const MachineInstr *MI) const;
282 /// printLabel - This method prints a local label used by debug and
283 /// exception handling tables.
284 void printLabel(const MachineInstr *MI) const;
286 /// printBasicBlockLabel - This method prints the label for the specified
287 /// MachineBasicBlock
288 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
289 bool printColon = false,
290 bool printComment = true) const;
292 /// printSetLabel - This method prints a set label for the specified
293 /// MachineBasicBlock
294 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
295 void printSetLabel(unsigned uid, unsigned uid2,
296 const MachineBasicBlock *MBB) const;
298 /// printDataDirective - This method prints the asm directive for the
300 void printDataDirective(const Type *type);
303 void EmitLLVMUsedList(Constant *List);
304 void EmitXXStructorList(Constant *List);
305 void EmitConstantPool(unsigned Alignment, const char *Section,
306 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);