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&) = delete;
51 void operator=(const MCFragment&) = delete;
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 ~MCEncodedFragment() override;
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 ~MCEncodedFragmentWithFixups() override;
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, const MCSubtargetInfo &STI,
311 MCSectionData *SD = nullptr)
312 : 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), Value(Value),
368 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
373 unsigned getAlignment() const { return Alignment; }
375 int64_t getValue() const { return Value; }
377 unsigned getValueSize() const { return ValueSize; }
379 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
381 bool hasEmitNops() const { return EmitNops; }
382 void setEmitNops(bool Value) { EmitNops = Value; }
386 static bool classof(const MCFragment *F) {
387 return F->getKind() == MCFragment::FT_Align;
391 class MCFillFragment : public MCFragment {
392 virtual void anchor();
394 /// Value - Value to use for filling bytes.
397 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
398 /// this is a virtual fill fragment.
401 /// Size - The number of bytes to insert.
405 MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
406 MCSectionData *SD = nullptr)
407 : MCFragment(FT_Fill, SD), Value(Value), ValueSize(ValueSize),
409 assert((!ValueSize || (Size % ValueSize) == 0) &&
410 "Fill size must be a multiple of the value size!");
416 int64_t getValue() const { return Value; }
418 unsigned getValueSize() const { return ValueSize; }
420 uint64_t getSize() const { return Size; }
424 static bool classof(const MCFragment *F) {
425 return F->getKind() == MCFragment::FT_Fill;
429 class MCOrgFragment : public MCFragment {
430 virtual void anchor();
432 /// Offset - The offset this fragment should start at.
433 const MCExpr *Offset;
435 /// Value - Value to use for filling bytes.
439 MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSectionData *SD = nullptr)
440 : MCFragment(FT_Org, SD), Offset(&Offset), Value(Value) {}
445 const MCExpr &getOffset() const { return *Offset; }
447 uint8_t getValue() const { return Value; }
451 static bool classof(const MCFragment *F) {
452 return F->getKind() == MCFragment::FT_Org;
456 class MCLEBFragment : public MCFragment {
457 virtual void anchor();
459 /// Value - The value this fragment should contain.
462 /// IsSigned - True if this is a sleb128, false if uleb128.
465 SmallString<8> Contents;
467 MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
468 MCSectionData *SD = nullptr)
469 : MCFragment(FT_LEB, SD),
470 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
475 const MCExpr &getValue() const { return *Value; }
477 bool isSigned() const { return IsSigned; }
479 SmallString<8> &getContents() { return Contents; }
480 const SmallString<8> &getContents() const { return Contents; }
484 static bool classof(const MCFragment *F) {
485 return F->getKind() == MCFragment::FT_LEB;
489 class MCDwarfLineAddrFragment : public MCFragment {
490 virtual void anchor();
492 /// LineDelta - the value of the difference between the two line numbers
493 /// between two .loc dwarf directives.
496 /// AddrDelta - The expression for the difference of the two symbols that
497 /// make up the address delta between two .loc dwarf directives.
498 const MCExpr *AddrDelta;
500 SmallString<8> Contents;
503 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
504 MCSectionData *SD = nullptr)
505 : MCFragment(FT_Dwarf, SD), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
506 Contents.push_back(0);
512 int64_t getLineDelta() const { return LineDelta; }
514 const MCExpr &getAddrDelta() const { return *AddrDelta; }
516 SmallString<8> &getContents() { return Contents; }
517 const SmallString<8> &getContents() const { return Contents; }
521 static bool classof(const MCFragment *F) {
522 return F->getKind() == MCFragment::FT_Dwarf;
526 class MCDwarfCallFrameFragment : public MCFragment {
527 virtual void anchor();
529 /// AddrDelta - The expression for the difference of the two symbols that
530 /// make up the address delta between two .cfi_* dwarf directives.
531 const MCExpr *AddrDelta;
533 SmallString<8> Contents;
536 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSectionData *SD = nullptr)
537 : MCFragment(FT_DwarfFrame, SD), AddrDelta(&AddrDelta) {
538 Contents.push_back(0);
544 const MCExpr &getAddrDelta() const { return *AddrDelta; }
546 SmallString<8> &getContents() { return Contents; }
547 const SmallString<8> &getContents() const { return Contents; }
551 static bool classof(const MCFragment *F) {
552 return F->getKind() == MCFragment::FT_DwarfFrame;
556 // FIXME: Should this be a separate class, or just merged into MCSection? Since
557 // we anticipate the fast path being through an MCAssembler, the only reason to
558 // keep it out is for API abstraction.
559 class MCSectionData : public ilist_node<MCSectionData> {
560 friend class MCAsmLayout;
562 MCSectionData(const MCSectionData&) = delete;
563 void operator=(const MCSectionData&) = delete;
566 typedef iplist<MCFragment> FragmentListType;
568 typedef FragmentListType::const_iterator const_iterator;
569 typedef FragmentListType::iterator iterator;
571 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
572 typedef FragmentListType::reverse_iterator reverse_iterator;
574 /// \brief Express the state of bundle locked groups while emitting code.
575 enum BundleLockStateType {
578 BundleLockedAlignToEnd
581 FragmentListType Fragments;
582 const MCSection *Section;
584 /// Ordinal - The section index in the assemblers section list.
587 /// LayoutOrder - The index of this section in the layout order.
588 unsigned LayoutOrder;
590 /// Alignment - The maximum alignment seen in this section.
593 /// \brief Keeping track of bundle-locked state.
594 BundleLockStateType BundleLockState;
596 /// \brief Current nesting depth of bundle_lock directives.
597 unsigned BundleLockNestingDepth;
599 /// \brief We've seen a bundle_lock directive but not its first instruction
601 bool BundleGroupBeforeFirstInst;
603 /// @name Assembler Backend Data
606 // FIXME: This could all be kept private to the assembler implementation.
608 /// HasInstructions - Whether this section has had instructions emitted into
610 unsigned HasInstructions : 1;
612 /// Mapping from subsection number to insertion point for subsection numbers
613 /// below that number.
614 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
619 // Only for use as sentinel.
621 MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
623 const MCSection &getSection() const { return *Section; }
625 unsigned getAlignment() const { return Alignment; }
626 void setAlignment(unsigned Value) { Alignment = Value; }
628 bool hasInstructions() const { return HasInstructions; }
629 void setHasInstructions(bool Value) { HasInstructions = Value; }
631 unsigned getOrdinal() const { return Ordinal; }
632 void setOrdinal(unsigned Value) { Ordinal = Value; }
634 unsigned getLayoutOrder() const { return LayoutOrder; }
635 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
637 /// @name Fragment Access
640 const FragmentListType &getFragmentList() const { return Fragments; }
641 FragmentListType &getFragmentList() { return Fragments; }
643 iterator begin() { return Fragments.begin(); }
644 const_iterator begin() const { return Fragments.begin(); }
646 iterator end() { return Fragments.end(); }
647 const_iterator end() const { return Fragments.end(); }
649 reverse_iterator rbegin() { return Fragments.rbegin(); }
650 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
652 reverse_iterator rend() { return Fragments.rend(); }
653 const_reverse_iterator rend() const { return Fragments.rend(); }
655 size_t size() const { return Fragments.size(); }
657 bool empty() const { return Fragments.empty(); }
659 iterator getSubsectionInsertionPoint(unsigned Subsection);
661 bool isBundleLocked() const {
662 return BundleLockState != NotBundleLocked;
665 BundleLockStateType getBundleLockState() const {
666 return BundleLockState;
669 void setBundleLockState(BundleLockStateType NewState);
671 bool isBundleGroupBeforeFirstInst() const {
672 return BundleGroupBeforeFirstInst;
675 void setBundleGroupBeforeFirstInst(bool IsFirst) {
676 BundleGroupBeforeFirstInst = IsFirst;
684 // FIXME: Same concerns as with SectionData.
685 class MCSymbolData : public ilist_node<MCSymbolData> {
686 const MCSymbol *Symbol;
688 /// Fragment - The fragment this symbol's value is relative to, if any. Also
689 /// stores if this symbol is visible outside this translation unit (bit 0) or
690 /// if it is private extern (bit 1).
691 PointerIntPair<MCFragment *, 2> Fragment;
694 /// Offset - The offset to apply to the fragment address to form this
698 /// CommonSize - The size of the symbol, if it is 'common'.
702 /// SymbolSize - An expression describing how to calculate the size of
703 /// a symbol. If a symbol has no size this field will be NULL.
704 const MCExpr *SymbolSize;
706 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
708 // FIXME: Pack this in with other fields?
709 unsigned CommonAlign;
711 /// Flags - The Flags field is used by object file implementations to store
712 /// additional per symbol information which is not easily classified.
715 /// Index - Index field, for use by the object file implementation.
719 // Only for use as sentinel.
721 MCSymbolData(const MCSymbol &Symbol, MCFragment *Fragment, uint64_t Offset,
722 MCAssembler *A = nullptr);
727 const MCSymbol &getSymbol() const { return *Symbol; }
729 MCFragment *getFragment() const { return Fragment.getPointer(); }
730 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
732 uint64_t getOffset() const {
736 void setOffset(uint64_t Value) {
742 /// @name Symbol Attributes
745 bool isExternal() const { return Fragment.getInt() & 1; }
746 void setExternal(bool Value) {
747 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
750 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
751 void setPrivateExtern(bool Value) {
752 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
755 /// isCommon - Is this a 'common' symbol.
756 bool isCommon() const { return CommonAlign != -1U; }
758 /// setCommon - Mark this symbol as being 'common'.
760 /// \param Size - The size of the symbol.
761 /// \param Align - The alignment of the symbol.
762 void setCommon(uint64_t Size, unsigned Align) {
763 assert(getOffset() == 0);
768 /// getCommonSize - Return the size of a 'common' symbol.
769 uint64_t getCommonSize() const {
770 assert(isCommon() && "Not a 'common' symbol!");
774 void setSize(const MCExpr *SS) {
778 const MCExpr *getSize() const {
783 /// getCommonAlignment - Return the alignment of a 'common' symbol.
784 unsigned getCommonAlignment() const {
785 assert(isCommon() && "Not a 'common' symbol!");
789 /// getFlags - Get the (implementation defined) symbol flags.
790 uint32_t getFlags() const { return Flags; }
792 /// setFlags - Set the (implementation defined) symbol flags.
793 void setFlags(uint32_t Value) { Flags = Value; }
795 /// modifyFlags - Modify the flags via a mask
796 void modifyFlags(uint32_t Value, uint32_t Mask) {
797 Flags = (Flags & ~Mask) | Value;
800 /// getIndex - Get the (implementation defined) index.
801 uint64_t getIndex() const { return Index; }
803 /// setIndex - Set the (implementation defined) index.
804 void setIndex(uint64_t Value) { Index = Value; }
811 // FIXME: This really doesn't belong here. See comments below.
812 struct IndirectSymbolData {
814 MCSectionData *SectionData;
817 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
819 struct DataRegionData {
820 // This enum should be kept in sync w/ the mach-o definition in
821 // llvm/Object/MachOFormat.h.
822 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
828 friend class MCAsmLayout;
831 typedef iplist<MCSectionData> SectionDataListType;
832 typedef iplist<MCSymbolData> SymbolDataListType;
834 typedef SectionDataListType::const_iterator const_iterator;
835 typedef SectionDataListType::iterator iterator;
837 typedef SymbolDataListType::const_iterator const_symbol_iterator;
838 typedef SymbolDataListType::iterator symbol_iterator;
840 typedef iterator_range<symbol_iterator> symbol_range;
841 typedef iterator_range<const_symbol_iterator> const_symbol_range;
843 typedef std::vector<std::string> FileNameVectorType;
844 typedef FileNameVectorType::const_iterator const_file_name_iterator;
846 typedef std::vector<IndirectSymbolData>::const_iterator
847 const_indirect_symbol_iterator;
848 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
850 typedef std::vector<DataRegionData>::const_iterator
851 const_data_region_iterator;
852 typedef std::vector<DataRegionData>::iterator data_region_iterator;
854 /// MachO specific deployment target version info.
855 // A Major version of 0 indicates that no version information was supplied
856 // and so the corresponding load command should not be emitted.
858 MCVersionMinType Kind;
862 } VersionMinInfoType;
864 MCAssembler(const MCAssembler&) = delete;
865 void operator=(const MCAssembler&) = delete;
869 MCAsmBackend &Backend;
871 MCCodeEmitter &Emitter;
873 MCObjectWriter &Writer;
877 iplist<MCSectionData> Sections;
879 iplist<MCSymbolData> Symbols;
881 DenseSet<const MCSymbol *> LocalsUsedInReloc;
883 /// The map of sections to their associated assembler backend data.
885 // FIXME: Avoid this indirection?
886 DenseMap<const MCSection*, MCSectionData*> SectionMap;
888 /// The map of symbols to their associated assembler backend data.
890 // FIXME: Avoid this indirection?
891 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
893 std::vector<IndirectSymbolData> IndirectSymbols;
895 std::vector<DataRegionData> DataRegions;
897 /// The list of linker options to propagate into the object file.
898 std::vector<std::vector<std::string> > LinkerOptions;
900 /// List of declared file names
901 FileNameVectorType FileNames;
903 /// The set of function symbols for which a .thumb_func directive has
906 // FIXME: We really would like this in target specific code rather than
907 // here. Maybe when the relocation stuff moves to target specific,
908 // this can go with it? The streamer would need some target specific
910 mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
912 /// \brief The bundle alignment size currently set in the assembler.
914 /// By default it's 0, which means bundling is disabled.
915 unsigned BundleAlignSize;
917 unsigned RelaxAll : 1;
918 unsigned SubsectionsViaSymbols : 1;
920 /// ELF specific e_header flags
921 // It would be good if there were an MCELFAssembler class to hold this.
922 // ELF header flags are used both by the integrated and standalone assemblers.
923 // Access to the flags is necessary in cases where assembler directives affect
924 // which flags to be set.
925 unsigned ELFHeaderEFlags;
927 /// Used to communicate Linker Optimization Hint information between
928 /// the Streamer and the .o writer
929 MCLOHContainer LOHContainer;
931 VersionMinInfoType VersionMinInfo;
933 /// Evaluate a fixup to a relocatable expression and the value which should be
934 /// placed into the fixup.
936 /// \param Layout The layout to use for evaluation.
937 /// \param Fixup The fixup to evaluate.
938 /// \param DF The fragment the fixup is inside.
939 /// \param Target [out] On return, the relocatable expression the fixup
941 /// \param Value [out] On return, the value of the fixup as currently laid
943 /// \return Whether the fixup value was fully resolved. This is true if the
944 /// \p Value result is fixed, otherwise the value may change due to
946 bool evaluateFixup(const MCAsmLayout &Layout,
947 const MCFixup &Fixup, const MCFragment *DF,
948 MCValue &Target, uint64_t &Value) const;
950 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
951 /// (increased in size, in order to hold its value correctly).
952 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
953 const MCAsmLayout &Layout) const;
955 /// Check whether the given fragment needs relaxation.
956 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
957 const MCAsmLayout &Layout) const;
959 /// \brief Perform one layout iteration and return true if any offsets
961 bool layoutOnce(MCAsmLayout &Layout);
963 /// \brief Perform one layout iteration of the given section and return true
964 /// if any offsets were adjusted.
965 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
967 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
969 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
971 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
972 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
973 MCDwarfCallFrameFragment &DF);
975 /// finishLayout - Finalize a layout, including fragment lowering.
976 void finishLayout(MCAsmLayout &Layout);
978 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
979 MCFragment &F, const MCFixup &Fixup);
982 void addLocalUsedInReloc(const MCSymbol &Sym);
983 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
985 /// Compute the effective fragment size assuming it is laid out at the given
986 /// \p SectionAddress and \p FragmentOffset.
987 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
988 const MCFragment &F) const;
990 /// Find the symbol which defines the atom containing the given symbol, or
991 /// null if there is no such symbol.
992 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
994 /// Check whether a particular symbol is visible to the linker and is required
995 /// in the symbol table, or whether it can be discarded by the assembler. This
996 /// also effects whether the assembler treats the label as potentially
997 /// defining a separate atom.
998 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
1000 /// Emit the section contents using the given object writer.
1001 void writeSectionData(const MCSectionData *Section,
1002 const MCAsmLayout &Layout) const;
1004 /// Check whether a given symbol has been flagged with .thumb_func.
1005 bool isThumbFunc(const MCSymbol *Func) const;
1007 /// Flag a function symbol as the target of a .thumb_func directive.
1008 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1010 /// ELF e_header flags
1011 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1012 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1014 /// MachO deployment target version information.
1015 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1016 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1018 VersionMinInfo.Kind = Kind;
1019 VersionMinInfo.Major = Major;
1020 VersionMinInfo.Minor = Minor;
1021 VersionMinInfo.Update = Update;
1025 /// Construct a new assembler instance.
1027 /// \param OS The stream to output to.
1029 // FIXME: How are we going to parameterize this? Two obvious options are stay
1030 // concrete and require clients to pass in a target like object. The other
1031 // option is to make this abstract, and have targets provide concrete
1032 // implementations as we do with AsmParser.
1033 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1034 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1038 /// Reuse an assembler instance
1042 MCContext &getContext() const { return Context; }
1044 MCAsmBackend &getBackend() const { return Backend; }
1046 MCCodeEmitter &getEmitter() const { return Emitter; }
1048 MCObjectWriter &getWriter() const { return Writer; }
1050 /// Finish - Do final processing and write the object to the output stream.
1051 /// \p Writer is used for custom object writer (as the MCJIT does),
1052 /// if not specified it is automatically created from backend.
1055 // FIXME: This does not belong here.
1056 bool getSubsectionsViaSymbols() const {
1057 return SubsectionsViaSymbols;
1059 void setSubsectionsViaSymbols(bool Value) {
1060 SubsectionsViaSymbols = Value;
1063 bool getRelaxAll() const { return RelaxAll; }
1064 void setRelaxAll(bool Value) { RelaxAll = Value; }
1066 bool isBundlingEnabled() const {
1067 return BundleAlignSize != 0;
1070 unsigned getBundleAlignSize() const {
1071 return BundleAlignSize;
1074 void setBundleAlignSize(unsigned Size) {
1075 assert((Size == 0 || !(Size & (Size - 1))) &&
1076 "Expect a power-of-two bundle align size");
1077 BundleAlignSize = Size;
1080 /// @name Section List Access
1083 const SectionDataListType &getSectionList() const { return Sections; }
1084 SectionDataListType &getSectionList() { return Sections; }
1086 iterator begin() { return Sections.begin(); }
1087 const_iterator begin() const { return Sections.begin(); }
1089 iterator end() { return Sections.end(); }
1090 const_iterator end() const { return Sections.end(); }
1092 size_t size() const { return Sections.size(); }
1095 /// @name Symbol List Access
1098 const SymbolDataListType &getSymbolList() const { return Symbols; }
1099 SymbolDataListType &getSymbolList() { return Symbols; }
1101 symbol_iterator symbol_begin() { return Symbols.begin(); }
1102 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1104 symbol_iterator symbol_end() { return Symbols.end(); }
1105 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1107 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1108 const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1110 size_t symbol_size() const { return Symbols.size(); }
1113 /// @name Indirect Symbol List Access
1116 // FIXME: This is a total hack, this should not be here. Once things are
1117 // factored so that the streamer has direct access to the .o writer, it can
1119 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1120 return IndirectSymbols;
1123 indirect_symbol_iterator indirect_symbol_begin() {
1124 return IndirectSymbols.begin();
1126 const_indirect_symbol_iterator indirect_symbol_begin() const {
1127 return IndirectSymbols.begin();
1130 indirect_symbol_iterator indirect_symbol_end() {
1131 return IndirectSymbols.end();
1133 const_indirect_symbol_iterator indirect_symbol_end() const {
1134 return IndirectSymbols.end();
1137 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1140 /// @name Linker Option List Access
1143 std::vector<std::vector<std::string> > &getLinkerOptions() {
1144 return LinkerOptions;
1148 /// @name Data Region List Access
1151 // FIXME: This is a total hack, this should not be here. Once things are
1152 // factored so that the streamer has direct access to the .o writer, it can
1154 std::vector<DataRegionData> &getDataRegions() {
1158 data_region_iterator data_region_begin() {
1159 return DataRegions.begin();
1161 const_data_region_iterator data_region_begin() const {
1162 return DataRegions.begin();
1165 data_region_iterator data_region_end() {
1166 return DataRegions.end();
1168 const_data_region_iterator data_region_end() const {
1169 return DataRegions.end();
1172 size_t data_region_size() const { return DataRegions.size(); }
1175 /// @name Data Region List Access
1178 // FIXME: This is a total hack, this should not be here. Once things are
1179 // factored so that the streamer has direct access to the .o writer, it can
1181 MCLOHContainer & getLOHContainer() {
1182 return LOHContainer;
1184 const MCLOHContainer & getLOHContainer() const {
1185 return const_cast<MCAssembler *>(this)->getLOHContainer();
1188 /// @name Backend Data Access
1191 MCSectionData &getSectionData(const MCSection &Section) const {
1192 MCSectionData *Entry = SectionMap.lookup(&Section);
1193 assert(Entry && "Missing section data!");
1197 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1198 bool *Created = nullptr) {
1199 MCSectionData *&Entry = SectionMap[&Section];
1201 if (Created) *Created = !Entry;
1203 Entry = new MCSectionData(Section, this);
1208 bool hasSymbolData(const MCSymbol &Symbol) const {
1209 return SymbolMap.lookup(&Symbol) != nullptr;
1212 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1213 return const_cast<MCSymbolData &>(
1214 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1217 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1218 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1219 assert(Entry && "Missing symbol data!");
1223 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1224 bool *Created = nullptr) {
1225 MCSymbolData *&Entry = SymbolMap[&Symbol];
1227 if (Created) *Created = !Entry;
1229 Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1234 const_file_name_iterator file_names_begin() const {
1235 return FileNames.begin();
1238 const_file_name_iterator file_names_end() const {
1239 return FileNames.end();
1242 void addFileName(StringRef FileName) {
1243 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1245 FileNames.push_back(FileName);
1248 /// \brief Write the necessary bundle padding to the given object writer.
1249 /// Expects a fragment \p F containing instructions and its size \p FSize.
1250 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
1251 MCObjectWriter *OW) const;
1258 /// \brief Compute the amount of padding required before the fragment \p F to
1259 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
1260 /// its section and \p FSize is the fragment's size.
1261 uint64_t computeBundlePadding(const MCAssembler &Assembler,
1262 const MCFragment *F,
1263 uint64_t FOffset, uint64_t FSize);
1265 } // end namespace llvm