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/PointerIntPair.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/ADT/ilist_node.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCLinkerOptimizationHint.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/DataTypes.h"
27 #include <vector> // FIXME: Shouldn't be needed.
40 class MCSubtargetInfo;
46 class MCFragment : public ilist_node<MCFragment> {
47 friend class MCAsmLayout;
49 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
50 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
56 FT_CompactEncodedInst,
68 /// Parent - The data for the section this fragment is in.
69 MCSectionData *Parent;
71 /// Atom - The atom this fragment is in, as represented by it's defining
75 /// @name Assembler Backend Data
78 // FIXME: This could all be kept private to the assembler implementation.
80 /// Offset - The offset of this fragment in its section. This is ~0 until
84 /// LayoutOrder - The layout order of this fragment.
90 MCFragment(FragmentType _Kind, MCSectionData *_Parent = nullptr);
95 virtual ~MCFragment();
97 FragmentType getKind() const { return Kind; }
99 MCSectionData *getParent() const { return Parent; }
100 void setParent(MCSectionData *Value) { Parent = Value; }
102 MCSymbolData *getAtom() const { return Atom; }
103 void setAtom(MCSymbolData *Value) { Atom = Value; }
105 unsigned getLayoutOrder() const { return LayoutOrder; }
106 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
108 /// \brief Does this fragment have instructions emitted into it? By default
109 /// this is false, but specific fragment types may set it to true.
110 virtual bool hasInstructions() const { return false; }
112 /// \brief Should this fragment be placed at the end of an aligned bundle?
113 virtual bool alignToBundleEnd() const { return false; }
114 virtual void setAlignToBundleEnd(bool V) { }
116 /// \brief Get the padding size that must be inserted before this fragment.
117 /// Used for bundling. By default, no padding is inserted.
118 /// Note that padding size is restricted to 8 bits. This is an optimization
119 /// to reduce the amount of space used for each fragment. In practice, larger
120 /// padding should never be required.
121 virtual uint8_t getBundlePadding() const {
125 /// \brief Set the padding size for this fragment. By default it's a no-op,
126 /// and only some fragments have a meaningful implementation.
127 virtual void setBundlePadding(uint8_t N) {
133 /// Interface implemented by fragments that contain encoded instructions and/or
136 class MCEncodedFragment : public MCFragment {
137 virtual void anchor();
139 uint8_t BundlePadding;
141 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = nullptr)
142 : MCFragment(FType, SD), BundlePadding(0)
145 virtual ~MCEncodedFragment();
147 virtual SmallVectorImpl<char> &getContents() = 0;
148 virtual const SmallVectorImpl<char> &getContents() const = 0;
150 uint8_t getBundlePadding() const override {
151 return BundlePadding;
154 void setBundlePadding(uint8_t N) override {
158 static bool classof(const MCFragment *F) {
159 MCFragment::FragmentType Kind = F->getKind();
163 case MCFragment::FT_Relaxable:
164 case MCFragment::FT_CompactEncodedInst:
165 case MCFragment::FT_Data:
171 /// Interface implemented by fragments that contain encoded instructions and/or
172 /// data and also have fixups registered.
174 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
175 void anchor() override;
178 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
179 MCSectionData *SD = nullptr)
180 : MCEncodedFragment(FType, SD)
184 virtual ~MCEncodedFragmentWithFixups();
186 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
187 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
189 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
190 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
192 virtual fixup_iterator fixup_begin() = 0;
193 virtual const_fixup_iterator fixup_begin() const = 0;
194 virtual fixup_iterator fixup_end() = 0;
195 virtual const_fixup_iterator fixup_end() const = 0;
197 static bool classof(const MCFragment *F) {
198 MCFragment::FragmentType Kind = F->getKind();
199 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
203 /// Fragment for data and encoded instructions.
205 class MCDataFragment : public MCEncodedFragmentWithFixups {
206 void anchor() override;
208 /// \brief Does this fragment contain encoded instructions anywhere in it?
209 bool HasInstructions;
211 /// \brief Should this fragment be aligned to the end of a bundle?
212 bool AlignToBundleEnd;
214 SmallVector<char, 32> Contents;
216 /// Fixups - The list of fixups in this fragment.
217 SmallVector<MCFixup, 4> Fixups;
219 MCDataFragment(MCSectionData *SD = nullptr)
220 : MCEncodedFragmentWithFixups(FT_Data, SD),
221 HasInstructions(false), AlignToBundleEnd(false)
225 SmallVectorImpl<char> &getContents() override { return Contents; }
226 const SmallVectorImpl<char> &getContents() const override {
230 SmallVectorImpl<MCFixup> &getFixups() override {
234 const SmallVectorImpl<MCFixup> &getFixups() const override {
238 bool hasInstructions() const override { return HasInstructions; }
239 virtual void setHasInstructions(bool V) { HasInstructions = V; }
241 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
242 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
244 fixup_iterator fixup_begin() override { return Fixups.begin(); }
245 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
247 fixup_iterator fixup_end() override {return Fixups.end();}
248 const_fixup_iterator fixup_end() const override {return Fixups.end();}
250 static bool classof(const MCFragment *F) {
251 return F->getKind() == MCFragment::FT_Data;
255 /// This is a compact (memory-size-wise) fragment for holding an encoded
256 /// instruction (non-relaxable) that has no fixups registered. When applicable,
257 /// it can be used instead of MCDataFragment and lead to lower memory
260 class MCCompactEncodedInstFragment : public MCEncodedFragment {
261 void anchor() override;
263 /// \brief Should this fragment be aligned to the end of a bundle?
264 bool AlignToBundleEnd;
266 SmallVector<char, 4> Contents;
268 MCCompactEncodedInstFragment(MCSectionData *SD = nullptr)
269 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
273 bool hasInstructions() const override {
277 SmallVectorImpl<char> &getContents() override { return Contents; }
278 const SmallVectorImpl<char> &getContents() const override { return Contents; }
280 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
281 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
283 static bool classof(const MCFragment *F) {
284 return F->getKind() == MCFragment::FT_CompactEncodedInst;
288 /// A relaxable fragment holds on to its MCInst, since it may need to be
289 /// relaxed during the assembler layout and relaxation stage.
291 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
292 void anchor() override;
294 /// Inst - The instruction this is a fragment for.
297 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
298 /// Keep a copy instead of a reference to make sure that updates to STI
299 /// in the assembler are not seen here.
300 const MCSubtargetInfo STI;
302 /// Contents - Binary data for the currently encoded instruction.
303 SmallVector<char, 8> Contents;
305 /// Fixups - The list of fixups in this fragment.
306 SmallVector<MCFixup, 1> Fixups;
309 MCRelaxableFragment(const MCInst &_Inst,
310 const MCSubtargetInfo &_STI,
311 MCSectionData *SD = nullptr)
312 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
315 SmallVectorImpl<char> &getContents() override { return Contents; }
316 const SmallVectorImpl<char> &getContents() const override { return Contents; }
318 const MCInst &getInst() const { return Inst; }
319 void setInst(const MCInst& Value) { Inst = Value; }
321 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
323 SmallVectorImpl<MCFixup> &getFixups() override {
327 const SmallVectorImpl<MCFixup> &getFixups() const override {
331 bool hasInstructions() const override { return true; }
333 fixup_iterator fixup_begin() override { return Fixups.begin(); }
334 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
336 fixup_iterator fixup_end() override {return Fixups.end();}
337 const_fixup_iterator fixup_end() const override {return Fixups.end();}
339 static bool classof(const MCFragment *F) {
340 return F->getKind() == MCFragment::FT_Relaxable;
344 class MCAlignFragment : public MCFragment {
345 virtual void anchor();
347 /// Alignment - The alignment to ensure, in bytes.
350 /// Value - Value to use for filling padding bytes.
353 /// ValueSize - The size of the integer (in bytes) of \p Value.
356 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
357 /// cannot be satisfied in this width then this fragment is ignored.
358 unsigned MaxBytesToEmit;
360 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
361 /// of using the provided value. The exact interpretation of this flag is
362 /// target dependent.
366 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
367 unsigned _MaxBytesToEmit, MCSectionData *SD = nullptr)
368 : MCFragment(FT_Align, SD), Alignment(_Alignment),
369 Value(_Value),ValueSize(_ValueSize),
370 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
375 unsigned getAlignment() const { return Alignment; }
377 int64_t getValue() const { return Value; }
379 unsigned getValueSize() const { return ValueSize; }
381 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
383 bool hasEmitNops() const { return EmitNops; }
384 void setEmitNops(bool Value) { EmitNops = Value; }
388 static bool classof(const MCFragment *F) {
389 return F->getKind() == MCFragment::FT_Align;
393 class MCFillFragment : public MCFragment {
394 virtual void anchor();
396 /// Value - Value to use for filling bytes.
399 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
400 /// this is a virtual fill fragment.
403 /// Size - The number of bytes to insert.
407 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
408 MCSectionData *SD = nullptr)
409 : MCFragment(FT_Fill, SD),
410 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
411 assert((!ValueSize || (Size % ValueSize) == 0) &&
412 "Fill size must be a multiple of the value size!");
418 int64_t getValue() const { return Value; }
420 unsigned getValueSize() const { return ValueSize; }
422 uint64_t getSize() const { return Size; }
426 static bool classof(const MCFragment *F) {
427 return F->getKind() == MCFragment::FT_Fill;
431 class MCOrgFragment : public MCFragment {
432 virtual void anchor();
434 /// Offset - The offset this fragment should start at.
435 const MCExpr *Offset;
437 /// Value - Value to use for filling bytes.
441 MCOrgFragment(const MCExpr &_Offset, int8_t _Value,
442 MCSectionData *SD = nullptr)
443 : MCFragment(FT_Org, SD),
444 Offset(&_Offset), Value(_Value) {}
449 const MCExpr &getOffset() const { return *Offset; }
451 uint8_t getValue() const { return Value; }
455 static bool classof(const MCFragment *F) {
456 return F->getKind() == MCFragment::FT_Org;
460 class MCLEBFragment : public MCFragment {
461 virtual void anchor();
463 /// Value - The value this fragment should contain.
466 /// IsSigned - True if this is a sleb128, false if uleb128.
469 SmallString<8> Contents;
471 MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
472 MCSectionData *SD = nullptr)
473 : MCFragment(FT_LEB, SD),
474 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
479 const MCExpr &getValue() const { return *Value; }
481 bool isSigned() const { return IsSigned; }
483 SmallString<8> &getContents() { return Contents; }
484 const SmallString<8> &getContents() const { return Contents; }
488 static bool classof(const MCFragment *F) {
489 return F->getKind() == MCFragment::FT_LEB;
493 class MCDwarfLineAddrFragment : public MCFragment {
494 virtual void anchor();
496 /// LineDelta - the value of the difference between the two line numbers
497 /// between two .loc dwarf directives.
500 /// AddrDelta - The expression for the difference of the two symbols that
501 /// make up the address delta between two .loc dwarf directives.
502 const MCExpr *AddrDelta;
504 SmallString<8> Contents;
507 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
508 MCSectionData *SD = nullptr)
509 : MCFragment(FT_Dwarf, SD),
510 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
515 int64_t getLineDelta() const { return LineDelta; }
517 const MCExpr &getAddrDelta() const { return *AddrDelta; }
519 SmallString<8> &getContents() { return Contents; }
520 const SmallString<8> &getContents() const { return Contents; }
524 static bool classof(const MCFragment *F) {
525 return F->getKind() == MCFragment::FT_Dwarf;
529 class MCDwarfCallFrameFragment : public MCFragment {
530 virtual void anchor();
532 /// AddrDelta - The expression for the difference of the two symbols that
533 /// make up the address delta between two .cfi_* dwarf directives.
534 const MCExpr *AddrDelta;
536 SmallString<8> Contents;
539 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta,
540 MCSectionData *SD = nullptr)
541 : MCFragment(FT_DwarfFrame, SD),
542 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
547 const MCExpr &getAddrDelta() const { return *AddrDelta; }
549 SmallString<8> &getContents() { return Contents; }
550 const SmallString<8> &getContents() const { return Contents; }
554 static bool classof(const MCFragment *F) {
555 return F->getKind() == MCFragment::FT_DwarfFrame;
559 // FIXME: Should this be a separate class, or just merged into MCSection? Since
560 // we anticipate the fast path being through an MCAssembler, the only reason to
561 // keep it out is for API abstraction.
562 class MCSectionData : public ilist_node<MCSectionData> {
563 friend class MCAsmLayout;
565 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
566 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
569 typedef iplist<MCFragment> FragmentListType;
571 typedef FragmentListType::const_iterator const_iterator;
572 typedef FragmentListType::iterator iterator;
574 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
575 typedef FragmentListType::reverse_iterator reverse_iterator;
577 /// \brief Express the state of bundle locked groups while emitting code.
578 enum BundleLockStateType {
581 BundleLockedAlignToEnd
584 FragmentListType Fragments;
585 const MCSection *Section;
587 /// Ordinal - The section index in the assemblers section list.
590 /// LayoutOrder - The index of this section in the layout order.
591 unsigned LayoutOrder;
593 /// Alignment - The maximum alignment seen in this section.
596 /// \brief Keeping track of bundle-locked state.
597 BundleLockStateType BundleLockState;
599 /// \brief We've seen a bundle_lock directive but not its first instruction
601 bool BundleGroupBeforeFirstInst;
603 /// @name Assembler Backend Data
606 // FIXME: This could all be kept private to the assembler implementation.
608 /// HasInstructions - Whether this section has had instructions emitted into
610 unsigned HasInstructions : 1;
612 /// Mapping from subsection number to insertion point for subsection numbers
613 /// below that number.
614 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
619 // Only for use as sentinel.
621 MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
623 const MCSection &getSection() const { return *Section; }
625 unsigned getAlignment() const { return Alignment; }
626 void setAlignment(unsigned Value) { Alignment = Value; }
628 bool hasInstructions() const { return HasInstructions; }
629 void setHasInstructions(bool Value) { HasInstructions = Value; }
631 unsigned getOrdinal() const { return Ordinal; }
632 void setOrdinal(unsigned Value) { Ordinal = Value; }
634 unsigned getLayoutOrder() const { return LayoutOrder; }
635 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
637 /// @name Fragment Access
640 const FragmentListType &getFragmentList() const { return Fragments; }
641 FragmentListType &getFragmentList() { return Fragments; }
643 iterator begin() { return Fragments.begin(); }
644 const_iterator begin() const { return Fragments.begin(); }
646 iterator end() { return Fragments.end(); }
647 const_iterator end() const { return Fragments.end(); }
649 reverse_iterator rbegin() { return Fragments.rbegin(); }
650 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
652 reverse_iterator rend() { return Fragments.rend(); }
653 const_reverse_iterator rend() const { return Fragments.rend(); }
655 size_t size() const { return Fragments.size(); }
657 bool empty() const { return Fragments.empty(); }
659 iterator getSubsectionInsertionPoint(unsigned Subsection);
661 bool isBundleLocked() const {
662 return BundleLockState != NotBundleLocked;
665 BundleLockStateType getBundleLockState() const {
666 return BundleLockState;
669 void setBundleLockState(BundleLockStateType NewState) {
670 BundleLockState = NewState;
673 bool isBundleGroupBeforeFirstInst() const {
674 return BundleGroupBeforeFirstInst;
677 void setBundleGroupBeforeFirstInst(bool IsFirst) {
678 BundleGroupBeforeFirstInst = IsFirst;
686 // FIXME: Same concerns as with SectionData.
687 class MCSymbolData : public ilist_node<MCSymbolData> {
688 const MCSymbol *Symbol;
690 /// Fragment - The fragment this symbol's value is relative to, if any. Also
691 /// stores if this symbol is visible outside this translation unit (bit 0) or
692 /// if it is private extern (bit 1).
693 PointerIntPair<MCFragment *, 2> Fragment;
696 /// Offset - The offset to apply to the fragment address to form this
700 /// CommonSize - The size of the symbol, if it is 'common'.
704 /// SymbolSize - An expression describing how to calculate the size of
705 /// a symbol. If a symbol has no size this field will be NULL.
706 const MCExpr *SymbolSize;
708 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
710 // FIXME: Pack this in with other fields?
711 unsigned CommonAlign;
713 /// Flags - The Flags field is used by object file implementations to store
714 /// additional per symbol information which is not easily classified.
717 /// Index - Index field, for use by the object file implementation.
721 // Only for use as sentinel.
723 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
724 MCAssembler *A = nullptr);
729 const MCSymbol &getSymbol() const { return *Symbol; }
731 MCFragment *getFragment() const { return Fragment.getPointer(); }
732 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
734 uint64_t getOffset() const {
738 void setOffset(uint64_t Value) {
744 /// @name Symbol Attributes
747 bool isExternal() const { return Fragment.getInt() & 1; }
748 void setExternal(bool Value) {
749 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
752 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
753 void setPrivateExtern(bool Value) {
754 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
757 /// isCommon - Is this a 'common' symbol.
758 bool isCommon() const { return CommonAlign != -1U; }
760 /// setCommon - Mark this symbol as being 'common'.
762 /// \param Size - The size of the symbol.
763 /// \param Align - The alignment of the symbol.
764 void setCommon(uint64_t Size, unsigned Align) {
765 assert(getOffset() == 0);
770 /// getCommonSize - Return the size of a 'common' symbol.
771 uint64_t getCommonSize() const {
772 assert(isCommon() && "Not a 'common' symbol!");
776 void setSize(const MCExpr *SS) {
780 const MCExpr *getSize() const {
785 /// getCommonAlignment - Return the alignment of a 'common' symbol.
786 unsigned getCommonAlignment() const {
787 assert(isCommon() && "Not a 'common' symbol!");
791 /// getFlags - Get the (implementation defined) symbol flags.
792 uint32_t getFlags() const { return Flags; }
794 /// setFlags - Set the (implementation defined) symbol flags.
795 void setFlags(uint32_t Value) { Flags = Value; }
797 /// modifyFlags - Modify the flags via a mask
798 void modifyFlags(uint32_t Value, uint32_t Mask) {
799 Flags = (Flags & ~Mask) | Value;
802 /// getIndex - Get the (implementation defined) index.
803 uint64_t getIndex() const { return Index; }
805 /// setIndex - Set the (implementation defined) index.
806 void setIndex(uint64_t Value) { Index = Value; }
813 // FIXME: This really doesn't belong here. See comments below.
814 struct IndirectSymbolData {
816 MCSectionData *SectionData;
819 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
821 struct DataRegionData {
822 // This enum should be kept in sync w/ the mach-o definition in
823 // llvm/Object/MachOFormat.h.
824 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
830 friend class MCAsmLayout;
833 typedef iplist<MCSectionData> SectionDataListType;
834 typedef iplist<MCSymbolData> SymbolDataListType;
836 typedef SectionDataListType::const_iterator const_iterator;
837 typedef SectionDataListType::iterator iterator;
839 typedef SymbolDataListType::const_iterator const_symbol_iterator;
840 typedef SymbolDataListType::iterator symbol_iterator;
842 typedef iterator_range<symbol_iterator> symbol_range;
843 typedef iterator_range<const_symbol_iterator> const_symbol_range;
845 typedef std::vector<std::string> FileNameVectorType;
846 typedef FileNameVectorType::const_iterator const_file_name_iterator;
848 typedef std::vector<IndirectSymbolData>::const_iterator
849 const_indirect_symbol_iterator;
850 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
852 typedef std::vector<DataRegionData>::const_iterator
853 const_data_region_iterator;
854 typedef std::vector<DataRegionData>::iterator data_region_iterator;
856 /// MachO specific deployment target version info.
857 // A Major version of 0 indicates that no version information was supplied
858 // and so the corresponding load command should not be emitted.
860 MCVersionMinType Kind;
864 } VersionMinInfoType;
866 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
867 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
871 MCAsmBackend &Backend;
873 MCCodeEmitter &Emitter;
875 MCObjectWriter &Writer;
879 iplist<MCSectionData> Sections;
881 iplist<MCSymbolData> Symbols;
883 /// The map of sections to their associated assembler backend data.
885 // FIXME: Avoid this indirection?
886 DenseMap<const MCSection*, MCSectionData*> SectionMap;
888 /// The map of symbols to their associated assembler backend data.
890 // FIXME: Avoid this indirection?
891 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
893 std::vector<IndirectSymbolData> IndirectSymbols;
895 std::vector<DataRegionData> DataRegions;
897 /// The list of linker options to propagate into the object file.
898 std::vector<std::vector<std::string> > LinkerOptions;
900 /// List of declared file names
901 FileNameVectorType FileNames;
903 /// The set of function symbols for which a .thumb_func directive has
906 // FIXME: We really would like this in target specific code rather than
907 // here. Maybe when the relocation stuff moves to target specific,
908 // this can go with it? The streamer would need some target specific
910 mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
912 /// \brief The bundle alignment size currently set in the assembler.
914 /// By default it's 0, which means bundling is disabled.
915 unsigned BundleAlignSize;
917 unsigned RelaxAll : 1;
918 unsigned SubsectionsViaSymbols : 1;
920 /// ELF specific e_header flags
921 // It would be good if there were an MCELFAssembler class to hold this.
922 // ELF header flags are used both by the integrated and standalone assemblers.
923 // Access to the flags is necessary in cases where assembler directives affect
924 // which flags to be set.
925 unsigned ELFHeaderEFlags;
927 /// Used to communicate Linker Optimization Hint information between
928 /// the Streamer and the .o writer
929 MCLOHContainer LOHContainer;
931 VersionMinInfoType VersionMinInfo;
933 /// Evaluate a fixup to a relocatable expression and the value which should be
934 /// placed into the fixup.
936 /// \param Layout The layout to use for evaluation.
937 /// \param Fixup The fixup to evaluate.
938 /// \param DF The fragment the fixup is inside.
939 /// \param Target [out] On return, the relocatable expression the fixup
941 /// \param Value [out] On return, the value of the fixup as currently laid
943 /// \return Whether the fixup value was fully resolved. This is true if the
944 /// \p Value result is fixed, otherwise the value may change due to
946 bool evaluateFixup(const MCAsmLayout &Layout,
947 const MCFixup &Fixup, const MCFragment *DF,
948 MCValue &Target, uint64_t &Value) const;
950 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
951 /// (increased in size, in order to hold its value correctly).
952 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
953 const MCAsmLayout &Layout) const;
955 /// Check whether the given fragment needs relaxation.
956 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
957 const MCAsmLayout &Layout) const;
959 /// \brief Perform one layout iteration and return true if any offsets
961 bool layoutOnce(MCAsmLayout &Layout);
963 /// \brief Perform one layout iteration of the given section and return true
964 /// if any offsets were adjusted.
965 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
967 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
969 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
971 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
972 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
973 MCDwarfCallFrameFragment &DF);
975 /// finishLayout - Finalize a layout, including fragment lowering.
976 void finishLayout(MCAsmLayout &Layout);
978 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
979 MCFragment &F, const MCFixup &Fixup);
982 /// Compute the effective fragment size assuming it is laid out at the given
983 /// \p SectionAddress and \p FragmentOffset.
984 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
985 const MCFragment &F) const;
987 /// Find the symbol which defines the atom containing the given symbol, or
988 /// null if there is no such symbol.
989 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
991 /// Check whether a particular symbol is visible to the linker and is required
992 /// in the symbol table, or whether it can be discarded by the assembler. This
993 /// also effects whether the assembler treats the label as potentially
994 /// defining a separate atom.
995 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
997 /// Emit the section contents using the given object writer.
998 void writeSectionData(const MCSectionData *Section,
999 const MCAsmLayout &Layout) const;
1001 /// Check whether a given symbol has been flagged with .thumb_func.
1002 bool isThumbFunc(const MCSymbol *Func) const;
1004 /// Flag a function symbol as the target of a .thumb_func directive.
1005 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1007 /// ELF e_header flags
1008 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1009 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1011 /// MachO deployment target version information.
1012 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1013 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1015 VersionMinInfo.Kind = Kind;
1016 VersionMinInfo.Major = Major;
1017 VersionMinInfo.Minor = Minor;
1018 VersionMinInfo.Update = Update;
1022 /// Construct a new assembler instance.
1024 /// \param OS The stream to output to.
1026 // FIXME: How are we going to parameterize this? Two obvious options are stay
1027 // concrete and require clients to pass in a target like object. The other
1028 // option is to make this abstract, and have targets provide concrete
1029 // implementations as we do with AsmParser.
1030 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1031 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1035 /// Reuse an assembler instance
1039 MCContext &getContext() const { return Context; }
1041 MCAsmBackend &getBackend() const { return Backend; }
1043 MCCodeEmitter &getEmitter() const { return Emitter; }
1045 MCObjectWriter &getWriter() const { return Writer; }
1047 /// Finish - Do final processing and write the object to the output stream.
1048 /// \p Writer is used for custom object writer (as the MCJIT does),
1049 /// if not specified it is automatically created from backend.
1052 // FIXME: This does not belong here.
1053 bool getSubsectionsViaSymbols() const {
1054 return SubsectionsViaSymbols;
1056 void setSubsectionsViaSymbols(bool Value) {
1057 SubsectionsViaSymbols = Value;
1060 bool getRelaxAll() const { return RelaxAll; }
1061 void setRelaxAll(bool Value) { RelaxAll = Value; }
1063 bool isBundlingEnabled() const {
1064 return BundleAlignSize != 0;
1067 unsigned getBundleAlignSize() const {
1068 return BundleAlignSize;
1071 void setBundleAlignSize(unsigned Size) {
1072 assert((Size == 0 || !(Size & (Size - 1))) &&
1073 "Expect a power-of-two bundle align size");
1074 BundleAlignSize = Size;
1077 /// @name Section List Access
1080 const SectionDataListType &getSectionList() const { return Sections; }
1081 SectionDataListType &getSectionList() { return Sections; }
1083 iterator begin() { return Sections.begin(); }
1084 const_iterator begin() const { return Sections.begin(); }
1086 iterator end() { return Sections.end(); }
1087 const_iterator end() const { return Sections.end(); }
1089 size_t size() const { return Sections.size(); }
1092 /// @name Symbol List Access
1095 const SymbolDataListType &getSymbolList() const { return Symbols; }
1096 SymbolDataListType &getSymbolList() { return Symbols; }
1098 symbol_iterator symbol_begin() { return Symbols.begin(); }
1099 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1101 symbol_iterator symbol_end() { return Symbols.end(); }
1102 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1104 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1105 const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1107 size_t symbol_size() const { return Symbols.size(); }
1110 /// @name Indirect Symbol List Access
1113 // FIXME: This is a total hack, this should not be here. Once things are
1114 // factored so that the streamer has direct access to the .o writer, it can
1116 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1117 return IndirectSymbols;
1120 indirect_symbol_iterator indirect_symbol_begin() {
1121 return IndirectSymbols.begin();
1123 const_indirect_symbol_iterator indirect_symbol_begin() const {
1124 return IndirectSymbols.begin();
1127 indirect_symbol_iterator indirect_symbol_end() {
1128 return IndirectSymbols.end();
1130 const_indirect_symbol_iterator indirect_symbol_end() const {
1131 return IndirectSymbols.end();
1134 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1137 /// @name Linker Option List Access
1140 std::vector<std::vector<std::string> > &getLinkerOptions() {
1141 return LinkerOptions;
1145 /// @name Data Region List Access
1148 // FIXME: This is a total hack, this should not be here. Once things are
1149 // factored so that the streamer has direct access to the .o writer, it can
1151 std::vector<DataRegionData> &getDataRegions() {
1155 data_region_iterator data_region_begin() {
1156 return DataRegions.begin();
1158 const_data_region_iterator data_region_begin() const {
1159 return DataRegions.begin();
1162 data_region_iterator data_region_end() {
1163 return DataRegions.end();
1165 const_data_region_iterator data_region_end() const {
1166 return DataRegions.end();
1169 size_t data_region_size() const { return DataRegions.size(); }
1172 /// @name Data Region List Access
1175 // FIXME: This is a total hack, this should not be here. Once things are
1176 // factored so that the streamer has direct access to the .o writer, it can
1178 MCLOHContainer & getLOHContainer() {
1179 return LOHContainer;
1181 const MCLOHContainer & getLOHContainer() const {
1182 return const_cast<MCAssembler *>(this)->getLOHContainer();
1185 /// @name Backend Data Access
1188 MCSectionData &getSectionData(const MCSection &Section) const {
1189 MCSectionData *Entry = SectionMap.lookup(&Section);
1190 assert(Entry && "Missing section data!");
1194 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1195 bool *Created = nullptr) {
1196 MCSectionData *&Entry = SectionMap[&Section];
1198 if (Created) *Created = !Entry;
1200 Entry = new MCSectionData(Section, this);
1205 bool hasSymbolData(const MCSymbol &Symbol) const {
1206 return SymbolMap.lookup(&Symbol) != nullptr;
1209 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1210 return const_cast<MCSymbolData &>(
1211 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1214 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1215 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1216 assert(Entry && "Missing symbol data!");
1220 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1221 bool *Created = nullptr) {
1222 MCSymbolData *&Entry = SymbolMap[&Symbol];
1224 if (Created) *Created = !Entry;
1226 Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1231 const_file_name_iterator file_names_begin() const {
1232 return FileNames.begin();
1235 const_file_name_iterator file_names_end() const {
1236 return FileNames.end();
1239 void addFileName(StringRef FileName) {
1240 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1242 FileNames.push_back(FileName);
1250 } // end namespace llvm