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