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"
21 #include "llvm/ADT/DenseMap.h"
28 class GCMetadataPrinter;
31 class MachineConstantPoolEntry;
32 class MachineConstantPoolValue;
33 class MachineModuleInfo;
38 /// AsmPrinter - This class is intended to be used as a driving class for all
40 class AsmPrinter : public MachineFunctionPass {
43 /// FunctionNumber - This provides a unique ID for each function emitted in
44 /// this translation unit. It is autoincremented by SetupMachineFunction,
45 /// and can be accessed with getFunctionNumber() and
46 /// IncrementFunctionNumber().
48 unsigned FunctionNumber;
50 /// MachineModuleInfo - This is needed because printDeclare() has to insert
51 /// DebugVariable entries into the dwarf table. This is a short term hack
52 /// that ought be fixed soon.
53 MachineModuleInfo *MMI;
55 // GCMetadataPrinters - The garbage collection metadata printer table.
56 typedef DenseMap<Collector*,GCMetadataPrinter*> gcp_map_type;
57 typedef gcp_map_type::iterator gcp_iterator;
58 gcp_map_type GCMetadataPrinters;
61 // Necessary for external weak linkage support
62 std::set<const GlobalValue*> ExtWeakSymbols;
65 /// Output stream on which we're printing assembly code.
69 /// Target machine description.
73 /// Target Asm Printer information.
75 const TargetAsmInfo *TAI;
77 /// Target Register Information.
79 const TargetRegisterInfo *TRI;
81 /// Name-mangler for global names.
85 /// Cache of mangled name for current function. This is recalculated at the
86 /// beginning of each call to runOnMachineFunction().
88 std::string CurrentFnName;
90 /// CurrentSection - The current section we are emitting to. This is
91 /// controlled and used by the SwitchSection method.
92 std::string CurrentSection;
94 /// IsInTextSection - True if the current section we are emitting to is a
99 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
102 virtual ~AsmPrinter();
104 /// SwitchToTextSection - Switch to the specified section of the executable
105 /// if we are not already in it! If GV is non-null and if the global has an
106 /// explicitly requested section, we switch to the section indicated for the
107 /// global instead of NewSection.
109 /// If the new section is an empty string, this method forgets what the
110 /// current section is, but does not emit a .section directive.
112 /// This method is used when about to emit executable code.
114 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
116 /// SwitchToDataSection - Switch to the specified section of the executable
117 /// if we are not already in it! If GV is non-null and if the global has an
118 /// explicitly requested section, we switch to the section indicated for the
119 /// global instead of NewSection.
121 /// If the new section is an empty string, this method forgets what the
122 /// current section is, but does not emit a .section directive.
124 /// This method is used when about to emit data. For most assemblers, this
125 /// is the same as the SwitchToTextSection method, but not all assemblers
128 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
130 /// getGlobalLinkName - Returns the asm/link name of of the specified
131 /// global variable. Should be overridden by each target asm printer to
132 /// generate the appropriate value.
133 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
135 /// EmitExternalGlobal - Emit the external reference to a global variable.
136 /// Should be overridden if an indirect reference should be used.
137 virtual void EmitExternalGlobal(const GlobalVariable *GV);
139 /// getCurrentFunctionEHName - Called to return (and cache) the
142 std::string getCurrentFunctionEHName(const MachineFunction *MF);
145 /// getAnalysisUsage - Record analysis usage.
147 void getAnalysisUsage(AnalysisUsage &AU) const;
149 /// doInitialization - Set up the AsmPrinter when we are working on a new
150 /// module. If your pass overrides this, it must make sure to explicitly
151 /// call this implementation.
152 bool doInitialization(Module &M);
154 /// doFinalization - Shut down the asmprinter. If you override this in your
155 /// pass, you must make sure to call it explicitly.
156 bool doFinalization(Module &M);
158 /// PrintSpecial - Print information related to the specified machine instr
159 /// that is independent of the operand, and may be independent of the instr
160 /// itself. This can be useful for portably encoding the comment character
161 /// or other bits of target-specific knowledge into the asmstrings. The
162 /// syntax used is ${:comment}. Targets can override this to add support
163 /// for their own strange codes.
164 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
166 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
167 /// instruction, using the specified assembler variant. Targets should
168 /// override this to format as appropriate. This method can return true if
169 /// the operand is erroneous.
170 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
171 unsigned AsmVariant, const char *ExtraCode);
173 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
174 /// instruction, using the specified assembler variant as an address.
175 /// Targets should override this to format as appropriate. This method can
176 /// return true if the operand is erroneous.
177 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
179 const char *ExtraCode);
181 /// getSectionForFunction - Return the section that we should emit the
182 /// specified function body into. This defaults to 'TextSection'. This
183 /// should most likely be overridden by the target to put linkonce/weak
184 /// functions into special sections.
185 virtual std::string getSectionForFunction(const Function &F) const;
187 /// SetupMachineFunction - This should be called when a new MachineFunction
188 /// is being processed from runOnMachineFunction.
189 void SetupMachineFunction(MachineFunction &MF);
191 /// getFunctionNumber - Return a unique ID for the current function.
193 unsigned getFunctionNumber() const { return FunctionNumber; }
195 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
196 /// not normally call this, as the counter is automatically bumped by
197 /// SetupMachineFunction.
198 void IncrementFunctionNumber() { FunctionNumber++; }
200 /// EmitConstantPool - Print to the current output stream assembly
201 /// representations of the constants in the constant pool MCP. This is
202 /// used to print out constants which have been "spilled to memory" by
203 /// the code generator.
205 void EmitConstantPool(MachineConstantPool *MCP);
207 /// EmitJumpTableInfo - Print assembly representations of the jump tables
208 /// used by the current function to the current output stream.
210 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
212 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
213 /// special global used by LLVM. If so, emit it and return true, otherwise
214 /// do nothing and return false.
215 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
218 //===------------------------------------------------------------------===//
219 /// LEB 128 number encoding.
221 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
222 /// representing an unsigned leb128 value.
223 void PrintULEB128(unsigned Value) const;
225 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
226 /// representing a signed leb128 value.
227 void PrintSLEB128(int Value) const;
229 //===------------------------------------------------------------------===//
230 // Emission and print routines
233 /// PrintHex - Print a value as a hexidecimal value.
235 void PrintHex(int Value) const;
237 /// EOL - Print a newline character to asm stream. If a comment is present
238 /// then it will be printed first. Comments should not contain '\n'.
240 void EOL(const std::string &Comment) const;
241 void EOL(const char* Comment) const;
243 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
244 /// unsigned leb128 value.
245 void EmitULEB128Bytes(unsigned Value) const;
247 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
248 /// signed leb128 value.
249 void EmitSLEB128Bytes(int Value) const;
251 /// EmitInt8 - Emit a byte directive and value.
253 void EmitInt8(int Value) const;
255 /// EmitInt16 - Emit a short directive and value.
257 void EmitInt16(int Value) const;
259 /// EmitInt32 - Emit a long directive and value.
261 void EmitInt32(int Value) const;
263 /// EmitInt64 - Emit a long long directive and value.
265 void EmitInt64(uint64_t Value) const;
267 /// EmitString - Emit a string with quotes and a null terminator.
268 /// Special characters are emitted properly.
269 /// @verbatim (Eg. '\t') @endverbatim
270 void EmitString(const std::string &String) const;
272 /// EmitFile - Emit a .file directive.
273 void EmitFile(unsigned Number, const std::string &Name) const;
275 //===------------------------------------------------------------------===//
277 /// EmitAlignment - Emit an alignment directive to the specified power of
278 /// two boundary. For example, if you pass in 3 here, you will get an 8
279 /// byte alignment. If a global value is specified, and if that global has
280 /// an explicit alignment requested, it will unconditionally override the
281 /// alignment request. However, if ForcedAlignBits is specified, this value
282 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
283 /// and the alignment computed with NumBits and the global. If UseFillExpr
284 /// is true, it also emits an optional second value FillValue which the
285 /// assembler uses to fill gaps to match alignment for text sections if the
286 /// has specified a non-zero fill value.
288 /// The algorithm is:
290 /// if (GV && GV->hasalignment) Align = GV->getalignment();
291 /// Align = std::max(Align, ForcedAlignBits);
293 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
294 unsigned ForcedAlignBits = 0,
295 bool UseFillExpr = true) const;
297 /// printLabel - This method prints a local label used by debug and
298 /// exception handling tables.
299 void printLabel(const MachineInstr *MI) const;
300 void printLabel(unsigned Id) const;
302 /// printDeclare - This method prints a local variable declaration used by
304 void printDeclare(const MachineInstr *MI) const;
307 /// EmitZeros - Emit a block of zeros.
309 void EmitZeros(uint64_t NumZeros) const;
311 /// EmitString - Emit a zero-byte-terminated string constant.
313 virtual void EmitString(const ConstantArray *CVA) const;
315 /// EmitConstantValueOnly - Print out the specified constant, without a
316 /// storage class. Only constants of first-class type are allowed here.
317 void EmitConstantValueOnly(const Constant *CV);
319 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
320 void EmitGlobalConstant(const Constant* CV);
322 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
324 /// printInlineAsm - This method formats and prints the specified machine
325 /// instruction that is an inline asm.
326 void printInlineAsm(const MachineInstr *MI) const;
328 /// printImplicitDef - This method prints the specified machine instruction
329 /// that is an implicit def.
330 virtual void printImplicitDef(const MachineInstr *MI) const;
332 /// printBasicBlockLabel - This method prints the label for the specified
333 /// MachineBasicBlock
334 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
335 bool printAlign = false,
336 bool printColon = false,
337 bool printComment = true) const;
339 /// printPICJumpTableSetLabel - This method prints a set label for the
340 /// specified MachineBasicBlock for a jumptable entry.
341 virtual void printPICJumpTableSetLabel(unsigned uid,
342 const MachineBasicBlock *MBB) const;
343 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
344 const MachineBasicBlock *MBB) const;
345 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
346 const MachineBasicBlock *MBB,
349 /// printDataDirective - This method prints the asm directive for the
351 void printDataDirective(const Type *type);
353 /// printSuffixedName - This prints a name with preceding
354 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
356 void printSuffixedName(const char *Name, const char *Suffix,
357 const char *Prefix = 0);
358 void printSuffixedName(const std::string &Name, const char* Suffix);
360 /// printVisibility - This prints visibility information about symbol, if
361 /// this is suported by the target.
362 void printVisibility(const std::string& Name, unsigned Visibility) const;
365 void EmitLLVMUsedList(Constant *List);
366 void EmitXXStructorList(Constant *List);
367 void EmitConstantPool(unsigned Alignment, const char *Section,
368 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
369 GCMetadataPrinter *GetOrCreateGCPrinter(Collector *C);