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