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/DebugLoc.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/DenseMap.h"
33 class GCMetadataPrinter;
36 class MachineBasicBlock;
37 class MachineFunction;
39 class MachineLoopInfo;
41 class MachineConstantPool;
42 class MachineConstantPoolEntry;
43 class MachineConstantPoolValue;
44 class MachineJumpTableInfo;
45 class MachineModuleInfo;
54 class TargetLoweringObjectFile;
56 class formatted_raw_ostream;
58 /// AsmPrinter - This class is intended to be used as a driving class for all
60 class AsmPrinter : public MachineFunctionPass {
63 /// FunctionNumber - This provides a unique ID for each function emitted in
64 /// this translation unit. It is autoincremented by SetupMachineFunction,
65 /// and can be accessed with getFunctionNumber() and
66 /// IncrementFunctionNumber().
68 unsigned FunctionNumber;
70 // GCMetadataPrinters - The garbage collection metadata printer table.
71 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
72 typedef gcp_map_type::iterator gcp_iterator;
73 gcp_map_type GCMetadataPrinters;
75 /// If VerboseAsm is set, a pointer to the loop info for this
81 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
82 MachineModuleInfo *MMI;
85 /// DW - If available, this is a pointer to the current dwarf writer.
89 /// Flags to specify different kinds of comments to output in
90 /// assembly code. These flags carry semantic information not
91 /// otherwise easily derivable from the IR text.
97 /// Output stream on which we're printing assembly code.
99 formatted_raw_ostream &O;
101 /// Target machine description.
105 /// getObjFileLowering - Return information about object file lowering.
106 TargetLoweringObjectFile &getObjFileLowering() const;
108 /// Target Asm Printer information.
110 const MCAsmInfo *MAI;
112 /// Target Register Information.
114 const TargetRegisterInfo *TRI;
116 /// OutContext - This is the context for the output file that we are
117 /// streaming. This owns all of the global MC-related objects for the
118 /// generated translation unit.
119 MCContext &OutContext;
121 /// OutStreamer - This is the MCStreamer object for the file we are
122 /// generating. This contains the transient state for the current
123 /// translation unit that we are generating (such as the current section
125 MCStreamer &OutStreamer;
127 /// The current machine function.
128 const MachineFunction *MF;
130 /// Name-mangler for global names.
134 /// Cache of mangled name for current function. This is recalculated at the
135 /// beginning of each call to runOnMachineFunction().
137 std::string CurrentFnName;
139 /// getCurrentSection() - Return the current section we are emitting to.
140 const MCSection *getCurrentSection() const;
143 /// VerboseAsm - Emit comments in assembly output if this is true.
147 /// Private state for PrintSpecial()
148 // Assign a unique ID to this machine instruction.
149 mutable const MachineInstr *LastMI;
150 mutable const Function *LastFn;
151 mutable unsigned Counter;
153 // Private state for processDebugLoc()
154 mutable DebugLocTuple PrevDLT;
157 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
158 const MCAsmInfo *T, bool V);
161 virtual ~AsmPrinter();
163 /// isVerbose - Return true if assembly output should contain comments.
165 bool isVerbose() const { return VerboseAsm; }
167 /// getFunctionNumber - Return a unique ID for the current function.
169 unsigned getFunctionNumber() const { return FunctionNumber; }
172 /// getAnalysisUsage - Record analysis usage.
174 void getAnalysisUsage(AnalysisUsage &AU) const;
176 /// doInitialization - Set up the AsmPrinter when we are working on a new
177 /// module. If your pass overrides this, it must make sure to explicitly
178 /// call this implementation.
179 bool doInitialization(Module &M);
181 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
182 /// that want to emit something at the start of their file.
183 virtual void EmitStartOfAsmFile(Module &) {}
185 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
186 /// want to emit something at the end of their file.
187 virtual void EmitEndOfAsmFile(Module &) {}
189 /// doFinalization - Shut down the asmprinter. If you override this in your
190 /// pass, you must make sure to call it explicitly.
191 bool doFinalization(Module &M);
193 /// PrintSpecial - Print information related to the specified machine instr
194 /// that is independent of the operand, and may be independent of the instr
195 /// itself. This can be useful for portably encoding the comment character
196 /// or other bits of target-specific knowledge into the asmstrings. The
197 /// syntax used is ${:comment}. Targets can override this to add support
198 /// for their own strange codes.
199 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
201 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
202 /// instruction, using the specified assembler variant. Targets should
203 /// override this to format as appropriate. This method can return true if
204 /// the operand is erroneous.
205 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
206 unsigned AsmVariant, const char *ExtraCode);
208 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
209 /// instruction, using the specified assembler variant as an address.
210 /// Targets should override this to format as appropriate. This method can
211 /// return true if the operand is erroneous.
212 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
214 const char *ExtraCode);
216 /// PrintGlobalVariable - Emit the specified global variable and its
217 /// initializer to the output stream.
218 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
220 /// SetupMachineFunction - This should be called when a new MachineFunction
221 /// is being processed from runOnMachineFunction.
222 void SetupMachineFunction(MachineFunction &MF);
224 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
225 /// not normally call this, as the counter is automatically bumped by
226 /// SetupMachineFunction.
227 void IncrementFunctionNumber() { FunctionNumber++; }
229 /// EmitConstantPool - Print to the current output stream assembly
230 /// representations of the constants in the constant pool MCP. This is
231 /// used to print out constants which have been "spilled to memory" by
232 /// the code generator.
234 void EmitConstantPool(MachineConstantPool *MCP);
236 /// EmitJumpTableInfo - Print assembly representations of the jump tables
237 /// used by the current function to the current output stream.
239 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
241 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
242 /// special global used by LLVM. If so, emit it and return true, otherwise
243 /// do nothing and return false.
244 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
247 //===------------------------------------------------------------------===//
248 /// LEB 128 number encoding.
250 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
251 /// representing an unsigned leb128 value.
252 void PrintULEB128(unsigned Value) const;
254 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
255 /// representing a signed leb128 value.
256 void PrintSLEB128(int Value) const;
258 //===------------------------------------------------------------------===//
259 // Emission and print routines
262 /// PrintHex - Print a value as a hexidecimal value.
264 void PrintHex(int Value) const;
266 /// EOL - Print a newline character to asm stream. If a comment is present
267 /// then it will be printed first. Comments should not contain '\n'.
269 void EOL(const std::string &Comment) const;
270 void EOL(const char* Comment) const;
271 void EOL(const char *Comment, unsigned Encoding) const;
273 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
274 /// unsigned leb128 value.
275 void EmitULEB128Bytes(unsigned Value) const;
277 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
278 /// signed leb128 value.
279 void EmitSLEB128Bytes(int Value) const;
281 /// EmitInt8 - Emit a byte directive and value.
283 void EmitInt8(int Value) const;
285 /// EmitInt16 - Emit a short directive and value.
287 void EmitInt16(int Value) const;
289 /// EmitInt32 - Emit a long directive and value.
291 void EmitInt32(int Value) const;
293 /// EmitInt64 - Emit a long long directive and value.
295 void EmitInt64(uint64_t Value) const;
297 /// EmitString - Emit a string with quotes and a null terminator.
298 /// Special characters are emitted properly.
299 /// @verbatim (Eg. '\t') @endverbatim
300 void EmitString(const StringRef String) const;
301 void EmitString(const char *String, unsigned Size) const;
303 /// EmitFile - Emit a .file directive.
304 void EmitFile(unsigned Number, const std::string &Name) const;
306 //===------------------------------------------------------------------===//
308 /// EmitAlignment - Emit an alignment directive to the specified power of
309 /// two boundary. For example, if you pass in 3 here, you will get an 8
310 /// byte alignment. If a global value is specified, and if that global has
311 /// an explicit alignment requested, it will unconditionally override the
312 /// alignment request. However, if ForcedAlignBits is specified, this value
313 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
314 /// and the alignment computed with NumBits and the global. If UseFillExpr
315 /// is true, it also emits an optional second value FillValue which the
316 /// assembler uses to fill gaps to match alignment for text sections if the
317 /// has specified a non-zero fill value.
319 /// The algorithm is:
321 /// if (GV && GV->hasalignment) Align = GV->getalignment();
322 /// Align = std::max(Align, ForcedAlignBits);
324 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
325 unsigned ForcedAlignBits = 0,
326 bool UseFillExpr = true) const;
328 /// printLabel - This method prints a local label used by debug and
329 /// exception handling tables.
330 void printLabel(const MachineInstr *MI) const;
331 void printLabel(unsigned Id) const;
333 /// printDeclare - This method prints a local variable declaration used by
335 void printDeclare(const MachineInstr *MI) const;
337 /// EmitComments - Pretty-print comments for instructions
338 void EmitComments(const MachineInstr &MI) const;
339 /// EmitComments - Pretty-print comments for basic blocks
340 void EmitComments(const MachineBasicBlock &MBB) const;
342 /// GetGlobalValueSymbol - Return the MCSymbol for the specified global
344 MCSymbol *GetGlobalValueSymbol(const GlobalValue *GV) const;
346 /// GetPrivateGlobalValueSymbolStub - Return the MCSymbol for a symbol with
347 /// global value name as its base, with the specified suffix, and where the
348 /// symbol is forced to have private linkage.
349 MCSymbol *GetPrivateGlobalValueSymbolStub(const GlobalValue *GV,
350 StringRef Suffix) const;
352 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
354 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
356 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
358 MCSymbol *GetMBBSymbol(unsigned MBBID) const;
360 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
361 /// uses of the specified basic block.
362 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA,
363 const char *Suffix = "") const;
364 MCSymbol *GetBlockAddressSymbol(const Function *F,
365 const BasicBlock *BB,
366 const char *Suffix = "") const;
368 /// EmitBasicBlockStart - This method prints the label for the specified
369 /// MachineBasicBlock, an alignment (if present) and a comment describing
370 /// it if appropriate.
371 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
373 /// EmitZeros - Emit a block of zeros.
375 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
377 /// EmitString - Emit a zero-byte-terminated string constant.
379 virtual void EmitString(const ConstantArray *CVA) const;
381 /// EmitConstantValueOnly - Print out the specified constant, without a
382 /// storage class. Only constants of first-class type are allowed here.
383 void EmitConstantValueOnly(const Constant *CV);
385 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
386 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
388 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
390 /// processDebugLoc - Processes the debug information of each machine
391 /// instruction's DebugLoc.
392 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
394 /// printInlineAsm - This method formats and prints the specified machine
395 /// instruction that is an inline asm.
396 void printInlineAsm(const MachineInstr *MI) const;
398 /// printImplicitDef - This method prints the specified machine instruction
399 /// that is an implicit def.
400 void printImplicitDef(const MachineInstr *MI) const;
402 /// printKill - This method prints the specified kill machine instruction.
403 void printKill(const MachineInstr *MI) const;
405 /// printPICJumpTableSetLabel - This method prints a set label for the
406 /// specified MachineBasicBlock for a jumptable entry.
407 virtual void printPICJumpTableSetLabel(unsigned uid,
408 const MachineBasicBlock *MBB) const;
409 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
410 const MachineBasicBlock *MBB) const;
411 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
412 const MachineBasicBlock *MBB,
415 /// printDataDirective - This method prints the asm directive for the
417 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
419 /// printVisibility - This prints visibility information about symbol, if
420 /// this is suported by the target.
421 void printVisibility(const MCSymbol *Sym, unsigned Visibility) const;
423 // FIXME: This is deprecated and should be removed.
424 void printVisibility(const std::string& Name, unsigned Visibility) const;
426 /// printOffset - This is just convenient handler for printing offsets.
427 void printOffset(int64_t Offset) const;
430 void EmitLLVMUsedList(Constant *List);
431 void EmitXXStructorList(Constant *List);
432 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
434 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
435 void EmitGlobalConstantVector(const ConstantVector* CP);
436 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
437 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
438 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);