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 uint64_t TailPadding); 384 385 /// Unnamed constant global variables solely contaning a pointer to 386 /// another globals variable act like a global variable "proxy", or GOT 387 /// equivalents, i.e., it's only used to hold the address of the latter. One 388 /// optimization is to replace accesses to these proxies by using the GOT 389 /// entry for the final global instead. Hence, we select GOT equivalent 390 /// candidates among all the module global variables, avoid emitting them 391 /// unnecessarily and finally replace references to them by pc relative 392 /// accesses to GOT entries. 393 void computeGlobalGOTEquivs(Module &M); 394 395 /// Constant expressions using GOT equivalent globals may not be 396 /// eligible for PC relative GOT entry conversion, in such cases we need to 397 /// emit the proxies we previously omitted in EmitGlobalVariable. 398 void emitGlobalGOTEquivs(); 399 400 /// Emit the stack maps. 401 void emitStackMaps(StackMaps &SM); 402 403 //===------------------------------------------------------------------===// 404 // Overridable Hooks 405 //===------------------------------------------------------------------===// 406 407 // Targets can, or in the case of EmitInstruction, must implement these to 408 // customize output. 409 410 /// This virtual method can be overridden by targets that want to emit 411 /// something at the start of their file. emitStartOfAsmFile(Module &)412 virtual void emitStartOfAsmFile(Module &) {} 413 414 /// This virtual method can be overridden by targets that want to emit 415 /// something at the end of their file. emitEndOfAsmFile(Module &)416 virtual void emitEndOfAsmFile(Module &) {} 417 418 /// Targets can override this to emit stuff before the first basic block in 419 /// the function. emitFunctionBodyStart()420 virtual void emitFunctionBodyStart() {} 421 422 /// Targets can override this to emit stuff after the last basic block in the 423 /// function. emitFunctionBodyEnd()424 virtual void emitFunctionBodyEnd() {} 425 426 /// Targets can override this to emit stuff at the start of a basic block. 427 /// By default, this method prints the label for the specified 428 /// MachineBasicBlock, an alignment (if present) and a comment describing it 429 /// if appropriate. 430 virtual void emitBasicBlockStart(const MachineBasicBlock &MBB); 431 432 /// Targets can override this to emit stuff at the end of a basic block. 433 virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB); 434 435 /// Targets should implement this to emit instructions. emitInstruction(const MachineInstr *)436 virtual void emitInstruction(const MachineInstr *) { 437 llvm_unreachable("EmitInstruction not implemented"); 438 } 439 440 /// Return the symbol for the specified constant pool entry. 441 virtual MCSymbol *GetCPISymbol(unsigned CPID) const; 442 443 virtual void emitFunctionEntryLabel(); 444 emitFunctionDescriptor()445 virtual void emitFunctionDescriptor() { 446 llvm_unreachable("Function descriptor is target-specific."); 447 } 448 449 virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 450 451 /// Targets can override this to change how global constants that are part of 452 /// a C++ static/global constructor list are emitted. emitXXStructor(const DataLayout & DL,const Constant * CV)453 virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) { 454 emitGlobalConstant(DL, CV, 0); 455 } 456 457 /// Return true if the basic block has exactly one predecessor and the control 458 /// transfer mechanism between the predecessor and this block is a 459 /// fall-through. 460 virtual bool 461 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; 462 463 /// Targets can override this to customize the output of IMPLICIT_DEF 464 /// instructions in verbose mode. 465 virtual void emitImplicitDef(const MachineInstr *MI) const; 466 467 /// Emit N NOP instructions. 468 void emitNops(unsigned N); 469 470 //===------------------------------------------------------------------===// 471 // Symbol Lowering Routines. 472 //===------------------------------------------------------------------===// 473 474 MCSymbol *createTempSymbol(const Twine &Name) const; 475 476 /// Return the MCSymbol for a private symbol with global value name as its 477 /// base, with the specified suffix. 478 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, 479 StringRef Suffix) const; 480 481 /// Return the MCSymbol for the specified ExternalSymbol. 482 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 483 484 /// Return the symbol for the specified jump table entry. 485 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; 486 487 /// Return the symbol for the specified jump table .set 488 /// FIXME: privatize to AsmPrinter. 489 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; 490 491 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified 492 /// basic block. 493 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; 494 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; 495 496 //===------------------------------------------------------------------===// 497 // Emission Helper Routines. 498 //===------------------------------------------------------------------===// 499 500 /// This is just convenient handler for printing offsets. 501 void printOffset(int64_t Offset, raw_ostream &OS) const; 502 503 /// Emit a byte directive and value. 504 void emitInt8(int Value) const; 505 506 /// Emit a short directive and value. 507 void emitInt16(int Value) const; 508 509 /// Emit a long directive and value. 510 void emitInt32(int Value) const; 511 512 /// Emit a long long directive and value. 513 void emitInt64(uint64_t Value) const; 514 515 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive 516 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses 517 /// .set if it is available. 518 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, 519 unsigned Size) const; 520 521 /// Emit something like ".uleb128 Hi-Lo". 522 void emitLabelDifferenceAsULEB128(const MCSymbol *Hi, 523 const MCSymbol *Lo) const; 524 525 /// Emit something like ".long Label+Offset" where the size in bytes of the 526 /// directive is specified by Size and Label specifies the label. This 527 /// implicitly uses .set if it is available. 528 void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, 529 unsigned Size, bool IsSectionRelative = false) const; 530 531 /// Emit something like ".long Label" where the size in bytes of the directive 532 /// is specified by Size and Label specifies the label. 533 void emitLabelReference(const MCSymbol *Label, unsigned Size, 534 bool IsSectionRelative = false) const { 535 emitLabelPlusOffset(Label, 0, Size, IsSectionRelative); 536 } 537 538 /// Emit something like ".long Label + Offset". 539 void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const; 540 541 //===------------------------------------------------------------------===// 542 // Dwarf Emission Helper Routines 543 //===------------------------------------------------------------------===// 544 545 /// Emit the specified signed leb128 value. 546 void emitSLEB128(int64_t Value, const char *Desc = nullptr) const; 547 548 /// Emit the specified unsigned leb128 value. 549 void emitULEB128(uint64_t Value, const char *Desc = nullptr, 550 unsigned PadTo = 0) const; 551 552 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose 553 /// assembly output is enabled, we output comments describing the encoding. 554 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA"). 555 void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const; 556 557 /// Return the size of the encoding in bytes. 558 unsigned GetSizeOfEncodedValue(unsigned Encoding) const; 559 560 /// Emit reference to a ttype global with a specified encoding. 561 void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) const; 562 563 /// Emit a reference to a symbol for use in dwarf. Different object formats 564 /// represent this in different ways. Some use a relocation others encode 565 /// the label offset in its section. 566 void emitDwarfSymbolReference(const MCSymbol *Label, 567 bool ForceOffset = false) const; 568 569 /// Emit the 4-byte offset of a string from the start of its section. 570 /// 571 /// When possible, emit a DwarfStringPool section offset without any 572 /// relocations, and without using the symbol. Otherwise, defers to \a 573 /// emitDwarfSymbolReference(). 574 void emitDwarfStringOffset(DwarfStringPoolEntry S) const; 575 576 /// Emit the 4-byte offset of a string from the start of its section. emitDwarfStringOffset(DwarfStringPoolEntryRef S)577 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const { 578 emitDwarfStringOffset(S.getEntry()); 579 } 580 581 /// Emit reference to a call site with a specified encoding 582 void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, 583 unsigned Encoding) const; 584 /// Emit an integer value corresponding to the call site encoding 585 void emitCallSiteValue(uint64_t Value, unsigned Encoding) const; 586 /// Emit a CHERI capability to a call site 587 void emitCallSiteCheriCapability(const MCSymbol *Hi, 588 const MCSymbol *Lo) const; 589 590 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified. getISAEncoding()591 virtual unsigned getISAEncoding() { return 0; } 592 593 /// Emit the directive and value for debug thread local expression 594 /// 595 /// \p Value - The value to emit. 596 /// \p Size - The size of the integer (in bytes) to emit. 597 virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const; 598 599 //===------------------------------------------------------------------===// 600 // Dwarf Lowering Routines 601 //===------------------------------------------------------------------===// 602 603 /// Emit frame instruction to describe the layout of the frame. 604 void emitCFIInstruction(const MCCFIInstruction &Inst) const; 605 606 /// Emit Dwarf abbreviation table. emitDwarfAbbrevs(const T & Abbrevs)607 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const { 608 // For each abbreviation. 609 for (const auto &Abbrev : Abbrevs) 610 emitDwarfAbbrev(*Abbrev); 611 612 // Mark end of abbreviations. 613 emitULEB128(0, "EOM(3)"); 614 } 615 616 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const; 617 618 /// Recursively emit Dwarf DIE tree. 619 void emitDwarfDIE(const DIE &Die) const; 620 621 //===------------------------------------------------------------------===// 622 // Inline Asm Support 623 //===------------------------------------------------------------------===// 624 625 // These are hooks that targets can override to implement inline asm 626 // support. These should probably be moved out of AsmPrinter someday. 627 628 /// Print information related to the specified machine instr that is 629 /// independent of the operand, and may be independent of the instr itself. 630 /// This can be useful for portably encoding the comment character or other 631 /// bits of target-specific knowledge into the asmstrings. The syntax used is 632 /// ${:comment}. Targets can override this to add support for their own 633 /// strange codes. 634 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 635 const char *Code) const; 636 637 /// Print the MachineOperand as a symbol. Targets with complex handling of 638 /// symbol references should override the base implementation. 639 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS); 640 641 /// Print the specified operand of MI, an INLINEASM instruction, using the 642 /// specified assembler variant. Targets should override this to format as 643 /// appropriate. This method can return true if the operand is erroneous. 644 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 645 const char *ExtraCode, raw_ostream &OS); 646 647 /// Print the specified operand of MI, an INLINEASM instruction, using the 648 /// specified assembler variant as an address. Targets should override this to 649 /// format as appropriate. This method can return true if the operand is 650 /// erroneous. 651 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 652 const char *ExtraCode, raw_ostream &OS); 653 654 /// Let the target do anything it needs to do before emitting inlineasm. 655 /// \p StartInfo - the subtarget info before parsing inline asm 656 virtual void emitInlineAsmStart() const; 657 658 /// Let the target do anything it needs to do after emitting inlineasm. 659 /// This callback can be used restore the original mode in case the 660 /// inlineasm contains directives to switch modes. 661 /// \p StartInfo - the original subtarget info before inline asm 662 /// \p EndInfo - the final subtarget info after parsing the inline asm, 663 /// or NULL if the value is unknown. 664 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, 665 const MCSubtargetInfo *EndInfo) const; 666 667 /// This emits visibility information about symbol, if this is supported by 668 /// the target. 669 void emitVisibility(MCSymbol *Sym, unsigned Visibility, 670 bool IsDefinition = true) const; 671 672 /// This emits linkage information about \p GVSym based on \p GV, if this is 673 /// supported by the target. 674 virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const; 675 676 /// Return the alignment for the specified \p GV. 677 static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL, 678 Align InAlign = Align(1)); 679 680 private: 681 /// Private state for PrintSpecial() 682 // Assign a unique ID to this machine instruction. 683 mutable const MachineInstr *LastMI = nullptr; 684 mutable unsigned LastFn = 0; 685 mutable unsigned Counter = ~0U; 686 687 /// This method emits the header for the current function. 688 virtual void emitFunctionHeader(); 689 690 /// This method emits a comment next to header for the current function. 691 virtual void emitFunctionHeaderComment(); 692 693 /// Emit a blob of inline asm to the output streamer. 694 void 695 emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, 696 const MCTargetOptions &MCOptions, 697 const MDNode *LocMDNode = nullptr, 698 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const; 699 700 /// This method formats and emits the specified machine instruction that is an 701 /// inline asm. 702 void emitInlineAsm(const MachineInstr *MI) const; 703 704 /// Add inline assembly info to the diagnostics machinery, so we can 705 /// emit file and position info. Returns SrcMgr memory buffer position. 706 unsigned addInlineAsmDiagBuffer(StringRef AsmStr, 707 const MDNode *LocMDNode) const; 708 709 //===------------------------------------------------------------------===// 710 // Internal Implementation Details 711 //===------------------------------------------------------------------===// 712 713 void emitJumpTableEntry(const MachineJumpTableInfo *MJTI, 714 const MachineBasicBlock *MBB, unsigned uid) const; 715 void emitLLVMUsedList(const ConstantArray *InitList); 716 /// Emit llvm.ident metadata in an '.ident' directive. 717 void emitModuleIdents(Module &M); 718 /// Emit bytes for llvm.commandline metadata. 719 void emitModuleCommandLines(Module &M); 720 void emitXXStructorList(const DataLayout &DL, const Constant *List, 721 bool isCtor); 722 723 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S); 724 /// Emit GlobalAlias or GlobalIFunc. 725 void emitGlobalIndirectSymbol(Module &M, const GlobalIndirectSymbol &GIS); 726 }; 727 728 } // end namespace llvm 729 730 #endif // LLVM_CODEGEN_ASMPRINTER_H 731