1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- 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 declares the MCStreamer class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_MC_MCSTREAMER_H 14 #define LLVM_MC_MCSTREAMER_H 15 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/MC/MCDirectives.h" 21 #include "llvm/MC/MCDwarf.h" 22 #include "llvm/MC/MCFragment.h" 23 #include "llvm/MC/MCLinkerOptimizationHint.h" 24 #include "llvm/MC/MCPseudoProbe.h" 25 #include "llvm/MC/MCWinEH.h" 26 #include "llvm/Support/Error.h" 27 #include "llvm/Support/MD5.h" 28 #include "llvm/Support/SMLoc.h" 29 #include "llvm/Support/VersionTuple.h" 30 #include "llvm/TargetParser/ARMTargetParser.h" 31 #include <cassert> 32 #include <cstdint> 33 #include <memory> 34 #include <optional> 35 #include <string> 36 #include <utility> 37 #include <vector> 38 39 namespace llvm { 40 41 class APInt; 42 class AssemblerConstantPools; 43 class MCAsmBackend; 44 class MCAssembler; 45 class MCContext; 46 class MCExpr; 47 class MCFragment; 48 class MCInst; 49 class MCInstPrinter; 50 class MCRegister; 51 class MCSection; 52 class MCStreamer; 53 class MCSubtargetInfo; 54 class MCSymbol; 55 class MCSymbolRefExpr; 56 class Triple; 57 class Twine; 58 class raw_ostream; 59 60 namespace codeview { 61 struct DefRangeRegisterRelHeader; 62 struct DefRangeSubfieldRegisterHeader; 63 struct DefRangeRegisterHeader; 64 struct DefRangeFramePointerRelHeader; 65 } 66 67 using MCSectionSubPair = std::pair<MCSection *, uint32_t>; 68 69 /// Target specific streamer interface. This is used so that targets can 70 /// implement support for target specific assembly directives. 71 /// 72 /// If target foo wants to use this, it should implement 3 classes: 73 /// * FooTargetStreamer : public MCTargetStreamer 74 /// * FooTargetAsmStreamer : public FooTargetStreamer 75 /// * FooTargetELFStreamer : public FooTargetStreamer 76 /// 77 /// FooTargetStreamer should have a pure virtual method for each directive. For 78 /// example, for a ".bar symbol_name" directive, it should have 79 /// virtual emitBar(const MCSymbol &Symbol) = 0; 80 /// 81 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the 82 /// method. The assembly streamer just prints ".bar symbol_name". The object 83 /// streamer does whatever is needed to implement .bar in the object file. 84 /// 85 /// In the assembly printer and parser the target streamer can be used by 86 /// calling getTargetStreamer and casting it to FooTargetStreamer: 87 /// 88 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer(); 89 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS); 90 /// 91 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should 92 /// *never* be treated differently. Callers should always talk to a 93 /// FooTargetStreamer. 94 class MCTargetStreamer { 95 protected: 96 MCStreamer &Streamer; 97 98 public: 99 MCTargetStreamer(MCStreamer &S); 100 virtual ~MCTargetStreamer(); 101 getStreamer()102 MCStreamer &getStreamer() { return Streamer; } 103 104 // Allow a target to add behavior to the EmitLabel of MCStreamer. 105 virtual void emitLabel(MCSymbol *Symbol); 106 // Allow a target to add behavior to the emitAssignment of MCStreamer. 107 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value); 108 109 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, uint64_t Address, 110 const MCInst &Inst, const MCSubtargetInfo &STI, 111 raw_ostream &OS); 112 113 virtual void emitDwarfFileDirective(StringRef Directive); 114 115 /// Update streamer for a new active section. 116 /// 117 /// This is called by popSection and switchSection, if the current 118 /// section changes. 119 virtual void changeSection(const MCSection *CurSection, MCSection *Section, 120 uint32_t SubSection, raw_ostream &OS); 121 122 virtual void emitValue(const MCExpr *Value); 123 124 /// Emit the bytes in \p Data into the output. 125 /// 126 /// This is used to emit bytes in \p Data as sequence of .byte directives. 127 virtual void emitRawBytes(StringRef Data); 128 129 virtual void emitConstantPools(); 130 131 virtual void finish(); 132 }; 133 134 // FIXME: declared here because it is used from 135 // lib/CodeGen/AsmPrinter/ARMException.cpp. 136 class ARMTargetStreamer : public MCTargetStreamer { 137 public: 138 ARMTargetStreamer(MCStreamer &S); 139 ~ARMTargetStreamer() override; 140 141 virtual void emitFnStart(); 142 virtual void emitFnEnd(); 143 virtual void emitCantUnwind(); 144 virtual void emitPersonality(const MCSymbol *Personality); 145 virtual void emitPersonalityIndex(unsigned Index); 146 virtual void emitHandlerData(); 147 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, 148 int64_t Offset = 0); 149 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0); 150 virtual void emitPad(int64_t Offset); 151 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList, 152 bool isVector); 153 virtual void emitUnwindRaw(int64_t StackOffset, 154 const SmallVectorImpl<uint8_t> &Opcodes); 155 156 virtual void switchVendor(StringRef Vendor); 157 virtual void emitAttribute(unsigned Attribute, unsigned Value); 158 virtual void emitTextAttribute(unsigned Attribute, StringRef String); 159 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, 160 StringRef StringValue = ""); 161 virtual void emitFPU(ARM::FPUKind FPU); 162 virtual void emitArch(ARM::ArchKind Arch); 163 virtual void emitArchExtension(uint64_t ArchExt); 164 virtual void emitObjectArch(ARM::ArchKind Arch); 165 void emitTargetAttributes(const MCSubtargetInfo &STI); 166 virtual void finishAttributeSection(); 167 virtual void emitInst(uint32_t Inst, char Suffix = '\0'); 168 169 virtual void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE); 170 171 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value); 172 173 void emitConstantPools() override; 174 175 virtual void emitARMWinCFIAllocStack(unsigned Size, bool Wide); 176 virtual void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide); 177 virtual void emitARMWinCFISaveSP(unsigned Reg); 178 virtual void emitARMWinCFISaveFRegs(unsigned First, unsigned Last); 179 virtual void emitARMWinCFISaveLR(unsigned Offset); 180 virtual void emitARMWinCFIPrologEnd(bool Fragment); 181 virtual void emitARMWinCFINop(bool Wide); 182 virtual void emitARMWinCFIEpilogStart(unsigned Condition); 183 virtual void emitARMWinCFIEpilogEnd(); 184 virtual void emitARMWinCFICustom(unsigned Opcode); 185 186 /// Reset any state between object emissions, i.e. the equivalent of 187 /// MCStreamer's reset method. 188 virtual void reset(); 189 190 /// Callback used to implement the ldr= pseudo. 191 /// Add a new entry to the constant pool for the current section and return an 192 /// MCExpr that can be used to refer to the constant pool location. 193 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc); 194 195 /// Callback used to implement the .ltorg directive. 196 /// Emit contents of constant pool for the current section. 197 void emitCurrentConstantPool(); 198 199 private: 200 std::unique_ptr<AssemblerConstantPools> ConstantPools; 201 }; 202 203 /// Streaming machine code generation interface. 204 /// 205 /// This interface is intended to provide a programmatic interface that is very 206 /// similar to the level that an assembler .s file provides. It has callbacks 207 /// to emit bytes, handle directives, etc. The implementation of this interface 208 /// retains state to know what the current section is etc. 209 /// 210 /// There are multiple implementations of this interface: one for writing out 211 /// a .s file, and implementations that write out .o files of various formats. 212 /// 213 class MCStreamer { 214 MCContext &Context; 215 std::unique_ptr<MCTargetStreamer> TargetStreamer; 216 217 std::vector<MCDwarfFrameInfo> DwarfFrameInfos; 218 // This is a pair of index into DwarfFrameInfos and the MCSection associated 219 // with the frame. Note, we use an index instead of an iterator because they 220 // can be invalidated in std::vector. 221 SmallVector<std::pair<size_t, MCSection *>, 1> FrameInfoStack; 222 MCDwarfFrameInfo *getCurrentDwarfFrameInfo(); 223 224 /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may 225 /// refer to each other, so use std::unique_ptr to provide pointer stability. 226 std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos; 227 228 WinEH::FrameInfo *CurrentWinFrameInfo; 229 size_t CurrentProcWinFrameInfoStartIndex; 230 231 /// This is stack of current and previous section values saved by 232 /// pushSection. 233 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack; 234 235 /// Pointer to the parser's SMLoc if available. This is used to provide 236 /// locations for diagnostics. 237 const SMLoc *StartTokLocPtr = nullptr; 238 239 /// The next unique ID to use when creating a WinCFI-related section (.pdata 240 /// or .xdata). This ID ensures that we have a one-to-one mapping from 241 /// code section to unwind info section, which MSVC's incremental linker 242 /// requires. 243 unsigned NextWinCFIID = 0; 244 245 bool UseAssemblerInfoForParsing = true; 246 247 /// Is the assembler allowed to insert padding automatically? For 248 /// correctness reasons, we sometimes need to ensure instructions aren't 249 /// separated in unexpected ways. At the moment, this feature is only 250 /// useable from an integrated assembler, but assembly syntax is under 251 /// discussion for future inclusion. 252 bool AllowAutoPadding = false; 253 254 protected: 255 MCFragment *CurFrag = nullptr; 256 257 MCStreamer(MCContext &Ctx); 258 259 /// This is called by popSection and switchSection, if the current 260 /// section changes. 261 virtual void changeSection(MCSection *, uint32_t); 262 263 virtual void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame); 264 virtual void emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame); 265 getCurrentWinFrameInfo()266 WinEH::FrameInfo *getCurrentWinFrameInfo() { 267 return CurrentWinFrameInfo; 268 } 269 270 virtual void emitWindowsUnwindTables(WinEH::FrameInfo *Frame); 271 272 virtual void emitWindowsUnwindTables(); 273 274 virtual void emitRawTextImpl(StringRef String); 275 276 /// Returns true if the .cv_loc directive is in the right section. 277 bool checkCVLocSection(unsigned FuncId, unsigned FileNo, SMLoc Loc); 278 279 public: 280 MCStreamer(const MCStreamer &) = delete; 281 MCStreamer &operator=(const MCStreamer &) = delete; 282 virtual ~MCStreamer(); 283 284 void visitUsedExpr(const MCExpr &Expr); 285 virtual void visitUsedSymbol(const MCSymbol &Sym); 286 setTargetStreamer(MCTargetStreamer * TS)287 void setTargetStreamer(MCTargetStreamer *TS) { 288 TargetStreamer.reset(TS); 289 } 290 setStartTokLocPtr(const SMLoc * Loc)291 void setStartTokLocPtr(const SMLoc *Loc) { StartTokLocPtr = Loc; } getStartTokLoc()292 SMLoc getStartTokLoc() const { 293 return StartTokLocPtr ? *StartTokLocPtr : SMLoc(); 294 } 295 296 /// State management 297 /// 298 virtual void reset(); 299 getContext()300 MCContext &getContext() const { return Context; } 301 302 // MCObjectStreamer has an MCAssembler and allows more expression folding at 303 // parse time. getAssemblerPtr()304 virtual MCAssembler *getAssemblerPtr() { return nullptr; } 305 setUseAssemblerInfoForParsing(bool v)306 void setUseAssemblerInfoForParsing(bool v) { UseAssemblerInfoForParsing = v; } getUseAssemblerInfoForParsing()307 bool getUseAssemblerInfoForParsing() { return UseAssemblerInfoForParsing; } 308 getTargetStreamer()309 MCTargetStreamer *getTargetStreamer() { 310 return TargetStreamer.get(); 311 } 312 setAllowAutoPadding(bool v)313 void setAllowAutoPadding(bool v) { AllowAutoPadding = v; } getAllowAutoPadding()314 bool getAllowAutoPadding() const { return AllowAutoPadding; } 315 316 /// When emitting an object file, create and emit a real label. When emitting 317 /// textual assembly, this should do nothing to avoid polluting our output. 318 virtual MCSymbol *emitCFILabel(); 319 320 /// Retrieve the current frame info if one is available and it is not yet 321 /// closed. Otherwise, issue an error and return null. 322 WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc); 323 324 unsigned getNumFrameInfos(); 325 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const; 326 327 bool hasUnfinishedDwarfFrameInfo(); 328 getNumWinFrameInfos()329 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); } getWinFrameInfos()330 ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const { 331 return WinFrameInfos; 332 } 333 334 void generateCompactUnwindEncodings(MCAsmBackend *MAB); 335 336 /// \name Assembly File Formatting. 337 /// @{ 338 339 /// Return true if this streamer supports verbose assembly and if it is 340 /// enabled. isVerboseAsm()341 virtual bool isVerboseAsm() const { return false; } 342 343 /// Return true if this asm streamer supports emitting unformatted text 344 /// to the .s file with EmitRawText. hasRawTextSupport()345 virtual bool hasRawTextSupport() const { return false; } 346 347 /// Is the integrated assembler required for this streamer to function 348 /// correctly? isIntegratedAssemblerRequired()349 virtual bool isIntegratedAssemblerRequired() const { return false; } 350 351 /// Add a textual comment. 352 /// 353 /// Typically for comments that can be emitted to the generated .s 354 /// file if applicable as a QoI issue to make the output of the compiler 355 /// more readable. This only affects the MCAsmStreamer, and only when 356 /// verbose assembly output is enabled. 357 /// 358 /// If the comment includes embedded \n's, they will each get the comment 359 /// prefix as appropriate. The added comment should not end with a \n. 360 /// By default, each comment is terminated with an end of line, i.e. the 361 /// EOL param is set to true by default. If one prefers not to end the 362 /// comment with a new line then the EOL param should be passed 363 /// with a false value. 364 virtual void AddComment(const Twine &T, bool EOL = true) {} 365 366 /// Return a raw_ostream that comments can be written to. Unlike 367 /// AddComment, you are required to terminate comments with \n if you use this 368 /// method. 369 virtual raw_ostream &getCommentOS(); 370 371 /// Print T and prefix it with the comment string (normally #) and 372 /// optionally a tab. This prints the comment immediately, not at the end of 373 /// the current line. It is basically a safe version of EmitRawText: since it 374 /// only prints comments, the object streamer ignores it instead of asserting. 375 virtual void emitRawComment(const Twine &T, bool TabPrefix = true); 376 377 /// Add explicit comment T. T is required to be a valid 378 /// comment in the output and does not need to be escaped. 379 virtual void addExplicitComment(const Twine &T); 380 381 /// Emit added explicit comments. 382 virtual void emitExplicitComments(); 383 384 /// Emit a blank line to a .s file to pretty it up. addBlankLine()385 virtual void addBlankLine() {} 386 387 /// @} 388 389 /// \name Symbol & Section Management 390 /// @{ 391 392 /// Return the current section that the streamer is emitting code to. getCurrentSection()393 MCSectionSubPair getCurrentSection() const { 394 if (!SectionStack.empty()) 395 return SectionStack.back().first; 396 return MCSectionSubPair(); 397 } getCurrentSectionOnly()398 MCSection *getCurrentSectionOnly() const { 399 return CurFrag->getParent(); 400 } 401 402 /// Return the previous section that the streamer is emitting code to. getPreviousSection()403 MCSectionSubPair getPreviousSection() const { 404 if (!SectionStack.empty()) 405 return SectionStack.back().second; 406 return MCSectionSubPair(); 407 } 408 getCurrentFragment()409 MCFragment *getCurrentFragment() const { 410 assert(!getCurrentSection().first || 411 CurFrag->getParent() == getCurrentSection().first); 412 return CurFrag; 413 } 414 415 /// Save the current and previous section on the section stack. pushSection()416 void pushSection() { 417 SectionStack.push_back( 418 std::make_pair(getCurrentSection(), getPreviousSection())); 419 } 420 421 /// Restore the current and previous section from the section stack. 422 /// Calls changeSection as needed. 423 /// 424 /// Returns false if the stack was empty. 425 bool popSection(); 426 427 /// Set the current section where code is being emitted to \p Section. This 428 /// is required to update CurSection. 429 /// 430 /// This corresponds to assembler directives like .section, .text, etc. 431 virtual void switchSection(MCSection *Section, uint32_t Subsec = 0); 432 bool switchSection(MCSection *Section, const MCExpr *); 433 434 /// Similar to switchSection, but does not print the section directive. 435 virtual void switchSectionNoPrint(MCSection *Section); 436 437 /// Create the default sections and set the initial one. 438 virtual void initSections(bool NoExecStack, const MCSubtargetInfo &STI); 439 440 MCSymbol *endSection(MCSection *Section); 441 442 /// Returns the mnemonic for \p MI, if the streamer has access to a 443 /// instruction printer and returns an empty string otherwise. getMnemonic(MCInst & MI)444 virtual StringRef getMnemonic(MCInst &MI) { return ""; } 445 446 /// Emit a label for \p Symbol into the current section. 447 /// 448 /// This corresponds to an assembler statement such as: 449 /// foo: 450 /// 451 /// \param Symbol - The symbol to emit. A given symbol should only be 452 /// emitted as a label once, and symbols emitted as a label should never be 453 /// used in an assignment. 454 // FIXME: These emission are non-const because we mutate the symbol to 455 // add the section we're emitting it to later. 456 virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()); 457 458 virtual void emitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol); 459 460 /// Note in the output the specified \p Flag. 461 virtual void emitAssemblerFlag(MCAssemblerFlag Flag); 462 463 /// Emit the given list \p Options of strings as linker 464 /// options into the output. emitLinkerOptions(ArrayRef<std::string> Kind)465 virtual void emitLinkerOptions(ArrayRef<std::string> Kind) {} 466 467 /// Note in the output the specified region \p Kind. emitDataRegion(MCDataRegionType Kind)468 virtual void emitDataRegion(MCDataRegionType Kind) {} 469 470 /// Specify the Mach-O minimum deployment target version. emitVersionMin(MCVersionMinType Type,unsigned Major,unsigned Minor,unsigned Update,VersionTuple SDKVersion)471 virtual void emitVersionMin(MCVersionMinType Type, unsigned Major, 472 unsigned Minor, unsigned Update, 473 VersionTuple SDKVersion) {} 474 475 /// Emit/Specify Mach-O build version command. 476 /// \p Platform should be one of MachO::PlatformType. emitBuildVersion(unsigned Platform,unsigned Major,unsigned Minor,unsigned Update,VersionTuple SDKVersion)477 virtual void emitBuildVersion(unsigned Platform, unsigned Major, 478 unsigned Minor, unsigned Update, 479 VersionTuple SDKVersion) {} 480 emitDarwinTargetVariantBuildVersion(unsigned Platform,unsigned Major,unsigned Minor,unsigned Update,VersionTuple SDKVersion)481 virtual void emitDarwinTargetVariantBuildVersion(unsigned Platform, 482 unsigned Major, 483 unsigned Minor, 484 unsigned Update, 485 VersionTuple SDKVersion) {} 486 487 void emitVersionForTarget(const Triple &Target, 488 const VersionTuple &SDKVersion, 489 const Triple *DarwinTargetVariantTriple, 490 const VersionTuple &DarwinTargetVariantSDKVersion); 491 492 /// Note in the output that the specified \p Func is a Thumb mode 493 /// function (ARM target only). 494 virtual void emitThumbFunc(MCSymbol *Func); 495 496 /// Emit an assignment of \p Value to \p Symbol. 497 /// 498 /// This corresponds to an assembler statement such as: 499 /// symbol = value 500 /// 501 /// The assignment generates no code, but has the side effect of binding the 502 /// value in the current context. For the assembly streamer, this prints the 503 /// binding into the .s file. 504 /// 505 /// \param Symbol - The symbol being assigned to. 506 /// \param Value - The value for the symbol. 507 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value); 508 509 /// Emit an assignment of \p Value to \p Symbol, but only if \p Value is also 510 /// emitted. 511 virtual void emitConditionalAssignment(MCSymbol *Symbol, const MCExpr *Value); 512 513 /// Emit an weak reference from \p Alias to \p Symbol. 514 /// 515 /// This corresponds to an assembler statement such as: 516 /// .weakref alias, symbol 517 /// 518 /// \param Alias - The alias that is being created. 519 /// \param Symbol - The symbol being aliased. 520 virtual void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); 521 522 /// Add the given \p Attribute to \p Symbol. 523 virtual bool emitSymbolAttribute(MCSymbol *Symbol, 524 MCSymbolAttr Attribute) = 0; 525 526 /// Set the \p DescValue for the \p Symbol. 527 /// 528 /// \param Symbol - The symbol to have its n_desc field set. 529 /// \param DescValue - The value to set into the n_desc field. 530 virtual void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 531 532 /// Start emitting COFF symbol definition 533 /// 534 /// \param Symbol - The symbol to have its External & Type fields set. 535 virtual void beginCOFFSymbolDef(const MCSymbol *Symbol); 536 537 /// Emit the storage class of the symbol. 538 /// 539 /// \param StorageClass - The storage class the symbol should have. 540 virtual void emitCOFFSymbolStorageClass(int StorageClass); 541 542 /// Emit the type of the symbol. 543 /// 544 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h) 545 virtual void emitCOFFSymbolType(int Type); 546 547 /// Marks the end of the symbol definition. 548 virtual void endCOFFSymbolDef(); 549 550 virtual void emitCOFFSafeSEH(MCSymbol const *Symbol); 551 552 /// Emits the symbol table index of a Symbol into the current section. 553 virtual void emitCOFFSymbolIndex(MCSymbol const *Symbol); 554 555 /// Emits a COFF section index. 556 /// 557 /// \param Symbol - Symbol the section number relocation should point to. 558 virtual void emitCOFFSectionIndex(MCSymbol const *Symbol); 559 560 /// Emits a COFF section relative relocation. 561 /// 562 /// \param Symbol - Symbol the section relative relocation should point to. 563 virtual void emitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset); 564 565 /// Emits a COFF image relative relocation. 566 /// 567 /// \param Symbol - Symbol the image relative relocation should point to. 568 virtual void emitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset); 569 570 /// Emits an lcomm directive with XCOFF csect information. 571 /// 572 /// \param LabelSym - Label on the block of storage. 573 /// \param Size - The size of the block of storage. 574 /// \param CsectSym - Csect name for the block of storage. 575 /// \param Alignment - The alignment of the symbol in bytes. 576 virtual void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, 577 MCSymbol *CsectSym, Align Alignment); 578 579 /// Emit a symbol's linkage and visibility with a linkage directive for XCOFF. 580 /// 581 /// \param Symbol - The symbol to emit. 582 /// \param Linkage - The linkage of the symbol to emit. 583 /// \param Visibility - The visibility of the symbol to emit or MCSA_Invalid 584 /// if the symbol does not have an explicit visibility. 585 virtual void emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol, 586 MCSymbolAttr Linkage, 587 MCSymbolAttr Visibility); 588 589 /// Emit a XCOFF .rename directive which creates a synonym for an illegal or 590 /// undesirable name. 591 /// 592 /// \param Name - The name used internally in the assembly for references to 593 /// the symbol. 594 /// \param Rename - The value to which the Name parameter is 595 /// changed at the end of assembly. 596 virtual void emitXCOFFRenameDirective(const MCSymbol *Name, StringRef Rename); 597 598 /// Emit an XCOFF .except directive which adds information about 599 /// a trap instruction to the object file exception section 600 /// 601 /// \param Symbol - The function containing the trap. 602 /// \param Lang - The language code for the exception entry. 603 /// \param Reason - The reason code for the exception entry. 604 virtual void emitXCOFFExceptDirective(const MCSymbol *Symbol, 605 const MCSymbol *Trap, 606 unsigned Lang, unsigned Reason, 607 unsigned FunctionSize, bool hasDebug); 608 609 /// Emit a XCOFF .ref directive which creates R_REF type entry in the 610 /// relocation table for one or more symbols. 611 /// 612 /// \param Sym - The symbol on the .ref directive. 613 virtual void emitXCOFFRefDirective(const MCSymbol *Symbol); 614 615 /// Emit a C_INFO symbol with XCOFF embedded metadata to the .info section. 616 /// 617 /// \param Name - The embedded metadata name 618 /// \param Metadata - The embedded metadata 619 virtual void emitXCOFFCInfoSym(StringRef Name, StringRef Metadata); 620 621 /// Emit an ELF .size directive. 622 /// 623 /// This corresponds to an assembler statement such as: 624 /// .size symbol, expression 625 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value); 626 627 /// Emit an ELF .symver directive. 628 /// 629 /// This corresponds to an assembler statement such as: 630 /// .symver _start, foo@@SOME_VERSION 631 virtual void emitELFSymverDirective(const MCSymbol *OriginalSym, 632 StringRef Name, bool KeepOriginalSym); 633 634 /// Emit a Linker Optimization Hint (LOH) directive. 635 /// \param Args - Arguments of the LOH. emitLOHDirective(MCLOHType Kind,const MCLOHArgs & Args)636 virtual void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {} 637 638 /// Emit a .gnu_attribute directive. emitGNUAttribute(unsigned Tag,unsigned Value)639 virtual void emitGNUAttribute(unsigned Tag, unsigned Value) {} 640 641 /// Emit a common symbol. 642 /// 643 /// \param Symbol - The common symbol to emit. 644 /// \param Size - The size of the common symbol. 645 /// \param ByteAlignment - The alignment of the symbol. 646 virtual void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 647 Align ByteAlignment) = 0; 648 649 /// Emit a local common (.lcomm) symbol. 650 /// 651 /// \param Symbol - The common symbol to emit. 652 /// \param Size - The size of the common symbol. 653 /// \param ByteAlignment - The alignment of the common symbol in bytes. 654 virtual void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, 655 Align ByteAlignment); 656 657 /// Emit the zerofill section and an optional symbol. 658 /// 659 /// \param Section - The zerofill section to create and or to put the symbol 660 /// \param Symbol - The zerofill symbol to emit, if non-NULL. 661 /// \param Size - The size of the zerofill symbol. 662 /// \param ByteAlignment - The alignment of the zerofill symbol. 663 virtual void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, 664 uint64_t Size = 0, Align ByteAlignment = Align(1), 665 SMLoc Loc = SMLoc()) = 0; 666 667 /// Emit a thread local bss (.tbss) symbol. 668 /// 669 /// \param Section - The thread local common section. 670 /// \param Symbol - The thread local common symbol to emit. 671 /// \param Size - The size of the symbol. 672 /// \param ByteAlignment - The alignment of the thread local common symbol. 673 virtual void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 674 uint64_t Size, Align ByteAlignment = Align(1)); 675 676 /// @} 677 /// \name Generating Data 678 /// @{ 679 680 /// Emit the bytes in \p Data into the output. 681 /// 682 /// This is used to implement assembler directives such as .byte, .ascii, 683 /// etc. 684 virtual void emitBytes(StringRef Data); 685 686 /// Functionally identical to EmitBytes. When emitting textual assembly, this 687 /// method uses .byte directives instead of .ascii or .asciz for readability. 688 virtual void emitBinaryData(StringRef Data); 689 690 /// Emit the expression \p Value into the output as a native 691 /// integer of the given \p Size bytes. 692 /// 693 /// This is used to implement assembler directives such as .word, .quad, 694 /// etc. 695 /// 696 /// \param Value - The value to emit. 697 /// \param Size - The size of the integer (in bytes) to emit. This must 698 /// match a native machine width. 699 /// \param Loc - The location of the expression for error reporting. 700 virtual void emitValueImpl(const MCExpr *Value, unsigned Size, 701 SMLoc Loc = SMLoc()); 702 703 void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc()); 704 705 /// Special case of EmitValue that avoids the client having 706 /// to pass in a MCExpr for constant integers. 707 virtual void emitIntValue(uint64_t Value, unsigned Size); 708 virtual void emitIntValue(const APInt &Value); 709 710 /// Special case of EmitValue that avoids the client having to pass 711 /// in a MCExpr for constant integers & prints in Hex format for certain 712 /// modes. emitIntValueInHex(uint64_t Value,unsigned Size)713 virtual void emitIntValueInHex(uint64_t Value, unsigned Size) { 714 emitIntValue(Value, Size); 715 } 716 emitInt8(uint64_t Value)717 void emitInt8(uint64_t Value) { emitIntValue(Value, 1); } emitInt16(uint64_t Value)718 void emitInt16(uint64_t Value) { emitIntValue(Value, 2); } emitInt32(uint64_t Value)719 void emitInt32(uint64_t Value) { emitIntValue(Value, 4); } emitInt64(uint64_t Value)720 void emitInt64(uint64_t Value) { emitIntValue(Value, 8); } 721 722 /// Special case of EmitValue that avoids the client having to pass 723 /// in a MCExpr for constant integers & prints in Hex format for certain 724 /// modes, pads the field with leading zeros to Size width emitIntValueInHexWithPadding(uint64_t Value,unsigned Size)725 virtual void emitIntValueInHexWithPadding(uint64_t Value, unsigned Size) { 726 emitIntValue(Value, Size); 727 } 728 729 virtual void emitULEB128Value(const MCExpr *Value); 730 731 virtual void emitSLEB128Value(const MCExpr *Value); 732 733 /// Special case of EmitULEB128Value that avoids the client having to 734 /// pass in a MCExpr for constant integers. 735 unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo = 0); 736 737 /// Special case of EmitSLEB128Value that avoids the client having to 738 /// pass in a MCExpr for constant integers. 739 unsigned emitSLEB128IntValue(int64_t Value); 740 741 /// Special case of EmitValue that avoids the client having to pass in 742 /// a MCExpr for MCSymbols. 743 void emitSymbolValue(const MCSymbol *Sym, unsigned Size, 744 bool IsSectionRelative = false); 745 746 /// Emit the expression \p Value into the output as a dtprel 747 /// (64-bit DTP relative) value. 748 /// 749 /// This is used to implement assembler directives such as .dtpreldword on 750 /// targets that support them. 751 virtual void emitDTPRel64Value(const MCExpr *Value); 752 753 /// Emit the expression \p Value into the output as a dtprel 754 /// (32-bit DTP relative) value. 755 /// 756 /// This is used to implement assembler directives such as .dtprelword on 757 /// targets that support them. 758 virtual void emitDTPRel32Value(const MCExpr *Value); 759 760 /// Emit the expression \p Value into the output as a tprel 761 /// (64-bit TP relative) value. 762 /// 763 /// This is used to implement assembler directives such as .tpreldword on 764 /// targets that support them. 765 virtual void emitTPRel64Value(const MCExpr *Value); 766 767 /// Emit the expression \p Value into the output as a tprel 768 /// (32-bit TP relative) value. 769 /// 770 /// This is used to implement assembler directives such as .tprelword on 771 /// targets that support them. 772 virtual void emitTPRel32Value(const MCExpr *Value); 773 774 /// Emit the expression \p Value into the output as a gprel64 (64-bit 775 /// GP relative) value. 776 /// 777 /// This is used to implement assembler directives such as .gpdword on 778 /// targets that support them. 779 virtual void emitGPRel64Value(const MCExpr *Value); 780 781 /// Emit the expression \p Value into the output as a gprel32 (32-bit 782 /// GP relative) value. 783 /// 784 /// This is used to implement assembler directives such as .gprel32 on 785 /// targets that support them. 786 virtual void emitGPRel32Value(const MCExpr *Value); 787 788 /// Emit NumBytes bytes worth of the value specified by FillValue. 789 /// This implements directives such as '.space'. 790 void emitFill(uint64_t NumBytes, uint8_t FillValue); 791 792 /// Emit \p Size bytes worth of the value specified by \p FillValue. 793 /// 794 /// This is used to implement assembler directives such as .space or .skip. 795 /// 796 /// \param NumBytes - The number of bytes to emit. 797 /// \param FillValue - The value to use when filling bytes. 798 /// \param Loc - The location of the expression for error reporting. 799 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue, 800 SMLoc Loc = SMLoc()); 801 802 /// Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is 803 /// taken from the lowest order 4 bytes of \p Expr expression. 804 /// 805 /// This is used to implement assembler directives such as .fill. 806 /// 807 /// \param NumValues - The number of copies of \p Size bytes to emit. 808 /// \param Size - The size (in bytes) of each repeated value. 809 /// \param Expr - The expression from which \p Size bytes are used. 810 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr, 811 SMLoc Loc = SMLoc()); 812 813 virtual void emitNops(int64_t NumBytes, int64_t ControlledNopLength, 814 SMLoc Loc, const MCSubtargetInfo& STI); 815 816 /// Emit NumBytes worth of zeros. 817 /// This function properly handles data in virtual sections. 818 void emitZeros(uint64_t NumBytes); 819 820 /// Emit some number of copies of \p Value until the byte alignment \p 821 /// ByteAlignment is reached. 822 /// 823 /// If the number of bytes need to emit for the alignment is not a multiple 824 /// of \p ValueSize, then the contents of the emitted fill bytes is 825 /// undefined. 826 /// 827 /// This used to implement the .align assembler directive. 828 /// 829 /// \param Alignment - The alignment to reach. 830 /// \param Value - The value to use when filling bytes. 831 /// \param ValueSize - The size of the integer (in bytes) to emit for 832 /// \p Value. This must match a native machine width. 833 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 834 /// the alignment cannot be reached in this many bytes, no bytes are 835 /// emitted. 836 virtual void emitValueToAlignment(Align Alignment, int64_t Value = 0, 837 unsigned ValueSize = 1, 838 unsigned MaxBytesToEmit = 0); 839 840 /// Emit nops until the byte alignment \p ByteAlignment is reached. 841 /// 842 /// This used to align code where the alignment bytes may be executed. This 843 /// can emit different bytes for different sizes to optimize execution. 844 /// 845 /// \param Alignment - The alignment to reach. 846 /// \param STI - The MCSubtargetInfo in operation when padding is emitted. 847 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 848 /// the alignment cannot be reached in this many bytes, no bytes are 849 /// emitted. 850 virtual void emitCodeAlignment(Align Alignment, const MCSubtargetInfo *STI, 851 unsigned MaxBytesToEmit = 0); 852 853 /// Emit some number of copies of \p Value until the byte offset \p 854 /// Offset is reached. 855 /// 856 /// This is used to implement assembler directives such as .org. 857 /// 858 /// \param Offset - The offset to reach. This may be an expression, but the 859 /// expression must be associated with the current section. 860 /// \param Value - The value to use when filling bytes. 861 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value, 862 SMLoc Loc); 863 864 /// @} 865 866 /// Switch to a new logical file. This is used to implement the '.file 867 /// "foo.c"' assembler directive. 868 virtual void emitFileDirective(StringRef Filename); 869 870 /// Emit ".file assembler diretive with additioal info. 871 virtual void emitFileDirective(StringRef Filename, StringRef CompilerVersion, 872 StringRef TimeStamp, StringRef Description); 873 874 /// Emit the "identifiers" directive. This implements the 875 /// '.ident "version foo"' assembler directive. emitIdent(StringRef IdentString)876 virtual void emitIdent(StringRef IdentString) {} 877 878 /// Associate a filename with a specified logical file number. This 879 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive. 880 unsigned emitDwarfFileDirective( 881 unsigned FileNo, StringRef Directory, StringRef Filename, 882 std::optional<MD5::MD5Result> Checksum = std::nullopt, 883 std::optional<StringRef> Source = std::nullopt, unsigned CUID = 0) { 884 return cantFail( 885 tryEmitDwarfFileDirective(FileNo, Directory, Filename, Checksum, 886 Source, CUID)); 887 } 888 889 /// Associate a filename with a specified logical file number. 890 /// Also associate a directory, optional checksum, and optional source 891 /// text with the logical file. This implements the DWARF2 892 /// '.file 4 "dir/foo.c"' assembler directive, and the DWARF5 893 /// '.file 4 "dir/foo.c" md5 "..." source "..."' assembler directive. 894 virtual Expected<unsigned> tryEmitDwarfFileDirective( 895 unsigned FileNo, StringRef Directory, StringRef Filename, 896 std::optional<MD5::MD5Result> Checksum = std::nullopt, 897 std::optional<StringRef> Source = std::nullopt, unsigned CUID = 0); 898 899 /// Specify the "root" file of the compilation, using the ".file 0" extension. 900 virtual void emitDwarfFile0Directive(StringRef Directory, StringRef Filename, 901 std::optional<MD5::MD5Result> Checksum, 902 std::optional<StringRef> Source, 903 unsigned CUID = 0); 904 905 virtual void emitCFIBKeyFrame(); 906 virtual void emitCFIMTETaggedFrame(); 907 908 /// This implements the DWARF2 '.loc fileno lineno ...' assembler 909 /// directive. 910 virtual void emitDwarfLocDirective(unsigned FileNo, unsigned Line, 911 unsigned Column, unsigned Flags, 912 unsigned Isa, unsigned Discriminator, 913 StringRef FileName); 914 915 /// Associate a filename with a specified logical file number, and also 916 /// specify that file's checksum information. This implements the '.cv_file 4 917 /// "foo.c"' assembler directive. Returns true on success. 918 virtual bool emitCVFileDirective(unsigned FileNo, StringRef Filename, 919 ArrayRef<uint8_t> Checksum, 920 unsigned ChecksumKind); 921 922 /// Introduces a function id for use with .cv_loc. 923 virtual bool emitCVFuncIdDirective(unsigned FunctionId); 924 925 /// Introduces an inline call site id for use with .cv_loc. Includes 926 /// extra information for inline line table generation. 927 virtual bool emitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc, 928 unsigned IAFile, unsigned IALine, 929 unsigned IACol, SMLoc Loc); 930 931 /// This implements the CodeView '.cv_loc' assembler directive. 932 virtual void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, 933 unsigned Line, unsigned Column, 934 bool PrologueEnd, bool IsStmt, 935 StringRef FileName, SMLoc Loc); 936 937 /// This implements the CodeView '.cv_linetable' assembler directive. 938 virtual void emitCVLinetableDirective(unsigned FunctionId, 939 const MCSymbol *FnStart, 940 const MCSymbol *FnEnd); 941 942 /// This implements the CodeView '.cv_inline_linetable' assembler 943 /// directive. 944 virtual void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, 945 unsigned SourceFileId, 946 unsigned SourceLineNum, 947 const MCSymbol *FnStartSym, 948 const MCSymbol *FnEndSym); 949 950 /// This implements the CodeView '.cv_def_range' assembler 951 /// directive. 952 virtual void emitCVDefRangeDirective( 953 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 954 StringRef FixedSizePortion); 955 956 virtual void emitCVDefRangeDirective( 957 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 958 codeview::DefRangeRegisterRelHeader DRHdr); 959 960 virtual void emitCVDefRangeDirective( 961 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 962 codeview::DefRangeSubfieldRegisterHeader DRHdr); 963 964 virtual void emitCVDefRangeDirective( 965 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 966 codeview::DefRangeRegisterHeader DRHdr); 967 968 virtual void emitCVDefRangeDirective( 969 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 970 codeview::DefRangeFramePointerRelHeader DRHdr); 971 972 /// This implements the CodeView '.cv_stringtable' assembler directive. emitCVStringTableDirective()973 virtual void emitCVStringTableDirective() {} 974 975 /// This implements the CodeView '.cv_filechecksums' assembler directive. emitCVFileChecksumsDirective()976 virtual void emitCVFileChecksumsDirective() {} 977 978 /// This implements the CodeView '.cv_filechecksumoffset' assembler 979 /// directive. emitCVFileChecksumOffsetDirective(unsigned FileNo)980 virtual void emitCVFileChecksumOffsetDirective(unsigned FileNo) {} 981 982 /// This implements the CodeView '.cv_fpo_data' assembler directive. 983 virtual void emitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {} 984 985 /// Emit the absolute difference between two symbols. 986 /// 987 /// \pre Offset of \c Hi is greater than the offset \c Lo. 988 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, 989 unsigned Size); 990 991 /// Emit the absolute difference between two symbols encoded with ULEB128. 992 virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, 993 const MCSymbol *Lo); 994 995 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID); 996 virtual void emitCFISections(bool EH, bool Debug); 997 void emitCFIStartProc(bool IsSimple, SMLoc Loc = SMLoc()); 998 void emitCFIEndProc(); 999 virtual void emitCFIDefCfa(int64_t Register, int64_t Offset, SMLoc Loc = {}); 1000 virtual void emitCFIDefCfaOffset(int64_t Offset, SMLoc Loc = {}); 1001 virtual void emitCFIDefCfaRegister(int64_t Register, SMLoc Loc = {}); 1002 virtual void emitCFILLVMDefAspaceCfa(int64_t Register, int64_t Offset, 1003 int64_t AddressSpace, SMLoc Loc = {}); 1004 virtual void emitCFIOffset(int64_t Register, int64_t Offset, SMLoc Loc = {}); 1005 virtual void emitCFIPersonality(const MCSymbol *Sym, unsigned Encoding); 1006 virtual void emitCFILsda(const MCSymbol *Sym, unsigned Encoding); 1007 virtual void emitCFIRememberState(SMLoc Loc); 1008 virtual void emitCFIRestoreState(SMLoc Loc); 1009 virtual void emitCFISameValue(int64_t Register, SMLoc Loc = {}); 1010 virtual void emitCFIRestore(int64_t Register, SMLoc Loc = {}); 1011 virtual void emitCFIRelOffset(int64_t Register, int64_t Offset, SMLoc Loc); 1012 virtual void emitCFIAdjustCfaOffset(int64_t Adjustment, SMLoc Loc = {}); 1013 virtual void emitCFIEscape(StringRef Values, SMLoc Loc = {}); 1014 virtual void emitCFIReturnColumn(int64_t Register); 1015 virtual void emitCFIGnuArgsSize(int64_t Size, SMLoc Loc = {}); 1016 virtual void emitCFISignalFrame(); 1017 virtual void emitCFIUndefined(int64_t Register, SMLoc Loc = {}); 1018 virtual void emitCFIRegister(int64_t Register1, int64_t Register2, 1019 SMLoc Loc = {}); 1020 virtual void emitCFIWindowSave(SMLoc Loc = {}); 1021 virtual void emitCFINegateRAState(SMLoc Loc = {}); 1022 virtual void emitCFILabelDirective(SMLoc Loc, StringRef Name); 1023 1024 virtual void emitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc()); 1025 virtual void emitWinCFIEndProc(SMLoc Loc = SMLoc()); 1026 /// This is used on platforms, such as Windows on ARM64, that require function 1027 /// or funclet sizes to be emitted in .xdata before the End marker is emitted 1028 /// for the frame. We cannot use the End marker, as it is not set at the 1029 /// point of emitting .xdata, in order to indicate that the frame is active. 1030 virtual void emitWinCFIFuncletOrFuncEnd(SMLoc Loc = SMLoc()); 1031 virtual void emitWinCFIStartChained(SMLoc Loc = SMLoc()); 1032 virtual void emitWinCFIEndChained(SMLoc Loc = SMLoc()); 1033 virtual void emitWinCFIPushReg(MCRegister Register, SMLoc Loc = SMLoc()); 1034 virtual void emitWinCFISetFrame(MCRegister Register, unsigned Offset, 1035 SMLoc Loc = SMLoc()); 1036 virtual void emitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc()); 1037 virtual void emitWinCFISaveReg(MCRegister Register, unsigned Offset, 1038 SMLoc Loc = SMLoc()); 1039 virtual void emitWinCFISaveXMM(MCRegister Register, unsigned Offset, 1040 SMLoc Loc = SMLoc()); 1041 virtual void emitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc()); 1042 virtual void emitWinCFIEndProlog(SMLoc Loc = SMLoc()); 1043 virtual void emitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except, 1044 SMLoc Loc = SMLoc()); 1045 virtual void emitWinEHHandlerData(SMLoc Loc = SMLoc()); 1046 1047 virtual void emitCGProfileEntry(const MCSymbolRefExpr *From, 1048 const MCSymbolRefExpr *To, uint64_t Count); 1049 1050 /// Get the .pdata section used for the given section. Typically the given 1051 /// section is either the main .text section or some other COMDAT .text 1052 /// section, but it may be any section containing code. 1053 MCSection *getAssociatedPDataSection(const MCSection *TextSec); 1054 1055 /// Get the .xdata section used for the given section. 1056 MCSection *getAssociatedXDataSection(const MCSection *TextSec); 1057 1058 virtual void emitSyntaxDirective(); 1059 1060 /// Record a relocation described by the .reloc directive. Return std::nullopt 1061 /// if succeeded. Otherwise, return a pair (Name is invalid, error message). 1062 virtual std::optional<std::pair<bool, std::string>> emitRelocDirective(const MCExpr & Offset,StringRef Name,const MCExpr * Expr,SMLoc Loc,const MCSubtargetInfo & STI)1063 emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, 1064 SMLoc Loc, const MCSubtargetInfo &STI) { 1065 return std::nullopt; 1066 } 1067 emitAddrsig()1068 virtual void emitAddrsig() {} emitAddrsigSym(const MCSymbol * Sym)1069 virtual void emitAddrsigSym(const MCSymbol *Sym) {} 1070 1071 /// Emit the given \p Instruction into the current section. 1072 virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI); 1073 1074 /// Emit the a pseudo probe into the current section. 1075 virtual void emitPseudoProbe(uint64_t Guid, uint64_t Index, uint64_t Type, 1076 uint64_t Attr, uint64_t Discriminator, 1077 const MCPseudoProbeInlineStack &InlineStack, 1078 MCSymbol *FnSym); 1079 1080 /// Set the bundle alignment mode from now on in the section. 1081 /// The value 1 means turn the bundle alignment off. 1082 virtual void emitBundleAlignMode(Align Alignment); 1083 1084 /// The following instructions are a bundle-locked group. 1085 /// 1086 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to 1087 /// the end of a bundle. 1088 virtual void emitBundleLock(bool AlignToEnd); 1089 1090 /// Ends a bundle-locked group. 1091 virtual void emitBundleUnlock(); 1092 1093 /// If this file is backed by a assembly streamer, this dumps the 1094 /// specified string in the output .s file. This capability is indicated by 1095 /// the hasRawTextSupport() predicate. By default this aborts. 1096 void emitRawText(const Twine &String); 1097 1098 /// Streamer specific finalization. 1099 virtual void finishImpl(); 1100 /// Finish emission of machine code. 1101 void finish(SMLoc EndLoc = SMLoc()); 1102 mayHaveInstructions(MCSection & Sec)1103 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; } 1104 1105 /// Emit a special value of 0xffffffff if producing 64-bit debugging info. 1106 void maybeEmitDwarf64Mark(); 1107 1108 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen 1109 /// according to the settings. 1110 virtual void emitDwarfUnitLength(uint64_t Length, const Twine &Comment); 1111 1112 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen 1113 /// according to the settings. 1114 /// Return the end symbol generated inside, the caller needs to emit it. 1115 virtual MCSymbol *emitDwarfUnitLength(const Twine &Prefix, 1116 const Twine &Comment); 1117 1118 /// Emit the debug line start label. 1119 virtual void emitDwarfLineStartLabel(MCSymbol *StartSym); 1120 1121 /// Emit the debug line end entry. emitDwarfLineEndEntry(MCSection * Section,MCSymbol * LastLabel)1122 virtual void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel) {} 1123 1124 /// If targets does not support representing debug line section by .loc/.file 1125 /// directives in assembly output, we need to populate debug line section with 1126 /// raw debug line contents. emitDwarfAdvanceLineAddr(int64_t LineDelta,const MCSymbol * LastLabel,const MCSymbol * Label,unsigned PointerSize)1127 virtual void emitDwarfAdvanceLineAddr(int64_t LineDelta, 1128 const MCSymbol *LastLabel, 1129 const MCSymbol *Label, 1130 unsigned PointerSize) {} 1131 1132 /// Do finalization for the streamer at the end of a section. doFinalizationAtSectionEnd(MCSection * Section)1133 virtual void doFinalizationAtSectionEnd(MCSection *Section) {} 1134 }; 1135 1136 /// Create a dummy machine code streamer, which does nothing. This is useful for 1137 /// timing the assembler front end. 1138 MCStreamer *createNullStreamer(MCContext &Ctx); 1139 1140 } // end namespace llvm 1141 1142 #endif // LLVM_MC_MCSTREAMER_H 1143