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