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