1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares the MCStreamer class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_MC_MCSTREAMER_H 15 #define LLVM_MC_MCSTREAMER_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/MC/MCDirectives.h" 20 #include "llvm/MC/MCDwarf.h" 21 #include "llvm/MC/MCLinkerOptimizationHint.h" 22 #include "llvm/MC/MCSymbol.h" 23 #include "llvm/MC/MCWinEH.h" 24 #include "llvm/Support/DataTypes.h" 25 #include "llvm/Support/SMLoc.h" 26 #include <string> 27 28 namespace llvm { 29 class MCAsmBackend; 30 class MCCodeEmitter; 31 class MCContext; 32 class MCExpr; 33 class MCInst; 34 class MCInstPrinter; 35 class MCSection; 36 class MCStreamer; 37 class MCSymbolELF; 38 class MCSymbolRefExpr; 39 class MCSubtargetInfo; 40 class StringRef; 41 class Twine; 42 class raw_ostream; 43 class formatted_raw_ostream; 44 class AssemblerConstantPools; 45 46 typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair; 47 48 /// Target specific streamer interface. This is used so that targets can 49 /// implement support for target specific assembly directives. 50 /// 51 /// If target foo wants to use this, it should implement 3 classes: 52 /// * FooTargetStreamer : public MCTargetStreamer 53 /// * FooTargetAsmStreamer : public FooTargetStreamer 54 /// * FooTargetELFStreamer : public FooTargetStreamer 55 /// 56 /// FooTargetStreamer should have a pure virtual method for each directive. For 57 /// example, for a ".bar symbol_name" directive, it should have 58 /// virtual emitBar(const MCSymbol &Symbol) = 0; 59 /// 60 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the 61 /// method. The assembly streamer just prints ".bar symbol_name". The object 62 /// streamer does whatever is needed to implement .bar in the object file. 63 /// 64 /// In the assembly printer and parser the target streamer can be used by 65 /// calling getTargetStreamer and casting it to FooTargetStreamer: 66 /// 67 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer(); 68 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS); 69 /// 70 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should 71 /// *never* be treated differently. Callers should always talk to a 72 /// FooTargetStreamer. 73 class MCTargetStreamer { 74 protected: 75 MCStreamer &Streamer; 76 77 public: 78 MCTargetStreamer(MCStreamer &S); 79 virtual ~MCTargetStreamer(); 80 getStreamer()81 MCStreamer &getStreamer() { return Streamer; } 82 83 // Allow a target to add behavior to the EmitLabel of MCStreamer. 84 virtual void emitLabel(MCSymbol *Symbol); 85 // Allow a target to add behavior to the emitAssignment of MCStreamer. 86 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value); 87 88 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS, 89 const MCInst &Inst, const MCSubtargetInfo &STI); 90 91 virtual void finish(); 92 }; 93 94 // FIXME: declared here because it is used from 95 // lib/CodeGen/AsmPrinter/ARMException.cpp. 96 class ARMTargetStreamer : public MCTargetStreamer { 97 public: 98 ARMTargetStreamer(MCStreamer &S); 99 ~ARMTargetStreamer() override; 100 101 virtual void emitFnStart(); 102 virtual void emitFnEnd(); 103 virtual void emitCantUnwind(); 104 virtual void emitPersonality(const MCSymbol *Personality); 105 virtual void emitPersonalityIndex(unsigned Index); 106 virtual void emitHandlerData(); 107 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, 108 int64_t Offset = 0); 109 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0); 110 virtual void emitPad(int64_t Offset); 111 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList, 112 bool isVector); 113 virtual void emitUnwindRaw(int64_t StackOffset, 114 const SmallVectorImpl<uint8_t> &Opcodes); 115 116 virtual void switchVendor(StringRef Vendor); 117 virtual void emitAttribute(unsigned Attribute, unsigned Value); 118 virtual void emitTextAttribute(unsigned Attribute, StringRef String); 119 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, 120 StringRef StringValue = ""); 121 virtual void emitFPU(unsigned FPU); 122 virtual void emitArch(unsigned Arch); 123 virtual void emitArchExtension(unsigned ArchExt); 124 virtual void emitObjectArch(unsigned Arch); 125 virtual void finishAttributeSection(); 126 virtual void emitInst(uint32_t Inst, char Suffix = '\0'); 127 128 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE); 129 130 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value); 131 132 void finish() override; 133 134 /// Reset any state between object emissions, i.e. the equivalent of 135 /// MCStreamer's reset method. 136 virtual void reset(); 137 138 /// Callback used to implement the ldr= pseudo. 139 /// Add a new entry to the constant pool for the current section and return an 140 /// MCExpr that can be used to refer to the constant pool location. 141 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc); 142 143 /// Callback used to implemnt the .ltorg directive. 144 /// Emit contents of constant pool for the current section. 145 void emitCurrentConstantPool(); 146 147 private: 148 std::unique_ptr<AssemblerConstantPools> ConstantPools; 149 }; 150 151 /// \brief Streaming machine code generation interface. 152 /// 153 /// This interface is intended to provide a programatic interface that is very 154 /// similar to the level that an assembler .s file provides. It has callbacks 155 /// to emit bytes, handle directives, etc. The implementation of this interface 156 /// retains state to know what the current section is etc. 157 /// 158 /// There are multiple implementations of this interface: one for writing out 159 /// a .s file, and implementations that write out .o files of various formats. 160 /// 161 class MCStreamer { 162 MCContext &Context; 163 std::unique_ptr<MCTargetStreamer> TargetStreamer; 164 165 MCStreamer(const MCStreamer &) = delete; 166 MCStreamer &operator=(const MCStreamer &) = delete; 167 168 std::vector<MCDwarfFrameInfo> DwarfFrameInfos; 169 MCDwarfFrameInfo *getCurrentDwarfFrameInfo(); 170 void EnsureValidDwarfFrame(); 171 172 MCSymbol *EmitCFICommon(); 173 174 std::vector<WinEH::FrameInfo *> WinFrameInfos; 175 WinEH::FrameInfo *CurrentWinFrameInfo; 176 void EnsureValidWinFrameInfo(); 177 178 /// \brief Tracks an index to represent the order a symbol was emitted in. 179 /// Zero means we did not emit that symbol. 180 DenseMap<const MCSymbol *, unsigned> SymbolOrdering; 181 182 /// \brief This is stack of current and previous section values saved by 183 /// PushSection. 184 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack; 185 186 /// The next unique ID to use when creating a WinCFI-related section (.pdata 187 /// or .xdata). This ID ensures that we have a one-to-one mapping from 188 /// code section to unwind info section, which MSVC's incremental linker 189 /// requires. 190 unsigned NextWinCFIID = 0; 191 192 protected: 193 MCStreamer(MCContext &Ctx); 194 195 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame); 196 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame); 197 getCurrentWinFrameInfo()198 WinEH::FrameInfo *getCurrentWinFrameInfo() { 199 return CurrentWinFrameInfo; 200 } 201 202 virtual void EmitWindowsUnwindTables(); 203 204 virtual void EmitRawTextImpl(StringRef String); 205 206 public: 207 virtual ~MCStreamer(); 208 209 void visitUsedExpr(const MCExpr &Expr); 210 virtual void visitUsedSymbol(const MCSymbol &Sym); 211 setTargetStreamer(MCTargetStreamer * TS)212 void setTargetStreamer(MCTargetStreamer *TS) { 213 TargetStreamer.reset(TS); 214 } 215 216 /// State management 217 /// 218 virtual void reset(); 219 getContext()220 MCContext &getContext() const { return Context; } 221 getTargetStreamer()222 MCTargetStreamer *getTargetStreamer() { 223 return TargetStreamer.get(); 224 } 225 getNumFrameInfos()226 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); } getDwarfFrameInfos()227 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const { 228 return DwarfFrameInfos; 229 } 230 231 bool hasUnfinishedDwarfFrameInfo(); 232 getNumWinFrameInfos()233 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); } getWinFrameInfos()234 ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const { 235 return WinFrameInfos; 236 } 237 238 void generateCompactUnwindEncodings(MCAsmBackend *MAB); 239 240 /// \name Assembly File Formatting. 241 /// @{ 242 243 /// \brief Return true if this streamer supports verbose assembly and if it is 244 /// enabled. isVerboseAsm()245 virtual bool isVerboseAsm() const { return false; } 246 247 /// \brief Return true if this asm streamer supports emitting unformatted text 248 /// to the .s file with EmitRawText. hasRawTextSupport()249 virtual bool hasRawTextSupport() const { return false; } 250 251 /// \brief Is the integrated assembler required for this streamer to function 252 /// correctly? isIntegratedAssemblerRequired()253 virtual bool isIntegratedAssemblerRequired() const { return false; } 254 255 /// \brief Add a textual comment. 256 /// 257 /// Typically for comments that can be emitted to the generated .s 258 /// file if applicable as a QoI issue to make the output of the compiler 259 /// more readable. This only affects the MCAsmStreamer, and only when 260 /// verbose assembly output is enabled. 261 /// 262 /// If the comment includes embedded \n's, they will each get the comment 263 /// prefix as appropriate. The added comment should not end with a \n. AddComment(const Twine & T)264 virtual void AddComment(const Twine &T) {} 265 266 /// \brief Return a raw_ostream that comments can be written to. Unlike 267 /// AddComment, you are required to terminate comments with \n if you use this 268 /// method. 269 virtual raw_ostream &GetCommentOS(); 270 271 /// \brief Print T and prefix it with the comment string (normally #) and 272 /// optionally a tab. This prints the comment immediately, not at the end of 273 /// the current line. It is basically a safe version of EmitRawText: since it 274 /// only prints comments, the object streamer ignores it instead of asserting. 275 virtual void emitRawComment(const Twine &T, bool TabPrefix = true); 276 277 /// \brief Add explicit comment T. T is required to be a valid 278 /// comment in the output and does not need to be escaped. 279 virtual void addExplicitComment(const Twine &T); 280 /// \brief Emit added explicit comments. 281 virtual void emitExplicitComments(); 282 283 /// AddBlankLine - Emit a blank line to a .s file to pretty it up. AddBlankLine()284 virtual void AddBlankLine() {} 285 286 /// @} 287 288 /// \name Symbol & Section Management 289 /// @{ 290 291 /// \brief Return the current section that the streamer is emitting code to. getCurrentSection()292 MCSectionSubPair getCurrentSection() const { 293 if (!SectionStack.empty()) 294 return SectionStack.back().first; 295 return MCSectionSubPair(); 296 } getCurrentSectionOnly()297 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; } 298 299 /// \brief Return the previous section that the streamer is emitting code to. getPreviousSection()300 MCSectionSubPair getPreviousSection() const { 301 if (!SectionStack.empty()) 302 return SectionStack.back().second; 303 return MCSectionSubPair(); 304 } 305 306 /// \brief Returns an index to represent the order a symbol was emitted in. 307 /// (zero if we did not emit that symbol) GetSymbolOrder(const MCSymbol * Sym)308 unsigned GetSymbolOrder(const MCSymbol *Sym) const { 309 return SymbolOrdering.lookup(Sym); 310 } 311 312 /// \brief Update streamer for a new active section. 313 /// 314 /// This is called by PopSection and SwitchSection, if the current 315 /// section changes. 316 virtual void ChangeSection(MCSection *, const MCExpr *); 317 318 /// \brief Save the current and previous section on the section stack. PushSection()319 void PushSection() { 320 SectionStack.push_back( 321 std::make_pair(getCurrentSection(), getPreviousSection())); 322 } 323 324 /// \brief Restore the current and previous section from the section stack. 325 /// Calls ChangeSection as needed. 326 /// 327 /// Returns false if the stack was empty. PopSection()328 bool PopSection() { 329 if (SectionStack.size() <= 1) 330 return false; 331 auto I = SectionStack.end(); 332 --I; 333 MCSectionSubPair OldSection = I->first; 334 --I; 335 MCSectionSubPair NewSection = I->first; 336 337 if (OldSection != NewSection) 338 ChangeSection(NewSection.first, NewSection.second); 339 SectionStack.pop_back(); 340 return true; 341 } 342 SubSection(const MCExpr * Subsection)343 bool SubSection(const MCExpr *Subsection) { 344 if (SectionStack.empty()) 345 return false; 346 347 SwitchSection(SectionStack.back().first.first, Subsection); 348 return true; 349 } 350 351 /// Set the current section where code is being emitted to \p Section. This 352 /// is required to update CurSection. 353 /// 354 /// This corresponds to assembler directives like .section, .text, etc. 355 virtual void SwitchSection(MCSection *Section, 356 const MCExpr *Subsection = nullptr); 357 358 /// \brief Set the current section where code is being emitted to \p Section. 359 /// This is required to update CurSection. This version does not call 360 /// ChangeSection. 361 void SwitchSectionNoChange(MCSection *Section, 362 const MCExpr *Subsection = nullptr) { 363 assert(Section && "Cannot switch to a null section!"); 364 MCSectionSubPair curSection = SectionStack.back().first; 365 SectionStack.back().second = curSection; 366 if (MCSectionSubPair(Section, Subsection) != curSection) 367 SectionStack.back().first = MCSectionSubPair(Section, Subsection); 368 } 369 370 /// \brief Create the default sections and set the initial one. 371 virtual void InitSections(bool NoExecStack); 372 373 MCSymbol *endSection(MCSection *Section); 374 375 /// \brief Sets the symbol's section. 376 /// 377 /// Each emitted symbol will be tracked in the ordering table, 378 /// so we can sort on them later. 379 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment); 380 381 /// \brief Emit a label for \p Symbol into the current section. 382 /// 383 /// This corresponds to an assembler statement such as: 384 /// foo: 385 /// 386 /// \param Symbol - The symbol to emit. A given symbol should only be 387 /// emitted as a label once, and symbols emitted as a label should never be 388 /// used in an assignment. 389 // FIXME: These emission are non-const because we mutate the symbol to 390 // add the section we're emitting it to later. 391 virtual void EmitLabel(MCSymbol *Symbol); 392 393 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol); 394 395 /// \brief Note in the output the specified \p Flag. 396 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); 397 398 /// \brief Emit the given list \p Options of strings as linker 399 /// options into the output. EmitLinkerOptions(ArrayRef<std::string> Kind)400 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {} 401 402 /// \brief Note in the output the specified region \p Kind. EmitDataRegion(MCDataRegionType Kind)403 virtual void EmitDataRegion(MCDataRegionType Kind) {} 404 405 /// \brief Specify the MachO minimum deployment target version. EmitVersionMin(MCVersionMinType,unsigned Major,unsigned Minor,unsigned Update)406 virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor, 407 unsigned Update) {} 408 409 /// \brief Note in the output that the specified \p Func is a Thumb mode 410 /// function (ARM target only). 411 virtual void EmitThumbFunc(MCSymbol *Func); 412 413 /// \brief Emit an assignment of \p Value to \p Symbol. 414 /// 415 /// This corresponds to an assembler statement such as: 416 /// symbol = value 417 /// 418 /// The assignment generates no code, but has the side effect of binding the 419 /// value in the current context. For the assembly streamer, this prints the 420 /// binding into the .s file. 421 /// 422 /// \param Symbol - The symbol being assigned to. 423 /// \param Value - The value for the symbol. 424 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); 425 426 /// \brief Emit an weak reference from \p Alias to \p Symbol. 427 /// 428 /// This corresponds to an assembler statement such as: 429 /// .weakref alias, symbol 430 /// 431 /// \param Alias - The alias that is being created. 432 /// \param Symbol - The symbol being aliased. 433 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); 434 435 /// \brief Add the given \p Attribute to \p Symbol. 436 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, 437 MCSymbolAttr Attribute) = 0; 438 439 /// \brief Set the \p DescValue for the \p Symbol. 440 /// 441 /// \param Symbol - The symbol to have its n_desc field set. 442 /// \param DescValue - The value to set into the n_desc field. 443 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 444 445 /// \brief Start emitting COFF symbol definition 446 /// 447 /// \param Symbol - The symbol to have its External & Type fields set. 448 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol); 449 450 /// \brief Emit the storage class of the symbol. 451 /// 452 /// \param StorageClass - The storage class the symbol should have. 453 virtual void EmitCOFFSymbolStorageClass(int StorageClass); 454 455 /// \brief Emit the type of the symbol. 456 /// 457 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h) 458 virtual void EmitCOFFSymbolType(int Type); 459 460 /// \brief Marks the end of the symbol definition. 461 virtual void EndCOFFSymbolDef(); 462 463 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol); 464 465 /// \brief Emits a COFF section index. 466 /// 467 /// \param Symbol - Symbol the section number relocation should point to. 468 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol); 469 470 /// \brief Emits a COFF section relative relocation. 471 /// 472 /// \param Symbol - Symbol the section relative relocation should point to. 473 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol); 474 475 /// \brief Emit an ELF .size directive. 476 /// 477 /// This corresponds to an assembler statement such as: 478 /// .size symbol, expression 479 virtual void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value); 480 481 /// \brief Emit a Linker Optimization Hint (LOH) directive. 482 /// \param Args - Arguments of the LOH. EmitLOHDirective(MCLOHType Kind,const MCLOHArgs & Args)483 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {} 484 485 /// \brief Emit a common symbol. 486 /// 487 /// \param Symbol - The common symbol to emit. 488 /// \param Size - The size of the common symbol. 489 /// \param ByteAlignment - The alignment of the symbol if 490 /// non-zero. This must be a power of 2. 491 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 492 unsigned ByteAlignment) = 0; 493 494 /// \brief Emit a local common (.lcomm) symbol. 495 /// 496 /// \param Symbol - The common symbol to emit. 497 /// \param Size - The size of the common symbol. 498 /// \param ByteAlignment - The alignment of the common symbol in bytes. 499 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, 500 unsigned ByteAlignment); 501 502 /// \brief Emit the zerofill section and an optional symbol. 503 /// 504 /// \param Section - The zerofill section to create and or to put the symbol 505 /// \param Symbol - The zerofill symbol to emit, if non-NULL. 506 /// \param Size - The size of the zerofill symbol. 507 /// \param ByteAlignment - The alignment of the zerofill symbol if 508 /// non-zero. This must be a power of 2 on some targets. 509 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, 510 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0; 511 512 /// \brief Emit a thread local bss (.tbss) symbol. 513 /// 514 /// \param Section - The thread local common section. 515 /// \param Symbol - The thread local common symbol to emit. 516 /// \param Size - The size of the symbol. 517 /// \param ByteAlignment - The alignment of the thread local common symbol 518 /// if non-zero. This must be a power of 2 on some targets. 519 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 520 uint64_t Size, unsigned ByteAlignment = 0); 521 522 /// @} 523 /// \name Generating Data 524 /// @{ 525 526 /// \brief Emit the bytes in \p Data into the output. 527 /// 528 /// This is used to implement assembler directives such as .byte, .ascii, 529 /// etc. 530 virtual void EmitBytes(StringRef Data); 531 532 /// Functionally identical to EmitBytes. When emitting textual assembly, this 533 /// method uses .byte directives instead of .ascii or .asciz for readability. 534 virtual void EmitBinaryData(StringRef Data); 535 536 /// \brief Emit the expression \p Value into the output as a native 537 /// integer of the given \p Size bytes. 538 /// 539 /// This is used to implement assembler directives such as .word, .quad, 540 /// etc. 541 /// 542 /// \param Value - The value to emit. 543 /// \param Size - The size of the integer (in bytes) to emit. This must 544 /// match a native machine width. 545 /// \param Loc - The location of the expression for error reporting. 546 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size, 547 SMLoc Loc = SMLoc()); 548 549 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc()); 550 551 /// \brief Special case of EmitValue that avoids the client having 552 /// to pass in a MCExpr for constant integers. 553 virtual void EmitIntValue(uint64_t Value, unsigned Size); 554 555 virtual void EmitULEB128Value(const MCExpr *Value); 556 557 virtual void EmitSLEB128Value(const MCExpr *Value); 558 559 /// \brief Special case of EmitULEB128Value that avoids the client having to 560 /// pass in a MCExpr for constant integers. 561 void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0); 562 563 /// \brief Special case of EmitSLEB128Value that avoids the client having to 564 /// pass in a MCExpr for constant integers. 565 void EmitSLEB128IntValue(int64_t Value); 566 567 /// \brief Special case of EmitValue that avoids the client having to pass in 568 /// a MCExpr for MCSymbols. 569 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 570 bool IsSectionRelative = false); 571 572 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit 573 /// GP relative) value. 574 /// 575 /// This is used to implement assembler directives such as .gpdword on 576 /// targets that support them. 577 virtual void EmitGPRel64Value(const MCExpr *Value); 578 579 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit 580 /// GP relative) value. 581 /// 582 /// This is used to implement assembler directives such as .gprel32 on 583 /// targets that support them. 584 virtual void EmitGPRel32Value(const MCExpr *Value); 585 586 /// \brief Emit NumBytes bytes worth of the value specified by FillValue. 587 /// This implements directives such as '.space'. 588 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue); 589 590 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue. 591 /// 592 /// This is used to implement assembler directives such as .space or .skip. 593 /// 594 /// \param NumBytes - The number of bytes to emit. 595 /// \param FillValue - The value to use when filling bytes. 596 /// \param Loc - The location of the expression for error reporting. 597 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue, 598 SMLoc Loc = SMLoc()); 599 600 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is 601 /// taken from the lowest order 4 bytes of \p Expr expression. 602 /// 603 /// This is used to implement assembler directives such as .fill. 604 /// 605 /// \param NumValues - The number of copies of \p Size bytes to emit. 606 /// \param Size - The size (in bytes) of each repeated value. 607 /// \param Expr - The expression from which \p Size bytes are used. 608 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr); 609 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr, 610 SMLoc Loc = SMLoc()); 611 612 /// \brief Emit NumBytes worth of zeros. 613 /// This function properly handles data in virtual sections. 614 void EmitZeros(uint64_t NumBytes); 615 616 /// \brief Emit some number of copies of \p Value until the byte alignment \p 617 /// ByteAlignment is reached. 618 /// 619 /// If the number of bytes need to emit for the alignment is not a multiple 620 /// of \p ValueSize, then the contents of the emitted fill bytes is 621 /// undefined. 622 /// 623 /// This used to implement the .align assembler directive. 624 /// 625 /// \param ByteAlignment - The alignment to reach. This must be a power of 626 /// two on some targets. 627 /// \param Value - The value to use when filling bytes. 628 /// \param ValueSize - The size of the integer (in bytes) to emit for 629 /// \p Value. This must match a native machine width. 630 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 631 /// the alignment cannot be reached in this many bytes, no bytes are 632 /// emitted. 633 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 634 unsigned ValueSize = 1, 635 unsigned MaxBytesToEmit = 0); 636 637 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached. 638 /// 639 /// This used to align code where the alignment bytes may be executed. This 640 /// can emit different bytes for different sizes to optimize execution. 641 /// 642 /// \param ByteAlignment - The alignment to reach. This must be a power of 643 /// two on some targets. 644 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 645 /// the alignment cannot be reached in this many bytes, no bytes are 646 /// emitted. 647 virtual void EmitCodeAlignment(unsigned ByteAlignment, 648 unsigned MaxBytesToEmit = 0); 649 650 /// \brief Emit some number of copies of \p Value until the byte offset \p 651 /// Offset is reached. 652 /// 653 /// This is used to implement assembler directives such as .org. 654 /// 655 /// \param Offset - The offset to reach. This may be an expression, but the 656 /// expression must be associated with the current section. 657 /// \param Value - The value to use when filling bytes. 658 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value = 0); 659 660 /// @} 661 662 /// \brief Switch to a new logical file. This is used to implement the '.file 663 /// "foo.c"' assembler directive. 664 virtual void EmitFileDirective(StringRef Filename); 665 666 /// \brief Emit the "identifiers" directive. This implements the 667 /// '.ident "version foo"' assembler directive. EmitIdent(StringRef IdentString)668 virtual void EmitIdent(StringRef IdentString) {} 669 670 /// \brief Associate a filename with a specified logical file number. This 671 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive. 672 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, 673 StringRef Filename, 674 unsigned CUID = 0); 675 676 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler 677 /// directive. 678 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 679 unsigned Column, unsigned Flags, 680 unsigned Isa, unsigned Discriminator, 681 StringRef FileName); 682 683 /// \brief Associate a filename with a specified logical file number. This 684 /// implements the '.cv_file 4 "foo.c"' assembler directive. 685 virtual unsigned EmitCVFileDirective(unsigned FileNo, StringRef Filename); 686 687 /// \brief This implements the CodeView '.cv_loc' assembler directive. 688 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, 689 unsigned Line, unsigned Column, 690 bool PrologueEnd, bool IsStmt, 691 StringRef FileName); 692 693 /// \brief This implements the CodeView '.cv_linetable' assembler directive. 694 virtual void EmitCVLinetableDirective(unsigned FunctionId, 695 const MCSymbol *FnStart, 696 const MCSymbol *FnEnd); 697 698 /// \brief This implements the CodeView '.cv_inline_linetable' assembler 699 /// directive. 700 virtual void EmitCVInlineLinetableDirective( 701 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, 702 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym, 703 ArrayRef<unsigned> SecondaryFunctionIds); 704 705 /// \brief This implements the CodeView '.cv_def_range' assembler 706 /// directive. 707 virtual void EmitCVDefRangeDirective( 708 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 709 StringRef FixedSizePortion); 710 711 /// \brief This implements the CodeView '.cv_stringtable' assembler directive. EmitCVStringTableDirective()712 virtual void EmitCVStringTableDirective() {} 713 714 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive. EmitCVFileChecksumsDirective()715 virtual void EmitCVFileChecksumsDirective() {} 716 717 /// Emit the absolute difference between two symbols. 718 /// 719 /// \pre Offset of \c Hi is greater than the offset \c Lo. 720 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, 721 unsigned Size); 722 723 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID); 724 virtual void EmitCFISections(bool EH, bool Debug); 725 void EmitCFIStartProc(bool IsSimple); 726 void EmitCFIEndProc(); 727 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset); 728 virtual void EmitCFIDefCfaOffset(int64_t Offset); 729 virtual void EmitCFIDefCfaRegister(int64_t Register); 730 virtual void EmitCFIOffset(int64_t Register, int64_t Offset); 731 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding); 732 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding); 733 virtual void EmitCFIRememberState(); 734 virtual void EmitCFIRestoreState(); 735 virtual void EmitCFISameValue(int64_t Register); 736 virtual void EmitCFIRestore(int64_t Register); 737 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset); 738 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment); 739 virtual void EmitCFIEscape(StringRef Values); 740 virtual void EmitCFIGnuArgsSize(int64_t Size); 741 virtual void EmitCFISignalFrame(); 742 virtual void EmitCFIUndefined(int64_t Register); 743 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2); 744 virtual void EmitCFIWindowSave(); 745 746 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol); 747 virtual void EmitWinCFIEndProc(); 748 virtual void EmitWinCFIStartChained(); 749 virtual void EmitWinCFIEndChained(); 750 virtual void EmitWinCFIPushReg(unsigned Register); 751 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset); 752 virtual void EmitWinCFIAllocStack(unsigned Size); 753 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset); 754 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset); 755 virtual void EmitWinCFIPushFrame(bool Code); 756 virtual void EmitWinCFIEndProlog(); 757 758 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except); 759 virtual void EmitWinEHHandlerData(); 760 761 /// Get the .pdata section used for the given section. Typically the given 762 /// section is either the main .text section or some other COMDAT .text 763 /// section, but it may be any section containing code. 764 MCSection *getAssociatedPDataSection(const MCSection *TextSec); 765 766 /// Get the .xdata section used for the given section. 767 MCSection *getAssociatedXDataSection(const MCSection *TextSec); 768 769 virtual void EmitSyntaxDirective(); 770 771 /// \brief Emit a .reloc directive. 772 /// Returns true if the relocation could not be emitted because Name is not 773 /// known. EmitRelocDirective(const MCExpr & Offset,StringRef Name,const MCExpr * Expr,SMLoc Loc)774 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name, 775 const MCExpr *Expr, SMLoc Loc) { 776 return true; 777 } 778 779 /// \brief Emit the given \p Instruction into the current section. 780 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI); 781 782 /// \brief Set the bundle alignment mode from now on in the section. 783 /// The argument is the power of 2 to which the alignment is set. The 784 /// value 0 means turn the bundle alignment off. 785 virtual void EmitBundleAlignMode(unsigned AlignPow2); 786 787 /// \brief The following instructions are a bundle-locked group. 788 /// 789 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to 790 /// the end of a bundle. 791 virtual void EmitBundleLock(bool AlignToEnd); 792 793 /// \brief Ends a bundle-locked group. 794 virtual void EmitBundleUnlock(); 795 796 /// \brief If this file is backed by a assembly streamer, this dumps the 797 /// specified string in the output .s file. This capability is indicated by 798 /// the hasRawTextSupport() predicate. By default this aborts. 799 void EmitRawText(const Twine &String); 800 801 /// \brief Streamer specific finalization. 802 virtual void FinishImpl(); 803 /// \brief Finish emission of machine code. 804 void Finish(); 805 mayHaveInstructions(MCSection & Sec)806 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; } 807 }; 808 809 /// Create a dummy machine code streamer, which does nothing. This is useful for 810 /// timing the assembler front end. 811 MCStreamer *createNullStreamer(MCContext &Ctx); 812 813 /// Create a machine code streamer which will print out assembly for the native 814 /// target, suitable for compiling with a native assembler. 815 /// 816 /// \param InstPrint - If given, the instruction printer to use. If not given 817 /// the MCInst representation will be printed. This method takes ownership of 818 /// InstPrint. 819 /// 820 /// \param CE - If given, a code emitter to use to show the instruction 821 /// encoding inline with the assembly. This method takes ownership of \p CE. 822 /// 823 /// \param TAB - If given, a target asm backend to use to show the fixup 824 /// information in conjunction with encoding information. This method takes 825 /// ownership of \p TAB. 826 /// 827 /// \param ShowInst - Whether to show the MCInst representation inline with 828 /// the assembly. 829 MCStreamer *createAsmStreamer(MCContext &Ctx, 830 std::unique_ptr<formatted_raw_ostream> OS, 831 bool isVerboseAsm, bool useDwarfDirectory, 832 MCInstPrinter *InstPrint, MCCodeEmitter *CE, 833 MCAsmBackend *TAB, bool ShowInst); 834 } // end namespace llvm 835 836 #endif 837