AsmPrinter.h revision 5b676ce7932cf60d4fd6d101323d8d54b8395804
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/Support/DataTypes.h" 21 22namespace llvm { 23 class BlockAddress; 24 class GCStrategy; 25 class Constant; 26 class GCMetadataPrinter; 27 class GlobalValue; 28 class GlobalVariable; 29 class MachineBasicBlock; 30 class MachineFunction; 31 class MachineInstr; 32 class MachineLocation; 33 class MachineLoopInfo; 34 class MachineLoop; 35 class MachineConstantPoolValue; 36 class MachineJumpTableInfo; 37 class MachineModuleInfo; 38 class MachineMove; 39 class MCAsmInfo; 40 class MCInst; 41 class MCContext; 42 class MCSection; 43 class MCStreamer; 44 class MCSymbol; 45 class MDNode; 46 class DwarfDebug; 47 class DwarfException; 48 class Mangler; 49 class TargetLoweringObjectFile; 50 class TargetData; 51 class TargetMachine; 52 class Twine; 53 class Type; 54 55 /// AsmPrinter - This class is intended to be used as a driving class for all 56 /// asm writers. 57 class AsmPrinter : public MachineFunctionPass { 58 public: 59 /// Target machine description. 60 /// 61 TargetMachine &TM; 62 63 /// Target Asm Printer information. 64 /// 65 const MCAsmInfo *MAI; 66 67 /// OutContext - This is the context for the output file that we are 68 /// streaming. This owns all of the global MC-related objects for the 69 /// generated translation unit. 70 MCContext &OutContext; 71 72 /// OutStreamer - This is the MCStreamer object for the file we are 73 /// generating. This contains the transient state for the current 74 /// translation unit that we are generating (such as the current section 75 /// etc). 76 MCStreamer &OutStreamer; 77 78 /// The current machine function. 79 const MachineFunction *MF; 80 81 /// MMI - This is a pointer to the current MachineModuleInfo. 82 MachineModuleInfo *MMI; 83 84 /// Name-mangler for global names. 85 /// 86 Mangler *Mang; 87 88 /// The symbol for the current function. This is recalculated at the 89 /// beginning of each call to runOnMachineFunction(). 90 /// 91 MCSymbol *CurrentFnSym; 92 93 private: 94 // GCMetadataPrinters - The garbage collection metadata printer table. 95 void *GCMetadataPrinters; // Really a DenseMap. 96 97 /// VerboseAsm - Emit comments in assembly output if this is true. 98 /// 99 bool VerboseAsm; 100 static char ID; 101 102 /// If VerboseAsm is set, a pointer to the loop info for this 103 /// function. 104 MachineLoopInfo *LI; 105 106 /// DD - If the target supports dwarf debug info, this pointer is non-null. 107 DwarfDebug *DD; 108 109 /// DE - If the target supports dwarf exception info, this pointer is 110 /// non-null. 111 DwarfException *DE; 112 113 protected: 114 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer); 115 116 public: 117 virtual ~AsmPrinter(); 118 119 /// isVerbose - Return true if assembly output should contain comments. 120 /// 121 bool isVerbose() const { return VerboseAsm; } 122 123 /// getFunctionNumber - Return a unique ID for the current function. 124 /// 125 unsigned getFunctionNumber() const; 126 127 /// getObjFileLowering - Return information about object file lowering. 128 const TargetLoweringObjectFile &getObjFileLowering() const; 129 130 /// getTargetData - Return information about data layout. 131 const TargetData &getTargetData() const; 132 133 /// getCurrentSection() - Return the current section we are emitting to. 134 const MCSection *getCurrentSection() const; 135 136 137 //===------------------------------------------------------------------===// 138 // MachineFunctionPass Implementation. 139 //===------------------------------------------------------------------===// 140 141 /// getAnalysisUsage - Record analysis usage. 142 /// 143 void getAnalysisUsage(AnalysisUsage &AU) const; 144 145 /// doInitialization - Set up the AsmPrinter when we are working on a new 146 /// module. If your pass overrides this, it must make sure to explicitly 147 /// call this implementation. 148 bool doInitialization(Module &M); 149 150 /// doFinalization - Shut down the asmprinter. If you override this in your 151 /// pass, you must make sure to call it explicitly. 152 bool doFinalization(Module &M); 153 154 /// runOnMachineFunction - Emit the specified function out to the 155 /// OutStreamer. 156 virtual bool runOnMachineFunction(MachineFunction &MF) { 157 SetupMachineFunction(MF); 158 EmitFunctionHeader(); 159 EmitFunctionBody(); 160 return false; 161 } 162 163 //===------------------------------------------------------------------===// 164 // Coarse grained IR lowering routines. 165 //===------------------------------------------------------------------===// 166 167 /// SetupMachineFunction - This should be called when a new MachineFunction 168 /// is being processed from runOnMachineFunction. 169 void SetupMachineFunction(MachineFunction &MF); 170 171 /// EmitFunctionHeader - This method emits the header for the current 172 /// function. 173 void EmitFunctionHeader(); 174 175 /// EmitFunctionBody - This method emits the body and trailer for a 176 /// function. 177 void EmitFunctionBody(); 178 179 void emitPrologLabel(const MachineInstr &MI); 180 181 enum CFIMoveType { 182 CFI_M_None, 183 CFI_M_EH, 184 CFI_M_Debug 185 }; 186 CFIMoveType needsCFIMoves(); 187 188 bool needsSEHMoves(); 189 190 /// needsRelocationsForDwarfStringPool - Specifies whether the object format 191 /// expects to use relocations to refer to debug entries. Alternatively we 192 /// emit section offsets in bytes from the start of the string pool. 193 bool needsRelocationsForDwarfStringPool() const; 194 195 /// EmitConstantPool - Print to the current output stream assembly 196 /// representations of the constants in the constant pool MCP. This is 197 /// used to print out constants which have been "spilled to memory" by 198 /// the code generator. 199 /// 200 virtual void EmitConstantPool(); 201 202 /// EmitJumpTableInfo - Print assembly representations of the jump tables 203 /// used by the current function to the current output stream. 204 /// 205 void EmitJumpTableInfo(); 206 207 /// EmitGlobalVariable - Emit the specified global variable to the .s file. 208 virtual void EmitGlobalVariable(const GlobalVariable *GV); 209 210 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 211 /// special global used by LLVM. If so, emit it and return true, otherwise 212 /// do nothing and return false. 213 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 214 215 /// EmitAlignment - Emit an alignment directive to the specified power of 216 /// two boundary. For example, if you pass in 3 here, you will get an 8 217 /// byte alignment. If a global value is specified, and if that global has 218 /// an explicit alignment requested, it will override the alignment request 219 /// if required for correctness. 220 /// 221 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const; 222 223 /// EmitBasicBlockStart - This method prints the label for the specified 224 /// MachineBasicBlock, an alignment (if present) and a comment describing 225 /// it if appropriate. 226 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const; 227 228 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 229 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0); 230 231 232 //===------------------------------------------------------------------===// 233 // Overridable Hooks 234 //===------------------------------------------------------------------===// 235 236 // Targets can, or in the case of EmitInstruction, must implement these to 237 // customize output. 238 239 /// EmitStartOfAsmFile - This virtual method can be overridden by targets 240 /// that want to emit something at the start of their file. 241 virtual void EmitStartOfAsmFile(Module &) {} 242 243 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that 244 /// want to emit something at the end of their file. 245 virtual void EmitEndOfAsmFile(Module &) {} 246 247 /// EmitFunctionBodyStart - Targets can override this to emit stuff before 248 /// the first basic block in the function. 249 virtual void EmitFunctionBodyStart() {} 250 251 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after 252 /// the last basic block in the function. 253 virtual void EmitFunctionBodyEnd() {} 254 255 /// EmitInstruction - Targets should implement this to emit instructions. 256 virtual void EmitInstruction(const MachineInstr *) { 257 assert(0 && "EmitInstruction not implemented"); 258 } 259 260 virtual void EmitFunctionEntryLabel(); 261 262 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 263 264 /// EmitXXStructor - Targets can override this to change how global 265 /// constants that are part of a C++ static/global constructor list are 266 /// emitted. 267 virtual void EmitXXStructor(const Constant *CV) { 268 EmitGlobalConstant(CV); 269 } 270 271 /// isBlockOnlyReachableByFallthough - Return true if the basic block has 272 /// exactly one predecessor and the control transfer mechanism between 273 /// the predecessor and this block is a fall-through. 274 virtual bool 275 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; 276 277 //===------------------------------------------------------------------===// 278 // Symbol Lowering Routines. 279 //===------------------------------------------------------------------===// 280 public: 281 282 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler 283 /// temporary label with the specified stem and unique ID. 284 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const; 285 286 /// GetTempSymbol - Return an assembler temporary label with the specified 287 /// stem. 288 MCSymbol *GetTempSymbol(StringRef Name) const; 289 290 291 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with 292 /// global value name as its base, with the specified suffix, and where the 293 /// symbol is forced to have private linkage if ForcePrivate is true. 294 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV, 295 StringRef Suffix, 296 bool ForcePrivate = true) const; 297 298 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified 299 /// ExternalSymbol. 300 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 301 302 /// GetCPISymbol - Return the symbol for the specified constant pool entry. 303 MCSymbol *GetCPISymbol(unsigned CPID) const; 304 305 /// GetJTISymbol - Return the symbol for the specified jump table entry. 306 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; 307 308 /// GetJTSetSymbol - Return the symbol for the specified jump table .set 309 /// FIXME: privatize to AsmPrinter. 310 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; 311 312 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress 313 /// uses of the specified basic block. 314 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; 315 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; 316 317 //===------------------------------------------------------------------===// 318 // Emission Helper Routines. 319 //===------------------------------------------------------------------===// 320 public: 321 /// printOffset - This is just convenient handler for printing offsets. 322 void printOffset(int64_t Offset, raw_ostream &OS) const; 323 324 /// EmitInt8 - Emit a byte directive and value. 325 /// 326 void EmitInt8(int Value) const; 327 328 /// EmitInt16 - Emit a short directive and value. 329 /// 330 void EmitInt16(int Value) const; 331 332 /// EmitInt32 - Emit a long directive and value. 333 /// 334 void EmitInt32(int Value) const; 335 336 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size 337 /// in bytes of the directive is specified by Size and Hi/Lo specify the 338 /// labels. This implicitly uses .set if it is available. 339 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, 340 unsigned Size) const; 341 342 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo" 343 /// where the size in bytes of the directive is specified by Size and Hi/Lo 344 /// specify the labels. This implicitly uses .set if it is available. 345 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset, 346 const MCSymbol *Lo, unsigned Size) const; 347 348 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset" 349 /// where the size in bytes of the directive is specified by Size and Label 350 /// specifies the label. This implicitly uses .set if it is available. 351 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, 352 unsigned Size) const; 353 354 //===------------------------------------------------------------------===// 355 // Dwarf Emission Helper Routines 356 //===------------------------------------------------------------------===// 357 358 /// EmitSLEB128 - emit the specified signed leb128 value. 359 void EmitSLEB128(int Value, const char *Desc = 0) const; 360 361 /// EmitULEB128 - emit the specified unsigned leb128 value. 362 void EmitULEB128(unsigned Value, const char *Desc = 0, 363 unsigned PadTo = 0) const; 364 365 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value. 366 void EmitCFAByte(unsigned Val) const; 367 368 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an 369 /// encoding. If verbose assembly output is enabled, we output comments 370 /// describing the encoding. Desc is a string saying what the encoding is 371 /// specifying (e.g. "LSDA"). 372 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const; 373 374 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes. 375 unsigned GetSizeOfEncodedValue(unsigned Encoding) const; 376 377 /// EmitReference - Emit a reference to a label with a specified encoding. 378 /// 379 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const; 380 void EmitReference(const GlobalValue *GV, unsigned Encoding) const; 381 382 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of 383 /// its section. This can be done with a special directive if the target 384 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at 385 /// the start of the section. 386 /// 387 /// SectionLabel is a temporary label emitted at the start of the section 388 /// that Label lives in. 389 void EmitSectionOffset(const MCSymbol *Label, 390 const MCSymbol *SectionLabel) const; 391 392 /// getDebugValueLocation - Get location information encoded by DBG_VALUE 393 /// operands. 394 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const; 395 396 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa 397 /// encoding specified. 398 virtual unsigned getISAEncoding() { return 0; } 399 400 /// EmitDwarfRegOp - Emit dwarf register operation. 401 virtual void EmitDwarfRegOp(const MachineLocation &MLoc) const; 402 403 //===------------------------------------------------------------------===// 404 // Dwarf Lowering Routines 405 //===------------------------------------------------------------------===// 406 407 /// EmitCFIFrameMove - Emit frame instruction to describe the layout of the 408 /// frame. 409 void EmitCFIFrameMove(const MachineMove &Move) const; 410 411 //===------------------------------------------------------------------===// 412 // Inline Asm Support 413 //===------------------------------------------------------------------===// 414 public: 415 // These are hooks that targets can override to implement inline asm 416 // support. These should probably be moved out of AsmPrinter someday. 417 418 /// PrintSpecial - Print information related to the specified machine instr 419 /// that is independent of the operand, and may be independent of the instr 420 /// itself. This can be useful for portably encoding the comment character 421 /// or other bits of target-specific knowledge into the asmstrings. The 422 /// syntax used is ${:comment}. Targets can override this to add support 423 /// for their own strange codes. 424 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 425 const char *Code) const; 426 427 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 428 /// instruction, using the specified assembler variant. Targets should 429 /// override this to format as appropriate. This method can return true if 430 /// the operand is erroneous. 431 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 432 unsigned AsmVariant, const char *ExtraCode, 433 raw_ostream &OS); 434 435 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 436 /// instruction, using the specified assembler variant as an address. 437 /// Targets should override this to format as appropriate. This method can 438 /// return true if the operand is erroneous. 439 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 440 unsigned AsmVariant, 441 const char *ExtraCode, 442 raw_ostream &OS); 443 444 private: 445 /// Private state for PrintSpecial() 446 // Assign a unique ID to this machine instruction. 447 mutable const MachineInstr *LastMI; 448 mutable unsigned LastFn; 449 mutable unsigned Counter; 450 mutable unsigned SetCounter; 451 452 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer. 453 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0) const; 454 455 /// EmitInlineAsm - This method formats and emits the specified machine 456 /// instruction that is an inline asm. 457 void EmitInlineAsm(const MachineInstr *MI) const; 458 459 //===------------------------------------------------------------------===// 460 // Internal Implementation Details 461 //===------------------------------------------------------------------===// 462 463 /// EmitVisibility - This emits visibility information about symbol, if 464 /// this is suported by the target. 465 void EmitVisibility(MCSymbol *Sym, unsigned Visibility, 466 bool IsDefinition = true) const; 467 468 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const; 469 470 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI, 471 const MachineBasicBlock *MBB, 472 unsigned uid) const; 473 void EmitLLVMUsedList(const Constant *List); 474 void EmitXXStructorList(const Constant *List, bool isCtor); 475 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C); 476 }; 477} 478 479#endif 480