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/BinaryFormat/Dwarf.h" 21 #include "llvm/CodeGen/AsmPrinterHandler.h" 22 #include "llvm/CodeGen/DwarfStringPoolEntry.h" 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/IR/InlineAsm.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include <cstdint> 27 #include <memory> 28 #include <utility> 29 #include <vector> 30 31 namespace llvm { 32 33 class AddrLabelMap; 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 GlobalObject; 45 class GlobalValue; 46 class GlobalVariable; 47 class MachineBasicBlock; 48 class MachineConstantPoolValue; 49 class MachineDominatorTree; 50 class MachineFunction; 51 class MachineInstr; 52 class MachineJumpTableInfo; 53 class MachineLoopInfo; 54 class MachineModuleInfo; 55 class MachineOptimizationRemarkEmitter; 56 class MCAsmInfo; 57 class MCCFIInstruction; 58 class MCContext; 59 class MCExpr; 60 class MCInst; 61 class MCSection; 62 class MCStreamer; 63 class MCSubtargetInfo; 64 class MCSymbol; 65 class MCTargetOptions; 66 class MDNode; 67 class Module; 68 class PseudoProbeHandler; 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 /// struct HandlerInfo and Handlers permit users or target extended 143 /// AsmPrinter to add their own handlers. 144 struct HandlerInfo { 145 std::unique_ptr<AsmPrinterHandler> Handler; 146 StringRef TimerName; 147 StringRef TimerDescription; 148 StringRef TimerGroupName; 149 StringRef TimerGroupDescription; 150 151 HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler, StringRef TimerName, 152 StringRef TimerDescription, StringRef TimerGroupName, 153 StringRef TimerGroupDescription) 154 : Handler(std::move(Handler)), TimerName(TimerName), 155 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName), 156 TimerGroupDescription(TimerGroupDescription) {} 157 }; 158 159 // Flags representing which CFI section is required for a function/module. 160 enum class CFISection : unsigned { 161 None = 0, ///< Do not emit either .eh_frame or .debug_frame 162 EH = 1, ///< Emit .eh_frame 163 Debug = 2 ///< Emit .debug_frame 164 }; 165 166 private: 167 MCSymbol *CurrentFnEnd = nullptr; 168 169 /// Map a basic block section ID to the exception symbol associated with that 170 /// section. Map entries are assigned and looked up via 171 /// AsmPrinter::getMBBExceptionSym. 172 DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms; 173 174 // The symbol used to represent the start of the current BB section of the 175 // function. This is used to calculate the size of the BB section. 176 MCSymbol *CurrentSectionBeginSym = nullptr; 177 178 /// This map keeps track of which symbol is being used for the specified basic 179 /// block's address of label. 180 std::unique_ptr<AddrLabelMap> AddrLabelSymbols; 181 182 // The garbage collection metadata printer table. 183 void *GCMetadataPrinters = nullptr; // Really a DenseMap. 184 185 /// Emit comments in assembly output if this is true. 186 bool VerboseAsm; 187 188 /// Output stream for the stack usage file (i.e., .su file). 189 std::unique_ptr<raw_fd_ostream> StackUsageStream; 190 191 static char ID; 192 193 protected: 194 MCSymbol *CurrentFnBegin = nullptr; 195 196 /// A vector of all debug/EH info emitters we should use. This vector 197 /// maintains ownership of the emitters. 198 std::vector<HandlerInfo> Handlers; 199 size_t NumUserHandlers = 0; 200 201 private: 202 /// If generated on the fly this own the instance. 203 std::unique_ptr<MachineDominatorTree> OwnedMDT; 204 205 /// If generated on the fly this own the instance. 206 std::unique_ptr<MachineLoopInfo> OwnedMLI; 207 208 /// If the target supports dwarf debug info, this pointer is non-null. 209 DwarfDebug *DD = nullptr; 210 211 /// A handler that supports pseudo probe emission with embedded inline 212 /// context. 213 PseudoProbeHandler *PP = nullptr; 214 215 /// CFISection type the module needs i.e. either .eh_frame or .debug_frame. 216 CFISection ModuleCFISection = CFISection::None; 217 218 /// True if the module contains split-stack functions. This is used to 219 /// emit .note.GNU-split-stack section as required by the linker for 220 /// special handling split-stack function calling no-split-stack function. 221 bool HasSplitStack = false; 222 223 /// True if the module contains no-split-stack functions. This is used to emit 224 /// .note.GNU-no-split-stack section when it also contains functions without a 225 /// split stack prologue. 226 bool HasNoSplitStack = false; 227 228 protected: 229 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer); 230 231 public: 232 ~AsmPrinter() override; 233 234 DwarfDebug *getDwarfDebug() { return DD; } 235 DwarfDebug *getDwarfDebug() const { return DD; } 236 237 uint16_t getDwarfVersion() const; 238 void setDwarfVersion(uint16_t Version); 239 240 bool isDwarf64() const; 241 242 /// Returns 4 for DWARF32 and 8 for DWARF64. 243 unsigned int getDwarfOffsetByteSize() const; 244 245 /// Returns 4 for DWARF32 and 12 for DWARF64. 246 unsigned int getUnitLengthFieldByteSize() const; 247 248 /// Returns information about the byte size of DW_FORM values. 249 dwarf::FormParams getDwarfFormParams() const; 250 251 bool isPositionIndependent() const; 252 253 /// Return true if assembly output should contain comments. 254 bool isVerbose() const { return VerboseAsm; } 255 256 /// Return a unique ID for the current function. 257 unsigned getFunctionNumber() const; 258 259 /// Return symbol for the function pseudo stack if the stack frame is not a 260 /// register based. 261 virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; } 262 263 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; } 264 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; } 265 266 // Return the exception symbol associated with the MBB section containing a 267 // given basic block. 268 MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB); 269 270 /// Return the symbol to be used for the specified basic block when its 271 /// address is taken. This cannot be its normal LBB label because the block 272 /// may be accessed outside its containing function. 273 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) { 274 return getAddrLabelSymbolToEmit(BB).front(); 275 } 276 277 /// Return the symbol to be used for the specified basic block when its 278 /// address is taken. If other blocks were RAUW'd to this one, we may have 279 /// to emit them as well, return the whole set. 280 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB); 281 282 /// If the specified function has had any references to address-taken blocks 283 /// generated, but the block got deleted, return the symbol now so we can 284 /// emit it. This prevents emitting a reference to a symbol that has no 285 /// definition. 286 void takeDeletedSymbolsForFunction(const Function *F, 287 std::vector<MCSymbol *> &Result); 288 289 /// Return information about object file lowering. 290 const TargetLoweringObjectFile &getObjFileLowering() const; 291 292 /// Return information about data layout. 293 const DataLayout &getDataLayout() const; 294 295 /// Return the pointer size from the TargetMachine 296 unsigned getPointerSize() const; 297 298 /// Return information about subtarget. 299 const MCSubtargetInfo &getSubtargetInfo() const; 300 301 void EmitToStreamer(MCStreamer &S, const MCInst &Inst); 302 303 /// Emits inital debug location directive. 304 void emitInitialRawDwarfLocDirective(const MachineFunction &MF); 305 306 /// Return the current section we are emitting to. 307 const MCSection *getCurrentSection() const; 308 309 void getNameWithPrefix(SmallVectorImpl<char> &Name, 310 const GlobalValue *GV) const; 311 312 MCSymbol *getSymbol(const GlobalValue *GV) const; 313 314 /// Similar to getSymbol() but preferred for references. On ELF, this uses a 315 /// local symbol if a reference to GV is guaranteed to be resolved to the 316 /// definition in the same module. 317 MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const; 318 319 //===------------------------------------------------------------------===// 320 // XRay instrumentation implementation. 321 //===------------------------------------------------------------------===// 322 public: 323 // This describes the kind of sled we're storing in the XRay table. 324 enum class SledKind : uint8_t { 325 FUNCTION_ENTER = 0, 326 FUNCTION_EXIT = 1, 327 TAIL_CALL = 2, 328 LOG_ARGS_ENTER = 3, 329 CUSTOM_EVENT = 4, 330 TYPED_EVENT = 5, 331 }; 332 333 // The table will contain these structs that point to the sled, the function 334 // containing the sled, and what kind of sled (and whether they should always 335 // be instrumented). We also use a version identifier that the runtime can use 336 // to decide what to do with the sled, depending on the version of the sled. 337 struct XRayFunctionEntry { 338 const MCSymbol *Sled; 339 const MCSymbol *Function; 340 SledKind Kind; 341 bool AlwaysInstrument; 342 const class Function *Fn; 343 uint8_t Version; 344 345 void emit(int, MCStreamer *) const; 346 }; 347 348 // All the sleds to be emitted. 349 SmallVector<XRayFunctionEntry, 4> Sleds; 350 351 // Helper function to record a given XRay sled. 352 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind, 353 uint8_t Version = 0); 354 355 /// Emit a table with all XRay instrumentation points. 356 void emitXRayTable(); 357 358 void emitPatchableFunctionEntries(); 359 360 //===------------------------------------------------------------------===// 361 // MachineFunctionPass Implementation. 362 //===------------------------------------------------------------------===// 363 364 /// Record analysis usage. 365 void getAnalysisUsage(AnalysisUsage &AU) const override; 366 367 /// Set up the AsmPrinter when we are working on a new module. If your pass 368 /// overrides this, it must make sure to explicitly call this implementation. 369 bool doInitialization(Module &M) override; 370 371 /// Shut down the asmprinter. If you override this in your pass, you must make 372 /// sure to call it explicitly. 373 bool doFinalization(Module &M) override; 374 375 /// Emit the specified function out to the OutStreamer. 376 bool runOnMachineFunction(MachineFunction &MF) override { 377 SetupMachineFunction(MF); 378 emitFunctionBody(); 379 return false; 380 } 381 382 //===------------------------------------------------------------------===// 383 // Coarse grained IR lowering routines. 384 //===------------------------------------------------------------------===// 385 386 /// This should be called when a new MachineFunction is being processed from 387 /// runOnMachineFunction. 388 virtual void SetupMachineFunction(MachineFunction &MF); 389 390 /// This method emits the body and trailer for a function. 391 void emitFunctionBody(); 392 393 void emitCFIInstruction(const MachineInstr &MI); 394 395 void emitFrameAlloc(const MachineInstr &MI); 396 397 void emitStackSizeSection(const MachineFunction &MF); 398 399 void emitStackUsage(const MachineFunction &MF); 400 401 void emitBBAddrMapSection(const MachineFunction &MF); 402 403 void emitPseudoProbe(const MachineInstr &MI); 404 405 void emitRemarksSection(remarks::RemarkStreamer &RS); 406 407 /// Get the CFISection type for a function. 408 CFISection getFunctionCFISectionType(const Function &F) const; 409 410 /// Get the CFISection type for a function. 411 CFISection getFunctionCFISectionType(const MachineFunction &MF) const; 412 413 /// Get the CFISection type for the module. 414 CFISection getModuleCFISectionType() const { return ModuleCFISection; } 415 416 bool needsSEHMoves(); 417 418 /// Since emitting CFI unwind information is entangled with supporting the 419 /// exceptions, this returns true for platforms which use CFI unwind 420 /// information for debugging purpose when 421 /// `MCAsmInfo::ExceptionsType == ExceptionHandling::None`. 422 bool needsCFIForDebug() const; 423 424 /// Print to the current output stream assembly representations of the 425 /// constants in the constant pool MCP. This is used to print out constants 426 /// which have been "spilled to memory" by the code generator. 427 virtual void emitConstantPool(); 428 429 /// Print assembly representations of the jump tables used by the current 430 /// function to the current output stream. 431 virtual void emitJumpTableInfo(); 432 433 /// Emit the specified global variable to the .s file. 434 virtual void emitGlobalVariable(const GlobalVariable *GV); 435 436 /// Check to see if the specified global is a special global used by LLVM. If 437 /// so, emit it and return true, otherwise do nothing and return false. 438 bool emitSpecialLLVMGlobal(const GlobalVariable *GV); 439 440 /// `llvm.global_ctors` and `llvm.global_dtors` are arrays of Structor 441 /// structs. 442 /// 443 /// Priority - init priority 444 /// Func - global initialization or global clean-up function 445 /// ComdatKey - associated data 446 struct Structor { 447 int Priority = 0; 448 Constant *Func = nullptr; 449 GlobalValue *ComdatKey = nullptr; 450 451 Structor() = default; 452 }; 453 454 /// This method gathers an array of Structors and then sorts them out by 455 /// Priority. 456 /// @param List The initializer of `llvm.global_ctors` or `llvm.global_dtors` 457 /// array. 458 /// @param[out] Structors Sorted Structor structs by Priority. 459 void preprocessXXStructorList(const DataLayout &DL, const Constant *List, 460 SmallVector<Structor, 8> &Structors); 461 462 /// This method emits `llvm.global_ctors` or `llvm.global_dtors` list. 463 virtual void emitXXStructorList(const DataLayout &DL, const Constant *List, 464 bool IsCtor); 465 466 /// Emit an alignment directive to the specified power of two boundary. If a 467 /// global value is specified, and if that global has an explicit alignment 468 /// requested, it will override the alignment request if required for 469 /// correctness. 470 void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr, 471 unsigned MaxBytesToEmit = 0) const; 472 473 /// Lower the specified LLVM Constant to an MCExpr. 474 virtual const MCExpr *lowerConstant(const Constant *CV); 475 476 /// Print a general LLVM constant to the .s file. 477 void emitGlobalConstant(const DataLayout &DL, const Constant *CV); 478 479 /// Unnamed constant global variables solely contaning a pointer to 480 /// another globals variable act like a global variable "proxy", or GOT 481 /// equivalents, i.e., it's only used to hold the address of the latter. One 482 /// optimization is to replace accesses to these proxies by using the GOT 483 /// entry for the final global instead. Hence, we select GOT equivalent 484 /// candidates among all the module global variables, avoid emitting them 485 /// unnecessarily and finally replace references to them by pc relative 486 /// accesses to GOT entries. 487 void computeGlobalGOTEquivs(Module &M); 488 489 /// Constant expressions using GOT equivalent globals may not be 490 /// eligible for PC relative GOT entry conversion, in such cases we need to 491 /// emit the proxies we previously omitted in EmitGlobalVariable. 492 void emitGlobalGOTEquivs(); 493 494 /// Emit the stack maps. 495 void emitStackMaps(StackMaps &SM); 496 497 //===------------------------------------------------------------------===// 498 // Overridable Hooks 499 //===------------------------------------------------------------------===// 500 501 void addAsmPrinterHandler(HandlerInfo Handler) { 502 Handlers.insert(Handlers.begin(), std::move(Handler)); 503 NumUserHandlers++; 504 } 505 506 // Targets can, or in the case of EmitInstruction, must implement these to 507 // customize output. 508 509 /// This virtual method can be overridden by targets that want to emit 510 /// something at the start of their file. 511 virtual void emitStartOfAsmFile(Module &) {} 512 513 /// This virtual method can be overridden by targets that want to emit 514 /// something at the end of their file. 515 virtual void emitEndOfAsmFile(Module &) {} 516 517 /// Targets can override this to emit stuff before the first basic block in 518 /// the function. 519 virtual void emitFunctionBodyStart() {} 520 521 /// Targets can override this to emit stuff after the last basic block in the 522 /// function. 523 virtual void emitFunctionBodyEnd() {} 524 525 /// Targets can override this to emit stuff at the start of a basic block. 526 /// By default, this method prints the label for the specified 527 /// MachineBasicBlock, an alignment (if present) and a comment describing it 528 /// if appropriate. 529 virtual void emitBasicBlockStart(const MachineBasicBlock &MBB); 530 531 /// Targets can override this to emit stuff at the end of a basic block. 532 virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB); 533 534 /// Targets should implement this to emit instructions. 535 virtual void emitInstruction(const MachineInstr *) { 536 llvm_unreachable("EmitInstruction not implemented"); 537 } 538 539 /// Return the symbol for the specified constant pool entry. 540 virtual MCSymbol *GetCPISymbol(unsigned CPID) const; 541 542 virtual void emitFunctionEntryLabel(); 543 544 virtual void emitFunctionDescriptor() { 545 llvm_unreachable("Function descriptor is target-specific."); 546 } 547 548 virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 549 550 /// Targets can override this to change how global constants that are part of 551 /// a C++ static/global constructor list are emitted. 552 virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) { 553 emitGlobalConstant(DL, CV); 554 } 555 556 /// Return true if the basic block has exactly one predecessor and the control 557 /// transfer mechanism between the predecessor and this block is a 558 /// fall-through. 559 virtual bool 560 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; 561 562 /// Targets can override this to customize the output of IMPLICIT_DEF 563 /// instructions in verbose mode. 564 virtual void emitImplicitDef(const MachineInstr *MI) const; 565 566 /// Emit N NOP instructions. 567 void emitNops(unsigned N); 568 569 //===------------------------------------------------------------------===// 570 // Symbol Lowering Routines. 571 //===------------------------------------------------------------------===// 572 573 MCSymbol *createTempSymbol(const Twine &Name) const; 574 575 /// Return the MCSymbol for a private symbol with global value name as its 576 /// base, with the specified suffix. 577 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, 578 StringRef Suffix) const; 579 580 /// Return the MCSymbol for the specified ExternalSymbol. 581 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 582 583 /// Return the symbol for the specified jump table entry. 584 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; 585 586 /// Return the symbol for the specified jump table .set 587 /// FIXME: privatize to AsmPrinter. 588 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; 589 590 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified 591 /// basic block. 592 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; 593 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; 594 595 //===------------------------------------------------------------------===// 596 // Emission Helper Routines. 597 //===------------------------------------------------------------------===// 598 599 /// This is just convenient handler for printing offsets. 600 void printOffset(int64_t Offset, raw_ostream &OS) const; 601 602 /// Emit a byte directive and value. 603 void emitInt8(int Value) const; 604 605 /// Emit a short directive and value. 606 void emitInt16(int Value) const; 607 608 /// Emit a long directive and value. 609 void emitInt32(int Value) const; 610 611 /// Emit a long long directive and value. 612 void emitInt64(uint64_t Value) const; 613 614 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive 615 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses 616 /// .set if it is available. 617 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, 618 unsigned Size) const; 619 620 /// Emit something like ".uleb128 Hi-Lo". 621 void emitLabelDifferenceAsULEB128(const MCSymbol *Hi, 622 const MCSymbol *Lo) const; 623 624 /// Emit something like ".long Label+Offset" where the size in bytes of the 625 /// directive is specified by Size and Label specifies the label. This 626 /// implicitly uses .set if it is available. 627 void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, 628 unsigned Size, bool IsSectionRelative = false) const; 629 630 /// Emit something like ".long Label" where the size in bytes of the directive 631 /// is specified by Size and Label specifies the label. 632 void emitLabelReference(const MCSymbol *Label, unsigned Size, 633 bool IsSectionRelative = false) const { 634 emitLabelPlusOffset(Label, 0, Size, IsSectionRelative); 635 } 636 637 //===------------------------------------------------------------------===// 638 // Dwarf Emission Helper Routines 639 //===------------------------------------------------------------------===// 640 641 /// Emit the specified signed leb128 value. 642 void emitSLEB128(int64_t Value, const char *Desc = nullptr) const; 643 644 /// Emit the specified unsigned leb128 value. 645 void emitULEB128(uint64_t Value, const char *Desc = nullptr, 646 unsigned PadTo = 0) const; 647 648 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose 649 /// assembly output is enabled, we output comments describing the encoding. 650 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA"). 651 void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const; 652 653 /// Return the size of the encoding in bytes. 654 unsigned GetSizeOfEncodedValue(unsigned Encoding) const; 655 656 /// Emit reference to a ttype global with a specified encoding. 657 virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding); 658 659 /// Emit a reference to a symbol for use in dwarf. Different object formats 660 /// represent this in different ways. Some use a relocation others encode 661 /// the label offset in its section. 662 void emitDwarfSymbolReference(const MCSymbol *Label, 663 bool ForceOffset = false) const; 664 665 /// Emit the 4- or 8-byte offset of a string from the start of its section. 666 /// 667 /// When possible, emit a DwarfStringPool section offset without any 668 /// relocations, and without using the symbol. Otherwise, defers to \a 669 /// emitDwarfSymbolReference(). 670 /// 671 /// The length of the emitted value depends on the DWARF format. 672 void emitDwarfStringOffset(DwarfStringPoolEntry S) const; 673 674 /// Emit the 4-or 8-byte offset of a string from the start of its section. 675 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const { 676 emitDwarfStringOffset(S.getEntry()); 677 } 678 679 /// Emit something like ".long Label + Offset" or ".quad Label + Offset" 680 /// depending on the DWARF format. 681 void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const; 682 683 /// Emit 32- or 64-bit value depending on the DWARF format. 684 void emitDwarfLengthOrOffset(uint64_t Value) const; 685 686 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen 687 /// according to the settings. 688 void emitDwarfUnitLength(uint64_t Length, const Twine &Comment) const; 689 690 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen 691 /// according to the settings. 692 /// Return the end symbol generated inside, the caller needs to emit it. 693 MCSymbol *emitDwarfUnitLength(const Twine &Prefix, 694 const Twine &Comment) const; 695 696 /// Emit reference to a call site with a specified encoding 697 void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, 698 unsigned Encoding) const; 699 /// Emit an integer value corresponding to the call site encoding 700 void emitCallSiteValue(uint64_t Value, unsigned Encoding) const; 701 702 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified. 703 virtual unsigned getISAEncoding() { return 0; } 704 705 /// Emit the directive and value for debug thread local expression 706 /// 707 /// \p Value - The value to emit. 708 /// \p Size - The size of the integer (in bytes) to emit. 709 virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const; 710 711 //===------------------------------------------------------------------===// 712 // Dwarf Lowering Routines 713 //===------------------------------------------------------------------===// 714 715 /// Emit frame instruction to describe the layout of the frame. 716 void emitCFIInstruction(const MCCFIInstruction &Inst) const; 717 718 /// Emit Dwarf abbreviation table. 719 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const { 720 // For each abbreviation. 721 for (const auto &Abbrev : Abbrevs) 722 emitDwarfAbbrev(*Abbrev); 723 724 // Mark end of abbreviations. 725 emitULEB128(0, "EOM(3)"); 726 } 727 728 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const; 729 730 /// Recursively emit Dwarf DIE tree. 731 void emitDwarfDIE(const DIE &Die) const; 732 733 //===------------------------------------------------------------------===// 734 // Inline Asm Support 735 //===------------------------------------------------------------------===// 736 737 // These are hooks that targets can override to implement inline asm 738 // support. These should probably be moved out of AsmPrinter someday. 739 740 /// Print information related to the specified machine instr that is 741 /// independent of the operand, and may be independent of the instr itself. 742 /// This can be useful for portably encoding the comment character or other 743 /// bits of target-specific knowledge into the asmstrings. The syntax used is 744 /// ${:comment}. Targets can override this to add support for their own 745 /// strange codes. 746 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 747 StringRef Code) const; 748 749 /// Print the MachineOperand as a symbol. Targets with complex handling of 750 /// symbol references should override the base implementation. 751 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS); 752 753 /// Print the specified operand of MI, an INLINEASM instruction, using the 754 /// specified assembler variant. Targets should override this to format as 755 /// appropriate. This method can return true if the operand is erroneous. 756 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 757 const char *ExtraCode, raw_ostream &OS); 758 759 /// Print the specified operand of MI, an INLINEASM instruction, using the 760 /// specified assembler variant as an address. Targets should override this to 761 /// format as appropriate. This method can return true if the operand is 762 /// erroneous. 763 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 764 const char *ExtraCode, raw_ostream &OS); 765 766 /// Let the target do anything it needs to do before emitting inlineasm. 767 /// \p StartInfo - the subtarget info before parsing inline asm 768 virtual void emitInlineAsmStart() const; 769 770 /// Let the target do anything it needs to do after emitting inlineasm. 771 /// This callback can be used restore the original mode in case the 772 /// inlineasm contains directives to switch modes. 773 /// \p StartInfo - the original subtarget info before inline asm 774 /// \p EndInfo - the final subtarget info after parsing the inline asm, 775 /// or NULL if the value is unknown. 776 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, 777 const MCSubtargetInfo *EndInfo) const; 778 779 /// This emits visibility information about symbol, if this is supported by 780 /// the target. 781 void emitVisibility(MCSymbol *Sym, unsigned Visibility, 782 bool IsDefinition = true) const; 783 784 /// This emits linkage information about \p GVSym based on \p GV, if this is 785 /// supported by the target. 786 virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const; 787 788 /// Return the alignment for the specified \p GV. 789 static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL, 790 Align InAlign = Align(1)); 791 792 private: 793 /// Private state for PrintSpecial() 794 // Assign a unique ID to this machine instruction. 795 mutable const MachineInstr *LastMI = nullptr; 796 mutable unsigned LastFn = 0; 797 mutable unsigned Counter = ~0U; 798 799 /// This method emits the header for the current function. 800 virtual void emitFunctionHeader(); 801 802 /// This method emits a comment next to header for the current function. 803 virtual void emitFunctionHeaderComment(); 804 805 /// Emit a blob of inline asm to the output streamer. 806 void 807 emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, 808 const MCTargetOptions &MCOptions, 809 const MDNode *LocMDNode = nullptr, 810 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const; 811 812 /// This method formats and emits the specified machine instruction that is an 813 /// inline asm. 814 void emitInlineAsm(const MachineInstr *MI) const; 815 816 /// Add inline assembly info to the diagnostics machinery, so we can 817 /// emit file and position info. Returns SrcMgr memory buffer position. 818 unsigned addInlineAsmDiagBuffer(StringRef AsmStr, 819 const MDNode *LocMDNode) const; 820 821 //===------------------------------------------------------------------===// 822 // Internal Implementation Details 823 //===------------------------------------------------------------------===// 824 825 void emitJumpTableEntry(const MachineJumpTableInfo *MJTI, 826 const MachineBasicBlock *MBB, unsigned uid) const; 827 void emitLLVMUsedList(const ConstantArray *InitList); 828 /// Emit llvm.ident metadata in an '.ident' directive. 829 void emitModuleIdents(Module &M); 830 /// Emit bytes for llvm.commandline metadata. 831 void emitModuleCommandLines(Module &M); 832 833 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S); 834 void emitGlobalAlias(Module &M, const GlobalAlias &GA); 835 void emitGlobalIFunc(Module &M, const GlobalIFunc &GI); 836 837 /// This method decides whether the specified basic block requires a label. 838 bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const; 839 840 protected: 841 virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const { 842 return false; 843 } 844 }; 845 846 } // end namespace llvm 847 848 #endif // LLVM_CODEGEN_ASMPRINTER_H 849