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 /// Name-mangler for global names.
73 /// Cache of mangled name for current function. This is recalculated at the
74 /// beginning of each call to runOnMachineFunction().
76 std::string CurrentFnName;
78 /// CurrentSection - The current section we are emitting to. This is
79 /// controlled and used by the SwitchSection method.
80 std::string CurrentSection;
83 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
86 /// SwitchToTextSection - Switch to the specified section of the executable
87 /// if we are not already in it! If GV is non-null and if the global has an
88 /// explicitly requested section, we switch to the section indicated for the
89 /// global instead of NewSection.
91 /// If the new section is an empty string, this method forgets what the
92 /// current section is, but does not emit a .section directive.
94 /// This method is used when about to emit executable code.
96 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
98 /// SwitchToDataSection - Switch to the specified section of the executable
99 /// if we are not already in it! If GV is non-null and if the global has an
100 /// explicitly requested section, we switch to the section indicated for the
101 /// global instead of NewSection.
103 /// If the new section is an empty string, this method forgets what the
104 /// current section is, but does not emit a .section directive.
106 /// This method is used when about to emit data. For most assemblers, this
107 /// is the same as the SwitchToTextSection method, but not all assemblers
110 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
112 /// getGlobalLinkName - Returns the asm/link name of of the specified
113 /// global variable. Should be overridden by each target asm printer to
114 /// generate the appropriate value.
115 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
117 /// EmitExternalGlobal - Emit the external reference to a global variable.
118 /// Should be overridden if an indirect reference should be used.
119 virtual void EmitExternalGlobal(const GlobalVariable *GV);
121 /// getCurrentFunctionEHName - Called to return (and cache) the
124 std::string getCurrentFunctionEHName(const MachineFunction *MF);
127 /// getAnalysisUsage - Record analysis usage.
129 void getAnalysisUsage(AnalysisUsage &AU) const;
131 /// doInitialization - Set up the AsmPrinter when we are working on a new
132 /// module. If your pass overrides this, it must make sure to explicitly
133 /// call this implementation.
134 bool doInitialization(Module &M);
136 /// doFinalization - Shut down the asmprinter. If you override this in your
137 /// pass, you must make sure to call it explicitly.
138 bool doFinalization(Module &M);
140 /// PrintSpecial - Print information related to the specified machine instr
141 /// that is independent of the operand, and may be independent of the instr
142 /// itself. This can be useful for portably encoding the comment character
143 /// or other bits of target-specific knowledge into the asmstrings. The
144 /// syntax used is ${:comment}. Targets can override this to add support
145 /// for their own strange codes.
146 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
148 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
149 /// instruction, using the specified assembler variant. Targets should
150 /// override this to format as appropriate. This method can return true if
151 /// the operand is erroneous.
152 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
153 unsigned AsmVariant, const char *ExtraCode);
155 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
156 /// instruction, using the specified assembler variant as an address.
157 /// Targets should override this to format as appropriate. This method can
158 /// return true if the operand is erroneous.
159 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
161 const char *ExtraCode);
163 /// getSectionForFunction - Return the section that we should emit the
164 /// specified function body into. This defaults to 'TextSection'. This
165 /// should most likely be overridden by the target to put linkonce/weak
166 /// functions into special sections.
167 virtual std::string getSectionForFunction(const Function &F) const;
169 /// SetupMachineFunction - This should be called when a new MachineFunction
170 /// is being processed from runOnMachineFunction.
171 void SetupMachineFunction(MachineFunction &MF);
173 /// getFunctionNumber - Return a unique ID for the current function.
175 unsigned getFunctionNumber() const { return FunctionNumber; }
177 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
178 /// not normally call this, as the counter is automatically bumped by
179 /// SetupMachineFunction.
180 void IncrementFunctionNumber() { FunctionNumber++; }
182 /// EmitConstantPool - Print to the current output stream assembly
183 /// representations of the constants in the constant pool MCP. This is
184 /// used to print out constants which have been "spilled to memory" by
185 /// the code generator.
187 void EmitConstantPool(MachineConstantPool *MCP);
189 /// EmitJumpTableInfo - Print assembly representations of the jump tables
190 /// used by the current function to the current output stream.
192 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
194 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
195 /// special global used by LLVM. If so, emit it and return true, otherwise
196 /// do nothing and return false.
197 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
200 //===------------------------------------------------------------------===//
201 /// LEB 128 number encoding.
203 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
204 /// representing an unsigned leb128 value.
205 void PrintULEB128(unsigned Value) const;
207 /// SizeULEB128 - Compute the number of bytes required for an unsigned
209 static unsigned SizeULEB128(unsigned Value);
211 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
212 /// representing a signed leb128 value.
213 void PrintSLEB128(int Value) const;
215 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
217 static unsigned SizeSLEB128(int Value);
219 //===------------------------------------------------------------------===//
220 // Emission and print routines
223 /// PrintHex - Print a value as a hexidecimal value.
225 void PrintHex(int Value) const;
227 /// EOL - Print a newline character to asm stream. If a comment is present
228 /// then it will be printed first. Comments should not contain '\n'.
230 void EOL(const std::string &Comment) const;
232 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
233 /// unsigned leb128 value.
234 void EmitULEB128Bytes(unsigned Value) const;
236 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
237 /// signed leb128 value.
238 void EmitSLEB128Bytes(int Value) const;
240 /// EmitInt8 - Emit a byte directive and value.
242 void EmitInt8(int Value) const;
244 /// EmitInt16 - Emit a short directive and value.
246 void EmitInt16(int Value) const;
248 /// EmitInt32 - Emit a long directive and value.
250 void EmitInt32(int Value) const;
252 /// EmitInt64 - Emit a long long directive and value.
254 void EmitInt64(uint64_t Value) const;
256 /// EmitString - Emit a string with quotes and a null terminator.
257 /// Special characters are emitted properly.
258 /// @verbatim (Eg. '\t') @endverbatim
259 void EmitString(const std::string &String) const;
261 /// EmitFile - Emit a .file directive.
262 void EmitFile(unsigned Number, const std::string &Name) const;
264 //===------------------------------------------------------------------===//
266 /// EmitAlignment - Emit an alignment directive to the specified power of
267 /// two boundary. For example, if you pass in 3 here, you will get an 8
268 /// byte alignment. If a global value is specified, and if that global has
269 /// an explicit alignment requested, it will unconditionally override the
270 /// alignment request. However, if ForcedAlignBits is specified, this value
271 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
272 /// and the alignment computed with NumBits and the global. If UseFillExpr
273 /// is true, it also emits an optional second value FillValue which the
274 /// assembler uses to fill gaps to match alignment.
276 /// The algorithm is:
278 /// if (GV && GV->hasalignment) Align = GV->getalignment();
279 /// Align = std::max(Align, ForcedAlignBits);
281 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
282 unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
283 unsigned FillValue = 0) const;
285 /// printLabel - This method prints a local label used by debug and
286 /// exception handling tables.
287 void printLabel(const MachineInstr *MI) const;
288 void printLabel(unsigned Id) const;
290 /// printDeclare - This method prints a local variable declaration used by
292 void printDeclare(const MachineInstr *MI) const;
295 /// EmitZeros - Emit a block of zeros.
297 void EmitZeros(uint64_t NumZeros) const;
299 /// EmitString - Emit a zero-byte-terminated string constant.
301 virtual void EmitString(const ConstantArray *CVA) const;
303 /// EmitConstantValueOnly - Print out the specified constant, without a
304 /// storage class. Only constants of first-class type are allowed here.
305 void EmitConstantValueOnly(const Constant *CV);
307 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
308 /// If Packed is false, pad to the ABI size.
309 void EmitGlobalConstant(const Constant* CV, bool Packed = false);
311 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
313 /// printInlineAsm - This method formats and prints the specified machine
314 /// instruction that is an inline asm.
315 void printInlineAsm(const MachineInstr *MI) const;
317 /// printBasicBlockLabel - This method prints the label for the specified
318 /// MachineBasicBlock
319 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
320 bool printColon = false,
321 bool printComment = true) const;
323 /// printPICJumpTableSetLabel - This method prints a set label for the
324 /// specified MachineBasicBlock for a jumptable entry.
325 virtual void printPICJumpTableSetLabel(unsigned uid,
326 const MachineBasicBlock *MBB) const;
327 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
328 const MachineBasicBlock *MBB) const;
329 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
330 const MachineBasicBlock *MBB,
333 /// printDataDirective - This method prints the asm directive for the
335 void printDataDirective(const Type *type);
338 void EmitLLVMUsedList(Constant *List);
339 void EmitXXStructorList(Constant *List);
340 void EmitConstantPool(unsigned Alignment, const char *Section,
341 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);