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/DenseSet.h"
15 #include "llvm/ADT/PointerIntPair.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/DataTypes.h"
28 #include <vector> // FIXME: Shouldn't be needed.
41 class MCSubtargetInfo;
47 class MCFragment : public ilist_node<MCFragment> {
48 friend class MCAsmLayout;
50 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
51 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
57 FT_CompactEncodedInst,
69 /// Parent - The data for the section this fragment is in.
70 MCSectionData *Parent;
72 /// Atom - The atom this fragment is in, as represented by it's defining
76 /// @name Assembler Backend Data
79 // FIXME: This could all be kept private to the assembler implementation.
81 /// Offset - The offset of this fragment in its section. This is ~0 until
85 /// LayoutOrder - The layout order of this fragment.
91 MCFragment(FragmentType _Kind, MCSectionData *_Parent = nullptr);
96 virtual ~MCFragment();
98 FragmentType getKind() const { return Kind; }
100 MCSectionData *getParent() const { return Parent; }
101 void setParent(MCSectionData *Value) { Parent = Value; }
103 MCSymbolData *getAtom() const { return Atom; }
104 void setAtom(MCSymbolData *Value) { Atom = Value; }
106 unsigned getLayoutOrder() const { return LayoutOrder; }
107 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
109 /// \brief Does this fragment have instructions emitted into it? By default
110 /// this is false, but specific fragment types may set it to true.
111 virtual bool hasInstructions() const { return false; }
113 /// \brief Should this fragment be placed at the end of an aligned bundle?
114 virtual bool alignToBundleEnd() const { return false; }
115 virtual void setAlignToBundleEnd(bool V) { }
117 /// \brief Get the padding size that must be inserted before this fragment.
118 /// Used for bundling. By default, no padding is inserted.
119 /// Note that padding size is restricted to 8 bits. This is an optimization
120 /// to reduce the amount of space used for each fragment. In practice, larger
121 /// padding should never be required.
122 virtual uint8_t getBundlePadding() const {
126 /// \brief Set the padding size for this fragment. By default it's a no-op,
127 /// and only some fragments have a meaningful implementation.
128 virtual void setBundlePadding(uint8_t N) {
134 /// Interface implemented by fragments that contain encoded instructions and/or
137 class MCEncodedFragment : public MCFragment {
138 virtual void anchor();
140 uint8_t BundlePadding;
142 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = nullptr)
143 : MCFragment(FType, SD), BundlePadding(0)
146 virtual ~MCEncodedFragment();
148 virtual SmallVectorImpl<char> &getContents() = 0;
149 virtual const SmallVectorImpl<char> &getContents() const = 0;
151 uint8_t getBundlePadding() const override {
152 return BundlePadding;
155 void setBundlePadding(uint8_t N) override {
159 static bool classof(const MCFragment *F) {
160 MCFragment::FragmentType Kind = F->getKind();
164 case MCFragment::FT_Relaxable:
165 case MCFragment::FT_CompactEncodedInst:
166 case MCFragment::FT_Data:
172 /// Interface implemented by fragments that contain encoded instructions and/or
173 /// data and also have fixups registered.
175 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
176 void anchor() override;
179 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
180 MCSectionData *SD = nullptr)
181 : MCEncodedFragment(FType, SD)
185 virtual ~MCEncodedFragmentWithFixups();
187 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
188 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
190 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
191 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
193 virtual fixup_iterator fixup_begin() = 0;
194 virtual const_fixup_iterator fixup_begin() const = 0;
195 virtual fixup_iterator fixup_end() = 0;
196 virtual const_fixup_iterator fixup_end() const = 0;
198 static bool classof(const MCFragment *F) {
199 MCFragment::FragmentType Kind = F->getKind();
200 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
204 /// Fragment for data and encoded instructions.
206 class MCDataFragment : public MCEncodedFragmentWithFixups {
207 void anchor() override;
209 /// \brief Does this fragment contain encoded instructions anywhere in it?
210 bool HasInstructions;
212 /// \brief Should this fragment be aligned to the end of a bundle?
213 bool AlignToBundleEnd;
215 SmallVector<char, 32> Contents;
217 /// Fixups - The list of fixups in this fragment.
218 SmallVector<MCFixup, 4> Fixups;
220 MCDataFragment(MCSectionData *SD = nullptr)
221 : MCEncodedFragmentWithFixups(FT_Data, SD),
222 HasInstructions(false), AlignToBundleEnd(false)
226 SmallVectorImpl<char> &getContents() override { return Contents; }
227 const SmallVectorImpl<char> &getContents() const override {
231 SmallVectorImpl<MCFixup> &getFixups() override {
235 const SmallVectorImpl<MCFixup> &getFixups() const override {
239 bool hasInstructions() const override { return HasInstructions; }
240 virtual void setHasInstructions(bool V) { HasInstructions = V; }
242 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
243 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
245 fixup_iterator fixup_begin() override { return Fixups.begin(); }
246 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
248 fixup_iterator fixup_end() override {return Fixups.end();}
249 const_fixup_iterator fixup_end() const override {return Fixups.end();}
251 static bool classof(const MCFragment *F) {
252 return F->getKind() == MCFragment::FT_Data;
256 /// This is a compact (memory-size-wise) fragment for holding an encoded
257 /// instruction (non-relaxable) that has no fixups registered. When applicable,
258 /// it can be used instead of MCDataFragment and lead to lower memory
261 class MCCompactEncodedInstFragment : public MCEncodedFragment {
262 void anchor() override;
264 /// \brief Should this fragment be aligned to the end of a bundle?
265 bool AlignToBundleEnd;
267 SmallVector<char, 4> Contents;
269 MCCompactEncodedInstFragment(MCSectionData *SD = nullptr)
270 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
274 bool hasInstructions() const override {
278 SmallVectorImpl<char> &getContents() override { return Contents; }
279 const SmallVectorImpl<char> &getContents() const override { return Contents; }
281 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
282 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
284 static bool classof(const MCFragment *F) {
285 return F->getKind() == MCFragment::FT_CompactEncodedInst;
289 /// A relaxable fragment holds on to its MCInst, since it may need to be
290 /// relaxed during the assembler layout and relaxation stage.
292 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
293 void anchor() override;
295 /// Inst - The instruction this is a fragment for.
298 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
299 /// Keep a copy instead of a reference to make sure that updates to STI
300 /// in the assembler are not seen here.
301 const MCSubtargetInfo STI;
303 /// Contents - Binary data for the currently encoded instruction.
304 SmallVector<char, 8> Contents;
306 /// Fixups - The list of fixups in this fragment.
307 SmallVector<MCFixup, 1> Fixups;
310 MCRelaxableFragment(const MCInst &_Inst,
311 const MCSubtargetInfo &_STI,
312 MCSectionData *SD = nullptr)
313 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
316 SmallVectorImpl<char> &getContents() override { return Contents; }
317 const SmallVectorImpl<char> &getContents() const override { return Contents; }
319 const MCInst &getInst() const { return Inst; }
320 void setInst(const MCInst& Value) { Inst = Value; }
322 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
324 SmallVectorImpl<MCFixup> &getFixups() override {
328 const SmallVectorImpl<MCFixup> &getFixups() const override {
332 bool hasInstructions() const override { return true; }
334 fixup_iterator fixup_begin() override { return Fixups.begin(); }
335 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
337 fixup_iterator fixup_end() override {return Fixups.end();}
338 const_fixup_iterator fixup_end() const override {return Fixups.end();}
340 static bool classof(const MCFragment *F) {
341 return F->getKind() == MCFragment::FT_Relaxable;
345 class MCAlignFragment : public MCFragment {
346 virtual void anchor();
348 /// Alignment - The alignment to ensure, in bytes.
351 /// Value - Value to use for filling padding bytes.
354 /// ValueSize - The size of the integer (in bytes) of \p Value.
357 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
358 /// cannot be satisfied in this width then this fragment is ignored.
359 unsigned MaxBytesToEmit;
361 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
362 /// of using the provided value. The exact interpretation of this flag is
363 /// target dependent.
367 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
368 unsigned _MaxBytesToEmit, MCSectionData *SD = nullptr)
369 : MCFragment(FT_Align, SD), Alignment(_Alignment),
370 Value(_Value),ValueSize(_ValueSize),
371 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
376 unsigned getAlignment() const { return Alignment; }
378 int64_t getValue() const { return Value; }
380 unsigned getValueSize() const { return ValueSize; }
382 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
384 bool hasEmitNops() const { return EmitNops; }
385 void setEmitNops(bool Value) { EmitNops = Value; }
389 static bool classof(const MCFragment *F) {
390 return F->getKind() == MCFragment::FT_Align;
394 class MCFillFragment : public MCFragment {
395 virtual void anchor();
397 /// Value - Value to use for filling bytes.
400 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
401 /// this is a virtual fill fragment.
404 /// Size - The number of bytes to insert.
408 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
409 MCSectionData *SD = nullptr)
410 : MCFragment(FT_Fill, SD),
411 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
412 assert((!ValueSize || (Size % ValueSize) == 0) &&
413 "Fill size must be a multiple of the value size!");
419 int64_t getValue() const { return Value; }
421 unsigned getValueSize() const { return ValueSize; }
423 uint64_t getSize() const { return Size; }
427 static bool classof(const MCFragment *F) {
428 return F->getKind() == MCFragment::FT_Fill;
432 class MCOrgFragment : public MCFragment {
433 virtual void anchor();
435 /// Offset - The offset this fragment should start at.
436 const MCExpr *Offset;
438 /// Value - Value to use for filling bytes.
442 MCOrgFragment(const MCExpr &_Offset, int8_t _Value,
443 MCSectionData *SD = nullptr)
444 : MCFragment(FT_Org, SD),
445 Offset(&_Offset), Value(_Value) {}
450 const MCExpr &getOffset() const { return *Offset; }
452 uint8_t getValue() const { return Value; }
456 static bool classof(const MCFragment *F) {
457 return F->getKind() == MCFragment::FT_Org;
461 class MCLEBFragment : public MCFragment {
462 virtual void anchor();
464 /// Value - The value this fragment should contain.
467 /// IsSigned - True if this is a sleb128, false if uleb128.
470 SmallString<8> Contents;
472 MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
473 MCSectionData *SD = nullptr)
474 : MCFragment(FT_LEB, SD),
475 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
480 const MCExpr &getValue() const { return *Value; }
482 bool isSigned() const { return IsSigned; }
484 SmallString<8> &getContents() { return Contents; }
485 const SmallString<8> &getContents() const { return Contents; }
489 static bool classof(const MCFragment *F) {
490 return F->getKind() == MCFragment::FT_LEB;
494 class MCDwarfLineAddrFragment : public MCFragment {
495 virtual void anchor();
497 /// LineDelta - the value of the difference between the two line numbers
498 /// between two .loc dwarf directives.
501 /// AddrDelta - The expression for the difference of the two symbols that
502 /// make up the address delta between two .loc dwarf directives.
503 const MCExpr *AddrDelta;
505 SmallString<8> Contents;
508 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
509 MCSectionData *SD = nullptr)
510 : MCFragment(FT_Dwarf, SD),
511 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
516 int64_t getLineDelta() const { return LineDelta; }
518 const MCExpr &getAddrDelta() const { return *AddrDelta; }
520 SmallString<8> &getContents() { return Contents; }
521 const SmallString<8> &getContents() const { return Contents; }
525 static bool classof(const MCFragment *F) {
526 return F->getKind() == MCFragment::FT_Dwarf;
530 class MCDwarfCallFrameFragment : public MCFragment {
531 virtual void anchor();
533 /// AddrDelta - The expression for the difference of the two symbols that
534 /// make up the address delta between two .cfi_* dwarf directives.
535 const MCExpr *AddrDelta;
537 SmallString<8> Contents;
540 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta,
541 MCSectionData *SD = nullptr)
542 : MCFragment(FT_DwarfFrame, SD),
543 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
548 const MCExpr &getAddrDelta() const { return *AddrDelta; }
550 SmallString<8> &getContents() { return Contents; }
551 const SmallString<8> &getContents() const { return Contents; }
555 static bool classof(const MCFragment *F) {
556 return F->getKind() == MCFragment::FT_DwarfFrame;
560 // FIXME: Should this be a separate class, or just merged into MCSection? Since
561 // we anticipate the fast path being through an MCAssembler, the only reason to
562 // keep it out is for API abstraction.
563 class MCSectionData : public ilist_node<MCSectionData> {
564 friend class MCAsmLayout;
566 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
567 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
570 typedef iplist<MCFragment> FragmentListType;
572 typedef FragmentListType::const_iterator const_iterator;
573 typedef FragmentListType::iterator iterator;
575 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
576 typedef FragmentListType::reverse_iterator reverse_iterator;
578 /// \brief Express the state of bundle locked groups while emitting code.
579 enum BundleLockStateType {
582 BundleLockedAlignToEnd
585 FragmentListType Fragments;
586 const MCSection *Section;
588 /// Ordinal - The section index in the assemblers section list.
591 /// LayoutOrder - The index of this section in the layout order.
592 unsigned LayoutOrder;
594 /// Alignment - The maximum alignment seen in this section.
597 /// \brief Keeping track of bundle-locked state.
598 BundleLockStateType BundleLockState;
600 /// \brief Current nesting depth of bundle_lock directives.
601 unsigned BundleLockNestingDepth;
603 /// \brief We've seen a bundle_lock directive but not its first instruction
605 bool BundleGroupBeforeFirstInst;
607 /// @name Assembler Backend Data
610 // FIXME: This could all be kept private to the assembler implementation.
612 /// HasInstructions - Whether this section has had instructions emitted into
614 unsigned HasInstructions : 1;
616 /// Mapping from subsection number to insertion point for subsection numbers
617 /// below that number.
618 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
623 // Only for use as sentinel.
625 MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
627 const MCSection &getSection() const { return *Section; }
629 unsigned getAlignment() const { return Alignment; }
630 void setAlignment(unsigned Value) { Alignment = Value; }
632 bool hasInstructions() const { return HasInstructions; }
633 void setHasInstructions(bool Value) { HasInstructions = Value; }
635 unsigned getOrdinal() const { return Ordinal; }
636 void setOrdinal(unsigned Value) { Ordinal = Value; }
638 unsigned getLayoutOrder() const { return LayoutOrder; }
639 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
641 /// @name Fragment Access
644 const FragmentListType &getFragmentList() const { return Fragments; }
645 FragmentListType &getFragmentList() { return Fragments; }
647 iterator begin() { return Fragments.begin(); }
648 const_iterator begin() const { return Fragments.begin(); }
650 iterator end() { return Fragments.end(); }
651 const_iterator end() const { return Fragments.end(); }
653 reverse_iterator rbegin() { return Fragments.rbegin(); }
654 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
656 reverse_iterator rend() { return Fragments.rend(); }
657 const_reverse_iterator rend() const { return Fragments.rend(); }
659 size_t size() const { return Fragments.size(); }
661 bool empty() const { return Fragments.empty(); }
663 iterator getSubsectionInsertionPoint(unsigned Subsection);
665 bool isBundleLocked() const {
666 return BundleLockState != NotBundleLocked;
669 BundleLockStateType getBundleLockState() const {
670 return BundleLockState;
673 void setBundleLockState(BundleLockStateType NewState);
675 bool isBundleGroupBeforeFirstInst() const {
676 return BundleGroupBeforeFirstInst;
679 void setBundleGroupBeforeFirstInst(bool IsFirst) {
680 BundleGroupBeforeFirstInst = IsFirst;
688 // FIXME: Same concerns as with SectionData.
689 class MCSymbolData : public ilist_node<MCSymbolData> {
690 const MCSymbol *Symbol;
692 /// Fragment - The fragment this symbol's value is relative to, if any. Also
693 /// stores if this symbol is visible outside this translation unit (bit 0) or
694 /// if it is private extern (bit 1).
695 PointerIntPair<MCFragment *, 2> Fragment;
698 /// Offset - The offset to apply to the fragment address to form this
702 /// CommonSize - The size of the symbol, if it is 'common'.
706 /// SymbolSize - An expression describing how to calculate the size of
707 /// a symbol. If a symbol has no size this field will be NULL.
708 const MCExpr *SymbolSize;
710 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
712 // FIXME: Pack this in with other fields?
713 unsigned CommonAlign;
715 /// Flags - The Flags field is used by object file implementations to store
716 /// additional per symbol information which is not easily classified.
719 /// Index - Index field, for use by the object file implementation.
723 // Only for use as sentinel.
725 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
726 MCAssembler *A = nullptr);
731 const MCSymbol &getSymbol() const { return *Symbol; }
733 MCFragment *getFragment() const { return Fragment.getPointer(); }
734 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
736 uint64_t getOffset() const {
740 void setOffset(uint64_t Value) {
746 /// @name Symbol Attributes
749 bool isExternal() const { return Fragment.getInt() & 1; }
750 void setExternal(bool Value) {
751 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
754 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
755 void setPrivateExtern(bool Value) {
756 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
759 /// isCommon - Is this a 'common' symbol.
760 bool isCommon() const { return CommonAlign != -1U; }
762 /// setCommon - Mark this symbol as being 'common'.
764 /// \param Size - The size of the symbol.
765 /// \param Align - The alignment of the symbol.
766 void setCommon(uint64_t Size, unsigned Align) {
767 assert(getOffset() == 0);
772 /// getCommonSize - Return the size of a 'common' symbol.
773 uint64_t getCommonSize() const {
774 assert(isCommon() && "Not a 'common' symbol!");
778 void setSize(const MCExpr *SS) {
782 const MCExpr *getSize() const {
787 /// getCommonAlignment - Return the alignment of a 'common' symbol.
788 unsigned getCommonAlignment() const {
789 assert(isCommon() && "Not a 'common' symbol!");
793 /// getFlags - Get the (implementation defined) symbol flags.
794 uint32_t getFlags() const { return Flags; }
796 /// setFlags - Set the (implementation defined) symbol flags.
797 void setFlags(uint32_t Value) { Flags = Value; }
799 /// modifyFlags - Modify the flags via a mask
800 void modifyFlags(uint32_t Value, uint32_t Mask) {
801 Flags = (Flags & ~Mask) | Value;
804 /// getIndex - Get the (implementation defined) index.
805 uint64_t getIndex() const { return Index; }
807 /// setIndex - Set the (implementation defined) index.
808 void setIndex(uint64_t Value) { Index = Value; }
815 // FIXME: This really doesn't belong here. See comments below.
816 struct IndirectSymbolData {
818 MCSectionData *SectionData;
821 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
823 struct DataRegionData {
824 // This enum should be kept in sync w/ the mach-o definition in
825 // llvm/Object/MachOFormat.h.
826 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
832 friend class MCAsmLayout;
835 typedef iplist<MCSectionData> SectionDataListType;
836 typedef iplist<MCSymbolData> SymbolDataListType;
838 typedef SectionDataListType::const_iterator const_iterator;
839 typedef SectionDataListType::iterator iterator;
841 typedef SymbolDataListType::const_iterator const_symbol_iterator;
842 typedef SymbolDataListType::iterator symbol_iterator;
844 typedef iterator_range<symbol_iterator> symbol_range;
845 typedef iterator_range<const_symbol_iterator> const_symbol_range;
847 typedef std::vector<std::string> FileNameVectorType;
848 typedef FileNameVectorType::const_iterator const_file_name_iterator;
850 typedef std::vector<IndirectSymbolData>::const_iterator
851 const_indirect_symbol_iterator;
852 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
854 typedef std::vector<DataRegionData>::const_iterator
855 const_data_region_iterator;
856 typedef std::vector<DataRegionData>::iterator data_region_iterator;
858 /// MachO specific deployment target version info.
859 // A Major version of 0 indicates that no version information was supplied
860 // and so the corresponding load command should not be emitted.
862 MCVersionMinType Kind;
866 } VersionMinInfoType;
868 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
869 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
873 MCAsmBackend &Backend;
875 MCCodeEmitter &Emitter;
877 MCObjectWriter &Writer;
881 iplist<MCSectionData> Sections;
883 iplist<MCSymbolData> Symbols;
885 DenseSet<const MCSymbol *> LocalsUsedInReloc;
887 /// The map of sections to their associated assembler backend data.
889 // FIXME: Avoid this indirection?
890 DenseMap<const MCSection*, MCSectionData*> SectionMap;
892 /// The map of symbols to their associated assembler backend data.
894 // FIXME: Avoid this indirection?
895 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
897 std::vector<IndirectSymbolData> IndirectSymbols;
899 std::vector<DataRegionData> DataRegions;
901 /// The list of linker options to propagate into the object file.
902 std::vector<std::vector<std::string> > LinkerOptions;
904 /// List of declared file names
905 FileNameVectorType FileNames;
907 /// The set of function symbols for which a .thumb_func directive has
910 // FIXME: We really would like this in target specific code rather than
911 // here. Maybe when the relocation stuff moves to target specific,
912 // this can go with it? The streamer would need some target specific
914 mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
916 /// \brief The bundle alignment size currently set in the assembler.
918 /// By default it's 0, which means bundling is disabled.
919 unsigned BundleAlignSize;
921 unsigned RelaxAll : 1;
922 unsigned SubsectionsViaSymbols : 1;
924 /// ELF specific e_header flags
925 // It would be good if there were an MCELFAssembler class to hold this.
926 // ELF header flags are used both by the integrated and standalone assemblers.
927 // Access to the flags is necessary in cases where assembler directives affect
928 // which flags to be set.
929 unsigned ELFHeaderEFlags;
931 /// Used to communicate Linker Optimization Hint information between
932 /// the Streamer and the .o writer
933 MCLOHContainer LOHContainer;
935 VersionMinInfoType VersionMinInfo;
937 /// Evaluate a fixup to a relocatable expression and the value which should be
938 /// placed into the fixup.
940 /// \param Layout The layout to use for evaluation.
941 /// \param Fixup The fixup to evaluate.
942 /// \param DF The fragment the fixup is inside.
943 /// \param Target [out] On return, the relocatable expression the fixup
945 /// \param Value [out] On return, the value of the fixup as currently laid
947 /// \return Whether the fixup value was fully resolved. This is true if the
948 /// \p Value result is fixed, otherwise the value may change due to
950 bool evaluateFixup(const MCAsmLayout &Layout,
951 const MCFixup &Fixup, const MCFragment *DF,
952 MCValue &Target, uint64_t &Value) const;
954 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
955 /// (increased in size, in order to hold its value correctly).
956 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
957 const MCAsmLayout &Layout) const;
959 /// Check whether the given fragment needs relaxation.
960 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
961 const MCAsmLayout &Layout) const;
963 /// \brief Perform one layout iteration and return true if any offsets
965 bool layoutOnce(MCAsmLayout &Layout);
967 /// \brief Perform one layout iteration of the given section and return true
968 /// if any offsets were adjusted.
969 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
971 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
973 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
975 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
976 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
977 MCDwarfCallFrameFragment &DF);
979 /// finishLayout - Finalize a layout, including fragment lowering.
980 void finishLayout(MCAsmLayout &Layout);
982 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
983 MCFragment &F, const MCFixup &Fixup);
986 void addLocalUsedInReloc(const MCSymbol &Sym);
987 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
989 /// Compute the effective fragment size assuming it is laid out at the given
990 /// \p SectionAddress and \p FragmentOffset.
991 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
992 const MCFragment &F) const;
994 /// Find the symbol which defines the atom containing the given symbol, or
995 /// null if there is no such symbol.
996 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
998 /// Check whether a particular symbol is visible to the linker and is required
999 /// in the symbol table, or whether it can be discarded by the assembler. This
1000 /// also effects whether the assembler treats the label as potentially
1001 /// defining a separate atom.
1002 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
1004 /// Emit the section contents using the given object writer.
1005 void writeSectionData(const MCSectionData *Section,
1006 const MCAsmLayout &Layout) const;
1008 /// Check whether a given symbol has been flagged with .thumb_func.
1009 bool isThumbFunc(const MCSymbol *Func) const;
1011 /// Flag a function symbol as the target of a .thumb_func directive.
1012 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1014 /// ELF e_header flags
1015 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1016 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1018 /// MachO deployment target version information.
1019 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1020 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1022 VersionMinInfo.Kind = Kind;
1023 VersionMinInfo.Major = Major;
1024 VersionMinInfo.Minor = Minor;
1025 VersionMinInfo.Update = Update;
1029 /// Construct a new assembler instance.
1031 /// \param OS The stream to output to.
1033 // FIXME: How are we going to parameterize this? Two obvious options are stay
1034 // concrete and require clients to pass in a target like object. The other
1035 // option is to make this abstract, and have targets provide concrete
1036 // implementations as we do with AsmParser.
1037 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1038 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1042 /// Reuse an assembler instance
1046 MCContext &getContext() const { return Context; }
1048 MCAsmBackend &getBackend() const { return Backend; }
1050 MCCodeEmitter &getEmitter() const { return Emitter; }
1052 MCObjectWriter &getWriter() const { return Writer; }
1054 /// Finish - Do final processing and write the object to the output stream.
1055 /// \p Writer is used for custom object writer (as the MCJIT does),
1056 /// if not specified it is automatically created from backend.
1059 // FIXME: This does not belong here.
1060 bool getSubsectionsViaSymbols() const {
1061 return SubsectionsViaSymbols;
1063 void setSubsectionsViaSymbols(bool Value) {
1064 SubsectionsViaSymbols = Value;
1067 bool getRelaxAll() const { return RelaxAll; }
1068 void setRelaxAll(bool Value) { RelaxAll = Value; }
1070 bool isBundlingEnabled() const {
1071 return BundleAlignSize != 0;
1074 unsigned getBundleAlignSize() const {
1075 return BundleAlignSize;
1078 void setBundleAlignSize(unsigned Size) {
1079 assert((Size == 0 || !(Size & (Size - 1))) &&
1080 "Expect a power-of-two bundle align size");
1081 BundleAlignSize = Size;
1084 /// @name Section List Access
1087 const SectionDataListType &getSectionList() const { return Sections; }
1088 SectionDataListType &getSectionList() { return Sections; }
1090 iterator begin() { return Sections.begin(); }
1091 const_iterator begin() const { return Sections.begin(); }
1093 iterator end() { return Sections.end(); }
1094 const_iterator end() const { return Sections.end(); }
1096 size_t size() const { return Sections.size(); }
1099 /// @name Symbol List Access
1102 const SymbolDataListType &getSymbolList() const { return Symbols; }
1103 SymbolDataListType &getSymbolList() { return Symbols; }
1105 symbol_iterator symbol_begin() { return Symbols.begin(); }
1106 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1108 symbol_iterator symbol_end() { return Symbols.end(); }
1109 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1111 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1112 const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1114 size_t symbol_size() const { return Symbols.size(); }
1117 /// @name Indirect Symbol List Access
1120 // FIXME: This is a total hack, this should not be here. Once things are
1121 // factored so that the streamer has direct access to the .o writer, it can
1123 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1124 return IndirectSymbols;
1127 indirect_symbol_iterator indirect_symbol_begin() {
1128 return IndirectSymbols.begin();
1130 const_indirect_symbol_iterator indirect_symbol_begin() const {
1131 return IndirectSymbols.begin();
1134 indirect_symbol_iterator indirect_symbol_end() {
1135 return IndirectSymbols.end();
1137 const_indirect_symbol_iterator indirect_symbol_end() const {
1138 return IndirectSymbols.end();
1141 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1144 /// @name Linker Option List Access
1147 std::vector<std::vector<std::string> > &getLinkerOptions() {
1148 return LinkerOptions;
1152 /// @name Data Region List Access
1155 // FIXME: This is a total hack, this should not be here. Once things are
1156 // factored so that the streamer has direct access to the .o writer, it can
1158 std::vector<DataRegionData> &getDataRegions() {
1162 data_region_iterator data_region_begin() {
1163 return DataRegions.begin();
1165 const_data_region_iterator data_region_begin() const {
1166 return DataRegions.begin();
1169 data_region_iterator data_region_end() {
1170 return DataRegions.end();
1172 const_data_region_iterator data_region_end() const {
1173 return DataRegions.end();
1176 size_t data_region_size() const { return DataRegions.size(); }
1179 /// @name Data Region List Access
1182 // FIXME: This is a total hack, this should not be here. Once things are
1183 // factored so that the streamer has direct access to the .o writer, it can
1185 MCLOHContainer & getLOHContainer() {
1186 return LOHContainer;
1188 const MCLOHContainer & getLOHContainer() const {
1189 return const_cast<MCAssembler *>(this)->getLOHContainer();
1192 /// @name Backend Data Access
1195 MCSectionData &getSectionData(const MCSection &Section) const {
1196 MCSectionData *Entry = SectionMap.lookup(&Section);
1197 assert(Entry && "Missing section data!");
1201 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1202 bool *Created = nullptr) {
1203 MCSectionData *&Entry = SectionMap[&Section];
1205 if (Created) *Created = !Entry;
1207 Entry = new MCSectionData(Section, this);
1212 bool hasSymbolData(const MCSymbol &Symbol) const {
1213 return SymbolMap.lookup(&Symbol) != nullptr;
1216 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1217 return const_cast<MCSymbolData &>(
1218 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1221 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1222 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1223 assert(Entry && "Missing symbol data!");
1227 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1228 bool *Created = nullptr) {
1229 MCSymbolData *&Entry = SymbolMap[&Symbol];
1231 if (Created) *Created = !Entry;
1233 Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1238 const_file_name_iterator file_names_begin() const {
1239 return FileNames.begin();
1242 const_file_name_iterator file_names_end() const {
1243 return FileNames.end();
1246 void addFileName(StringRef FileName) {
1247 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1249 FileNames.push_back(FileName);
1257 } // end namespace llvm