AsmPrinter.h revision 74bfe21b50c14c15f55ce3bd5857d65b588fae3c
1//===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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 12// all asm writers. 13// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_CODEGEN_ASMPRINTER_H 17#define LLVM_CODEGEN_ASMPRINTER_H 18 19#include "llvm/CodeGen/MachineFunctionPass.h" 20#include "llvm/Analysis/DebugInfo.h" 21#include "llvm/Support/DebugLoc.h" 22#include "llvm/Target/TargetMachine.h" 23#include "llvm/ADT/DenseMap.h" 24 25namespace llvm { 26 class BlockAddress; 27 class GCStrategy; 28 class Constant; 29 class ConstantArray; 30 class ConstantFP; 31 class ConstantInt; 32 class ConstantStruct; 33 class ConstantVector; 34 class GCMetadataPrinter; 35 class GlobalValue; 36 class GlobalVariable; 37 class MachineBasicBlock; 38 class MachineFunction; 39 class MachineInstr; 40 class MachineLoopInfo; 41 class MachineLoop; 42 class MachineConstantPool; 43 class MachineConstantPoolEntry; 44 class MachineConstantPoolValue; 45 class MachineJumpTableInfo; 46 class MachineModuleInfo; 47 class MCInst; 48 class MCContext; 49 class MCSection; 50 class MCStreamer; 51 class MCSymbol; 52 class DwarfWriter; 53 class Mangler; 54 class MCAsmInfo; 55 class TargetLoweringObjectFile; 56 class Twine; 57 class Type; 58 class formatted_raw_ostream; 59 60 /// AsmPrinter - This class is intended to be used as a driving class for all 61 /// asm writers. 62 class AsmPrinter : public MachineFunctionPass { 63 static char ID; 64 65 /// FunctionNumber - This provides a unique ID for each function emitted in 66 /// this translation unit. It is autoincremented by SetupMachineFunction, 67 /// and can be accessed with getFunctionNumber() and 68 /// IncrementFunctionNumber(). 69 /// 70 unsigned FunctionNumber; 71 72 // GCMetadataPrinters - The garbage collection metadata printer table. 73 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type; 74 typedef gcp_map_type::iterator gcp_iterator; 75 gcp_map_type GCMetadataPrinters; 76 77 /// If VerboseAsm is set, a pointer to the loop info for this 78 /// function. 79 /// 80 MachineLoopInfo *LI; 81 82 public: 83 /// MMI - If available, this is a pointer to the current MachineModuleInfo. 84 MachineModuleInfo *MMI; 85 86 protected: 87 /// DW - If available, this is a pointer to the current dwarf writer. 88 DwarfWriter *DW; 89 90 public: 91 /// Flags to specify different kinds of comments to output in 92 /// assembly code. These flags carry semantic information not 93 /// otherwise easily derivable from the IR text. 94 /// 95 enum CommentFlag { 96 ReloadReuse = 0x1 97 }; 98 99 /// Output stream on which we're printing assembly code. 100 /// 101 formatted_raw_ostream &O; 102 103 /// Target machine description. 104 /// 105 TargetMachine &TM; 106 107 /// getObjFileLowering - Return information about object file lowering. 108 TargetLoweringObjectFile &getObjFileLowering() const; 109 110 /// Target Asm Printer information. 111 /// 112 const MCAsmInfo *MAI; 113 114 /// Target Register Information. 115 /// 116 const TargetRegisterInfo *TRI; 117 118 /// OutContext - This is the context for the output file that we are 119 /// streaming. This owns all of the global MC-related objects for the 120 /// generated translation unit. 121 MCContext &OutContext; 122 123 /// OutStreamer - This is the MCStreamer object for the file we are 124 /// generating. This contains the transient state for the current 125 /// translation unit that we are generating (such as the current section 126 /// etc). 127 MCStreamer &OutStreamer; 128 129 /// The current machine function. 130 const MachineFunction *MF; 131 132 /// Name-mangler for global names. 133 /// 134 Mangler *Mang; 135 136 /// The symbol for the current function. This is recalculated at the 137 /// beginning of each call to runOnMachineFunction(). 138 /// 139 MCSymbol *CurrentFnSym; 140 141 /// getCurrentSection() - Return the current section we are emitting to. 142 const MCSection *getCurrentSection() const; 143 144 145 /// VerboseAsm - Emit comments in assembly output if this is true. 146 /// 147 bool VerboseAsm; 148 149 /// Private state for PrintSpecial() 150 // Assign a unique ID to this machine instruction. 151 mutable const MachineInstr *LastMI; 152 mutable const Function *LastFn; 153 mutable unsigned Counter; 154 155 // Private state for processDebugLoc() 156 mutable DILocation PrevDLT; 157 158 protected: 159 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM, 160 const MCAsmInfo *T, bool V); 161 162 public: 163 virtual ~AsmPrinter(); 164 165 /// isVerbose - Return true if assembly output should contain comments. 166 /// 167 bool isVerbose() const { return VerboseAsm; } 168 169 /// getFunctionNumber - Return a unique ID for the current function. 170 /// 171 unsigned getFunctionNumber() const { return FunctionNumber; } 172 173 protected: 174 /// getAnalysisUsage - Record analysis usage. 175 /// 176 void getAnalysisUsage(AnalysisUsage &AU) const; 177 178 /// doInitialization - Set up the AsmPrinter when we are working on a new 179 /// module. If your pass overrides this, it must make sure to explicitly 180 /// call this implementation. 181 bool doInitialization(Module &M); 182 183 /// EmitStartOfAsmFile - This virtual method can be overridden by targets 184 /// that want to emit something at the start of their file. 185 virtual void EmitStartOfAsmFile(Module &) {} 186 187 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that 188 /// want to emit something at the end of their file. 189 virtual void EmitEndOfAsmFile(Module &) {} 190 191 /// doFinalization - Shut down the asmprinter. If you override this in your 192 /// pass, you must make sure to call it explicitly. 193 bool doFinalization(Module &M); 194 195 /// PrintSpecial - Print information related to the specified machine instr 196 /// that is independent of the operand, and may be independent of the instr 197 /// itself. This can be useful for portably encoding the comment character 198 /// or other bits of target-specific knowledge into the asmstrings. The 199 /// syntax used is ${:comment}. Targets can override this to add support 200 /// for their own strange codes. 201 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const; 202 203 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 204 /// instruction, using the specified assembler variant. Targets should 205 /// override this to format as appropriate. This method can return true if 206 /// the operand is erroneous. 207 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 208 unsigned AsmVariant, const char *ExtraCode); 209 210 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 211 /// instruction, using the specified assembler variant as an address. 212 /// Targets should override this to format as appropriate. This method can 213 /// return true if the operand is erroneous. 214 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 215 unsigned AsmVariant, 216 const char *ExtraCode); 217 218 /// SetupMachineFunction - This should be called when a new MachineFunction 219 /// is being processed from runOnMachineFunction. 220 void SetupMachineFunction(MachineFunction &MF); 221 222 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should 223 /// not normally call this, as the counter is automatically bumped by 224 /// SetupMachineFunction. 225 void IncrementFunctionNumber() { FunctionNumber++; } 226 227 /// EmitConstantPool - Print to the current output stream assembly 228 /// representations of the constants in the constant pool MCP. This is 229 /// used to print out constants which have been "spilled to memory" by 230 /// the code generator. 231 /// 232 void EmitConstantPool(MachineConstantPool *MCP); 233 234 /// EmitJumpTableInfo - Print assembly representations of the jump tables 235 /// used by the current function to the current output stream. 236 /// 237 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF); 238 239 /// EmitGlobalVariable - Emit the specified global variable to the .s file. 240 virtual void EmitGlobalVariable(const GlobalVariable *GV); 241 242 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 243 /// special global used by LLVM. If so, emit it and return true, otherwise 244 /// do nothing and return false. 245 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 246 247 public: 248 //===------------------------------------------------------------------===// 249 /// LEB 128 number encoding. 250 251 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas) 252 /// representing an unsigned leb128 value. 253 void PrintULEB128(unsigned Value) const; 254 255 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas) 256 /// representing a signed leb128 value. 257 void PrintSLEB128(int Value) const; 258 259 //===------------------------------------------------------------------===// 260 // Emission and print routines 261 // 262 263 /// PrintHex - Print a value as a hexidecimal value. 264 /// 265 void PrintHex(uint64_t Value) const; 266 267 /// EOL - Print a newline character to asm stream. If a comment is present 268 /// then it will be printed first. Comments should not contain '\n'. 269 void EOL() const; 270 void EOL(const Twine &Comment) const; 271 void EOL(const Twine &Comment, unsigned Encoding) const; 272 273 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 274 /// unsigned leb128 value. 275 void EmitULEB128Bytes(unsigned Value) const; 276 277 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 278 /// signed leb128 value. 279 void EmitSLEB128Bytes(int Value) const; 280 281 /// EmitInt8 - Emit a byte directive and value. 282 /// 283 void EmitInt8(int Value) const; 284 285 /// EmitInt16 - Emit a short directive and value. 286 /// 287 void EmitInt16(int Value) const; 288 289 /// EmitInt32 - Emit a long directive and value. 290 /// 291 void EmitInt32(int Value) const; 292 293 /// EmitInt64 - Emit a long long directive and value. 294 /// 295 void EmitInt64(uint64_t Value) const; 296 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; 302 303 /// EmitFile - Emit a .file directive. 304 void EmitFile(unsigned Number, StringRef Name) const; 305 306 //===------------------------------------------------------------------===// 307 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. 318 /// 319 /// The algorithm is: 320 /// Align = NumBits; 321 /// if (GV && GV->hasalignment) Align = GV->getalignment(); 322 /// Align = std::max(Align, ForcedAlignBits); 323 /// 324 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0, 325 unsigned ForcedAlignBits = 0, 326 bool UseFillExpr = true) const; 327 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; 332 333 /// printDeclare - This method prints a local variable declaration used by 334 /// debug tables. 335 void printDeclare(const MachineInstr *MI) const; 336 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; 341 342 /// GetGlobalValueSymbol - Return the MCSymbol for the specified global 343 /// value. 344 MCSymbol *GetGlobalValueSymbol(const GlobalValue *GV) const; 345 346 /// GetSymbolWithGlobalValueBase - 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 if ForcePrivate is true. 349 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV, 350 StringRef Suffix, 351 bool ForcePrivate = true) const; 352 353 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified 354 /// ExternalSymbol. 355 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 356 357 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic 358 /// block label. 359 MCSymbol *GetMBBSymbol(unsigned MBBID) const; 360 361 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress 362 /// uses of the specified basic block. 363 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA, 364 const char *Suffix = "") const; 365 MCSymbol *GetBlockAddressSymbol(const Function *F, 366 const BasicBlock *BB, 367 const char *Suffix = "") const; 368 369 /// EmitBasicBlockStart - This method prints the label for the specified 370 /// MachineBasicBlock, an alignment (if present) and a comment describing 371 /// it if appropriate. 372 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const; 373 protected: 374 /// EmitZeros - Emit a block of zeros. 375 /// 376 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const; 377 378 /// EmitString - Emit a zero-byte-terminated string constant. 379 /// 380 virtual void EmitString(const ConstantArray *CVA) const; 381 382 /// EmitConstantValueOnly - Print out the specified constant, without a 383 /// storage class. Only constants of first-class type are allowed here. 384 void EmitConstantValueOnly(const Constant *CV); 385 386 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 387 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0); 388 389 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 390 391 /// processDebugLoc - Processes the debug information of each machine 392 /// instruction's DebugLoc. 393 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn); 394 395 /// printInlineAsm - This method formats and prints the specified machine 396 /// instruction that is an inline asm. 397 void printInlineAsm(const MachineInstr *MI) const; 398 399 /// printImplicitDef - This method prints the specified machine instruction 400 /// that is an implicit def. 401 void printImplicitDef(const MachineInstr *MI) const; 402 403 /// printKill - This method prints the specified kill machine instruction. 404 void printKill(const MachineInstr *MI) const; 405 406 /// printPICJumpTableSetLabel - This method prints a set label for the 407 /// specified MachineBasicBlock for a jumptable entry. 408 virtual void printPICJumpTableSetLabel(unsigned uid, 409 const MachineBasicBlock *MBB) const; 410 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 411 const MachineBasicBlock *MBB) const; 412 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 413 const MachineBasicBlock *MBB, 414 unsigned uid) const; 415 416 /// printDataDirective - This method prints the asm directive for the 417 /// specified type. 418 void printDataDirective(const Type *type, unsigned AddrSpace = 0); 419 420 /// printVisibility - This prints visibility information about symbol, if 421 /// this is suported by the target. 422 void printVisibility(const MCSymbol *Sym, unsigned Visibility) const; 423 424 /// printOffset - This is just convenient handler for printing offsets. 425 void printOffset(int64_t Offset) const; 426 427 private: 428 void EmitLLVMUsedList(Constant *List); 429 void EmitXXStructorList(Constant *List); 430 void EmitGlobalConstantStruct(const ConstantStruct* CVS, 431 unsigned AddrSpace); 432 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace); 433 void EmitGlobalConstantVector(const ConstantVector* CP); 434 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace); 435 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace); 436 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C); 437 }; 438} 439 440#endif 441