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/SmallPtrSet.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/ilist.h"
17 #include "llvm/ADT/ilist_node.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCLinkerOptimizationHint.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/DataTypes.h"
26 #include <vector> // FIXME: Shouldn't be needed.
39 class MCSubtargetInfo;
45 class MCFragment : public ilist_node<MCFragment> {
46 friend class MCAsmLayout;
48 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
49 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
55 FT_CompactEncodedInst,
67 /// Parent - The data for the section this fragment is in.
68 MCSectionData *Parent;
70 /// Atom - The atom this fragment is in, as represented by it's defining
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 /// LayoutOrder - The layout order of this fragment.
89 MCFragment(FragmentType _Kind, MCSectionData *_Parent = nullptr);
94 virtual ~MCFragment();
96 FragmentType getKind() const { return Kind; }
98 MCSectionData *getParent() const { return Parent; }
99 void setParent(MCSectionData *Value) { Parent = Value; }
101 MCSymbolData *getAtom() const { return Atom; }
102 void setAtom(MCSymbolData *Value) { Atom = Value; }
104 unsigned getLayoutOrder() const { return LayoutOrder; }
105 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
107 /// \brief Does this fragment have instructions emitted into it? By default
108 /// this is false, but specific fragment types may set it to true.
109 virtual bool hasInstructions() const { return false; }
111 /// \brief Should this fragment be placed at the end of an aligned bundle?
112 virtual bool alignToBundleEnd() const { return false; }
113 virtual void setAlignToBundleEnd(bool V) { }
115 /// \brief Get the padding size that must be inserted before this fragment.
116 /// Used for bundling. By default, no padding is inserted.
117 /// Note that padding size is restricted to 8 bits. This is an optimization
118 /// to reduce the amount of space used for each fragment. In practice, larger
119 /// padding should never be required.
120 virtual uint8_t getBundlePadding() const {
124 /// \brief Set the padding size for this fragment. By default it's a no-op,
125 /// and only some fragments have a meaningful implementation.
126 virtual void setBundlePadding(uint8_t N) {
132 /// Interface implemented by fragments that contain encoded instructions and/or
135 class MCEncodedFragment : public MCFragment {
136 virtual void anchor();
138 uint8_t BundlePadding;
140 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = nullptr)
141 : MCFragment(FType, SD), BundlePadding(0)
144 virtual ~MCEncodedFragment();
146 virtual SmallVectorImpl<char> &getContents() = 0;
147 virtual const SmallVectorImpl<char> &getContents() const = 0;
149 uint8_t getBundlePadding() const override {
150 return BundlePadding;
153 void setBundlePadding(uint8_t N) override {
157 static bool classof(const MCFragment *F) {
158 MCFragment::FragmentType Kind = F->getKind();
162 case MCFragment::FT_Relaxable:
163 case MCFragment::FT_CompactEncodedInst:
164 case MCFragment::FT_Data:
170 /// Interface implemented by fragments that contain encoded instructions and/or
171 /// data and also have fixups registered.
173 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
174 void anchor() override;
177 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
178 MCSectionData *SD = nullptr)
179 : MCEncodedFragment(FType, SD)
183 virtual ~MCEncodedFragmentWithFixups();
185 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
186 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
188 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
189 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
191 virtual fixup_iterator fixup_begin() = 0;
192 virtual const_fixup_iterator fixup_begin() const = 0;
193 virtual fixup_iterator fixup_end() = 0;
194 virtual const_fixup_iterator fixup_end() const = 0;
196 static bool classof(const MCFragment *F) {
197 MCFragment::FragmentType Kind = F->getKind();
198 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
202 /// Fragment for data and encoded instructions.
204 class MCDataFragment : public MCEncodedFragmentWithFixups {
205 void anchor() override;
207 /// \brief Does this fragment contain encoded instructions anywhere in it?
208 bool HasInstructions;
210 /// \brief Should this fragment be aligned to the end of a bundle?
211 bool AlignToBundleEnd;
213 SmallVector<char, 32> Contents;
215 /// Fixups - The list of fixups in this fragment.
216 SmallVector<MCFixup, 4> Fixups;
218 MCDataFragment(MCSectionData *SD = nullptr)
219 : MCEncodedFragmentWithFixups(FT_Data, SD),
220 HasInstructions(false), AlignToBundleEnd(false)
224 SmallVectorImpl<char> &getContents() override { return Contents; }
225 const SmallVectorImpl<char> &getContents() const override {
229 SmallVectorImpl<MCFixup> &getFixups() override {
233 const SmallVectorImpl<MCFixup> &getFixups() const override {
237 bool hasInstructions() const override { return HasInstructions; }
238 virtual void setHasInstructions(bool V) { HasInstructions = V; }
240 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
241 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
243 fixup_iterator fixup_begin() override { return Fixups.begin(); }
244 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
246 fixup_iterator fixup_end() override {return Fixups.end();}
247 const_fixup_iterator fixup_end() const override {return Fixups.end();}
249 static bool classof(const MCFragment *F) {
250 return F->getKind() == MCFragment::FT_Data;
254 /// This is a compact (memory-size-wise) fragment for holding an encoded
255 /// instruction (non-relaxable) that has no fixups registered. When applicable,
256 /// it can be used instead of MCDataFragment and lead to lower memory
259 class MCCompactEncodedInstFragment : public MCEncodedFragment {
260 void anchor() override;
262 /// \brief Should this fragment be aligned to the end of a bundle?
263 bool AlignToBundleEnd;
265 SmallVector<char, 4> Contents;
267 MCCompactEncodedInstFragment(MCSectionData *SD = nullptr)
268 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
272 bool hasInstructions() const override {
276 SmallVectorImpl<char> &getContents() override { return Contents; }
277 const SmallVectorImpl<char> &getContents() const override { return Contents; }
279 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
280 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
282 static bool classof(const MCFragment *F) {
283 return F->getKind() == MCFragment::FT_CompactEncodedInst;
287 /// A relaxable fragment holds on to its MCInst, since it may need to be
288 /// relaxed during the assembler layout and relaxation stage.
290 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
291 void anchor() override;
293 /// Inst - The instruction this is a fragment for.
296 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
297 /// Keep a copy instead of a reference to make sure that updates to STI
298 /// in the assembler are not seen here.
299 const MCSubtargetInfo STI;
301 /// Contents - Binary data for the currently encoded instruction.
302 SmallVector<char, 8> Contents;
304 /// Fixups - The list of fixups in this fragment.
305 SmallVector<MCFixup, 1> Fixups;
308 MCRelaxableFragment(const MCInst &_Inst,
309 const MCSubtargetInfo &_STI,
310 MCSectionData *SD = nullptr)
311 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
314 SmallVectorImpl<char> &getContents() override { return Contents; }
315 const SmallVectorImpl<char> &getContents() const override { return Contents; }
317 const MCInst &getInst() const { return Inst; }
318 void setInst(const MCInst& Value) { Inst = Value; }
320 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
322 SmallVectorImpl<MCFixup> &getFixups() override {
326 const SmallVectorImpl<MCFixup> &getFixups() const override {
330 bool hasInstructions() const override { return true; }
332 fixup_iterator fixup_begin() override { return Fixups.begin(); }
333 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
335 fixup_iterator fixup_end() override {return Fixups.end();}
336 const_fixup_iterator fixup_end() const override {return Fixups.end();}
338 static bool classof(const MCFragment *F) {
339 return F->getKind() == MCFragment::FT_Relaxable;
343 class MCAlignFragment : public MCFragment {
344 virtual void anchor();
346 /// Alignment - The alignment to ensure, in bytes.
349 /// Value - Value to use for filling padding bytes.
352 /// ValueSize - The size of the integer (in bytes) of \p Value.
355 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
356 /// cannot be satisfied in this width then this fragment is ignored.
357 unsigned MaxBytesToEmit;
359 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
360 /// of using the provided value. The exact interpretation of this flag is
361 /// target dependent.
365 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
366 unsigned _MaxBytesToEmit, MCSectionData *SD = nullptr)
367 : MCFragment(FT_Align, SD), Alignment(_Alignment),
368 Value(_Value),ValueSize(_ValueSize),
369 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
374 unsigned getAlignment() const { return Alignment; }
376 int64_t getValue() const { return Value; }
378 unsigned getValueSize() const { return ValueSize; }
380 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
382 bool hasEmitNops() const { return EmitNops; }
383 void setEmitNops(bool Value) { EmitNops = Value; }
387 static bool classof(const MCFragment *F) {
388 return F->getKind() == MCFragment::FT_Align;
392 class MCFillFragment : public MCFragment {
393 virtual void anchor();
395 /// Value - Value to use for filling bytes.
398 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
399 /// this is a virtual fill fragment.
402 /// Size - The number of bytes to insert.
406 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
407 MCSectionData *SD = nullptr)
408 : MCFragment(FT_Fill, SD),
409 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
410 assert((!ValueSize || (Size % ValueSize) == 0) &&
411 "Fill size must be a multiple of the value size!");
417 int64_t getValue() const { return Value; }
419 unsigned getValueSize() const { return ValueSize; }
421 uint64_t getSize() const { return Size; }
425 static bool classof(const MCFragment *F) {
426 return F->getKind() == MCFragment::FT_Fill;
430 class MCOrgFragment : public MCFragment {
431 virtual void anchor();
433 /// Offset - The offset this fragment should start at.
434 const MCExpr *Offset;
436 /// Value - Value to use for filling bytes.
440 MCOrgFragment(const MCExpr &_Offset, int8_t _Value,
441 MCSectionData *SD = nullptr)
442 : MCFragment(FT_Org, SD),
443 Offset(&_Offset), Value(_Value) {}
448 const MCExpr &getOffset() const { return *Offset; }
450 uint8_t getValue() const { return Value; }
454 static bool classof(const MCFragment *F) {
455 return F->getKind() == MCFragment::FT_Org;
459 class MCLEBFragment : public MCFragment {
460 virtual void anchor();
462 /// Value - The value this fragment should contain.
465 /// IsSigned - True if this is a sleb128, false if uleb128.
468 SmallString<8> Contents;
470 MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
471 MCSectionData *SD = nullptr)
472 : MCFragment(FT_LEB, SD),
473 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
478 const MCExpr &getValue() const { return *Value; }
480 bool isSigned() const { return IsSigned; }
482 SmallString<8> &getContents() { return Contents; }
483 const SmallString<8> &getContents() const { return Contents; }
487 static bool classof(const MCFragment *F) {
488 return F->getKind() == MCFragment::FT_LEB;
492 class MCDwarfLineAddrFragment : public MCFragment {
493 virtual void anchor();
495 /// LineDelta - the value of the difference between the two line numbers
496 /// between two .loc dwarf directives.
499 /// AddrDelta - The expression for the difference of the two symbols that
500 /// make up the address delta between two .loc dwarf directives.
501 const MCExpr *AddrDelta;
503 SmallString<8> Contents;
506 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
507 MCSectionData *SD = nullptr)
508 : MCFragment(FT_Dwarf, SD),
509 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
514 int64_t getLineDelta() const { return LineDelta; }
516 const MCExpr &getAddrDelta() const { return *AddrDelta; }
518 SmallString<8> &getContents() { return Contents; }
519 const SmallString<8> &getContents() const { return Contents; }
523 static bool classof(const MCFragment *F) {
524 return F->getKind() == MCFragment::FT_Dwarf;
528 class MCDwarfCallFrameFragment : public MCFragment {
529 virtual void anchor();
531 /// AddrDelta - The expression for the difference of the two symbols that
532 /// make up the address delta between two .cfi_* dwarf directives.
533 const MCExpr *AddrDelta;
535 SmallString<8> Contents;
538 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta,
539 MCSectionData *SD = nullptr)
540 : MCFragment(FT_DwarfFrame, SD),
541 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
546 const MCExpr &getAddrDelta() const { return *AddrDelta; }
548 SmallString<8> &getContents() { return Contents; }
549 const SmallString<8> &getContents() const { return Contents; }
553 static bool classof(const MCFragment *F) {
554 return F->getKind() == MCFragment::FT_DwarfFrame;
558 // FIXME: Should this be a separate class, or just merged into MCSection? Since
559 // we anticipate the fast path being through an MCAssembler, the only reason to
560 // keep it out is for API abstraction.
561 class MCSectionData : public ilist_node<MCSectionData> {
562 friend class MCAsmLayout;
564 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
565 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
568 typedef iplist<MCFragment> FragmentListType;
570 typedef FragmentListType::const_iterator const_iterator;
571 typedef FragmentListType::iterator iterator;
573 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
574 typedef FragmentListType::reverse_iterator reverse_iterator;
576 /// \brief Express the state of bundle locked groups while emitting code.
577 enum BundleLockStateType {
580 BundleLockedAlignToEnd
583 FragmentListType Fragments;
584 const MCSection *Section;
586 /// Ordinal - The section index in the assemblers section list.
589 /// LayoutOrder - The index of this section in the layout order.
590 unsigned LayoutOrder;
592 /// Alignment - The maximum alignment seen in this section.
595 /// \brief Keeping track of bundle-locked state.
596 BundleLockStateType BundleLockState;
598 /// \brief We've seen a bundle_lock directive but not its first instruction
600 bool BundleGroupBeforeFirstInst;
602 /// @name Assembler Backend Data
605 // FIXME: This could all be kept private to the assembler implementation.
607 /// HasInstructions - Whether this section has had instructions emitted into
609 unsigned HasInstructions : 1;
611 /// Mapping from subsection number to insertion point for subsection numbers
612 /// below that number.
613 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
618 // Only for use as sentinel.
620 MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
622 const MCSection &getSection() const { return *Section; }
624 unsigned getAlignment() const { return Alignment; }
625 void setAlignment(unsigned Value) { Alignment = Value; }
627 bool hasInstructions() const { return HasInstructions; }
628 void setHasInstructions(bool Value) { HasInstructions = Value; }
630 unsigned getOrdinal() const { return Ordinal; }
631 void setOrdinal(unsigned Value) { Ordinal = Value; }
633 unsigned getLayoutOrder() const { return LayoutOrder; }
634 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
636 /// @name Fragment Access
639 const FragmentListType &getFragmentList() const { return Fragments; }
640 FragmentListType &getFragmentList() { return Fragments; }
642 iterator begin() { return Fragments.begin(); }
643 const_iterator begin() const { return Fragments.begin(); }
645 iterator end() { return Fragments.end(); }
646 const_iterator end() const { return Fragments.end(); }
648 reverse_iterator rbegin() { return Fragments.rbegin(); }
649 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
651 reverse_iterator rend() { return Fragments.rend(); }
652 const_reverse_iterator rend() const { return Fragments.rend(); }
654 size_t size() const { return Fragments.size(); }
656 bool empty() const { return Fragments.empty(); }
658 iterator getSubsectionInsertionPoint(unsigned Subsection);
660 bool isBundleLocked() const {
661 return BundleLockState != NotBundleLocked;
664 BundleLockStateType getBundleLockState() const {
665 return BundleLockState;
668 void setBundleLockState(BundleLockStateType NewState) {
669 BundleLockState = NewState;
672 bool isBundleGroupBeforeFirstInst() const {
673 return BundleGroupBeforeFirstInst;
676 void setBundleGroupBeforeFirstInst(bool IsFirst) {
677 BundleGroupBeforeFirstInst = IsFirst;
685 // FIXME: Same concerns as with SectionData.
686 class MCSymbolData : public ilist_node<MCSymbolData> {
688 const MCSymbol *Symbol;
690 /// Fragment - The fragment this symbol's value is relative to, if any.
691 MCFragment *Fragment;
693 /// Offset - The offset to apply to the fragment address to form this symbol's
697 /// IsExternal - True if this symbol is visible outside this translation
699 unsigned IsExternal : 1;
701 /// IsPrivateExtern - True if this symbol is private extern.
702 unsigned IsPrivateExtern : 1;
704 /// CommonSize - The size of the symbol, if it is 'common', or 0.
706 // FIXME: Pack this in with other fields? We could put it in offset, since a
707 // common symbol can never get a definition.
710 /// SymbolSize - An expression describing how to calculate the size of
711 /// a symbol. If a symbol has no size this field will be NULL.
712 const MCExpr *SymbolSize;
714 /// CommonAlign - The alignment of the symbol, if it is 'common'.
716 // FIXME: Pack this in with other fields?
717 unsigned CommonAlign;
719 /// Flags - The Flags field is used by object file implementations to store
720 /// additional per symbol information which is not easily classified.
723 /// Index - Index field, for use by the object file implementation.
727 // Only for use as sentinel.
729 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
730 MCAssembler *A = nullptr);
735 const MCSymbol &getSymbol() const { return *Symbol; }
737 MCFragment *getFragment() const { return Fragment; }
738 void setFragment(MCFragment *Value) { Fragment = Value; }
740 uint64_t getOffset() const { return Offset; }
741 void setOffset(uint64_t Value) { Offset = Value; }
744 /// @name Symbol Attributes
747 bool isExternal() const { return IsExternal; }
748 void setExternal(bool Value) { IsExternal = Value; }
750 bool isPrivateExtern() const { return IsPrivateExtern; }
751 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
753 /// isCommon - Is this a 'common' symbol.
754 bool isCommon() const { return CommonSize != 0; }
756 /// setCommon - Mark this symbol as being 'common'.
758 /// \param Size - The size of the symbol.
759 /// \param Align - The alignment of the symbol.
760 void setCommon(uint64_t Size, unsigned Align) {
765 /// getCommonSize - Return the size of a 'common' symbol.
766 uint64_t getCommonSize() const {
767 assert(isCommon() && "Not a 'common' symbol!");
771 void setSize(const MCExpr *SS) {
775 const MCExpr *getSize() const {
780 /// getCommonAlignment - Return the alignment of a 'common' symbol.
781 unsigned getCommonAlignment() const {
782 assert(isCommon() && "Not a 'common' symbol!");
786 /// getFlags - Get the (implementation defined) symbol flags.
787 uint32_t getFlags() const { return Flags; }
789 /// setFlags - Set the (implementation defined) symbol flags.
790 void setFlags(uint32_t Value) { Flags = Value; }
792 /// modifyFlags - Modify the flags via a mask
793 void modifyFlags(uint32_t Value, uint32_t Mask) {
794 Flags = (Flags & ~Mask) | Value;
797 /// getIndex - Get the (implementation defined) index.
798 uint64_t getIndex() const { return Index; }
800 /// setIndex - Set the (implementation defined) index.
801 void setIndex(uint64_t Value) { Index = Value; }
808 // FIXME: This really doesn't belong here. See comments below.
809 struct IndirectSymbolData {
811 MCSectionData *SectionData;
814 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
816 struct DataRegionData {
817 // This enum should be kept in sync w/ the mach-o definition in
818 // llvm/Object/MachOFormat.h.
819 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
825 friend class MCAsmLayout;
828 typedef iplist<MCSectionData> SectionDataListType;
829 typedef iplist<MCSymbolData> SymbolDataListType;
831 typedef SectionDataListType::const_iterator const_iterator;
832 typedef SectionDataListType::iterator iterator;
834 typedef SymbolDataListType::const_iterator const_symbol_iterator;
835 typedef SymbolDataListType::iterator symbol_iterator;
837 typedef iterator_range<symbol_iterator> symbol_range;
838 typedef iterator_range<const_symbol_iterator> const_symbol_range;
840 typedef std::vector<std::string> FileNameVectorType;
841 typedef FileNameVectorType::const_iterator const_file_name_iterator;
843 typedef std::vector<IndirectSymbolData>::const_iterator
844 const_indirect_symbol_iterator;
845 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
847 typedef std::vector<DataRegionData>::const_iterator
848 const_data_region_iterator;
849 typedef std::vector<DataRegionData>::iterator data_region_iterator;
851 /// MachO specific deployment target version info.
852 // A Major version of 0 indicates that no version information was supplied
853 // and so the corresponding load command should not be emitted.
855 MCVersionMinType Kind;
859 } VersionMinInfoType;
861 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
862 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
866 MCAsmBackend &Backend;
868 MCCodeEmitter &Emitter;
870 MCObjectWriter &Writer;
874 iplist<MCSectionData> Sections;
876 iplist<MCSymbolData> Symbols;
878 /// The map of sections to their associated assembler backend data.
880 // FIXME: Avoid this indirection?
881 DenseMap<const MCSection*, MCSectionData*> SectionMap;
883 /// The map of symbols to their associated assembler backend data.
885 // FIXME: Avoid this indirection?
886 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
888 std::vector<IndirectSymbolData> IndirectSymbols;
890 std::vector<DataRegionData> DataRegions;
892 /// The list of linker options to propagate into the object file.
893 std::vector<std::vector<std::string> > LinkerOptions;
895 /// List of declared file names
896 FileNameVectorType FileNames;
898 /// The set of function symbols for which a .thumb_func directive has
901 // FIXME: We really would like this in target specific code rather than
902 // here. Maybe when the relocation stuff moves to target specific,
903 // this can go with it? The streamer would need some target specific
905 mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
907 /// \brief The bundle alignment size currently set in the assembler.
909 /// By default it's 0, which means bundling is disabled.
910 unsigned BundleAlignSize;
912 unsigned RelaxAll : 1;
913 unsigned NoExecStack : 1;
914 unsigned SubsectionsViaSymbols : 1;
916 /// ELF specific e_header flags
917 // It would be good if there were an MCELFAssembler class to hold this.
918 // ELF header flags are used both by the integrated and standalone assemblers.
919 // Access to the flags is necessary in cases where assembler directives affect
920 // which flags to be set.
921 unsigned ELFHeaderEFlags;
923 /// Used to communicate Linker Optimization Hint information between
924 /// the Streamer and the .o writer
925 MCLOHContainer LOHContainer;
927 VersionMinInfoType VersionMinInfo;
929 /// Evaluate a fixup to a relocatable expression and the value which should be
930 /// placed into the fixup.
932 /// \param Layout The layout to use for evaluation.
933 /// \param Fixup The fixup to evaluate.
934 /// \param DF The fragment the fixup is inside.
935 /// \param Target [out] On return, the relocatable expression the fixup
937 /// \param Value [out] On return, the value of the fixup as currently laid
939 /// \return Whether the fixup value was fully resolved. This is true if the
940 /// \p Value result is fixed, otherwise the value may change due to
942 bool evaluateFixup(const MCAsmLayout &Layout,
943 const MCFixup &Fixup, const MCFragment *DF,
944 MCValue &Target, uint64_t &Value) const;
946 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
947 /// (increased in size, in order to hold its value correctly).
948 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
949 const MCAsmLayout &Layout) const;
951 /// Check whether the given fragment needs relaxation.
952 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
953 const MCAsmLayout &Layout) const;
955 /// \brief Perform one layout iteration and return true if any offsets
957 bool layoutOnce(MCAsmLayout &Layout);
959 /// \brief Perform one layout iteration of the given section and return true
960 /// if any offsets were adjusted.
961 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
963 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
965 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
967 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
968 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
969 MCDwarfCallFrameFragment &DF);
971 /// finishLayout - Finalize a layout, including fragment lowering.
972 void finishLayout(MCAsmLayout &Layout);
974 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
975 MCFragment &F, const MCFixup &Fixup);
978 /// Compute the effective fragment size assuming it is laid out at the given
979 /// \p SectionAddress and \p FragmentOffset.
980 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
981 const MCFragment &F) const;
983 /// Find the symbol which defines the atom containing the given symbol, or
984 /// null if there is no such symbol.
985 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
987 /// Check whether a particular symbol is visible to the linker and is required
988 /// in the symbol table, or whether it can be discarded by the assembler. This
989 /// also effects whether the assembler treats the label as potentially
990 /// defining a separate atom.
991 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
993 /// Emit the section contents using the given object writer.
994 void writeSectionData(const MCSectionData *Section,
995 const MCAsmLayout &Layout) const;
997 /// Check whether a given symbol has been flagged with .thumb_func.
998 bool isThumbFunc(const MCSymbol *Func) const;
1000 /// Flag a function symbol as the target of a .thumb_func directive.
1001 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1003 /// ELF e_header flags
1004 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1005 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1007 /// MachO deployment target version information.
1008 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1009 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1011 VersionMinInfo.Kind = Kind;
1012 VersionMinInfo.Major = Major;
1013 VersionMinInfo.Minor = Minor;
1014 VersionMinInfo.Update = Update;
1018 /// Construct a new assembler instance.
1020 /// \param OS The stream to output to.
1022 // FIXME: How are we going to parameterize this? Two obvious options are stay
1023 // concrete and require clients to pass in a target like object. The other
1024 // option is to make this abstract, and have targets provide concrete
1025 // implementations as we do with AsmParser.
1026 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1027 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1031 /// Reuse an assembler instance
1035 MCContext &getContext() const { return Context; }
1037 MCAsmBackend &getBackend() const { return Backend; }
1039 MCCodeEmitter &getEmitter() const { return Emitter; }
1041 MCObjectWriter &getWriter() const { return Writer; }
1043 /// Finish - Do final processing and write the object to the output stream.
1044 /// \p Writer is used for custom object writer (as the MCJIT does),
1045 /// if not specified it is automatically created from backend.
1048 // FIXME: This does not belong here.
1049 bool getSubsectionsViaSymbols() const {
1050 return SubsectionsViaSymbols;
1052 void setSubsectionsViaSymbols(bool Value) {
1053 SubsectionsViaSymbols = Value;
1056 bool getRelaxAll() const { return RelaxAll; }
1057 void setRelaxAll(bool Value) { RelaxAll = Value; }
1059 bool getNoExecStack() const { return NoExecStack; }
1060 void setNoExecStack(bool Value) { NoExecStack = Value; }
1062 bool isBundlingEnabled() const {
1063 return BundleAlignSize != 0;
1066 unsigned getBundleAlignSize() const {
1067 return BundleAlignSize;
1070 void setBundleAlignSize(unsigned Size) {
1071 assert((Size == 0 || !(Size & (Size - 1))) &&
1072 "Expect a power-of-two bundle align size");
1073 BundleAlignSize = Size;
1076 /// @name Section List Access
1079 const SectionDataListType &getSectionList() const { return Sections; }
1080 SectionDataListType &getSectionList() { return Sections; }
1082 iterator begin() { return Sections.begin(); }
1083 const_iterator begin() const { return Sections.begin(); }
1085 iterator end() { return Sections.end(); }
1086 const_iterator end() const { return Sections.end(); }
1088 size_t size() const { return Sections.size(); }
1091 /// @name Symbol List Access
1094 const SymbolDataListType &getSymbolList() const { return Symbols; }
1095 SymbolDataListType &getSymbolList() { return Symbols; }
1097 symbol_iterator symbol_begin() { return Symbols.begin(); }
1098 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1100 symbol_iterator symbol_end() { return Symbols.end(); }
1101 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1103 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1104 const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1106 size_t symbol_size() const { return Symbols.size(); }
1109 /// @name Indirect Symbol List Access
1112 // FIXME: This is a total hack, this should not be here. Once things are
1113 // factored so that the streamer has direct access to the .o writer, it can
1115 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1116 return IndirectSymbols;
1119 indirect_symbol_iterator indirect_symbol_begin() {
1120 return IndirectSymbols.begin();
1122 const_indirect_symbol_iterator indirect_symbol_begin() const {
1123 return IndirectSymbols.begin();
1126 indirect_symbol_iterator indirect_symbol_end() {
1127 return IndirectSymbols.end();
1129 const_indirect_symbol_iterator indirect_symbol_end() const {
1130 return IndirectSymbols.end();
1133 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1136 /// @name Linker Option List Access
1139 std::vector<std::vector<std::string> > &getLinkerOptions() {
1140 return LinkerOptions;
1144 /// @name Data Region List Access
1147 // FIXME: This is a total hack, this should not be here. Once things are
1148 // factored so that the streamer has direct access to the .o writer, it can
1150 std::vector<DataRegionData> &getDataRegions() {
1154 data_region_iterator data_region_begin() {
1155 return DataRegions.begin();
1157 const_data_region_iterator data_region_begin() const {
1158 return DataRegions.begin();
1161 data_region_iterator data_region_end() {
1162 return DataRegions.end();
1164 const_data_region_iterator data_region_end() const {
1165 return DataRegions.end();
1168 size_t data_region_size() const { return DataRegions.size(); }
1171 /// @name Data Region List Access
1174 // FIXME: This is a total hack, this should not be here. Once things are
1175 // factored so that the streamer has direct access to the .o writer, it can
1177 MCLOHContainer & getLOHContainer() {
1178 return LOHContainer;
1180 const MCLOHContainer & getLOHContainer() const {
1181 return const_cast<MCAssembler *>(this)->getLOHContainer();
1184 /// @name Backend Data Access
1187 MCSectionData &getSectionData(const MCSection &Section) const {
1188 MCSectionData *Entry = SectionMap.lookup(&Section);
1189 assert(Entry && "Missing section data!");
1193 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1194 bool *Created = nullptr) {
1195 MCSectionData *&Entry = SectionMap[&Section];
1197 if (Created) *Created = !Entry;
1199 Entry = new MCSectionData(Section, this);
1204 bool hasSymbolData(const MCSymbol &Symbol) const {
1205 return SymbolMap.lookup(&Symbol) != nullptr;
1208 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1209 return const_cast<MCSymbolData &>(
1210 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1213 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1214 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1215 assert(Entry && "Missing symbol data!");
1219 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1220 bool *Created = nullptr) {
1221 MCSymbolData *&Entry = SymbolMap[&Symbol];
1223 if (Created) *Created = !Entry;
1225 Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1230 const_file_name_iterator file_names_begin() const {
1231 return FileNames.begin();
1234 const_file_name_iterator file_names_end() const {
1235 return FileNames.end();
1238 void addFileName(StringRef FileName) {
1239 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1241 FileNames.push_back(FileName);
1249 } // end namespace llvm