MCAssembler.h revision 8315a357e48f2eeb4fa929168d3cb65924d9893e
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/SmallString.h" 15#include "llvm/ADT/ilist.h" 16#include "llvm/ADT/ilist_node.h" 17#include "llvm/Support/Casting.h" 18#include "llvm/MC/MCFixup.h" 19#include "llvm/System/DataTypes.h" 20#include <vector> // FIXME: Shouldn't be needed. 21 22namespace llvm { 23class raw_ostream; 24class MCAssembler; 25class MCContext; 26class MCExpr; 27class MCFragment; 28class MCSection; 29class MCSectionData; 30class MCSymbol; 31class TargetAsmBackend; 32 33/// MCAsmFixup - Represent a fixed size region of bytes inside some fragment 34/// which needs to be rewritten. This region will either be rewritten by the 35/// assembler or cause a relocation entry to be generated. 36struct MCAsmFixup { 37 /// Offset - The offset inside the fragment which needs to be rewritten. 38 uint64_t Offset; 39 40 /// Value - The expression to eventually write into the fragment. 41 const MCExpr *Value; 42 43 /// Kind - The fixup kind. 44 MCFixupKind Kind; 45 46 /// FixedValue - The value to replace the fix up by. 47 // 48 // FIXME: This should not be here. 49 uint64_t FixedValue; 50 51public: 52 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind) 53 : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {} 54}; 55 56class MCFragment : public ilist_node<MCFragment> { 57 MCFragment(const MCFragment&); // DO NOT IMPLEMENT 58 void operator=(const MCFragment&); // DO NOT IMPLEMENT 59 60public: 61 enum FragmentType { 62 FT_Data, 63 FT_Align, 64 FT_Fill, 65 FT_Org, 66 FT_ZeroFill 67 }; 68 69private: 70 FragmentType Kind; 71 72 /// Parent - The data for the section this fragment is in. 73 MCSectionData *Parent; 74 75 /// @name Assembler Backend Data 76 /// @{ 77 // 78 // FIXME: This could all be kept private to the assembler implementation. 79 80 /// Offset - The offset of this fragment in its section. This is ~0 until 81 /// initialized. 82 uint64_t Offset; 83 84 /// FileSize - The file size of this section. This is ~0 until initialized. 85 uint64_t FileSize; 86 87 /// @} 88 89protected: 90 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0); 91 92public: 93 // Only for sentinel. 94 MCFragment(); 95 virtual ~MCFragment(); 96 97 FragmentType getKind() const { return Kind; } 98 99 MCSectionData *getParent() const { return Parent; } 100 void setParent(MCSectionData *Value) { Parent = Value; } 101 102 // FIXME: This should be abstract, fix sentinel. 103 virtual uint64_t getMaxFileSize() const { 104 assert(0 && "Invalid getMaxFileSize call!"); 105 return 0; 106 } 107 108 /// @name Assembler Backend Support 109 /// @{ 110 // 111 // FIXME: This could all be kept private to the assembler implementation. 112 113 uint64_t getAddress() const; 114 115 uint64_t getFileSize() const { 116 assert(FileSize != ~UINT64_C(0) && "File size not set!"); 117 return FileSize; 118 } 119 void setFileSize(uint64_t Value) { 120 assert(Value <= getMaxFileSize() && "Invalid file size!"); 121 FileSize = Value; 122 } 123 124 uint64_t getOffset() const { 125 assert(Offset != ~UINT64_C(0) && "File offset not set!"); 126 return Offset; 127 } 128 void setOffset(uint64_t Value) { Offset = Value; } 129 130 /// @} 131 132 static bool classof(const MCFragment *O) { return true; } 133 134 virtual void dump(); 135}; 136 137class MCDataFragment : public MCFragment { 138 SmallString<32> Contents; 139 140 /// Fixups - The list of fixups in this fragment. 141 std::vector<MCAsmFixup> Fixups; 142 143public: 144 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator; 145 typedef std::vector<MCAsmFixup>::iterator fixup_iterator; 146 147public: 148 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {} 149 150 /// @name Accessors 151 /// @{ 152 153 uint64_t getMaxFileSize() const { 154 return Contents.size(); 155 } 156 157 SmallString<32> &getContents() { return Contents; } 158 const SmallString<32> &getContents() const { return Contents; } 159 160 /// @} 161 162 /// @name Fixup Access 163 /// @{ 164 165 void addFixup(MCAsmFixup Fixup) { 166 // Enforce invariant that fixups are in offset order. 167 assert(Fixups.empty() || Fixup.Offset > Fixups.back().Offset && 168 "Fixups must be added in order!"); 169 Fixups.push_back(Fixup); 170 } 171 172 std::vector<MCAsmFixup> &getFixups() { return Fixups; } 173 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; } 174 175 fixup_iterator fixup_begin() { return Fixups.begin(); } 176 const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 177 178 fixup_iterator fixup_end() {return Fixups.end();} 179 const_fixup_iterator fixup_end() const {return Fixups.end();} 180 181 size_t fixup_size() const { return Fixups.size(); } 182 183 /// @} 184 185 static bool classof(const MCFragment *F) { 186 return F->getKind() == MCFragment::FT_Data; 187 } 188 static bool classof(const MCDataFragment *) { return true; } 189 190 virtual void dump(); 191}; 192 193class MCAlignFragment : public MCFragment { 194 /// Alignment - The alignment to ensure, in bytes. 195 unsigned Alignment; 196 197 /// Value - Value to use for filling padding bytes. 198 int64_t Value; 199 200 /// ValueSize - The size of the integer (in bytes) of \arg Value. 201 unsigned ValueSize; 202 203 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment 204 /// cannot be satisfied in this width then this fragment is ignored. 205 unsigned MaxBytesToEmit; 206 207 /// EmitNops - true when aligning code and optimal nops to be used for filling 208 bool EmitNops; 209 210public: 211 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize, 212 unsigned _MaxBytesToEmit, bool _EmitNops, 213 MCSectionData *SD = 0) 214 : MCFragment(FT_Align, SD), Alignment(_Alignment), 215 Value(_Value),ValueSize(_ValueSize), 216 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {} 217 218 /// @name Accessors 219 /// @{ 220 221 uint64_t getMaxFileSize() const { 222 return std::max(Alignment - 1, MaxBytesToEmit); 223 } 224 225 unsigned getAlignment() const { return Alignment; } 226 227 int64_t getValue() const { return Value; } 228 229 unsigned getValueSize() const { return ValueSize; } 230 231 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; } 232 233 unsigned getEmitNops() const { return EmitNops; } 234 235 /// @} 236 237 static bool classof(const MCFragment *F) { 238 return F->getKind() == MCFragment::FT_Align; 239 } 240 static bool classof(const MCAlignFragment *) { return true; } 241 242 virtual void dump(); 243}; 244 245class MCFillFragment : public MCFragment { 246 /// Value - Value to use for filling bytes. 247 int64_t Value; 248 249 /// ValueSize - The size (in bytes) of \arg Value to use when filling. 250 unsigned ValueSize; 251 252 /// Count - The number of copies of \arg Value to insert. 253 uint64_t Count; 254 255public: 256 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count, 257 MCSectionData *SD = 0) 258 : MCFragment(FT_Fill, SD), 259 Value(_Value), ValueSize(_ValueSize), Count(_Count) {} 260 261 /// @name Accessors 262 /// @{ 263 264 uint64_t getMaxFileSize() const { 265 return ValueSize * Count; 266 } 267 268 int64_t getValue() const { return Value; } 269 270 unsigned getValueSize() const { return ValueSize; } 271 272 uint64_t getCount() const { return Count; } 273 274 /// @} 275 276 static bool classof(const MCFragment *F) { 277 return F->getKind() == MCFragment::FT_Fill; 278 } 279 static bool classof(const MCFillFragment *) { return true; } 280 281 virtual void dump(); 282}; 283 284class MCOrgFragment : public MCFragment { 285 /// Offset - The offset this fragment should start at. 286 const MCExpr *Offset; 287 288 /// Value - Value to use for filling bytes. 289 int8_t Value; 290 291public: 292 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0) 293 : MCFragment(FT_Org, SD), 294 Offset(&_Offset), Value(_Value) {} 295 296 /// @name Accessors 297 /// @{ 298 299 uint64_t getMaxFileSize() const { 300 // FIXME: This doesn't make much sense. 301 return ~UINT64_C(0); 302 } 303 304 const MCExpr &getOffset() const { return *Offset; } 305 306 uint8_t getValue() const { return Value; } 307 308 /// @} 309 310 static bool classof(const MCFragment *F) { 311 return F->getKind() == MCFragment::FT_Org; 312 } 313 static bool classof(const MCOrgFragment *) { return true; } 314 315 virtual void dump(); 316}; 317 318/// MCZeroFillFragment - Represent data which has a fixed size and alignment, 319/// but requires no physical space in the object file. 320class MCZeroFillFragment : public MCFragment { 321 /// Size - The size of this fragment. 322 uint64_t Size; 323 324 /// Alignment - The alignment for this fragment. 325 unsigned Alignment; 326 327public: 328 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0) 329 : MCFragment(FT_ZeroFill, SD), 330 Size(_Size), Alignment(_Alignment) {} 331 332 /// @name Accessors 333 /// @{ 334 335 uint64_t getMaxFileSize() const { 336 // FIXME: This also doesn't make much sense, this method is misnamed. 337 return ~UINT64_C(0); 338 } 339 340 uint64_t getSize() const { return Size; } 341 342 unsigned getAlignment() const { return Alignment; } 343 344 /// @} 345 346 static bool classof(const MCFragment *F) { 347 return F->getKind() == MCFragment::FT_ZeroFill; 348 } 349 static bool classof(const MCZeroFillFragment *) { return true; } 350 351 virtual void dump(); 352}; 353 354// FIXME: Should this be a separate class, or just merged into MCSection? Since 355// we anticipate the fast path being through an MCAssembler, the only reason to 356// keep it out is for API abstraction. 357class MCSectionData : public ilist_node<MCSectionData> { 358 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT 359 void operator=(const MCSectionData&); // DO NOT IMPLEMENT 360 361public: 362 typedef iplist<MCFragment> FragmentListType; 363 364 typedef FragmentListType::const_iterator const_iterator; 365 typedef FragmentListType::iterator iterator; 366 367 typedef FragmentListType::const_reverse_iterator const_reverse_iterator; 368 typedef FragmentListType::reverse_iterator reverse_iterator; 369 370private: 371 iplist<MCFragment> Fragments; 372 const MCSection *Section; 373 374 /// Alignment - The maximum alignment seen in this section. 375 unsigned Alignment; 376 377 /// @name Assembler Backend Data 378 /// @{ 379 // 380 // FIXME: This could all be kept private to the assembler implementation. 381 382 /// Address - The computed address of this section. This is ~0 until 383 /// initialized. 384 uint64_t Address; 385 386 /// Size - The content size of this section. This is ~0 until initialized. 387 uint64_t Size; 388 389 /// FileSize - The size of this section in the object file. This is ~0 until 390 /// initialized. 391 uint64_t FileSize; 392 393 /// HasInstructions - Whether this section has had instructions emitted into 394 /// it. 395 unsigned HasInstructions : 1; 396 397 /// @} 398 399public: 400 // Only for use as sentinel. 401 MCSectionData(); 402 MCSectionData(const MCSection &Section, MCAssembler *A = 0); 403 404 const MCSection &getSection() const { return *Section; } 405 406 unsigned getAlignment() const { return Alignment; } 407 void setAlignment(unsigned Value) { Alignment = Value; } 408 409 /// @name Fragment Access 410 /// @{ 411 412 const FragmentListType &getFragmentList() const { return Fragments; } 413 FragmentListType &getFragmentList() { return Fragments; } 414 415 iterator begin() { return Fragments.begin(); } 416 const_iterator begin() const { return Fragments.begin(); } 417 418 iterator end() { return Fragments.end(); } 419 const_iterator end() const { return Fragments.end(); } 420 421 reverse_iterator rbegin() { return Fragments.rbegin(); } 422 const_reverse_iterator rbegin() const { return Fragments.rbegin(); } 423 424 reverse_iterator rend() { return Fragments.rend(); } 425 const_reverse_iterator rend() const { return Fragments.rend(); } 426 427 size_t size() const { return Fragments.size(); } 428 429 bool empty() const { return Fragments.empty(); } 430 431 /// @} 432 /// @name Assembler Backend Support 433 /// @{ 434 // 435 // FIXME: This could all be kept private to the assembler implementation. 436 437 uint64_t getAddress() const { 438 assert(Address != ~UINT64_C(0) && "Address not set!"); 439 return Address; 440 } 441 void setAddress(uint64_t Value) { Address = Value; } 442 443 uint64_t getSize() const { 444 assert(Size != ~UINT64_C(0) && "File size not set!"); 445 return Size; 446 } 447 void setSize(uint64_t Value) { Size = Value; } 448 449 uint64_t getFileSize() const { 450 assert(FileSize != ~UINT64_C(0) && "File size not set!"); 451 return FileSize; 452 } 453 void setFileSize(uint64_t Value) { FileSize = Value; } 454 455 bool hasInstructions() const { return HasInstructions; } 456 void setHasInstructions(bool Value) { HasInstructions = Value; } 457 458 /// @} 459 460 void dump(); 461}; 462 463// FIXME: Same concerns as with SectionData. 464class MCSymbolData : public ilist_node<MCSymbolData> { 465public: 466 const MCSymbol *Symbol; 467 468 /// Fragment - The fragment this symbol's value is relative to, if any. 469 MCFragment *Fragment; 470 471 /// Offset - The offset to apply to the fragment address to form this symbol's 472 /// value. 473 uint64_t Offset; 474 475 /// IsExternal - True if this symbol is visible outside this translation 476 /// unit. 477 unsigned IsExternal : 1; 478 479 /// IsPrivateExtern - True if this symbol is private extern. 480 unsigned IsPrivateExtern : 1; 481 482 /// CommonSize - The size of the symbol, if it is 'common', or 0. 483 // 484 // FIXME: Pack this in with other fields? We could put it in offset, since a 485 // common symbol can never get a definition. 486 uint64_t CommonSize; 487 488 /// CommonAlign - The alignment of the symbol, if it is 'common'. 489 // 490 // FIXME: Pack this in with other fields? 491 unsigned CommonAlign; 492 493 /// Flags - The Flags field is used by object file implementations to store 494 /// additional per symbol information which is not easily classified. 495 uint32_t Flags; 496 497 /// Index - Index field, for use by the object file implementation. 498 uint64_t Index; 499 500public: 501 // Only for use as sentinel. 502 MCSymbolData(); 503 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset, 504 MCAssembler *A = 0); 505 506 /// @name Accessors 507 /// @{ 508 509 const MCSymbol &getSymbol() const { return *Symbol; } 510 511 MCFragment *getFragment() const { return Fragment; } 512 void setFragment(MCFragment *Value) { Fragment = Value; } 513 514 uint64_t getOffset() const { return Offset; } 515 void setOffset(uint64_t Value) { Offset = Value; } 516 517 uint64_t getAddress() const { 518 assert(getFragment() && "Invalid getAddress() on undefined symbol!"); 519 return getFragment()->getAddress() + getOffset(); 520 } 521 522 /// @} 523 /// @name Symbol Attributes 524 /// @{ 525 526 bool isExternal() const { return IsExternal; } 527 void setExternal(bool Value) { IsExternal = Value; } 528 529 bool isPrivateExtern() const { return IsPrivateExtern; } 530 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; } 531 532 /// isCommon - Is this a 'common' symbol. 533 bool isCommon() const { return CommonSize != 0; } 534 535 /// setCommon - Mark this symbol as being 'common'. 536 /// 537 /// \param Size - The size of the symbol. 538 /// \param Align - The alignment of the symbol. 539 void setCommon(uint64_t Size, unsigned Align) { 540 CommonSize = Size; 541 CommonAlign = Align; 542 } 543 544 /// getCommonSize - Return the size of a 'common' symbol. 545 uint64_t getCommonSize() const { 546 assert(isCommon() && "Not a 'common' symbol!"); 547 return CommonSize; 548 } 549 550 /// getCommonAlignment - Return the alignment of a 'common' symbol. 551 unsigned getCommonAlignment() const { 552 assert(isCommon() && "Not a 'common' symbol!"); 553 return CommonAlign; 554 } 555 556 /// getFlags - Get the (implementation defined) symbol flags. 557 uint32_t getFlags() const { return Flags; } 558 559 /// setFlags - Set the (implementation defined) symbol flags. 560 void setFlags(uint32_t Value) { Flags = Value; } 561 562 /// getIndex - Get the (implementation defined) index. 563 uint64_t getIndex() const { return Index; } 564 565 /// setIndex - Set the (implementation defined) index. 566 void setIndex(uint64_t Value) { Index = Value; } 567 568 /// @} 569 570 void dump(); 571}; 572 573// FIXME: This really doesn't belong here. See comments below. 574struct IndirectSymbolData { 575 MCSymbol *Symbol; 576 MCSectionData *SectionData; 577}; 578 579class MCAssembler { 580public: 581 typedef iplist<MCSectionData> SectionDataListType; 582 typedef iplist<MCSymbolData> SymbolDataListType; 583 584 typedef SectionDataListType::const_iterator const_iterator; 585 typedef SectionDataListType::iterator iterator; 586 587 typedef SymbolDataListType::const_iterator const_symbol_iterator; 588 typedef SymbolDataListType::iterator symbol_iterator; 589 590 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator; 591 592private: 593 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT 594 void operator=(const MCAssembler&); // DO NOT IMPLEMENT 595 596 MCContext &Context; 597 598 TargetAsmBackend &Backend; 599 600 raw_ostream &OS; 601 602 iplist<MCSectionData> Sections; 603 604 iplist<MCSymbolData> Symbols; 605 606 /// The map of sections to their associated assembler backend data. 607 // 608 // FIXME: Avoid this indirection? 609 DenseMap<const MCSection*, MCSectionData*> SectionMap; 610 611 /// The map of symbols to their associated assembler backend data. 612 // 613 // FIXME: Avoid this indirection? 614 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; 615 616 std::vector<IndirectSymbolData> IndirectSymbols; 617 618 unsigned SubsectionsViaSymbols : 1; 619 620private: 621 /// LayoutSection - Assign offsets and sizes to the fragments in the section 622 /// \arg SD, and update the section size. The section file offset should 623 /// already have been computed. 624 void LayoutSection(MCSectionData &SD); 625 626public: 627 /// Construct a new assembler instance. 628 /// 629 /// \arg OS - The stream to output to. 630 // 631 // FIXME: How are we going to parameterize this? Two obvious options are stay 632 // concrete and require clients to pass in a target like object. The other 633 // option is to make this abstract, and have targets provide concrete 634 // implementations as we do with AsmParser. 635 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS); 636 ~MCAssembler(); 637 638 MCContext &getContext() const { return Context; } 639 640 TargetAsmBackend &getBackend() const { return Backend; } 641 642 /// Finish - Do final processing and write the object to the output stream. 643 void Finish(); 644 645 // FIXME: This does not belong here. 646 bool getSubsectionsViaSymbols() const { 647 return SubsectionsViaSymbols; 648 } 649 void setSubsectionsViaSymbols(bool Value) { 650 SubsectionsViaSymbols = Value; 651 } 652 653 /// @name Section List Access 654 /// @{ 655 656 const SectionDataListType &getSectionList() const { return Sections; } 657 SectionDataListType &getSectionList() { return Sections; } 658 659 iterator begin() { return Sections.begin(); } 660 const_iterator begin() const { return Sections.begin(); } 661 662 iterator end() { return Sections.end(); } 663 const_iterator end() const { return Sections.end(); } 664 665 size_t size() const { return Sections.size(); } 666 667 /// @} 668 /// @name Symbol List Access 669 /// @{ 670 671 const SymbolDataListType &getSymbolList() const { return Symbols; } 672 SymbolDataListType &getSymbolList() { return Symbols; } 673 674 symbol_iterator symbol_begin() { return Symbols.begin(); } 675 const_symbol_iterator symbol_begin() const { return Symbols.begin(); } 676 677 symbol_iterator symbol_end() { return Symbols.end(); } 678 const_symbol_iterator symbol_end() const { return Symbols.end(); } 679 680 size_t symbol_size() const { return Symbols.size(); } 681 682 /// @} 683 /// @name Indirect Symbol List Access 684 /// @{ 685 686 // FIXME: This is a total hack, this should not be here. Once things are 687 // factored so that the streamer has direct access to the .o writer, it can 688 // disappear. 689 std::vector<IndirectSymbolData> &getIndirectSymbols() { 690 return IndirectSymbols; 691 } 692 693 indirect_symbol_iterator indirect_symbol_begin() { 694 return IndirectSymbols.begin(); 695 } 696 697 indirect_symbol_iterator indirect_symbol_end() { 698 return IndirectSymbols.end(); 699 } 700 701 size_t indirect_symbol_size() const { return IndirectSymbols.size(); } 702 703 /// @} 704 /// @name Backend Data Access 705 /// @{ 706 707 MCSectionData &getSectionData(const MCSection &Section) { 708 MCSectionData *&Entry = SectionMap[&Section]; 709 assert(Entry && "Missing section data!"); 710 return *Entry; 711 } 712 713 MCSectionData &getOrCreateSectionData(const MCSection &Section, 714 bool *Created = 0) { 715 MCSectionData *&Entry = SectionMap[&Section]; 716 717 if (Created) *Created = !Entry; 718 if (!Entry) 719 Entry = new MCSectionData(Section, this); 720 721 return *Entry; 722 } 723 724 MCSymbolData &getSymbolData(const MCSymbol &Symbol) { 725 MCSymbolData *&Entry = SymbolMap[&Symbol]; 726 assert(Entry && "Missing symbol data!"); 727 return *Entry; 728 } 729 730 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol, 731 bool *Created = 0) { 732 MCSymbolData *&Entry = SymbolMap[&Symbol]; 733 734 if (Created) *Created = !Entry; 735 if (!Entry) 736 Entry = new MCSymbolData(Symbol, 0, 0, this); 737 738 return *Entry; 739 } 740 741 /// @} 742 743 void dump(); 744}; 745 746} // end namespace llvm 747 748#endif 749