1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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;
30 class MachineModuleInfo;
35 /// AsmPrinter - This class is intended to be used as a driving class for all
37 class AsmPrinter : public MachineFunctionPass {
40 /// FunctionNumber - This provides a unique ID for each function emitted in
41 /// this translation unit. It is autoincremented by SetupMachineFunction,
42 /// and can be accessed with getFunctionNumber() and
43 /// IncrementFunctionNumber().
45 unsigned FunctionNumber;
47 /// MachineModuleInfo - This is needed because printDeclare() has to insert
48 /// DebugVariable entries into the dwarf table. This is a short term hack
49 /// that ought be fixed soon.
50 MachineModuleInfo *MMI;
53 // Necessary for external weak linkage support
54 std::set<const GlobalValue*> ExtWeakSymbols;
57 /// Output stream on which we're printing assembly code.
61 /// Target machine description.
65 /// Target Asm Printer information.
67 const TargetAsmInfo *TAI;
69 /// Target Register Information.
71 const TargetRegisterInfo *TRI;
73 /// Name-mangler for global names.
77 /// Cache of mangled name for current function. This is recalculated at the
78 /// beginning of each call to runOnMachineFunction().
80 std::string CurrentFnName;
82 /// CurrentSection - The current section we are emitting to. This is
83 /// controlled and used by the SwitchSection method.
84 std::string CurrentSection;
86 /// IsInTextSection - True if the current section we are emitting to is a
91 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
94 /// SwitchToTextSection - Switch to the specified section of the executable
95 /// if we are not already in it! If GV is non-null and if the global has an
96 /// explicitly requested section, we switch to the section indicated for the
97 /// global instead of NewSection.
99 /// If the new section is an empty string, this method forgets what the
100 /// current section is, but does not emit a .section directive.
102 /// This method is used when about to emit executable code.
104 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
106 /// SwitchToDataSection - Switch to the specified section of the executable
107 /// if we are not already in it! If GV is non-null and if the global has an
108 /// explicitly requested section, we switch to the section indicated for the
109 /// global instead of NewSection.
111 /// If the new section is an empty string, this method forgets what the
112 /// current section is, but does not emit a .section directive.
114 /// This method is used when about to emit data. For most assemblers, this
115 /// is the same as the SwitchToTextSection method, but not all assemblers
118 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
120 /// getGlobalLinkName - Returns the asm/link name of of the specified
121 /// global variable. Should be overridden by each target asm printer to
122 /// generate the appropriate value.
123 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
125 /// EmitExternalGlobal - Emit the external reference to a global variable.
126 /// Should be overridden if an indirect reference should be used.
127 virtual void EmitExternalGlobal(const GlobalVariable *GV);
129 /// getCurrentFunctionEHName - Called to return (and cache) the
132 std::string getCurrentFunctionEHName(const MachineFunction *MF);
135 /// getAnalysisUsage - Record analysis usage.
137 void getAnalysisUsage(AnalysisUsage &AU) const;
139 /// doInitialization - Set up the AsmPrinter when we are working on a new
140 /// module. If your pass overrides this, it must make sure to explicitly
141 /// call this implementation.
142 bool doInitialization(Module &M);
144 /// doFinalization - Shut down the asmprinter. If you override this in your
145 /// pass, you must make sure to call it explicitly.
146 bool doFinalization(Module &M);
148 /// PrintSpecial - Print information related to the specified machine instr
149 /// that is independent of the operand, and may be independent of the instr
150 /// itself. This can be useful for portably encoding the comment character
151 /// or other bits of target-specific knowledge into the asmstrings. The
152 /// syntax used is ${:comment}. Targets can override this to add support
153 /// for their own strange codes.
154 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
156 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
157 /// instruction, using the specified assembler variant. Targets should
158 /// override this to format as appropriate. This method can return true if
159 /// the operand is erroneous.
160 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
161 unsigned AsmVariant, const char *ExtraCode);
163 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
164 /// instruction, using the specified assembler variant as an address.
165 /// Targets should override this to format as appropriate. This method can
166 /// return true if the operand is erroneous.
167 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
169 const char *ExtraCode);
171 /// getSectionForFunction - Return the section that we should emit the
172 /// specified function body into. This defaults to 'TextSection'. This
173 /// should most likely be overridden by the target to put linkonce/weak
174 /// functions into special sections.
175 virtual std::string getSectionForFunction(const Function &F) const;
177 /// SetupMachineFunction - This should be called when a new MachineFunction
178 /// is being processed from runOnMachineFunction.
179 void SetupMachineFunction(MachineFunction &MF);
181 /// getFunctionNumber - Return a unique ID for the current function.
183 unsigned getFunctionNumber() const { return FunctionNumber; }
185 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
186 /// not normally call this, as the counter is automatically bumped by
187 /// SetupMachineFunction.
188 void IncrementFunctionNumber() { FunctionNumber++; }
190 /// EmitConstantPool - Print to the current output stream assembly
191 /// representations of the constants in the constant pool MCP. This is
192 /// used to print out constants which have been "spilled to memory" by
193 /// the code generator.
195 void EmitConstantPool(MachineConstantPool *MCP);
197 /// EmitJumpTableInfo - Print assembly representations of the jump tables
198 /// used by the current function to the current output stream.
200 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
202 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
203 /// special global used by LLVM. If so, emit it and return true, otherwise
204 /// do nothing and return false.
205 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
208 //===------------------------------------------------------------------===//
209 /// LEB 128 number encoding.
211 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
212 /// representing an unsigned leb128 value.
213 void PrintULEB128(unsigned Value) const;
215 /// SizeULEB128 - Compute the number of bytes required for an unsigned
217 static unsigned SizeULEB128(unsigned Value);
219 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
220 /// representing a signed leb128 value.
221 void PrintSLEB128(int Value) const;
223 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
225 static unsigned SizeSLEB128(int Value);
227 //===------------------------------------------------------------------===//
228 // Emission and print routines
231 /// PrintHex - Print a value as a hexidecimal value.
233 void PrintHex(int Value) const;
235 /// EOL - Print a newline character to asm stream. If a comment is present
236 /// then it will be printed first. Comments should not contain '\n'.
238 void EOL(const std::string &Comment) const;
240 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
241 /// unsigned leb128 value.
242 void EmitULEB128Bytes(unsigned Value) const;
244 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
245 /// signed leb128 value.
246 void EmitSLEB128Bytes(int Value) const;
248 /// EmitInt8 - Emit a byte directive and value.
250 void EmitInt8(int Value) const;
252 /// EmitInt16 - Emit a short directive and value.
254 void EmitInt16(int Value) const;
256 /// EmitInt32 - Emit a long directive and value.
258 void EmitInt32(int Value) const;
260 /// EmitInt64 - Emit a long long directive and value.
262 void EmitInt64(uint64_t Value) const;
264 /// EmitString - Emit a string with quotes and a null terminator.
265 /// Special characters are emitted properly.
266 /// @verbatim (Eg. '\t') @endverbatim
267 void EmitString(const std::string &String) const;
269 /// EmitFile - Emit a .file directive.
270 void EmitFile(unsigned Number, const std::string &Name) const;
272 //===------------------------------------------------------------------===//
274 /// EmitAlignment - Emit an alignment directive to the specified power of
275 /// two boundary. For example, if you pass in 3 here, you will get an 8
276 /// byte alignment. If a global value is specified, and if that global has
277 /// an explicit alignment requested, it will unconditionally override the
278 /// alignment request. However, if ForcedAlignBits is specified, this value
279 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
280 /// and the alignment computed with NumBits and the global. If UseFillExpr
281 /// is true, it also emits an optional second value FillValue which the
282 /// assembler uses to fill gaps to match alignment for text sections if the
283 /// has specified a non-zero fill value.
285 /// The algorithm is:
287 /// if (GV && GV->hasalignment) Align = GV->getalignment();
288 /// Align = std::max(Align, ForcedAlignBits);
290 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
291 unsigned ForcedAlignBits = 0,
292 bool UseFillExpr = true) const;
294 /// printLabel - This method prints a local label used by debug and
295 /// exception handling tables.
296 void printLabel(const MachineInstr *MI) const;
297 void printLabel(unsigned Id) const;
299 /// printDeclare - This method prints a local variable declaration used by
301 void printDeclare(const MachineInstr *MI) const;
304 /// EmitZeros - Emit a block of zeros.
306 void EmitZeros(uint64_t NumZeros) const;
308 /// EmitString - Emit a zero-byte-terminated string constant.
310 virtual void EmitString(const ConstantArray *CVA) const;
312 /// EmitConstantValueOnly - Print out the specified constant, without a
313 /// storage class. Only constants of first-class type are allowed here.
314 void EmitConstantValueOnly(const Constant *CV);
316 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
317 void EmitGlobalConstant(const Constant* CV);
319 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
321 /// printInlineAsm - This method formats and prints the specified machine
322 /// instruction that is an inline asm.
323 void printInlineAsm(const MachineInstr *MI) const;
325 /// printImplicitDef - This method prints the specified machine instruction
326 /// that is an implicit def.
327 virtual void printImplicitDef(const MachineInstr *MI) const;
329 /// printBasicBlockLabel - This method prints the label for the specified
330 /// MachineBasicBlock
331 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
332 bool printAlign = false,
333 bool printColon = false,
334 bool printComment = true) const;
336 /// printPICJumpTableSetLabel - This method prints a set label for the
337 /// specified MachineBasicBlock for a jumptable entry.
338 virtual void printPICJumpTableSetLabel(unsigned uid,
339 const MachineBasicBlock *MBB) const;
340 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
341 const MachineBasicBlock *MBB) const;
342 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
343 const MachineBasicBlock *MBB,
346 /// printDataDirective - This method prints the asm directive for the
348 void printDataDirective(const Type *type);
350 /// printSuffixedName - This prints a name with preceding
351 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
353 void printSuffixedName(std::string &Name, const char* Suffix);
356 void EmitLLVMUsedList(Constant *List);
357 void EmitXXStructorList(Constant *List);
358 void EmitConstantPool(unsigned Alignment, const char *Section,
359 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);