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"
27 class MachineConstantPoolEntry;
28 class MachineConstantPoolValue;
33 /// AsmPrinter - This class is intended to be used as a driving class for all
35 class AsmPrinter : public MachineFunctionPass {
36 /// FunctionNumber - This provides a unique ID for each function emitted in
37 /// this translation unit. It is autoincremented by SetupMachineFunction,
38 /// and can be accessed with getFunctionNumber() and
39 /// IncrementFunctionNumber().
41 unsigned FunctionNumber;
44 // Necessary for external weak linkage support
45 std::set<const GlobalValue*> ExtWeakSymbols;
48 /// Output stream on which we're printing assembly code.
52 /// Target machine description.
56 /// Target Asm Printer information.
58 const TargetAsmInfo *TAI;
60 /// Name-mangler for global names.
64 /// Cache of mangled name for current function. This is recalculated at the
65 /// beginning of each call to runOnMachineFunction().
67 std::string CurrentFnName;
69 /// CurrentSection - The current section we are emitting to. This is
70 /// controlled and used by the SwitchSection method.
71 std::string CurrentSection;
74 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
77 /// SwitchToTextSection - Switch to the specified section of the executable
78 /// if we are not already in it! If GV is non-null and if the global has an
79 /// explicitly requested section, we switch to the section indicated for the
80 /// global instead of NewSection.
82 /// If the new section is an empty string, this method forgets what the
83 /// current section is, but does not emit a .section directive.
85 /// This method is used when about to emit executable code.
87 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
89 /// SwitchToDataSection - Switch to the specified section of the executable
90 /// if we are not already in it! If GV is non-null and if the global has an
91 /// explicitly requested section, we switch to the section indicated for the
92 /// global instead of NewSection.
94 /// If the new section is an empty string, this method forgets what the
95 /// current section is, but does not emit a .section directive.
97 /// This method is used when about to emit data. For most assemblers, this
98 /// is the same as the SwitchToTextSection method, but not all assemblers
101 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
103 /// getGlobalLinkName - Returns the asm/link name of of the specified
104 /// global variable. Should be overridden by each target asm printer to
105 /// generate the appropriate value.
106 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
108 /// EmitExternalGlobal - Emit the external reference to a global variable.
109 /// Should be overridden if an indirect reference should be used.
110 virtual void EmitExternalGlobal(const GlobalVariable *GV);
113 /// doInitialization - Set up the AsmPrinter when we are working on a new
114 /// module. If your pass overrides this, it must make sure to explicitly
115 /// call this implementation.
116 bool doInitialization(Module &M);
118 /// doFinalization - Shut down the asmprinter. If you override this in your
119 /// pass, you must make sure to call it explicitly.
120 bool doFinalization(Module &M);
122 /// PrintSpecial - Print information related to the specified machine instr
123 /// that is independent of the operand, and may be independent of the instr
124 /// itself. This can be useful for portably encoding the comment character
125 /// or other bits of target-specific knowledge into the asmstrings. The
126 /// syntax used is ${:comment}. Targets can override this to add support
127 /// for their own strange codes.
128 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
130 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
131 /// instruction, using the specified assembler variant. Targets should
132 /// override this to format as appropriate. This method can return true if
133 /// the operand is erroneous.
134 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
135 unsigned AsmVariant, const char *ExtraCode);
137 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
138 /// instruction, using the specified assembler variant as an address.
139 /// Targets should override this to format as appropriate. This method can
140 /// return true if the operand is erroneous.
141 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
143 const char *ExtraCode);
145 /// getSectionForFunction - Return the section that we should emit the
146 /// specified function body into. This defaults to 'TextSection'. This
147 /// should most likely be overridden by the target to put linkonce/weak
148 /// functions into special sections.
149 virtual std::string getSectionForFunction(const Function &F) const;
151 /// SetupMachineFunction - This should be called when a new MachineFunction
152 /// is being processed from runOnMachineFunction.
153 void SetupMachineFunction(MachineFunction &MF);
155 /// getFunctionNumber - Return a unique ID for the current function.
157 unsigned getFunctionNumber() const { return FunctionNumber; }
159 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
160 /// not normally call this, as the counter is automatically bumped by
161 /// SetupMachineFunction.
162 void IncrementFunctionNumber() { FunctionNumber++; }
164 /// EmitConstantPool - Print to the current output stream assembly
165 /// representations of the constants in the constant pool MCP. This is
166 /// used to print out constants which have been "spilled to memory" by
167 /// the code generator.
169 void EmitConstantPool(MachineConstantPool *MCP);
171 /// EmitJumpTableInfo - Print assembly representations of the jump tables
172 /// used by the current function to the current output stream.
174 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
176 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
177 /// special global used by LLVM. If so, emit it and return true, otherwise
178 /// do nothing and return false.
179 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
182 //===------------------------------------------------------------------===//
183 /// LEB 128 number encoding.
185 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
186 /// representing an unsigned leb128 value.
187 void PrintULEB128(unsigned Value) const;
189 /// SizeULEB128 - Compute the number of bytes required for an unsigned
191 static unsigned SizeULEB128(unsigned Value);
193 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
194 /// representing a signed leb128 value.
195 void PrintSLEB128(int Value) const;
197 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
199 static unsigned SizeSLEB128(int Value);
201 //===------------------------------------------------------------------===//
202 // Emission and print routines
205 /// PrintHex - Print a value as a hexidecimal value.
207 void PrintHex(int Value) const;
209 /// EOL - Print a newline character to asm stream. If a comment is present
210 /// then it will be printed first. Comments should not contain '\n'.
212 void EOL(const std::string &Comment) const;
214 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
215 /// unsigned leb128 value.
216 void EmitULEB128Bytes(unsigned Value) const;
218 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
219 /// signed leb128 value.
220 void EmitSLEB128Bytes(int Value) const;
222 /// EmitInt8 - Emit a byte directive and value.
224 void EmitInt8(int Value) const;
226 /// EmitInt16 - Emit a short directive and value.
228 void EmitInt16(int Value) const;
230 /// EmitInt32 - Emit a long directive and value.
232 void EmitInt32(int Value) const;
234 /// EmitInt64 - Emit a long long directive and value.
236 void EmitInt64(uint64_t Value) const;
238 /// EmitString - Emit a string with quotes and a null terminator.
239 /// Special characters are emitted properly.
240 /// \literal (Eg. '\t') \endliteral
241 void EmitString(const std::string &String) const;
243 //===------------------------------------------------------------------===//
245 /// EmitAlignment - Emit an alignment directive to the specified power of
246 /// two boundary. For example, if you pass in 3 here, you will get an 8
247 /// byte alignment. If a global value is specified, and if that global has
248 /// an explicit alignment requested, it will override the alignment request.
249 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
252 /// EmitZeros - Emit a block of zeros.
254 void EmitZeros(uint64_t NumZeros) const;
256 /// EmitString - Emit a zero-byte-terminated string constant.
258 virtual void EmitString(const ConstantArray *CVA) const;
260 /// EmitConstantValueOnly - Print out the specified constant, without a
261 /// storage class. Only constants of first-class type are allowed here.
262 void EmitConstantValueOnly(const Constant *CV);
264 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
266 void EmitGlobalConstant(const Constant* CV);
268 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
270 /// printInlineAsm - This method formats and prints the specified machine
271 /// instruction that is an inline asm.
272 void printInlineAsm(const MachineInstr *MI) const;
274 /// printLabel - This method prints a local label used by debug and
275 /// exception handling tables.
276 void printLabel(const MachineInstr *MI) const;
278 /// printBasicBlockLabel - This method prints the label for the specified
279 /// MachineBasicBlock
280 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
281 bool printColon = false,
282 bool printComment = true) const;
284 /// printSetLabel - This method prints a set label for the specified
285 /// MachineBasicBlock
286 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
287 void printSetLabel(unsigned uid, unsigned uid2,
288 const MachineBasicBlock *MBB) const;
290 /// printDataDirective - This method prints the asm directive for the
292 void printDataDirective(const Type *type);
295 void EmitLLVMUsedList(Constant *List);
296 void EmitXXStructorList(Constant *List);
297 void EmitConstantPool(unsigned Alignment, const char *Section,
298 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);