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 Current nesting depth of bundle_lock directives.
600 unsigned BundleLockNestingDepth;
602 /// \brief We've seen a bundle_lock directive but not its first instruction
604 bool BundleGroupBeforeFirstInst;
606 /// @name Assembler Backend Data
609 // FIXME: This could all be kept private to the assembler implementation.
611 /// HasInstructions - Whether this section has had instructions emitted into
613 unsigned HasInstructions : 1;
615 /// Mapping from subsection number to insertion point for subsection numbers
616 /// below that number.
617 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
622 // Only for use as sentinel.
624 MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
626 const MCSection &getSection() const { return *Section; }
628 unsigned getAlignment() const { return Alignment; }
629 void setAlignment(unsigned Value) { Alignment = Value; }
631 bool hasInstructions() const { return HasInstructions; }
632 void setHasInstructions(bool Value) { HasInstructions = Value; }
634 unsigned getOrdinal() const { return Ordinal; }
635 void setOrdinal(unsigned Value) { Ordinal = Value; }
637 unsigned getLayoutOrder() const { return LayoutOrder; }
638 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
640 /// @name Fragment Access
643 const FragmentListType &getFragmentList() const { return Fragments; }
644 FragmentListType &getFragmentList() { return Fragments; }
646 iterator begin() { return Fragments.begin(); }
647 const_iterator begin() const { return Fragments.begin(); }
649 iterator end() { return Fragments.end(); }
650 const_iterator end() const { return Fragments.end(); }
652 reverse_iterator rbegin() { return Fragments.rbegin(); }
653 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
655 reverse_iterator rend() { return Fragments.rend(); }
656 const_reverse_iterator rend() const { return Fragments.rend(); }
658 size_t size() const { return Fragments.size(); }
660 bool empty() const { return Fragments.empty(); }
662 iterator getSubsectionInsertionPoint(unsigned Subsection);
664 bool isBundleLocked() const {
665 return BundleLockState != NotBundleLocked;
668 BundleLockStateType getBundleLockState() const {
669 return BundleLockState;
672 void setBundleLockState(BundleLockStateType NewState);
674 bool isBundleGroupBeforeFirstInst() const {
675 return BundleGroupBeforeFirstInst;
678 void setBundleGroupBeforeFirstInst(bool IsFirst) {
679 BundleGroupBeforeFirstInst = IsFirst;
687 // FIXME: Same concerns as with SectionData.
688 class MCSymbolData : public ilist_node<MCSymbolData> {
689 const MCSymbol *Symbol;
691 /// Fragment - The fragment this symbol's value is relative to, if any. Also
692 /// stores if this symbol is visible outside this translation unit (bit 0) or
693 /// if it is private extern (bit 1).
694 PointerIntPair<MCFragment *, 2> Fragment;
697 /// Offset - The offset to apply to the fragment address to form this
701 /// CommonSize - The size of the symbol, if it is 'common'.
705 /// SymbolSize - An expression describing how to calculate the size of
706 /// a symbol. If a symbol has no size this field will be NULL.
707 const MCExpr *SymbolSize;
709 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
711 // FIXME: Pack this in with other fields?
712 unsigned CommonAlign;
714 /// Flags - The Flags field is used by object file implementations to store
715 /// additional per symbol information which is not easily classified.
718 /// Index - Index field, for use by the object file implementation.
722 // Only for use as sentinel.
724 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
725 MCAssembler *A = nullptr);
730 const MCSymbol &getSymbol() const { return *Symbol; }
732 MCFragment *getFragment() const { return Fragment.getPointer(); }
733 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
735 uint64_t getOffset() const {
739 void setOffset(uint64_t Value) {
745 /// @name Symbol Attributes
748 bool isExternal() const { return Fragment.getInt() & 1; }
749 void setExternal(bool Value) {
750 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
753 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
754 void setPrivateExtern(bool Value) {
755 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
758 /// isCommon - Is this a 'common' symbol.
759 bool isCommon() const { return CommonAlign != -1U; }
761 /// setCommon - Mark this symbol as being 'common'.
763 /// \param Size - The size of the symbol.
764 /// \param Align - The alignment of the symbol.
765 void setCommon(uint64_t Size, unsigned Align) {
766 assert(getOffset() == 0);
771 /// getCommonSize - Return the size of a 'common' symbol.
772 uint64_t getCommonSize() const {
773 assert(isCommon() && "Not a 'common' symbol!");
777 void setSize(const MCExpr *SS) {
781 const MCExpr *getSize() const {
786 /// getCommonAlignment - Return the alignment of a 'common' symbol.
787 unsigned getCommonAlignment() const {
788 assert(isCommon() && "Not a 'common' symbol!");
792 /// getFlags - Get the (implementation defined) symbol flags.
793 uint32_t getFlags() const { return Flags; }
795 /// setFlags - Set the (implementation defined) symbol flags.
796 void setFlags(uint32_t Value) { Flags = Value; }
798 /// modifyFlags - Modify the flags via a mask
799 void modifyFlags(uint32_t Value, uint32_t Mask) {
800 Flags = (Flags & ~Mask) | Value;
803 /// getIndex - Get the (implementation defined) index.
804 uint64_t getIndex() const { return Index; }
806 /// setIndex - Set the (implementation defined) index.
807 void setIndex(uint64_t Value) { Index = Value; }
814 // FIXME: This really doesn't belong here. See comments below.
815 struct IndirectSymbolData {
817 MCSectionData *SectionData;
820 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
822 struct DataRegionData {
823 // This enum should be kept in sync w/ the mach-o definition in
824 // llvm/Object/MachOFormat.h.
825 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
831 friend class MCAsmLayout;
834 typedef iplist<MCSectionData> SectionDataListType;
835 typedef iplist<MCSymbolData> SymbolDataListType;
837 typedef SectionDataListType::const_iterator const_iterator;
838 typedef SectionDataListType::iterator iterator;
840 typedef SymbolDataListType::const_iterator const_symbol_iterator;
841 typedef SymbolDataListType::iterator symbol_iterator;
843 typedef iterator_range<symbol_iterator> symbol_range;
844 typedef iterator_range<const_symbol_iterator> const_symbol_range;
846 typedef std::vector<std::string> FileNameVectorType;
847 typedef FileNameVectorType::const_iterator const_file_name_iterator;
849 typedef std::vector<IndirectSymbolData>::const_iterator
850 const_indirect_symbol_iterator;
851 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
853 typedef std::vector<DataRegionData>::const_iterator
854 const_data_region_iterator;
855 typedef std::vector<DataRegionData>::iterator data_region_iterator;
857 /// MachO specific deployment target version info.
858 // A Major version of 0 indicates that no version information was supplied
859 // and so the corresponding load command should not be emitted.
861 MCVersionMinType Kind;
865 } VersionMinInfoType;
867 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
868 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
872 MCAsmBackend &Backend;
874 MCCodeEmitter &Emitter;
876 MCObjectWriter &Writer;
880 iplist<MCSectionData> Sections;
882 iplist<MCSymbolData> Symbols;
884 /// The map of sections to their associated assembler backend data.
886 // FIXME: Avoid this indirection?
887 DenseMap<const MCSection*, MCSectionData*> SectionMap;
889 /// The map of symbols to their associated assembler backend data.
891 // FIXME: Avoid this indirection?
892 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
894 std::vector<IndirectSymbolData> IndirectSymbols;
896 std::vector<DataRegionData> DataRegions;
898 /// The list of linker options to propagate into the object file.
899 std::vector<std::vector<std::string> > LinkerOptions;
901 /// List of declared file names
902 FileNameVectorType FileNames;
904 /// The set of function symbols for which a .thumb_func directive has
907 // FIXME: We really would like this in target specific code rather than
908 // here. Maybe when the relocation stuff moves to target specific,
909 // this can go with it? The streamer would need some target specific
911 mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
913 /// \brief The bundle alignment size currently set in the assembler.
915 /// By default it's 0, which means bundling is disabled.
916 unsigned BundleAlignSize;
918 unsigned RelaxAll : 1;
919 unsigned SubsectionsViaSymbols : 1;
921 /// ELF specific e_header flags
922 // It would be good if there were an MCELFAssembler class to hold this.
923 // ELF header flags are used both by the integrated and standalone assemblers.
924 // Access to the flags is necessary in cases where assembler directives affect
925 // which flags to be set.
926 unsigned ELFHeaderEFlags;
928 /// Used to communicate Linker Optimization Hint information between
929 /// the Streamer and the .o writer
930 MCLOHContainer LOHContainer;
932 VersionMinInfoType VersionMinInfo;
934 /// Evaluate a fixup to a relocatable expression and the value which should be
935 /// placed into the fixup.
937 /// \param Layout The layout to use for evaluation.
938 /// \param Fixup The fixup to evaluate.
939 /// \param DF The fragment the fixup is inside.
940 /// \param Target [out] On return, the relocatable expression the fixup
942 /// \param Value [out] On return, the value of the fixup as currently laid
944 /// \return Whether the fixup value was fully resolved. This is true if the
945 /// \p Value result is fixed, otherwise the value may change due to
947 bool evaluateFixup(const MCAsmLayout &Layout,
948 const MCFixup &Fixup, const MCFragment *DF,
949 MCValue &Target, uint64_t &Value) const;
951 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
952 /// (increased in size, in order to hold its value correctly).
953 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
954 const MCAsmLayout &Layout) const;
956 /// Check whether the given fragment needs relaxation.
957 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
958 const MCAsmLayout &Layout) const;
960 /// \brief Perform one layout iteration and return true if any offsets
962 bool layoutOnce(MCAsmLayout &Layout);
964 /// \brief Perform one layout iteration of the given section and return true
965 /// if any offsets were adjusted.
966 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
968 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
970 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
972 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
973 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
974 MCDwarfCallFrameFragment &DF);
976 /// finishLayout - Finalize a layout, including fragment lowering.
977 void finishLayout(MCAsmLayout &Layout);
979 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
980 MCFragment &F, const MCFixup &Fixup);
983 /// Compute the effective fragment size assuming it is laid out at the given
984 /// \p SectionAddress and \p FragmentOffset.
985 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
986 const MCFragment &F) const;
988 /// Find the symbol which defines the atom containing the given symbol, or
989 /// null if there is no such symbol.
990 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
992 /// Check whether a particular symbol is visible to the linker and is required
993 /// in the symbol table, or whether it can be discarded by the assembler. This
994 /// also effects whether the assembler treats the label as potentially
995 /// defining a separate atom.
996 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
998 /// Emit the section contents using the given object writer.
999 void writeSectionData(const MCSectionData *Section,
1000 const MCAsmLayout &Layout) const;
1002 /// Check whether a given symbol has been flagged with .thumb_func.
1003 bool isThumbFunc(const MCSymbol *Func) const;
1005 /// Flag a function symbol as the target of a .thumb_func directive.
1006 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1008 /// ELF e_header flags
1009 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1010 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1012 /// MachO deployment target version information.
1013 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1014 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1016 VersionMinInfo.Kind = Kind;
1017 VersionMinInfo.Major = Major;
1018 VersionMinInfo.Minor = Minor;
1019 VersionMinInfo.Update = Update;
1023 /// Construct a new assembler instance.
1025 /// \param OS The stream to output to.
1027 // FIXME: How are we going to parameterize this? Two obvious options are stay
1028 // concrete and require clients to pass in a target like object. The other
1029 // option is to make this abstract, and have targets provide concrete
1030 // implementations as we do with AsmParser.
1031 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1032 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1036 /// Reuse an assembler instance
1040 MCContext &getContext() const { return Context; }
1042 MCAsmBackend &getBackend() const { return Backend; }
1044 MCCodeEmitter &getEmitter() const { return Emitter; }
1046 MCObjectWriter &getWriter() const { return Writer; }
1048 /// Finish - Do final processing and write the object to the output stream.
1049 /// \p Writer is used for custom object writer (as the MCJIT does),
1050 /// if not specified it is automatically created from backend.
1053 // FIXME: This does not belong here.
1054 bool getSubsectionsViaSymbols() const {
1055 return SubsectionsViaSymbols;
1057 void setSubsectionsViaSymbols(bool Value) {
1058 SubsectionsViaSymbols = Value;
1061 bool getRelaxAll() const { return RelaxAll; }
1062 void setRelaxAll(bool Value) { RelaxAll = Value; }
1064 bool isBundlingEnabled() const {
1065 return BundleAlignSize != 0;
1068 unsigned getBundleAlignSize() const {
1069 return BundleAlignSize;
1072 void setBundleAlignSize(unsigned Size) {
1073 assert((Size == 0 || !(Size & (Size - 1))) &&
1074 "Expect a power-of-two bundle align size");
1075 BundleAlignSize = Size;
1078 /// @name Section List Access
1081 const SectionDataListType &getSectionList() const { return Sections; }
1082 SectionDataListType &getSectionList() { return Sections; }
1084 iterator begin() { return Sections.begin(); }
1085 const_iterator begin() const { return Sections.begin(); }
1087 iterator end() { return Sections.end(); }
1088 const_iterator end() const { return Sections.end(); }
1090 size_t size() const { return Sections.size(); }
1093 /// @name Symbol List Access
1096 const SymbolDataListType &getSymbolList() const { return Symbols; }
1097 SymbolDataListType &getSymbolList() { return Symbols; }
1099 symbol_iterator symbol_begin() { return Symbols.begin(); }
1100 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1102 symbol_iterator symbol_end() { return Symbols.end(); }
1103 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1105 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1106 const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1108 size_t symbol_size() const { return Symbols.size(); }
1111 /// @name Indirect Symbol List Access
1114 // FIXME: This is a total hack, this should not be here. Once things are
1115 // factored so that the streamer has direct access to the .o writer, it can
1117 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1118 return IndirectSymbols;
1121 indirect_symbol_iterator indirect_symbol_begin() {
1122 return IndirectSymbols.begin();
1124 const_indirect_symbol_iterator indirect_symbol_begin() const {
1125 return IndirectSymbols.begin();
1128 indirect_symbol_iterator indirect_symbol_end() {
1129 return IndirectSymbols.end();
1131 const_indirect_symbol_iterator indirect_symbol_end() const {
1132 return IndirectSymbols.end();
1135 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1138 /// @name Linker Option List Access
1141 std::vector<std::vector<std::string> > &getLinkerOptions() {
1142 return LinkerOptions;
1146 /// @name Data Region List Access
1149 // FIXME: This is a total hack, this should not be here. Once things are
1150 // factored so that the streamer has direct access to the .o writer, it can
1152 std::vector<DataRegionData> &getDataRegions() {
1156 data_region_iterator data_region_begin() {
1157 return DataRegions.begin();
1159 const_data_region_iterator data_region_begin() const {
1160 return DataRegions.begin();
1163 data_region_iterator data_region_end() {
1164 return DataRegions.end();
1166 const_data_region_iterator data_region_end() const {
1167 return DataRegions.end();
1170 size_t data_region_size() const { return DataRegions.size(); }
1173 /// @name Data Region List Access
1176 // FIXME: This is a total hack, this should not be here. Once things are
1177 // factored so that the streamer has direct access to the .o writer, it can
1179 MCLOHContainer & getLOHContainer() {
1180 return LOHContainer;
1182 const MCLOHContainer & getLOHContainer() const {
1183 return const_cast<MCAssembler *>(this)->getLOHContainer();
1186 /// @name Backend Data Access
1189 MCSectionData &getSectionData(const MCSection &Section) const {
1190 MCSectionData *Entry = SectionMap.lookup(&Section);
1191 assert(Entry && "Missing section data!");
1195 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1196 bool *Created = nullptr) {
1197 MCSectionData *&Entry = SectionMap[&Section];
1199 if (Created) *Created = !Entry;
1201 Entry = new MCSectionData(Section, this);
1206 bool hasSymbolData(const MCSymbol &Symbol) const {
1207 return SymbolMap.lookup(&Symbol) != nullptr;
1210 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1211 return const_cast<MCSymbolData &>(
1212 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1215 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1216 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1217 assert(Entry && "Missing symbol data!");
1221 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1222 bool *Created = nullptr) {
1223 MCSymbolData *&Entry = SymbolMap[&Symbol];
1225 if (Created) *Created = !Entry;
1227 Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1232 const_file_name_iterator file_names_begin() const {
1233 return FileNames.begin();
1236 const_file_name_iterator file_names_end() const {
1237 return FileNames.end();
1240 void addFileName(StringRef FileName) {
1241 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1243 FileNames.push_back(FileName);
1251 } // end namespace llvm