1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
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.
35 class TargetAsmBackend;
37 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
38 /// which needs to be rewritten. This region will either be rewritten by the
39 /// assembler or cause a relocation entry to be generated.
41 /// Offset - The offset inside the fragment which needs to be rewritten.
44 /// Value - The expression to eventually write into the fragment.
47 /// Kind - The fixup kind.
51 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
52 : Offset(_Offset), Value(&_Value), Kind(_Kind) {}
55 class MCFragment : public ilist_node<MCFragment> {
56 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
57 void operator=(const MCFragment&); // DO NOT IMPLEMENT
71 /// Parent - The data for the section this fragment is in.
72 MCSectionData *Parent;
74 /// @name Assembler Backend Data
77 // FIXME: This could all be kept private to the assembler implementation.
79 /// Offset - The offset of this fragment in its section. This is ~0 until
83 /// FileSize - The file size of this section. This is ~0 until initialized.
89 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
94 virtual ~MCFragment();
96 FragmentType getKind() const { return Kind; }
98 MCSectionData *getParent() const { return Parent; }
99 void setParent(MCSectionData *Value) { Parent = Value; }
101 // FIXME: This should be abstract, fix sentinel.
102 virtual uint64_t getMaxFileSize() const {
103 assert(0 && "Invalid getMaxFileSize call!");
107 /// @name Assembler Backend Support
110 // FIXME: This could all be kept private to the assembler implementation.
112 uint64_t getAddress() const;
114 uint64_t getFileSize() const {
115 assert(FileSize != ~UINT64_C(0) && "File size not set!");
118 void setFileSize(uint64_t Value) {
119 assert(Value <= getMaxFileSize() && "Invalid file size!");
123 uint64_t getOffset() const {
124 assert(Offset != ~UINT64_C(0) && "File offset not set!");
127 void setOffset(uint64_t Value) { Offset = Value; }
131 static bool classof(const MCFragment *O) { return true; }
136 class MCDataFragment : public MCFragment {
137 SmallString<32> Contents;
139 /// Fixups - The list of fixups in this fragment.
140 std::vector<MCAsmFixup> Fixups;
143 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
144 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
147 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
152 uint64_t getMaxFileSize() const {
153 return Contents.size();
156 SmallString<32> &getContents() { return Contents; }
157 const SmallString<32> &getContents() const { return Contents; }
161 /// @name Fixup Access
164 void addFixup(MCAsmFixup Fixup) {
165 // Enforce invariant that fixups are in offset order.
166 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
167 "Fixups must be added in order!");
168 Fixups.push_back(Fixup);
171 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
172 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
174 fixup_iterator fixup_begin() { return Fixups.begin(); }
175 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
177 fixup_iterator fixup_end() {return Fixups.end();}
178 const_fixup_iterator fixup_end() const {return Fixups.end();}
180 size_t fixup_size() const { return Fixups.size(); }
184 static bool classof(const MCFragment *F) {
185 return F->getKind() == MCFragment::FT_Data;
187 static bool classof(const MCDataFragment *) { return true; }
192 class MCAlignFragment : public MCFragment {
193 /// Alignment - The alignment to ensure, in bytes.
196 /// Value - Value to use for filling padding bytes.
199 /// ValueSize - The size of the integer (in bytes) of \arg Value.
202 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
203 /// cannot be satisfied in this width then this fragment is ignored.
204 unsigned MaxBytesToEmit;
206 /// EmitNops - true when aligning code and optimal nops to be used for
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) {}
221 uint64_t getMaxFileSize() const {
222 return std::max(Alignment - 1, MaxBytesToEmit);
225 unsigned getAlignment() const { return Alignment; }
227 int64_t getValue() const { return Value; }
229 unsigned getValueSize() const { return ValueSize; }
231 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
233 unsigned getEmitNops() const { return EmitNops; }
237 static bool classof(const MCFragment *F) {
238 return F->getKind() == MCFragment::FT_Align;
240 static bool classof(const MCAlignFragment *) { return true; }
245 class MCFillFragment : public MCFragment {
246 /// Value - Value to use for filling bytes.
249 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
252 /// Count - The number of copies of \arg Value to insert.
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) {}
264 uint64_t getMaxFileSize() const {
265 return ValueSize * Count;
268 int64_t getValue() const { return Value; }
270 unsigned getValueSize() const { return ValueSize; }
272 uint64_t getCount() const { return Count; }
276 static bool classof(const MCFragment *F) {
277 return F->getKind() == MCFragment::FT_Fill;
279 static bool classof(const MCFillFragment *) { return true; }
284 class MCOrgFragment : public MCFragment {
285 /// Offset - The offset this fragment should start at.
286 const MCExpr *Offset;
288 /// Value - Value to use for filling bytes.
292 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
293 : MCFragment(FT_Org, SD),
294 Offset(&_Offset), Value(_Value) {}
299 uint64_t getMaxFileSize() const {
300 // FIXME: This doesn't make much sense.
304 const MCExpr &getOffset() const { return *Offset; }
306 uint8_t getValue() const { return Value; }
310 static bool classof(const MCFragment *F) {
311 return F->getKind() == MCFragment::FT_Org;
313 static bool classof(const MCOrgFragment *) { return true; }
318 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
319 /// but requires no physical space in the object file.
320 class MCZeroFillFragment : public MCFragment {
321 /// Size - The size of this fragment.
324 /// Alignment - The alignment for this fragment.
328 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
329 : MCFragment(FT_ZeroFill, SD),
330 Size(_Size), Alignment(_Alignment) {}
335 uint64_t getMaxFileSize() const {
336 // FIXME: This also doesn't make much sense, this method is misnamed.
340 uint64_t getSize() const { return Size; }
342 unsigned getAlignment() const { return Alignment; }
346 static bool classof(const MCFragment *F) {
347 return F->getKind() == MCFragment::FT_ZeroFill;
349 static bool classof(const MCZeroFillFragment *) { return true; }
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.
357 class MCSectionData : public ilist_node<MCSectionData> {
358 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
359 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
362 typedef iplist<MCFragment> FragmentListType;
364 typedef FragmentListType::const_iterator const_iterator;
365 typedef FragmentListType::iterator iterator;
367 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
368 typedef FragmentListType::reverse_iterator reverse_iterator;
371 iplist<MCFragment> Fragments;
372 const MCSection *Section;
374 /// Alignment - The maximum alignment seen in this section.
377 /// @name Assembler Backend Data
380 // FIXME: This could all be kept private to the assembler implementation.
382 /// Address - The computed address of this section. This is ~0 until
386 /// Size - The content size of this section. This is ~0 until initialized.
389 /// FileSize - The size of this section in the object file. This is ~0 until
393 /// HasInstructions - Whether this section has had instructions emitted into
395 unsigned HasInstructions : 1;
400 // Only for use as sentinel.
402 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
404 const MCSection &getSection() const { return *Section; }
406 unsigned getAlignment() const { return Alignment; }
407 void setAlignment(unsigned Value) { Alignment = Value; }
409 /// @name Fragment Access
412 const FragmentListType &getFragmentList() const { return Fragments; }
413 FragmentListType &getFragmentList() { return Fragments; }
415 iterator begin() { return Fragments.begin(); }
416 const_iterator begin() const { return Fragments.begin(); }
418 iterator end() { return Fragments.end(); }
419 const_iterator end() const { return Fragments.end(); }
421 reverse_iterator rbegin() { return Fragments.rbegin(); }
422 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
424 reverse_iterator rend() { return Fragments.rend(); }
425 const_reverse_iterator rend() const { return Fragments.rend(); }
427 size_t size() const { return Fragments.size(); }
429 bool empty() const { return Fragments.empty(); }
432 /// @name Assembler Backend Support
435 // FIXME: This could all be kept private to the assembler implementation.
437 uint64_t getAddress() const {
438 assert(Address != ~UINT64_C(0) && "Address not set!");
441 void setAddress(uint64_t Value) { Address = Value; }
443 uint64_t getSize() const {
444 assert(Size != ~UINT64_C(0) && "File size not set!");
447 void setSize(uint64_t Value) { Size = Value; }
449 uint64_t getFileSize() const {
450 assert(FileSize != ~UINT64_C(0) && "File size not set!");
453 void setFileSize(uint64_t Value) { FileSize = Value; }
455 bool hasInstructions() const { return HasInstructions; }
456 void setHasInstructions(bool Value) { HasInstructions = Value; }
463 // FIXME: Same concerns as with SectionData.
464 class MCSymbolData : public ilist_node<MCSymbolData> {
466 const MCSymbol *Symbol;
468 /// Fragment - The fragment this symbol's value is relative to, if any.
469 MCFragment *Fragment;
471 /// Offset - The offset to apply to the fragment address to form this symbol's
475 /// IsExternal - True if this symbol is visible outside this translation
477 unsigned IsExternal : 1;
479 /// IsPrivateExtern - True if this symbol is private extern.
480 unsigned IsPrivateExtern : 1;
482 /// CommonSize - The size of the symbol, if it is 'common', or 0.
484 // FIXME: Pack this in with other fields? We could put it in offset, since a
485 // common symbol can never get a definition.
488 /// CommonAlign - The alignment of the symbol, if it is 'common'.
490 // FIXME: Pack this in with other fields?
491 unsigned CommonAlign;
493 /// Flags - The Flags field is used by object file implementations to store
494 /// additional per symbol information which is not easily classified.
497 /// Index - Index field, for use by the object file implementation.
501 // Only for use as sentinel.
503 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
509 const MCSymbol &getSymbol() const { return *Symbol; }
511 MCFragment *getFragment() const { return Fragment; }
512 void setFragment(MCFragment *Value) { Fragment = Value; }
514 uint64_t getOffset() const { return Offset; }
515 void setOffset(uint64_t Value) { Offset = Value; }
517 uint64_t getAddress() const {
518 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
519 return getFragment()->getAddress() + getOffset();
523 /// @name Symbol Attributes
526 bool isExternal() const { return IsExternal; }
527 void setExternal(bool Value) { IsExternal = Value; }
529 bool isPrivateExtern() const { return IsPrivateExtern; }
530 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
532 /// isCommon - Is this a 'common' symbol.
533 bool isCommon() const { return CommonSize != 0; }
535 /// setCommon - Mark this symbol as being 'common'.
537 /// \param Size - The size of the symbol.
538 /// \param Align - The alignment of the symbol.
539 void setCommon(uint64_t Size, unsigned Align) {
544 /// getCommonSize - Return the size of a 'common' symbol.
545 uint64_t getCommonSize() const {
546 assert(isCommon() && "Not a 'common' symbol!");
550 /// getCommonAlignment - Return the alignment of a 'common' symbol.
551 unsigned getCommonAlignment() const {
552 assert(isCommon() && "Not a 'common' symbol!");
556 /// getFlags - Get the (implementation defined) symbol flags.
557 uint32_t getFlags() const { return Flags; }
559 /// setFlags - Set the (implementation defined) symbol flags.
560 void setFlags(uint32_t Value) { Flags = Value; }
562 /// getIndex - Get the (implementation defined) index.
563 uint64_t getIndex() const { return Index; }
565 /// setIndex - Set the (implementation defined) index.
566 void setIndex(uint64_t Value) { Index = Value; }
573 // FIXME: This really doesn't belong here. See comments below.
574 struct IndirectSymbolData {
576 MCSectionData *SectionData;
581 typedef iplist<MCSectionData> SectionDataListType;
582 typedef iplist<MCSymbolData> SymbolDataListType;
584 typedef SectionDataListType::const_iterator const_iterator;
585 typedef SectionDataListType::iterator iterator;
587 typedef SymbolDataListType::const_iterator const_symbol_iterator;
588 typedef SymbolDataListType::iterator symbol_iterator;
590 typedef std::vector<IndirectSymbolData>::const_iterator
591 const_indirect_symbol_iterator;
592 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
595 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
596 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
600 TargetAsmBackend &Backend;
602 MCCodeEmitter &Emitter;
606 iplist<MCSectionData> Sections;
608 iplist<MCSymbolData> Symbols;
610 /// The map of sections to their associated assembler backend data.
612 // FIXME: Avoid this indirection?
613 DenseMap<const MCSection*, MCSectionData*> SectionMap;
615 /// The map of symbols to their associated assembler backend data.
617 // FIXME: Avoid this indirection?
618 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
620 std::vector<IndirectSymbolData> IndirectSymbols;
622 unsigned SubsectionsViaSymbols : 1;
625 /// Evaluate a fixup to a relocatable expression and the value which should be
626 /// placed into the fixup.
628 /// \param Layout The layout to use for evaluation.
629 /// \param Fixup The fixup to evaluate.
630 /// \param DF The fragment the fixup is inside.
631 /// \param Target [out] On return, the relocatable expression the fixup
633 /// \param Value [out] On return, the value of the fixup as currently layed
635 /// \return Whether the fixup value was fully resolved. This is true if the
636 /// \arg Value result is fixed, otherwise the value may change due to
638 bool EvaluateFixup(const MCAsmLayout &Layout,
639 MCAsmFixup &Fixup, MCDataFragment *DF,
640 MCValue &Target, uint64_t &Value) const;
642 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
643 /// (increased in size, in order to hold its value correctly).
644 bool FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF);
646 /// LayoutSection - Assign offsets and sizes to the fragments in the section
647 /// \arg SD, and update the section size. The section file offset should
648 /// already have been computed.
649 void LayoutSection(MCSectionData &SD);
651 /// LayoutOnce - Perform one layout iteration and return true if any offsets
656 /// Find the symbol which defines the atom containing given address, inside
657 /// the given section, or null if there is no such symbol.
659 // FIXME: Eliminate this, it is very slow.
660 const MCSymbolData *getAtomForAddress(const MCSectionData *Section,
661 uint64_t Address) const;
663 /// Find the symbol which defines the atom containing the given symbol, or
664 /// null if there is no such symbol.
666 // FIXME: Eliminate this, it is very slow.
667 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
669 /// Check whether a particular symbol is visible to the linker and is required
670 /// in the symbol table, or whether it can be discarded by the assembler. This
671 /// also effects whether the assembler treats the label as potentially
672 /// defining a separate atom.
673 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
675 /// Emit the section contents using the given object writer.
677 // FIXME: Should MCAssembler always have a reference to the object writer?
678 void WriteSectionData(const MCSectionData *Section, MCObjectWriter *OW) const;
681 /// Construct a new assembler instance.
683 /// \arg OS - The stream to output to.
685 // FIXME: How are we going to parameterize this? Two obvious options are stay
686 // concrete and require clients to pass in a target like object. The other
687 // option is to make this abstract, and have targets provide concrete
688 // implementations as we do with AsmParser.
689 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
690 MCCodeEmitter &_Emitter, raw_ostream &OS);
693 MCContext &getContext() const { return Context; }
695 TargetAsmBackend &getBackend() const { return Backend; }
697 MCCodeEmitter &getEmitter() const { return Emitter; }
699 /// Finish - Do final processing and write the object to the output stream.
702 // FIXME: This does not belong here.
703 bool getSubsectionsViaSymbols() const {
704 return SubsectionsViaSymbols;
706 void setSubsectionsViaSymbols(bool Value) {
707 SubsectionsViaSymbols = Value;
710 /// @name Section List Access
713 const SectionDataListType &getSectionList() const { return Sections; }
714 SectionDataListType &getSectionList() { return Sections; }
716 iterator begin() { return Sections.begin(); }
717 const_iterator begin() const { return Sections.begin(); }
719 iterator end() { return Sections.end(); }
720 const_iterator end() const { return Sections.end(); }
722 size_t size() const { return Sections.size(); }
725 /// @name Symbol List Access
728 const SymbolDataListType &getSymbolList() const { return Symbols; }
729 SymbolDataListType &getSymbolList() { return Symbols; }
731 symbol_iterator symbol_begin() { return Symbols.begin(); }
732 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
734 symbol_iterator symbol_end() { return Symbols.end(); }
735 const_symbol_iterator symbol_end() const { return Symbols.end(); }
737 size_t symbol_size() const { return Symbols.size(); }
740 /// @name Indirect Symbol List Access
743 // FIXME: This is a total hack, this should not be here. Once things are
744 // factored so that the streamer has direct access to the .o writer, it can
746 std::vector<IndirectSymbolData> &getIndirectSymbols() {
747 return IndirectSymbols;
750 indirect_symbol_iterator indirect_symbol_begin() {
751 return IndirectSymbols.begin();
753 const_indirect_symbol_iterator indirect_symbol_begin() const {
754 return IndirectSymbols.begin();
757 indirect_symbol_iterator indirect_symbol_end() {
758 return IndirectSymbols.end();
760 const_indirect_symbol_iterator indirect_symbol_end() const {
761 return IndirectSymbols.end();
764 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
767 /// @name Backend Data Access
770 MCSectionData &getSectionData(const MCSection &Section) const {
771 MCSectionData *Entry = SectionMap.lookup(&Section);
772 assert(Entry && "Missing section data!");
776 MCSectionData &getOrCreateSectionData(const MCSection &Section,
778 MCSectionData *&Entry = SectionMap[&Section];
780 if (Created) *Created = !Entry;
782 Entry = new MCSectionData(Section, this);
787 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
788 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
789 assert(Entry && "Missing symbol data!");
793 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
795 MCSymbolData *&Entry = SymbolMap[&Symbol];
797 if (Created) *Created = !Entry;
799 Entry = new MCSymbolData(Symbol, 0, 0, this);
809 } // end namespace llvm