1 //===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a class to be used as the base class for target specific 10 // asm writers. This class primarily handles common functionality used by 11 // all asm writers. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CODEGEN_ASMPRINTER_H 16 #define LLVM_CODEGEN_ASMPRINTER_H 17 18 #include "llvm/ADT/MapVector.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/CodeGen/AsmPrinterHandler.h" 21 #include "llvm/CodeGen/DwarfStringPoolEntry.h" 22 #include "llvm/CodeGen/MachineFunctionPass.h" 23 #include "llvm/IR/InlineAsm.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/SourceMgr.h" 27 #include <cstdint> 28 #include <memory> 29 #include <utility> 30 #include <vector> 31 32 namespace llvm { 33 34 class BasicBlock; 35 class BlockAddress; 36 class Constant; 37 class ConstantArray; 38 class DataLayout; 39 class DIE; 40 class DIEAbbrev; 41 class DwarfDebug; 42 class GCMetadataPrinter; 43 class GCStrategy; 44 class GlobalIndirectSymbol; 45 class GlobalObject; 46 class GlobalValue; 47 class GlobalVariable; 48 class MachineBasicBlock; 49 class MachineConstantPoolValue; 50 class MachineDominatorTree; 51 class MachineFunction; 52 class MachineInstr; 53 class MachineJumpTableInfo; 54 class MachineLoopInfo; 55 class MachineModuleInfo; 56 class MachineOptimizationRemarkEmitter; 57 class MCAsmInfo; 58 class MCCFIInstruction; 59 class MCContext; 60 class MCExpr; 61 class MCInst; 62 class MCSection; 63 class MCStreamer; 64 class MCSubtargetInfo; 65 class MCSymbol; 66 class MCTargetOptions; 67 class MDNode; 68 class Module; 69 class raw_ostream; 70 class StackMaps; 71 class StringRef; 72 class TargetLoweringObjectFile; 73 class TargetMachine; 74 class Twine; 75 76 namespace remarks { 77 class RemarkStreamer; 78 } 79 80 /// This class is intended to be used as a driving class for all asm writers. 81 class AsmPrinter : public MachineFunctionPass { 82 public: 83 /// Target machine description. 84 TargetMachine &TM; 85 86 /// Target Asm Printer information. 87 const MCAsmInfo *MAI; 88 89 /// This is the context for the output file that we are streaming. This owns 90 /// all of the global MC-related objects for the generated translation unit. 91 MCContext &OutContext; 92 93 /// This is the MCStreamer object for the file we are generating. This 94 /// contains the transient state for the current translation unit that we are 95 /// generating (such as the current section etc). 96 std::unique_ptr<MCStreamer> OutStreamer; 97 98 /// The current machine function. 99 MachineFunction *MF = nullptr; 100 101 /// This is a pointer to the current MachineModuleInfo. 102 MachineModuleInfo *MMI = nullptr; 103 104 /// This is a pointer to the current MachineDominatorTree. 105 MachineDominatorTree *MDT = nullptr; 106 107 /// This is a pointer to the current MachineLoopInfo. 108 MachineLoopInfo *MLI = nullptr; 109 110 /// Optimization remark emitter. 111 MachineOptimizationRemarkEmitter *ORE; 112 113 /// The symbol for the entry in __patchable_function_entires. 114 MCSymbol *CurrentPatchableFunctionEntrySym = nullptr; 115 116 /// The symbol for the current function. This is recalculated at the beginning 117 /// of each call to runOnMachineFunction(). 118 MCSymbol *CurrentFnSym = nullptr; 119 120 /// The symbol for the current function descriptor on AIX. This is created 121 /// at the beginning of each call to SetupMachineFunction(). 122 MCSymbol *CurrentFnDescSym = nullptr; 123 124 /// The symbol used to represent the start of the current function for the 125 /// purpose of calculating its size (e.g. using the .size directive). By 126 /// default, this is equal to CurrentFnSym. 127 MCSymbol *CurrentFnSymForSize = nullptr; 128 129 /// Map a basic block section ID to the begin and end symbols of that section 130 /// which determine the section's range. 131 struct MBBSectionRange { 132 MCSymbol *BeginLabel, *EndLabel; 133 }; 134 135 MapVector<unsigned, MBBSectionRange> MBBSectionRanges; 136 137 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of 138 /// its number of uses by other globals. 139 using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>; 140 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs; 141 142 private: 143 MCSymbol *CurrentFnEnd = nullptr; 144 MCSymbol *CurExceptionSym = nullptr; 145 146 // The symbol used to represent the start of the current BB section of the 147 // function. This is used to calculate the size of the BB section. 148 MCSymbol *CurrentSectionBeginSym = nullptr; 149 150 // The garbage collection metadata printer table. 151 void *GCMetadataPrinters = nullptr; // Really a DenseMap. 152 153 /// Emit comments in assembly output if this is true. 154 bool VerboseAsm; 155 156 static char ID; 157 158 protected: 159 MCSymbol *CurrentFnBegin = nullptr; 160 161 /// Protected struct HandlerInfo and Handlers permit target extended 162 /// AsmPrinter adds their own handlers. 163 struct HandlerInfo { 164 std::unique_ptr<AsmPrinterHandler> Handler; 165 const char *TimerName; 166 const char *TimerDescription; 167 const char *TimerGroupName; 168 const char *TimerGroupDescription; 169 HandlerInfoHandlerInfo170 HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler, 171 const char *TimerName, const char *TimerDescription, 172 const char *TimerGroupName, const char *TimerGroupDescription) 173 : Handler(std::move(Handler)), TimerName(TimerName), 174 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName), 175 TimerGroupDescription(TimerGroupDescription) {} 176 }; 177 178 /// A vector of all debug/EH info emitters we should use. This vector 179 /// maintains ownership of the emitters. 180 SmallVector<HandlerInfo, 1> Handlers; 181 182 public: 183 struct SrcMgrDiagInfo { 184 SourceMgr SrcMgr; 185 std::vector<const MDNode *> LocInfos; 186 LLVMContext::InlineAsmDiagHandlerTy DiagHandler; 187 void *DiagContext; 188 }; 189 190 private: 191 /// If generated on the fly this own the instance. 192 std::unique_ptr<MachineDominatorTree> OwnedMDT; 193 194 /// If generated on the fly this own the instance. 195 std::unique_ptr<MachineLoopInfo> OwnedMLI; 196 197 /// Structure for generating diagnostics for inline assembly. Only initialised 198 /// when necessary. 199 mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo; 200 201 /// If the target supports dwarf debug info, this pointer is non-null. 202 DwarfDebug *DD = nullptr; 203 204 /// If the current module uses dwarf CFI annotations strictly for debugging. 205 bool isCFIMoveForDebugging = false; 206 207 protected: 208 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer); 209 210 public: 211 ~AsmPrinter() override; 212 getDwarfDebug()213 DwarfDebug *getDwarfDebug() { return DD; } getDwarfDebug()214 DwarfDebug *getDwarfDebug() const { return DD; } 215 216 uint16_t getDwarfVersion() const; 217 void setDwarfVersion(uint16_t Version); 218 219 bool isPositionIndependent() const; 220 221 /// Return true if assembly output should contain comments. isVerbose()222 bool isVerbose() const { return VerboseAsm; } 223 224 /// Return a unique ID for the current function. 225 unsigned getFunctionNumber() const; 226 227 /// Return symbol for the function pseudo stack if the stack frame is not a 228 /// register based. getFunctionFrameSymbol()229 virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; } 230 getFunctionBegin()231 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; } getFunctionEnd()232 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; } 233 MCSymbol *getCurExceptionSym(); 234 235 /// Return information about object file lowering. 236 const TargetLoweringObjectFile &getObjFileLowering() const; 237 238 /// Return information about data layout. 239 const DataLayout &getDataLayout() const; 240 241 /// Return the pointer size from the TargetMachine 242 unsigned getPointerSize() const; 243 244 /// Return information about subtarget. 245 const MCSubtargetInfo &getSubtargetInfo() const; 246 247 void EmitToStreamer(MCStreamer &S, const MCInst &Inst); 248 249 /// Emits inital debug location directive. 250 void emitInitialRawDwarfLocDirective(const MachineFunction &MF); 251 252 /// Return the current section we are emitting to. 253 const MCSection *getCurrentSection() const; 254 255 void getNameWithPrefix(SmallVectorImpl<char> &Name, 256 const GlobalValue *GV) const; 257 258 MCSymbol *getSymbol(const GlobalValue *GV) const; 259 260 /// Similar to getSymbol() but preferred for references. On ELF, this uses a 261 /// local symbol if a reference to GV is guaranteed to be resolved to the 262 /// definition in the same module. 263 MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const; 264 265 //===------------------------------------------------------------------===// 266 // XRay instrumentation implementation. 267 //===------------------------------------------------------------------===// 268 public: 269 // This describes the kind of sled we're storing in the XRay table. 270 enum class SledKind : uint8_t { 271 FUNCTION_ENTER = 0, 272 FUNCTION_EXIT = 1, 273 TAIL_CALL = 2, 274 LOG_ARGS_ENTER = 3, 275 CUSTOM_EVENT = 4, 276 TYPED_EVENT = 5, 277 }; 278 279 // The table will contain these structs that point to the sled, the function 280 // containing the sled, and what kind of sled (and whether they should always 281 // be instrumented). We also use a version identifier that the runtime can use 282 // to decide what to do with the sled, depending on the version of the sled. 283 struct XRayFunctionEntry { 284 const MCSymbol *Sled; 285 const MCSymbol *Function; 286 SledKind Kind; 287 bool AlwaysInstrument; 288 const class Function *Fn; 289 uint8_t Version; 290 291 void emit(int, MCStreamer *) const; 292 }; 293 294 // All the sleds to be emitted. 295 SmallVector<XRayFunctionEntry, 4> Sleds; 296 297 // Helper function to record a given XRay sled. 298 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind, 299 uint8_t Version = 0); 300 301 /// Emit a table with all XRay instrumentation points. 302 void emitXRayTable(); 303 304 void emitPatchableFunctionEntries(); 305 306 //===------------------------------------------------------------------===// 307 // MachineFunctionPass Implementation. 308 //===------------------------------------------------------------------===// 309 310 /// Record analysis usage. 311 void getAnalysisUsage(AnalysisUsage &AU) const override; 312 313 /// Set up the AsmPrinter when we are working on a new module. If your pass 314 /// overrides this, it must make sure to explicitly call this implementation. 315 bool doInitialization(Module &M) override; 316 317 /// Shut down the asmprinter. If you override this in your pass, you must make 318 /// sure to call it explicitly. 319 bool doFinalization(Module &M) override; 320 321 /// Emit the specified function out to the OutStreamer. runOnMachineFunction(MachineFunction & MF)322 bool runOnMachineFunction(MachineFunction &MF) override { 323 SetupMachineFunction(MF); 324 emitFunctionBody(); 325 return false; 326 } 327 328 //===------------------------------------------------------------------===// 329 // Coarse grained IR lowering routines. 330 //===------------------------------------------------------------------===// 331 332 /// This should be called when a new MachineFunction is being processed from 333 /// runOnMachineFunction. 334 virtual void SetupMachineFunction(MachineFunction &MF); 335 336 /// This method emits the body and trailer for a function. 337 void emitFunctionBody(); 338 339 void emitCFIInstruction(const MachineInstr &MI); 340 341 void emitFrameAlloc(const MachineInstr &MI); 342 343 void emitStackSizeSection(const MachineFunction &MF); 344 345 void emitRemarksSection(remarks::RemarkStreamer &RS); 346 347 enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug }; 348 CFIMoveType needsCFIMoves() const; 349 350 /// Returns false if needsCFIMoves() == CFI_M_EH for any function 351 /// in the module. needsOnlyDebugCFIMoves()352 bool needsOnlyDebugCFIMoves() const { return isCFIMoveForDebugging; } 353 354 bool needsSEHMoves(); 355 356 /// Print to the current output stream assembly representations of the 357 /// constants in the constant pool MCP. This is used to print out constants 358 /// which have been "spilled to memory" by the code generator. 359 virtual void emitConstantPool(); 360 361 /// Print assembly representations of the jump tables used by the current 362 /// function to the current output stream. 363 virtual void emitJumpTableInfo(); 364 365 /// Emit the specified global variable to the .s file. 366 virtual void emitGlobalVariable(const GlobalVariable *GV); 367 368 /// Check to see if the specified global is a special global used by LLVM. If 369 /// so, emit it and return true, otherwise do nothing and return false. 370 bool emitSpecialLLVMGlobal(const GlobalVariable *GV); 371 372 /// Emit an alignment directive to the specified power of two boundary. If a 373 /// global value is specified, and if that global has an explicit alignment 374 /// requested, it will override the alignment request if required for 375 /// correctness. 376 void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr) const; 377 378 /// Lower the specified LLVM Constant to an MCExpr. 379 virtual const MCExpr *lowerConstant(const Constant *CV); 380 381 /// Print a general LLVM constant to the .s file. 382 void emitGlobalConstant(const DataLayout &DL, const Constant *CV); 383 384 /// Unnamed constant global variables solely contaning a pointer to 385 /// another globals variable act like a global variable "proxy", or GOT 386 /// equivalents, i.e., it's only used to hold the address of the latter. One 387 /// optimization is to replace accesses to these proxies by using the GOT 388 /// entry for the final global instead. Hence, we select GOT equivalent 389 /// candidates among all the module global variables, avoid emitting them 390 /// unnecessarily and finally replace references to them by pc relative 391 /// accesses to GOT entries. 392 void computeGlobalGOTEquivs(Module &M); 393 394 /// Constant expressions using GOT equivalent globals may not be 395 /// eligible for PC relative GOT entry conversion, in such cases we need to 396 /// emit the proxies we previously omitted in EmitGlobalVariable. 397 void emitGlobalGOTEquivs(); 398 399 /// Emit the stack maps. 400 void emitStackMaps(StackMaps &SM); 401 402 //===------------------------------------------------------------------===// 403 // Overridable Hooks 404 //===------------------------------------------------------------------===// 405 406 // Targets can, or in the case of EmitInstruction, must implement these to 407 // customize output. 408 409 /// This virtual method can be overridden by targets that want to emit 410 /// something at the start of their file. emitStartOfAsmFile(Module &)411 virtual void emitStartOfAsmFile(Module &) {} 412 413 /// This virtual method can be overridden by targets that want to emit 414 /// something at the end of their file. emitEndOfAsmFile(Module &)415 virtual void emitEndOfAsmFile(Module &) {} 416 417 /// Targets can override this to emit stuff before the first basic block in 418 /// the function. emitFunctionBodyStart()419 virtual void emitFunctionBodyStart() {} 420 421 /// Targets can override this to emit stuff after the last basic block in the 422 /// function. emitFunctionBodyEnd()423 virtual void emitFunctionBodyEnd() {} 424 425 /// Targets can override this to emit stuff at the start of a basic block. 426 /// By default, this method prints the label for the specified 427 /// MachineBasicBlock, an alignment (if present) and a comment describing it 428 /// if appropriate. 429 virtual void emitBasicBlockStart(const MachineBasicBlock &MBB); 430 431 /// Targets can override this to emit stuff at the end of a basic block. 432 virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB); 433 434 /// Targets should implement this to emit instructions. emitInstruction(const MachineInstr *)435 virtual void emitInstruction(const MachineInstr *) { 436 llvm_unreachable("EmitInstruction not implemented"); 437 } 438 439 /// Return the symbol for the specified constant pool entry. 440 virtual MCSymbol *GetCPISymbol(unsigned CPID) const; 441 442 virtual void emitFunctionEntryLabel(); 443 emitFunctionDescriptor()444 virtual void emitFunctionDescriptor() { 445 llvm_unreachable("Function descriptor is target-specific."); 446 } 447 448 virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 449 450 /// Targets can override this to change how global constants that are part of 451 /// a C++ static/global constructor list are emitted. emitXXStructor(const DataLayout & DL,const Constant * CV)452 virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) { 453 emitGlobalConstant(DL, CV); 454 } 455 456 /// Return true if the basic block has exactly one predecessor and the control 457 /// transfer mechanism between the predecessor and this block is a 458 /// fall-through. 459 virtual bool 460 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; 461 462 /// Targets can override this to customize the output of IMPLICIT_DEF 463 /// instructions in verbose mode. 464 virtual void emitImplicitDef(const MachineInstr *MI) const; 465 466 /// Emit N NOP instructions. 467 void emitNops(unsigned N); 468 469 //===------------------------------------------------------------------===// 470 // Symbol Lowering Routines. 471 //===------------------------------------------------------------------===// 472 473 MCSymbol *createTempSymbol(const Twine &Name) const; 474 475 /// Return the MCSymbol for a private symbol with global value name as its 476 /// base, with the specified suffix. 477 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, 478 StringRef Suffix) const; 479 480 /// Return the MCSymbol for the specified ExternalSymbol. 481 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 482 483 /// Return the symbol for the specified jump table entry. 484 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; 485 486 /// Return the symbol for the specified jump table .set 487 /// FIXME: privatize to AsmPrinter. 488 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; 489 490 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified 491 /// basic block. 492 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; 493 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; 494 495 //===------------------------------------------------------------------===// 496 // Emission Helper Routines. 497 //===------------------------------------------------------------------===// 498 499 /// This is just convenient handler for printing offsets. 500 void printOffset(int64_t Offset, raw_ostream &OS) const; 501 502 /// Emit a byte directive and value. 503 void emitInt8(int Value) const; 504 505 /// Emit a short directive and value. 506 void emitInt16(int Value) const; 507 508 /// Emit a long directive and value. 509 void emitInt32(int Value) const; 510 511 /// Emit a long long directive and value. 512 void emitInt64(uint64_t Value) const; 513 514 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive 515 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses 516 /// .set if it is available. 517 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, 518 unsigned Size) const; 519 520 /// Emit something like ".uleb128 Hi-Lo". 521 void emitLabelDifferenceAsULEB128(const MCSymbol *Hi, 522 const MCSymbol *Lo) const; 523 524 /// Emit something like ".long Label+Offset" where the size in bytes of the 525 /// directive is specified by Size and Label specifies the label. This 526 /// implicitly uses .set if it is available. 527 void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, 528 unsigned Size, bool IsSectionRelative = false) const; 529 530 /// Emit something like ".long Label" where the size in bytes of the directive 531 /// is specified by Size and Label specifies the label. 532 void emitLabelReference(const MCSymbol *Label, unsigned Size, 533 bool IsSectionRelative = false) const { 534 emitLabelPlusOffset(Label, 0, Size, IsSectionRelative); 535 } 536 537 /// Emit something like ".long Label + Offset". 538 void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const; 539 540 //===------------------------------------------------------------------===// 541 // Dwarf Emission Helper Routines 542 //===------------------------------------------------------------------===// 543 544 /// Emit the specified signed leb128 value. 545 void emitSLEB128(int64_t Value, const char *Desc = nullptr) const; 546 547 /// Emit the specified unsigned leb128 value. 548 void emitULEB128(uint64_t Value, const char *Desc = nullptr, 549 unsigned PadTo = 0) const; 550 551 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose 552 /// assembly output is enabled, we output comments describing the encoding. 553 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA"). 554 void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const; 555 556 /// Return the size of the encoding in bytes. 557 unsigned GetSizeOfEncodedValue(unsigned Encoding) const; 558 559 /// Emit reference to a ttype global with a specified encoding. 560 void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) const; 561 562 /// Emit a reference to a symbol for use in dwarf. Different object formats 563 /// represent this in different ways. Some use a relocation others encode 564 /// the label offset in its section. 565 void emitDwarfSymbolReference(const MCSymbol *Label, 566 bool ForceOffset = false) const; 567 568 /// Emit the 4-byte offset of a string from the start of its section. 569 /// 570 /// When possible, emit a DwarfStringPool section offset without any 571 /// relocations, and without using the symbol. Otherwise, defers to \a 572 /// emitDwarfSymbolReference(). 573 void emitDwarfStringOffset(DwarfStringPoolEntry S) const; 574 575 /// Emit the 4-byte offset of a string from the start of its section. emitDwarfStringOffset(DwarfStringPoolEntryRef S)576 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const { 577 emitDwarfStringOffset(S.getEntry()); 578 } 579 580 /// Emit reference to a call site with a specified encoding 581 void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, 582 unsigned Encoding) const; 583 /// Emit an integer value corresponding to the call site encoding 584 void emitCallSiteValue(uint64_t Value, unsigned Encoding) const; 585 586 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified. getISAEncoding()587 virtual unsigned getISAEncoding() { return 0; } 588 589 /// Emit the directive and value for debug thread local expression 590 /// 591 /// \p Value - The value to emit. 592 /// \p Size - The size of the integer (in bytes) to emit. 593 virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const; 594 595 //===------------------------------------------------------------------===// 596 // Dwarf Lowering Routines 597 //===------------------------------------------------------------------===// 598 599 /// Emit frame instruction to describe the layout of the frame. 600 void emitCFIInstruction(const MCCFIInstruction &Inst) const; 601 602 /// Emit Dwarf abbreviation table. emitDwarfAbbrevs(const T & Abbrevs)603 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const { 604 // For each abbreviation. 605 for (const auto &Abbrev : Abbrevs) 606 emitDwarfAbbrev(*Abbrev); 607 608 // Mark end of abbreviations. 609 emitULEB128(0, "EOM(3)"); 610 } 611 612 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const; 613 614 /// Recursively emit Dwarf DIE tree. 615 void emitDwarfDIE(const DIE &Die) const; 616 617 //===------------------------------------------------------------------===// 618 // Inline Asm Support 619 //===------------------------------------------------------------------===// 620 621 // These are hooks that targets can override to implement inline asm 622 // support. These should probably be moved out of AsmPrinter someday. 623 624 /// Print information related to the specified machine instr that is 625 /// independent of the operand, and may be independent of the instr itself. 626 /// This can be useful for portably encoding the comment character or other 627 /// bits of target-specific knowledge into the asmstrings. The syntax used is 628 /// ${:comment}. Targets can override this to add support for their own 629 /// strange codes. 630 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 631 const char *Code) const; 632 633 /// Print the MachineOperand as a symbol. Targets with complex handling of 634 /// symbol references should override the base implementation. 635 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS); 636 637 /// Print the specified operand of MI, an INLINEASM instruction, using the 638 /// specified assembler variant. Targets should override this to format as 639 /// appropriate. This method can return true if the operand is erroneous. 640 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 641 const char *ExtraCode, raw_ostream &OS); 642 643 /// Print the specified operand of MI, an INLINEASM instruction, using the 644 /// specified assembler variant as an address. Targets should override this to 645 /// format as appropriate. This method can return true if the operand is 646 /// erroneous. 647 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 648 const char *ExtraCode, raw_ostream &OS); 649 650 /// Let the target do anything it needs to do before emitting inlineasm. 651 /// \p StartInfo - the subtarget info before parsing inline asm 652 virtual void emitInlineAsmStart() const; 653 654 /// Let the target do anything it needs to do after emitting inlineasm. 655 /// This callback can be used restore the original mode in case the 656 /// inlineasm contains directives to switch modes. 657 /// \p StartInfo - the original subtarget info before inline asm 658 /// \p EndInfo - the final subtarget info after parsing the inline asm, 659 /// or NULL if the value is unknown. 660 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, 661 const MCSubtargetInfo *EndInfo) const; 662 663 /// This emits visibility information about symbol, if this is supported by 664 /// the target. 665 void emitVisibility(MCSymbol *Sym, unsigned Visibility, 666 bool IsDefinition = true) const; 667 668 /// This emits linkage information about \p GVSym based on \p GV, if this is 669 /// supported by the target. 670 virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const; 671 672 /// Return the alignment for the specified \p GV. 673 static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL, 674 Align InAlign = Align(1)); 675 676 private: 677 /// Private state for PrintSpecial() 678 // Assign a unique ID to this machine instruction. 679 mutable const MachineInstr *LastMI = nullptr; 680 mutable unsigned LastFn = 0; 681 mutable unsigned Counter = ~0U; 682 683 /// This method emits the header for the current function. 684 virtual void emitFunctionHeader(); 685 686 /// This method emits a comment next to header for the current function. 687 virtual void emitFunctionHeaderComment(); 688 689 /// Emit a blob of inline asm to the output streamer. 690 void 691 emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, 692 const MCTargetOptions &MCOptions, 693 const MDNode *LocMDNode = nullptr, 694 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const; 695 696 /// This method formats and emits the specified machine instruction that is an 697 /// inline asm. 698 void emitInlineAsm(const MachineInstr *MI) const; 699 700 /// Add inline assembly info to the diagnostics machinery, so we can 701 /// emit file and position info. Returns SrcMgr memory buffer position. 702 unsigned addInlineAsmDiagBuffer(StringRef AsmStr, 703 const MDNode *LocMDNode) const; 704 705 //===------------------------------------------------------------------===// 706 // Internal Implementation Details 707 //===------------------------------------------------------------------===// 708 709 void emitJumpTableEntry(const MachineJumpTableInfo *MJTI, 710 const MachineBasicBlock *MBB, unsigned uid) const; 711 void emitLLVMUsedList(const ConstantArray *InitList); 712 /// Emit llvm.ident metadata in an '.ident' directive. 713 void emitModuleIdents(Module &M); 714 /// Emit bytes for llvm.commandline metadata. 715 void emitModuleCommandLines(Module &M); 716 void emitXXStructorList(const DataLayout &DL, const Constant *List, 717 bool isCtor); 718 719 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S); 720 /// Emit GlobalAlias or GlobalIFunc. 721 void emitGlobalIndirectSymbol(Module &M, const GlobalIndirectSymbol &GIS); 722 }; 723 724 } // end namespace llvm 725 726 #endif // LLVM_CODEGEN_ASMPRINTER_H 727