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