MCAssembler.h revision fa8de2403078353fe3c7ae160bec22fa23d4d315
1//===- MCAssembler.h - Object File Generation -------------------*- 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#ifndef LLVM_MC_MCASSEMBLER_H 11#define LLVM_MC_MCASSEMBLER_H 12 13#include "llvm/ADT/DenseMap.h" 14#include "llvm/ADT/SmallPtrSet.h" 15#include "llvm/ADT/SmallString.h" 16#include "llvm/ADT/ilist.h" 17#include "llvm/ADT/ilist_node.h" 18#include "llvm/MC/MCFixup.h" 19#include "llvm/MC/MCInst.h" 20#include "llvm/Support/Casting.h" 21#include "llvm/Support/DataTypes.h" 22#include <vector> // FIXME: Shouldn't be needed. 23 24namespace llvm { 25class raw_ostream; 26class MCAsmLayout; 27class MCAssembler; 28class MCContext; 29class MCCodeEmitter; 30class MCExpr; 31class MCFragment; 32class MCObjectWriter; 33class MCSection; 34class MCSectionData; 35class MCSymbol; 36class MCSymbolData; 37class MCValue; 38class MCAsmBackend; 39 40class MCFragment : public ilist_node<MCFragment> { 41 friend class MCAsmLayout; 42 43 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION; 44 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION; 45 46public: 47 enum FragmentType { 48 FT_Align, 49 FT_Data, 50 FT_Fill, 51 FT_Inst, 52 FT_Org, 53 FT_Dwarf, 54 FT_DwarfFrame, 55 FT_LEB 56 }; 57 58private: 59 FragmentType Kind; 60 61 /// Parent - The data for the section this fragment is in. 62 MCSectionData *Parent; 63 64 /// Atom - The atom this fragment is in, as represented by it's defining 65 /// symbol. Atom's are only used by backends which set 66 /// \see MCAsmBackend::hasReliableSymbolDifference(). 67 MCSymbolData *Atom; 68 69 /// @name Assembler Backend Data 70 /// @{ 71 // 72 // FIXME: This could all be kept private to the assembler implementation. 73 74 /// Offset - The offset of this fragment in its section. This is ~0 until 75 /// initialized. 76 uint64_t Offset; 77 78 /// LayoutOrder - The layout order of this fragment. 79 unsigned LayoutOrder; 80 81 /// @} 82 83protected: 84 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0); 85 86public: 87 // Only for sentinel. 88 MCFragment(); 89 virtual ~MCFragment(); 90 91 FragmentType getKind() const { return Kind; } 92 93 MCSectionData *getParent() const { return Parent; } 94 void setParent(MCSectionData *Value) { Parent = Value; } 95 96 MCSymbolData *getAtom() const { return Atom; } 97 void setAtom(MCSymbolData *Value) { Atom = Value; } 98 99 unsigned getLayoutOrder() const { return LayoutOrder; } 100 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; } 101 102 void dump(); 103}; 104 105class MCDataFragment : public MCFragment { 106 virtual void anchor(); 107 SmallString<32> Contents; 108 109 /// Fixups - The list of fixups in this fragment. 110 std::vector<MCFixup> Fixups; 111 112public: 113 typedef std::vector<MCFixup>::const_iterator const_fixup_iterator; 114 typedef std::vector<MCFixup>::iterator fixup_iterator; 115 116public: 117 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {} 118 119 /// @name Accessors 120 /// @{ 121 122 SmallString<32> &getContents() { return Contents; } 123 const SmallString<32> &getContents() const { return Contents; } 124 125 /// @} 126 /// @name Fixup Access 127 /// @{ 128 129 void addFixup(MCFixup Fixup) { 130 // Enforce invariant that fixups are in offset order. 131 assert((Fixups.empty() || Fixup.getOffset() >= Fixups.back().getOffset()) && 132 "Fixups must be added in order!"); 133 Fixups.push_back(Fixup); 134 } 135 136 std::vector<MCFixup> &getFixups() { return Fixups; } 137 const std::vector<MCFixup> &getFixups() const { return Fixups; } 138 139 fixup_iterator fixup_begin() { return Fixups.begin(); } 140 const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 141 142 fixup_iterator fixup_end() {return Fixups.end();} 143 const_fixup_iterator fixup_end() const {return Fixups.end();} 144 145 /// @} 146 147 static bool classof(const MCFragment *F) { 148 return F->getKind() == MCFragment::FT_Data; 149 } 150}; 151 152// FIXME: This current incarnation of MCInstFragment doesn't make much sense, as 153// it is almost entirely a duplicate of MCDataFragment. If we decide to stick 154// with this approach (as opposed to making MCInstFragment a very light weight 155// object with just the MCInst and a code size, then we should just change 156// MCDataFragment to have an optional MCInst at its end. 157class MCInstFragment : public MCFragment { 158 virtual void anchor(); 159 160 /// Inst - The instruction this is a fragment for. 161 MCInst Inst; 162 163 /// Code - Binary data for the currently encoded instruction. 164 SmallString<8> Code; 165 166 /// Fixups - The list of fixups in this fragment. 167 SmallVector<MCFixup, 1> Fixups; 168 169public: 170 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator; 171 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator; 172 173public: 174 MCInstFragment(const MCInst &_Inst, MCSectionData *SD = 0) 175 : MCFragment(FT_Inst, SD), Inst(_Inst) { 176 } 177 178 /// @name Accessors 179 /// @{ 180 181 SmallVectorImpl<char> &getCode() { return Code; } 182 const SmallVectorImpl<char> &getCode() const { return Code; } 183 184 unsigned getInstSize() const { return Code.size(); } 185 const MCInst &getInst() const { return Inst; } 186 void setInst(const MCInst& Value) { Inst = Value; } 187 188 /// @} 189 /// @name Fixup Access 190 /// @{ 191 192 SmallVectorImpl<MCFixup> &getFixups() { return Fixups; } 193 const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; } 194 195 fixup_iterator fixup_begin() { return Fixups.begin(); } 196 const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 197 198 fixup_iterator fixup_end() {return Fixups.end();} 199 const_fixup_iterator fixup_end() const {return Fixups.end();} 200 201 /// @} 202 203 static bool classof(const MCFragment *F) { 204 return F->getKind() == MCFragment::FT_Inst; 205 } 206}; 207 208class MCAlignFragment : public MCFragment { 209 virtual void anchor(); 210 211 /// Alignment - The alignment to ensure, in bytes. 212 unsigned Alignment; 213 214 /// Value - Value to use for filling padding bytes. 215 int64_t Value; 216 217 /// ValueSize - The size of the integer (in bytes) of \p Value. 218 unsigned ValueSize; 219 220 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment 221 /// cannot be satisfied in this width then this fragment is ignored. 222 unsigned MaxBytesToEmit; 223 224 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead 225 /// of using the provided value. The exact interpretation of this flag is 226 /// target dependent. 227 bool EmitNops : 1; 228 229public: 230 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize, 231 unsigned _MaxBytesToEmit, MCSectionData *SD = 0) 232 : MCFragment(FT_Align, SD), Alignment(_Alignment), 233 Value(_Value),ValueSize(_ValueSize), 234 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {} 235 236 /// @name Accessors 237 /// @{ 238 239 unsigned getAlignment() const { return Alignment; } 240 241 int64_t getValue() const { return Value; } 242 243 unsigned getValueSize() const { return ValueSize; } 244 245 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; } 246 247 bool hasEmitNops() const { return EmitNops; } 248 void setEmitNops(bool Value) { EmitNops = Value; } 249 250 /// @} 251 252 static bool classof(const MCFragment *F) { 253 return F->getKind() == MCFragment::FT_Align; 254 } 255}; 256 257class MCFillFragment : public MCFragment { 258 virtual void anchor(); 259 260 /// Value - Value to use for filling bytes. 261 int64_t Value; 262 263 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if 264 /// this is a virtual fill fragment. 265 unsigned ValueSize; 266 267 /// Size - The number of bytes to insert. 268 uint64_t Size; 269 270public: 271 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size, 272 MCSectionData *SD = 0) 273 : MCFragment(FT_Fill, SD), 274 Value(_Value), ValueSize(_ValueSize), Size(_Size) { 275 assert((!ValueSize || (Size % ValueSize) == 0) && 276 "Fill size must be a multiple of the value size!"); 277 } 278 279 /// @name Accessors 280 /// @{ 281 282 int64_t getValue() const { return Value; } 283 284 unsigned getValueSize() const { return ValueSize; } 285 286 uint64_t getSize() const { return Size; } 287 288 /// @} 289 290 static bool classof(const MCFragment *F) { 291 return F->getKind() == MCFragment::FT_Fill; 292 } 293}; 294 295class MCOrgFragment : public MCFragment { 296 virtual void anchor(); 297 298 /// Offset - The offset this fragment should start at. 299 const MCExpr *Offset; 300 301 /// Value - Value to use for filling bytes. 302 int8_t Value; 303 304public: 305 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0) 306 : MCFragment(FT_Org, SD), 307 Offset(&_Offset), Value(_Value) {} 308 309 /// @name Accessors 310 /// @{ 311 312 const MCExpr &getOffset() const { return *Offset; } 313 314 uint8_t getValue() const { return Value; } 315 316 /// @} 317 318 static bool classof(const MCFragment *F) { 319 return F->getKind() == MCFragment::FT_Org; 320 } 321}; 322 323class MCLEBFragment : public MCFragment { 324 virtual void anchor(); 325 326 /// Value - The value this fragment should contain. 327 const MCExpr *Value; 328 329 /// IsSigned - True if this is a sleb128, false if uleb128. 330 bool IsSigned; 331 332 SmallString<8> Contents; 333public: 334 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD) 335 : MCFragment(FT_LEB, SD), 336 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); } 337 338 /// @name Accessors 339 /// @{ 340 341 const MCExpr &getValue() const { return *Value; } 342 343 bool isSigned() const { return IsSigned; } 344 345 SmallString<8> &getContents() { return Contents; } 346 const SmallString<8> &getContents() const { return Contents; } 347 348 /// @} 349 350 static bool classof(const MCFragment *F) { 351 return F->getKind() == MCFragment::FT_LEB; 352 } 353}; 354 355class MCDwarfLineAddrFragment : public MCFragment { 356 virtual void anchor(); 357 358 /// LineDelta - the value of the difference between the two line numbers 359 /// between two .loc dwarf directives. 360 int64_t LineDelta; 361 362 /// AddrDelta - The expression for the difference of the two symbols that 363 /// make up the address delta between two .loc dwarf directives. 364 const MCExpr *AddrDelta; 365 366 SmallString<8> Contents; 367 368public: 369 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta, 370 MCSectionData *SD) 371 : MCFragment(FT_Dwarf, SD), 372 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); } 373 374 /// @name Accessors 375 /// @{ 376 377 int64_t getLineDelta() const { return LineDelta; } 378 379 const MCExpr &getAddrDelta() const { return *AddrDelta; } 380 381 SmallString<8> &getContents() { return Contents; } 382 const SmallString<8> &getContents() const { return Contents; } 383 384 /// @} 385 386 static bool classof(const MCFragment *F) { 387 return F->getKind() == MCFragment::FT_Dwarf; 388 } 389}; 390 391class MCDwarfCallFrameFragment : public MCFragment { 392 virtual void anchor(); 393 394 /// AddrDelta - The expression for the difference of the two symbols that 395 /// make up the address delta between two .cfi_* dwarf directives. 396 const MCExpr *AddrDelta; 397 398 SmallString<8> Contents; 399 400public: 401 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD) 402 : MCFragment(FT_DwarfFrame, SD), 403 AddrDelta(&_AddrDelta) { Contents.push_back(0); } 404 405 /// @name Accessors 406 /// @{ 407 408 const MCExpr &getAddrDelta() const { return *AddrDelta; } 409 410 SmallString<8> &getContents() { return Contents; } 411 const SmallString<8> &getContents() const { return Contents; } 412 413 /// @} 414 415 static bool classof(const MCFragment *F) { 416 return F->getKind() == MCFragment::FT_DwarfFrame; 417 } 418}; 419 420// FIXME: Should this be a separate class, or just merged into MCSection? Since 421// we anticipate the fast path being through an MCAssembler, the only reason to 422// keep it out is for API abstraction. 423class MCSectionData : public ilist_node<MCSectionData> { 424 friend class MCAsmLayout; 425 426 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION; 427 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION; 428 429public: 430 typedef iplist<MCFragment> FragmentListType; 431 432 typedef FragmentListType::const_iterator const_iterator; 433 typedef FragmentListType::iterator iterator; 434 435 typedef FragmentListType::const_reverse_iterator const_reverse_iterator; 436 typedef FragmentListType::reverse_iterator reverse_iterator; 437 438private: 439 FragmentListType Fragments; 440 const MCSection *Section; 441 442 /// Ordinal - The section index in the assemblers section list. 443 unsigned Ordinal; 444 445 /// LayoutOrder - The index of this section in the layout order. 446 unsigned LayoutOrder; 447 448 /// Alignment - The maximum alignment seen in this section. 449 unsigned Alignment; 450 451 /// @name Assembler Backend Data 452 /// @{ 453 // 454 // FIXME: This could all be kept private to the assembler implementation. 455 456 /// HasInstructions - Whether this section has had instructions emitted into 457 /// it. 458 unsigned HasInstructions : 1; 459 460 /// @} 461 462public: 463 // Only for use as sentinel. 464 MCSectionData(); 465 MCSectionData(const MCSection &Section, MCAssembler *A = 0); 466 467 const MCSection &getSection() const { return *Section; } 468 469 unsigned getAlignment() const { return Alignment; } 470 void setAlignment(unsigned Value) { Alignment = Value; } 471 472 bool hasInstructions() const { return HasInstructions; } 473 void setHasInstructions(bool Value) { HasInstructions = Value; } 474 475 unsigned getOrdinal() const { return Ordinal; } 476 void setOrdinal(unsigned Value) { Ordinal = Value; } 477 478 unsigned getLayoutOrder() const { return LayoutOrder; } 479 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; } 480 481 /// @name Fragment Access 482 /// @{ 483 484 const FragmentListType &getFragmentList() const { return Fragments; } 485 FragmentListType &getFragmentList() { return Fragments; } 486 487 iterator begin() { return Fragments.begin(); } 488 const_iterator begin() const { return Fragments.begin(); } 489 490 iterator end() { return Fragments.end(); } 491 const_iterator end() const { return Fragments.end(); } 492 493 reverse_iterator rbegin() { return Fragments.rbegin(); } 494 const_reverse_iterator rbegin() const { return Fragments.rbegin(); } 495 496 reverse_iterator rend() { return Fragments.rend(); } 497 const_reverse_iterator rend() const { return Fragments.rend(); } 498 499 size_t size() const { return Fragments.size(); } 500 501 bool empty() const { return Fragments.empty(); } 502 503 void dump(); 504 505 /// @} 506}; 507 508// FIXME: Same concerns as with SectionData. 509class MCSymbolData : public ilist_node<MCSymbolData> { 510public: 511 const MCSymbol *Symbol; 512 513 /// Fragment - The fragment this symbol's value is relative to, if any. 514 MCFragment *Fragment; 515 516 /// Offset - The offset to apply to the fragment address to form this symbol's 517 /// value. 518 uint64_t Offset; 519 520 /// IsExternal - True if this symbol is visible outside this translation 521 /// unit. 522 unsigned IsExternal : 1; 523 524 /// IsPrivateExtern - True if this symbol is private extern. 525 unsigned IsPrivateExtern : 1; 526 527 /// CommonSize - The size of the symbol, if it is 'common', or 0. 528 // 529 // FIXME: Pack this in with other fields? We could put it in offset, since a 530 // common symbol can never get a definition. 531 uint64_t CommonSize; 532 533 /// SymbolSize - An expression describing how to calculate the size of 534 /// a symbol. If a symbol has no size this field will be NULL. 535 const MCExpr *SymbolSize; 536 537 /// CommonAlign - The alignment of the symbol, if it is 'common'. 538 // 539 // FIXME: Pack this in with other fields? 540 unsigned CommonAlign; 541 542 /// Flags - The Flags field is used by object file implementations to store 543 /// additional per symbol information which is not easily classified. 544 uint32_t Flags; 545 546 /// Index - Index field, for use by the object file implementation. 547 uint64_t Index; 548 549public: 550 // Only for use as sentinel. 551 MCSymbolData(); 552 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset, 553 MCAssembler *A = 0); 554 555 /// @name Accessors 556 /// @{ 557 558 const MCSymbol &getSymbol() const { return *Symbol; } 559 560 MCFragment *getFragment() const { return Fragment; } 561 void setFragment(MCFragment *Value) { Fragment = Value; } 562 563 uint64_t getOffset() const { return Offset; } 564 void setOffset(uint64_t Value) { Offset = Value; } 565 566 /// @} 567 /// @name Symbol Attributes 568 /// @{ 569 570 bool isExternal() const { return IsExternal; } 571 void setExternal(bool Value) { IsExternal = Value; } 572 573 bool isPrivateExtern() const { return IsPrivateExtern; } 574 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; } 575 576 /// isCommon - Is this a 'common' symbol. 577 bool isCommon() const { return CommonSize != 0; } 578 579 /// setCommon - Mark this symbol as being 'common'. 580 /// 581 /// \param Size - The size of the symbol. 582 /// \param Align - The alignment of the symbol. 583 void setCommon(uint64_t Size, unsigned Align) { 584 CommonSize = Size; 585 CommonAlign = Align; 586 } 587 588 /// getCommonSize - Return the size of a 'common' symbol. 589 uint64_t getCommonSize() const { 590 assert(isCommon() && "Not a 'common' symbol!"); 591 return CommonSize; 592 } 593 594 void setSize(const MCExpr *SS) { 595 SymbolSize = SS; 596 } 597 598 const MCExpr *getSize() const { 599 return SymbolSize; 600 } 601 602 603 /// getCommonAlignment - Return the alignment of a 'common' symbol. 604 unsigned getCommonAlignment() const { 605 assert(isCommon() && "Not a 'common' symbol!"); 606 return CommonAlign; 607 } 608 609 /// getFlags - Get the (implementation defined) symbol flags. 610 uint32_t getFlags() const { return Flags; } 611 612 /// setFlags - Set the (implementation defined) symbol flags. 613 void setFlags(uint32_t Value) { Flags = Value; } 614 615 /// modifyFlags - Modify the flags via a mask 616 void modifyFlags(uint32_t Value, uint32_t Mask) { 617 Flags = (Flags & ~Mask) | Value; 618 } 619 620 /// getIndex - Get the (implementation defined) index. 621 uint64_t getIndex() const { return Index; } 622 623 /// setIndex - Set the (implementation defined) index. 624 void setIndex(uint64_t Value) { Index = Value; } 625 626 /// @} 627 628 void dump(); 629}; 630 631// FIXME: This really doesn't belong here. See comments below. 632struct IndirectSymbolData { 633 MCSymbol *Symbol; 634 MCSectionData *SectionData; 635}; 636 637// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk 638// to one another. 639struct DataRegionData { 640 // This enum should be kept in sync w/ the mach-o definition in 641 // llvm/Object/MachOFormat.h. 642 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind; 643 MCSymbol *Start; 644 MCSymbol *End; 645}; 646 647class MCAssembler { 648 friend class MCAsmLayout; 649 650public: 651 typedef iplist<MCSectionData> SectionDataListType; 652 typedef iplist<MCSymbolData> SymbolDataListType; 653 654 typedef SectionDataListType::const_iterator const_iterator; 655 typedef SectionDataListType::iterator iterator; 656 657 typedef SymbolDataListType::const_iterator const_symbol_iterator; 658 typedef SymbolDataListType::iterator symbol_iterator; 659 660 typedef std::vector<IndirectSymbolData>::const_iterator 661 const_indirect_symbol_iterator; 662 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator; 663 664 typedef std::vector<DataRegionData>::const_iterator 665 const_data_region_iterator; 666 typedef std::vector<DataRegionData>::iterator data_region_iterator; 667 668private: 669 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION; 670 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION; 671 672 MCContext &Context; 673 674 MCAsmBackend &Backend; 675 676 MCCodeEmitter &Emitter; 677 678 MCObjectWriter &Writer; 679 680 raw_ostream &OS; 681 682 iplist<MCSectionData> Sections; 683 684 iplist<MCSymbolData> Symbols; 685 686 /// The map of sections to their associated assembler backend data. 687 // 688 // FIXME: Avoid this indirection? 689 DenseMap<const MCSection*, MCSectionData*> SectionMap; 690 691 /// The map of symbols to their associated assembler backend data. 692 // 693 // FIXME: Avoid this indirection? 694 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; 695 696 std::vector<IndirectSymbolData> IndirectSymbols; 697 698 std::vector<DataRegionData> DataRegions; 699 /// The set of function symbols for which a .thumb_func directive has 700 /// been seen. 701 // 702 // FIXME: We really would like this in target specific code rather than 703 // here. Maybe when the relocation stuff moves to target specific, 704 // this can go with it? The streamer would need some target specific 705 // refactoring too. 706 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs; 707 708 unsigned RelaxAll : 1; 709 unsigned NoExecStack : 1; 710 unsigned SubsectionsViaSymbols : 1; 711 712private: 713 /// Evaluate a fixup to a relocatable expression and the value which should be 714 /// placed into the fixup. 715 /// 716 /// \param Layout The layout to use for evaluation. 717 /// \param Fixup The fixup to evaluate. 718 /// \param DF The fragment the fixup is inside. 719 /// \param Target [out] On return, the relocatable expression the fixup 720 /// evaluates to. 721 /// \param Value [out] On return, the value of the fixup as currently laid 722 /// out. 723 /// \return Whether the fixup value was fully resolved. This is true if the 724 /// \p Value result is fixed, otherwise the value may change due to 725 /// relocation. 726 bool evaluateFixup(const MCAsmLayout &Layout, 727 const MCFixup &Fixup, const MCFragment *DF, 728 MCValue &Target, uint64_t &Value) const; 729 730 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed 731 /// (increased in size, in order to hold its value correctly). 732 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCInstFragment *DF, 733 const MCAsmLayout &Layout) const; 734 735 /// Check whether the given fragment needs relaxation. 736 bool fragmentNeedsRelaxation(const MCInstFragment *IF, 737 const MCAsmLayout &Layout) const; 738 739 /// layoutOnce - Perform one layout iteration and return true if any offsets 740 /// were adjusted. 741 bool layoutOnce(MCAsmLayout &Layout); 742 743 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD); 744 745 bool relaxInstruction(MCAsmLayout &Layout, MCInstFragment &IF); 746 747 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF); 748 749 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF); 750 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout, 751 MCDwarfCallFrameFragment &DF); 752 753 /// finishLayout - Finalize a layout, including fragment lowering. 754 void finishLayout(MCAsmLayout &Layout); 755 756 uint64_t handleFixup(const MCAsmLayout &Layout, 757 MCFragment &F, const MCFixup &Fixup); 758 759public: 760 /// Compute the effective fragment size assuming it is laid out at the given 761 /// \p SectionAddress and \p FragmentOffset. 762 uint64_t computeFragmentSize(const MCAsmLayout &Layout, 763 const MCFragment &F) const; 764 765 /// Find the symbol which defines the atom containing the given symbol, or 766 /// null if there is no such symbol. 767 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const; 768 769 /// Check whether a particular symbol is visible to the linker and is required 770 /// in the symbol table, or whether it can be discarded by the assembler. This 771 /// also effects whether the assembler treats the label as potentially 772 /// defining a separate atom. 773 bool isSymbolLinkerVisible(const MCSymbol &SD) const; 774 775 /// Emit the section contents using the given object writer. 776 void writeSectionData(const MCSectionData *Section, 777 const MCAsmLayout &Layout) const; 778 779 /// Check whether a given symbol has been flagged with .thumb_func. 780 bool isThumbFunc(const MCSymbol *Func) const { 781 return ThumbFuncs.count(Func); 782 } 783 784 /// Flag a function symbol as the target of a .thumb_func directive. 785 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); } 786 787public: 788 /// Construct a new assembler instance. 789 /// 790 /// \param OS The stream to output to. 791 // 792 // FIXME: How are we going to parameterize this? Two obvious options are stay 793 // concrete and require clients to pass in a target like object. The other 794 // option is to make this abstract, and have targets provide concrete 795 // implementations as we do with AsmParser. 796 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, 797 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, 798 raw_ostream &OS); 799 ~MCAssembler(); 800 801 MCContext &getContext() const { return Context; } 802 803 MCAsmBackend &getBackend() const { return Backend; } 804 805 MCCodeEmitter &getEmitter() const { return Emitter; } 806 807 MCObjectWriter &getWriter() const { return Writer; } 808 809 /// Finish - Do final processing and write the object to the output stream. 810 /// \p Writer is used for custom object writer (as the MCJIT does), 811 /// if not specified it is automatically created from backend. 812 void Finish(); 813 814 // FIXME: This does not belong here. 815 bool getSubsectionsViaSymbols() const { 816 return SubsectionsViaSymbols; 817 } 818 void setSubsectionsViaSymbols(bool Value) { 819 SubsectionsViaSymbols = Value; 820 } 821 822 bool getRelaxAll() const { return RelaxAll; } 823 void setRelaxAll(bool Value) { RelaxAll = Value; } 824 825 bool getNoExecStack() const { return NoExecStack; } 826 void setNoExecStack(bool Value) { NoExecStack = Value; } 827 828 /// @name Section List Access 829 /// @{ 830 831 const SectionDataListType &getSectionList() const { return Sections; } 832 SectionDataListType &getSectionList() { return Sections; } 833 834 iterator begin() { return Sections.begin(); } 835 const_iterator begin() const { return Sections.begin(); } 836 837 iterator end() { return Sections.end(); } 838 const_iterator end() const { return Sections.end(); } 839 840 size_t size() const { return Sections.size(); } 841 842 /// @} 843 /// @name Symbol List Access 844 /// @{ 845 846 const SymbolDataListType &getSymbolList() const { return Symbols; } 847 SymbolDataListType &getSymbolList() { return Symbols; } 848 849 symbol_iterator symbol_begin() { return Symbols.begin(); } 850 const_symbol_iterator symbol_begin() const { return Symbols.begin(); } 851 852 symbol_iterator symbol_end() { return Symbols.end(); } 853 const_symbol_iterator symbol_end() const { return Symbols.end(); } 854 855 size_t symbol_size() const { return Symbols.size(); } 856 857 /// @} 858 /// @name Indirect Symbol List Access 859 /// @{ 860 861 // FIXME: This is a total hack, this should not be here. Once things are 862 // factored so that the streamer has direct access to the .o writer, it can 863 // disappear. 864 std::vector<IndirectSymbolData> &getIndirectSymbols() { 865 return IndirectSymbols; 866 } 867 868 indirect_symbol_iterator indirect_symbol_begin() { 869 return IndirectSymbols.begin(); 870 } 871 const_indirect_symbol_iterator indirect_symbol_begin() const { 872 return IndirectSymbols.begin(); 873 } 874 875 indirect_symbol_iterator indirect_symbol_end() { 876 return IndirectSymbols.end(); 877 } 878 const_indirect_symbol_iterator indirect_symbol_end() const { 879 return IndirectSymbols.end(); 880 } 881 882 size_t indirect_symbol_size() const { return IndirectSymbols.size(); } 883 884 /// @} 885 /// @name Data Region List Access 886 /// @{ 887 888 // FIXME: This is a total hack, this should not be here. Once things are 889 // factored so that the streamer has direct access to the .o writer, it can 890 // disappear. 891 std::vector<DataRegionData> &getDataRegions() { 892 return DataRegions; 893 } 894 895 data_region_iterator data_region_begin() { 896 return DataRegions.begin(); 897 } 898 const_data_region_iterator data_region_begin() const { 899 return DataRegions.begin(); 900 } 901 902 data_region_iterator data_region_end() { 903 return DataRegions.end(); 904 } 905 const_data_region_iterator data_region_end() const { 906 return DataRegions.end(); 907 } 908 909 size_t data_region_size() const { return DataRegions.size(); } 910 911 /// @} 912 /// @name Backend Data Access 913 /// @{ 914 915 MCSectionData &getSectionData(const MCSection &Section) const { 916 MCSectionData *Entry = SectionMap.lookup(&Section); 917 assert(Entry && "Missing section data!"); 918 return *Entry; 919 } 920 921 MCSectionData &getOrCreateSectionData(const MCSection &Section, 922 bool *Created = 0) { 923 MCSectionData *&Entry = SectionMap[&Section]; 924 925 if (Created) *Created = !Entry; 926 if (!Entry) 927 Entry = new MCSectionData(Section, this); 928 929 return *Entry; 930 } 931 932 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const { 933 MCSymbolData *Entry = SymbolMap.lookup(&Symbol); 934 assert(Entry && "Missing symbol data!"); 935 return *Entry; 936 } 937 938 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol, 939 bool *Created = 0) { 940 MCSymbolData *&Entry = SymbolMap[&Symbol]; 941 942 if (Created) *Created = !Entry; 943 if (!Entry) 944 Entry = new MCSymbolData(Symbol, 0, 0, this); 945 946 return *Entry; 947 } 948 949 /// @} 950 951 void dump(); 952}; 953 954} // end namespace llvm 955 956#endif 957