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/MCFixup.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/DataTypes.h"
24 #include <vector> // FIXME: Shouldn't be needed.
37 class MCSubtargetInfo;
43 class MCFragment : public ilist_node<MCFragment> {
44 friend class MCAsmLayout;
46 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
47 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
53 FT_CompactEncodedInst,
65 /// Parent - The data for the section this fragment is in.
66 MCSectionData *Parent;
68 /// Atom - The atom this fragment is in, as represented by it's defining
69 /// symbol. Atom's are only used by backends which set
70 /// \see MCAsmBackend::hasReliableSymbolDifference().
73 /// @name Assembler Backend Data
76 // FIXME: This could all be kept private to the assembler implementation.
78 /// Offset - The offset of this fragment in its section. This is ~0 until
82 /// LayoutOrder - The layout order of this fragment.
88 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
93 virtual ~MCFragment();
95 FragmentType getKind() const { return Kind; }
97 MCSectionData *getParent() const { return Parent; }
98 void setParent(MCSectionData *Value) { Parent = Value; }
100 MCSymbolData *getAtom() const { return Atom; }
101 void setAtom(MCSymbolData *Value) { Atom = Value; }
103 unsigned getLayoutOrder() const { return LayoutOrder; }
104 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
106 /// \brief Does this fragment have instructions emitted into it? By default
107 /// this is false, but specific fragment types may set it to true.
108 virtual bool hasInstructions() const { return false; }
110 /// \brief Should this fragment be placed at the end of an aligned bundle?
111 virtual bool alignToBundleEnd() const { return false; }
112 virtual void setAlignToBundleEnd(bool V) { }
114 /// \brief Get the padding size that must be inserted before this fragment.
115 /// Used for bundling. By default, no padding is inserted.
116 /// Note that padding size is restricted to 8 bits. This is an optimization
117 /// to reduce the amount of space used for each fragment. In practice, larger
118 /// padding should never be required.
119 virtual uint8_t getBundlePadding() const {
123 /// \brief Set the padding size for this fragment. By default it's a no-op,
124 /// and only some fragments have a meaningful implementation.
125 virtual void setBundlePadding(uint8_t N) {
131 /// Interface implemented by fragments that contain encoded instructions and/or
134 class MCEncodedFragment : public MCFragment {
135 virtual void anchor();
137 uint8_t BundlePadding;
139 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
140 : MCFragment(FType, SD), BundlePadding(0)
143 virtual ~MCEncodedFragment();
145 virtual SmallVectorImpl<char> &getContents() = 0;
146 virtual const SmallVectorImpl<char> &getContents() const = 0;
148 uint8_t getBundlePadding() const override {
149 return BundlePadding;
152 void setBundlePadding(uint8_t N) override {
156 static bool classof(const MCFragment *F) {
157 MCFragment::FragmentType Kind = F->getKind();
161 case MCFragment::FT_Relaxable:
162 case MCFragment::FT_CompactEncodedInst:
163 case MCFragment::FT_Data:
169 /// Interface implemented by fragments that contain encoded instructions and/or
170 /// data and also have fixups registered.
172 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
173 void anchor() override;
176 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
177 MCSectionData *SD = 0)
178 : MCEncodedFragment(FType, SD)
182 virtual ~MCEncodedFragmentWithFixups();
184 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
185 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
187 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
188 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
190 virtual fixup_iterator fixup_begin() = 0;
191 virtual const_fixup_iterator fixup_begin() const = 0;
192 virtual fixup_iterator fixup_end() = 0;
193 virtual const_fixup_iterator fixup_end() const = 0;
195 static bool classof(const MCFragment *F) {
196 MCFragment::FragmentType Kind = F->getKind();
197 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
201 /// Fragment for data and encoded instructions.
203 class MCDataFragment : public MCEncodedFragmentWithFixups {
204 void anchor() override;
206 /// \brief Does this fragment contain encoded instructions anywhere in it?
207 bool HasInstructions;
209 /// \brief Should this fragment be aligned to the end of a bundle?
210 bool AlignToBundleEnd;
212 SmallVector<char, 32> Contents;
214 /// Fixups - The list of fixups in this fragment.
215 SmallVector<MCFixup, 4> Fixups;
217 MCDataFragment(MCSectionData *SD = 0)
218 : MCEncodedFragmentWithFixups(FT_Data, SD),
219 HasInstructions(false), AlignToBundleEnd(false)
223 SmallVectorImpl<char> &getContents() override { return Contents; }
224 const SmallVectorImpl<char> &getContents() const override {
228 SmallVectorImpl<MCFixup> &getFixups() override {
232 const SmallVectorImpl<MCFixup> &getFixups() const override {
236 bool hasInstructions() const override { return HasInstructions; }
237 virtual void setHasInstructions(bool V) { HasInstructions = V; }
239 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
240 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
242 fixup_iterator fixup_begin() override { return Fixups.begin(); }
243 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
245 fixup_iterator fixup_end() override {return Fixups.end();}
246 const_fixup_iterator fixup_end() const override {return Fixups.end();}
248 static bool classof(const MCFragment *F) {
249 return F->getKind() == MCFragment::FT_Data;
253 /// This is a compact (memory-size-wise) fragment for holding an encoded
254 /// instruction (non-relaxable) that has no fixups registered. When applicable,
255 /// it can be used instead of MCDataFragment and lead to lower memory
258 class MCCompactEncodedInstFragment : public MCEncodedFragment {
259 void anchor() override;
261 /// \brief Should this fragment be aligned to the end of a bundle?
262 bool AlignToBundleEnd;
264 SmallVector<char, 4> Contents;
266 MCCompactEncodedInstFragment(MCSectionData *SD = 0)
267 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
271 bool hasInstructions() const override {
275 SmallVectorImpl<char> &getContents() override { return Contents; }
276 const SmallVectorImpl<char> &getContents() const override { return Contents; }
278 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
279 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
281 static bool classof(const MCFragment *F) {
282 return F->getKind() == MCFragment::FT_CompactEncodedInst;
286 /// A relaxable fragment holds on to its MCInst, since it may need to be
287 /// relaxed during the assembler layout and relaxation stage.
289 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
290 void anchor() override;
292 /// Inst - The instruction this is a fragment for.
295 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
296 /// Keep a copy instead of a reference to make sure that updates to STI
297 /// in the assembler are not seen here.
298 const MCSubtargetInfo STI;
300 /// Contents - Binary data for the currently encoded instruction.
301 SmallVector<char, 8> Contents;
303 /// Fixups - The list of fixups in this fragment.
304 SmallVector<MCFixup, 1> Fixups;
307 MCRelaxableFragment(const MCInst &_Inst,
308 const MCSubtargetInfo &_STI,
309 MCSectionData *SD = 0)
310 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
313 SmallVectorImpl<char> &getContents() override { return Contents; }
314 const SmallVectorImpl<char> &getContents() const override { return Contents; }
316 const MCInst &getInst() const { return Inst; }
317 void setInst(const MCInst& Value) { Inst = Value; }
319 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
321 SmallVectorImpl<MCFixup> &getFixups() override {
325 const SmallVectorImpl<MCFixup> &getFixups() const override {
329 bool hasInstructions() const override { return true; }
331 fixup_iterator fixup_begin() override { return Fixups.begin(); }
332 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
334 fixup_iterator fixup_end() override {return Fixups.end();}
335 const_fixup_iterator fixup_end() const override {return Fixups.end();}
337 static bool classof(const MCFragment *F) {
338 return F->getKind() == MCFragment::FT_Relaxable;
342 class MCAlignFragment : public MCFragment {
343 virtual void anchor();
345 /// Alignment - The alignment to ensure, in bytes.
348 /// Value - Value to use for filling padding bytes.
351 /// ValueSize - The size of the integer (in bytes) of \p Value.
354 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
355 /// cannot be satisfied in this width then this fragment is ignored.
356 unsigned MaxBytesToEmit;
358 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
359 /// of using the provided value. The exact interpretation of this flag is
360 /// target dependent.
364 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
365 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
366 : MCFragment(FT_Align, SD), Alignment(_Alignment),
367 Value(_Value),ValueSize(_ValueSize),
368 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 = 0)
407 : MCFragment(FT_Fill, SD),
408 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
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 = 0)
440 : MCFragment(FT_Org, SD),
441 Offset(&_Offset), Value(_Value) {}
446 const MCExpr &getOffset() const { return *Offset; }
448 uint8_t getValue() const { return Value; }
452 static bool classof(const MCFragment *F) {
453 return F->getKind() == MCFragment::FT_Org;
457 class MCLEBFragment : public MCFragment {
458 virtual void anchor();
460 /// Value - The value this fragment should contain.
463 /// IsSigned - True if this is a sleb128, false if uleb128.
466 SmallString<8> Contents;
468 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD = 0)
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 = 0)
505 : MCFragment(FT_Dwarf, SD),
506 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
511 int64_t getLineDelta() const { return LineDelta; }
513 const MCExpr &getAddrDelta() const { return *AddrDelta; }
515 SmallString<8> &getContents() { return Contents; }
516 const SmallString<8> &getContents() const { return Contents; }
520 static bool classof(const MCFragment *F) {
521 return F->getKind() == MCFragment::FT_Dwarf;
525 class MCDwarfCallFrameFragment : public MCFragment {
526 virtual void anchor();
528 /// AddrDelta - The expression for the difference of the two symbols that
529 /// make up the address delta between two .cfi_* dwarf directives.
530 const MCExpr *AddrDelta;
532 SmallString<8> Contents;
535 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
536 : MCFragment(FT_DwarfFrame, SD),
537 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
542 const MCExpr &getAddrDelta() const { return *AddrDelta; }
544 SmallString<8> &getContents() { return Contents; }
545 const SmallString<8> &getContents() const { return Contents; }
549 static bool classof(const MCFragment *F) {
550 return F->getKind() == MCFragment::FT_DwarfFrame;
554 // FIXME: Should this be a separate class, or just merged into MCSection? Since
555 // we anticipate the fast path being through an MCAssembler, the only reason to
556 // keep it out is for API abstraction.
557 class MCSectionData : public ilist_node<MCSectionData> {
558 friend class MCAsmLayout;
560 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
561 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
564 typedef iplist<MCFragment> FragmentListType;
566 typedef FragmentListType::const_iterator const_iterator;
567 typedef FragmentListType::iterator iterator;
569 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
570 typedef FragmentListType::reverse_iterator reverse_iterator;
572 /// \brief Express the state of bundle locked groups while emitting code.
573 enum BundleLockStateType {
576 BundleLockedAlignToEnd
579 FragmentListType Fragments;
580 const MCSection *Section;
582 /// Ordinal - The section index in the assemblers section list.
585 /// LayoutOrder - The index of this section in the layout order.
586 unsigned LayoutOrder;
588 /// Alignment - The maximum alignment seen in this section.
591 /// \brief Keeping track of bundle-locked state.
592 BundleLockStateType BundleLockState;
594 /// \brief We've seen a bundle_lock directive but not its first instruction
596 bool BundleGroupBeforeFirstInst;
598 /// @name Assembler Backend Data
601 // FIXME: This could all be kept private to the assembler implementation.
603 /// HasInstructions - Whether this section has had instructions emitted into
605 unsigned HasInstructions : 1;
607 /// Mapping from subsection number to insertion point for subsection numbers
608 /// below that number.
609 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
614 // Only for use as sentinel.
616 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
618 const MCSection &getSection() const { return *Section; }
620 unsigned getAlignment() const { return Alignment; }
621 void setAlignment(unsigned Value) { Alignment = Value; }
623 bool hasInstructions() const { return HasInstructions; }
624 void setHasInstructions(bool Value) { HasInstructions = Value; }
626 unsigned getOrdinal() const { return Ordinal; }
627 void setOrdinal(unsigned Value) { Ordinal = Value; }
629 unsigned getLayoutOrder() const { return LayoutOrder; }
630 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
632 /// @name Fragment Access
635 const FragmentListType &getFragmentList() const { return Fragments; }
636 FragmentListType &getFragmentList() { return Fragments; }
638 iterator begin() { return Fragments.begin(); }
639 const_iterator begin() const { return Fragments.begin(); }
641 iterator end() { return Fragments.end(); }
642 const_iterator end() const { return Fragments.end(); }
644 reverse_iterator rbegin() { return Fragments.rbegin(); }
645 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
647 reverse_iterator rend() { return Fragments.rend(); }
648 const_reverse_iterator rend() const { return Fragments.rend(); }
650 size_t size() const { return Fragments.size(); }
652 bool empty() const { return Fragments.empty(); }
654 iterator getSubsectionInsertionPoint(unsigned Subsection);
656 bool isBundleLocked() const {
657 return BundleLockState != NotBundleLocked;
660 BundleLockStateType getBundleLockState() const {
661 return BundleLockState;
664 void setBundleLockState(BundleLockStateType NewState) {
665 BundleLockState = NewState;
668 bool isBundleGroupBeforeFirstInst() const {
669 return BundleGroupBeforeFirstInst;
672 void setBundleGroupBeforeFirstInst(bool IsFirst) {
673 BundleGroupBeforeFirstInst = IsFirst;
681 // FIXME: Same concerns as with SectionData.
682 class MCSymbolData : public ilist_node<MCSymbolData> {
684 const MCSymbol *Symbol;
686 /// Fragment - The fragment this symbol's value is relative to, if any.
687 MCFragment *Fragment;
689 /// Offset - The offset to apply to the fragment address to form this symbol's
693 /// IsExternal - True if this symbol is visible outside this translation
695 unsigned IsExternal : 1;
697 /// IsPrivateExtern - True if this symbol is private extern.
698 unsigned IsPrivateExtern : 1;
700 /// CommonSize - The size of the symbol, if it is 'common', or 0.
702 // FIXME: Pack this in with other fields? We could put it in offset, since a
703 // common symbol can never get a definition.
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'.
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,
731 const MCSymbol &getSymbol() const { return *Symbol; }
733 MCFragment *getFragment() const { return Fragment; }
734 void setFragment(MCFragment *Value) { Fragment = Value; }
736 uint64_t getOffset() const { return Offset; }
737 void setOffset(uint64_t Value) { Offset = Value; }
740 /// @name Symbol Attributes
743 bool isExternal() const { return IsExternal; }
744 void setExternal(bool Value) { IsExternal = Value; }
746 bool isPrivateExtern() const { return IsPrivateExtern; }
747 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
749 /// isCommon - Is this a 'common' symbol.
750 bool isCommon() const { return CommonSize != 0; }
752 /// setCommon - Mark this symbol as being 'common'.
754 /// \param Size - The size of the symbol.
755 /// \param Align - The alignment of the symbol.
756 void setCommon(uint64_t Size, unsigned Align) {
761 /// getCommonSize - Return the size of a 'common' symbol.
762 uint64_t getCommonSize() const {
763 assert(isCommon() && "Not a 'common' symbol!");
767 void setSize(const MCExpr *SS) {
771 const MCExpr *getSize() const {
776 /// getCommonAlignment - Return the alignment of a 'common' symbol.
777 unsigned getCommonAlignment() const {
778 assert(isCommon() && "Not a 'common' symbol!");
782 /// getFlags - Get the (implementation defined) symbol flags.
783 uint32_t getFlags() const { return Flags; }
785 /// setFlags - Set the (implementation defined) symbol flags.
786 void setFlags(uint32_t Value) { Flags = Value; }
788 /// modifyFlags - Modify the flags via a mask
789 void modifyFlags(uint32_t Value, uint32_t Mask) {
790 Flags = (Flags & ~Mask) | Value;
793 /// getIndex - Get the (implementation defined) index.
794 uint64_t getIndex() const { return Index; }
796 /// setIndex - Set the (implementation defined) index.
797 void setIndex(uint64_t Value) { Index = Value; }
804 // FIXME: This really doesn't belong here. See comments below.
805 struct IndirectSymbolData {
807 MCSectionData *SectionData;
810 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
812 struct DataRegionData {
813 // This enum should be kept in sync w/ the mach-o definition in
814 // llvm/Object/MachOFormat.h.
815 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
821 friend class MCAsmLayout;
824 typedef iplist<MCSectionData> SectionDataListType;
825 typedef iplist<MCSymbolData> SymbolDataListType;
827 typedef SectionDataListType::const_iterator const_iterator;
828 typedef SectionDataListType::iterator iterator;
830 typedef SymbolDataListType::const_iterator const_symbol_iterator;
831 typedef SymbolDataListType::iterator symbol_iterator;
833 typedef std::vector<std::string> FileNameVectorType;
834 typedef FileNameVectorType::const_iterator const_file_name_iterator;
836 typedef std::vector<IndirectSymbolData>::const_iterator
837 const_indirect_symbol_iterator;
838 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
840 typedef std::vector<DataRegionData>::const_iterator
841 const_data_region_iterator;
842 typedef std::vector<DataRegionData>::iterator data_region_iterator;
845 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
846 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
850 MCAsmBackend &Backend;
852 MCCodeEmitter &Emitter;
854 MCObjectWriter &Writer;
858 iplist<MCSectionData> Sections;
860 iplist<MCSymbolData> Symbols;
862 /// The map of sections to their associated assembler backend data.
864 // FIXME: Avoid this indirection?
865 DenseMap<const MCSection*, MCSectionData*> SectionMap;
867 /// The map of symbols to their associated assembler backend data.
869 // FIXME: Avoid this indirection?
870 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
872 std::vector<IndirectSymbolData> IndirectSymbols;
874 std::vector<DataRegionData> DataRegions;
876 /// The list of linker options to propagate into the object file.
877 std::vector<std::vector<std::string> > LinkerOptions;
879 /// List of declared file names
880 FileNameVectorType FileNames;
882 /// The set of function symbols for which a .thumb_func directive has
885 // FIXME: We really would like this in target specific code rather than
886 // here. Maybe when the relocation stuff moves to target specific,
887 // this can go with it? The streamer would need some target specific
889 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
891 /// \brief The bundle alignment size currently set in the assembler.
893 /// By default it's 0, which means bundling is disabled.
894 unsigned BundleAlignSize;
896 unsigned RelaxAll : 1;
897 unsigned NoExecStack : 1;
898 unsigned SubsectionsViaSymbols : 1;
900 /// ELF specific e_header flags
901 // It would be good if there were an MCELFAssembler class to hold this.
902 // ELF header flags are used both by the integrated and standalone assemblers.
903 // Access to the flags is necessary in cases where assembler directives affect
904 // which flags to be set.
905 unsigned ELFHeaderEFlags;
907 /// Evaluate a fixup to a relocatable expression and the value which should be
908 /// placed into the fixup.
910 /// \param Layout The layout to use for evaluation.
911 /// \param Fixup The fixup to evaluate.
912 /// \param DF The fragment the fixup is inside.
913 /// \param Target [out] On return, the relocatable expression the fixup
915 /// \param Value [out] On return, the value of the fixup as currently laid
917 /// \return Whether the fixup value was fully resolved. This is true if the
918 /// \p Value result is fixed, otherwise the value may change due to
920 bool evaluateFixup(const MCAsmLayout &Layout,
921 const MCFixup &Fixup, const MCFragment *DF,
922 MCValue &Target, uint64_t &Value) const;
924 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
925 /// (increased in size, in order to hold its value correctly).
926 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
927 const MCAsmLayout &Layout) const;
929 /// Check whether the given fragment needs relaxation.
930 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
931 const MCAsmLayout &Layout) const;
933 /// \brief Perform one layout iteration and return true if any offsets
935 bool layoutOnce(MCAsmLayout &Layout);
937 /// \brief Perform one layout iteration of the given section and return true
938 /// if any offsets were adjusted.
939 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
941 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
943 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
945 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
946 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
947 MCDwarfCallFrameFragment &DF);
949 /// finishLayout - Finalize a layout, including fragment lowering.
950 void finishLayout(MCAsmLayout &Layout);
952 uint64_t handleFixup(const MCAsmLayout &Layout,
953 MCFragment &F, const MCFixup &Fixup);
956 /// Compute the effective fragment size assuming it is laid out at the given
957 /// \p SectionAddress and \p FragmentOffset.
958 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
959 const MCFragment &F) const;
961 /// Find the symbol which defines the atom containing the given symbol, or
962 /// null if there is no such symbol.
963 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
965 /// Check whether a particular symbol is visible to the linker and is required
966 /// in the symbol table, or whether it can be discarded by the assembler. This
967 /// also effects whether the assembler treats the label as potentially
968 /// defining a separate atom.
969 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
971 /// Emit the section contents using the given object writer.
972 void writeSectionData(const MCSectionData *Section,
973 const MCAsmLayout &Layout) const;
975 /// Check whether a given symbol has been flagged with .thumb_func.
976 bool isThumbFunc(const MCSymbol *Func) const {
977 return ThumbFuncs.count(Func);
980 /// Flag a function symbol as the target of a .thumb_func directive.
981 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
983 /// ELF e_header flags
984 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
985 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
988 /// Construct a new assembler instance.
990 /// \param OS The stream to output to.
992 // FIXME: How are we going to parameterize this? Two obvious options are stay
993 // concrete and require clients to pass in a target like object. The other
994 // option is to make this abstract, and have targets provide concrete
995 // implementations as we do with AsmParser.
996 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
997 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1001 /// Reuse an assembler instance
1005 MCContext &getContext() const { return Context; }
1007 MCAsmBackend &getBackend() const { return Backend; }
1009 MCCodeEmitter &getEmitter() const { return Emitter; }
1011 MCObjectWriter &getWriter() const { return Writer; }
1013 /// Finish - Do final processing and write the object to the output stream.
1014 /// \p Writer is used for custom object writer (as the MCJIT does),
1015 /// if not specified it is automatically created from backend.
1018 // FIXME: This does not belong here.
1019 bool getSubsectionsViaSymbols() const {
1020 return SubsectionsViaSymbols;
1022 void setSubsectionsViaSymbols(bool Value) {
1023 SubsectionsViaSymbols = Value;
1026 bool getRelaxAll() const { return RelaxAll; }
1027 void setRelaxAll(bool Value) { RelaxAll = Value; }
1029 bool getNoExecStack() const { return NoExecStack; }
1030 void setNoExecStack(bool Value) { NoExecStack = Value; }
1032 bool isBundlingEnabled() const {
1033 return BundleAlignSize != 0;
1036 unsigned getBundleAlignSize() const {
1037 return BundleAlignSize;
1040 void setBundleAlignSize(unsigned Size) {
1041 assert((Size == 0 || !(Size & (Size - 1))) &&
1042 "Expect a power-of-two bundle align size");
1043 BundleAlignSize = Size;
1046 /// @name Section List Access
1049 const SectionDataListType &getSectionList() const { return Sections; }
1050 SectionDataListType &getSectionList() { return Sections; }
1052 iterator begin() { return Sections.begin(); }
1053 const_iterator begin() const { return Sections.begin(); }
1055 iterator end() { return Sections.end(); }
1056 const_iterator end() const { return Sections.end(); }
1058 size_t size() const { return Sections.size(); }
1061 /// @name Symbol List Access
1064 const SymbolDataListType &getSymbolList() const { return Symbols; }
1065 SymbolDataListType &getSymbolList() { return Symbols; }
1067 symbol_iterator symbol_begin() { return Symbols.begin(); }
1068 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1070 symbol_iterator symbol_end() { return Symbols.end(); }
1071 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1073 size_t symbol_size() const { return Symbols.size(); }
1076 /// @name Indirect Symbol List Access
1079 // FIXME: This is a total hack, this should not be here. Once things are
1080 // factored so that the streamer has direct access to the .o writer, it can
1082 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1083 return IndirectSymbols;
1086 indirect_symbol_iterator indirect_symbol_begin() {
1087 return IndirectSymbols.begin();
1089 const_indirect_symbol_iterator indirect_symbol_begin() const {
1090 return IndirectSymbols.begin();
1093 indirect_symbol_iterator indirect_symbol_end() {
1094 return IndirectSymbols.end();
1096 const_indirect_symbol_iterator indirect_symbol_end() const {
1097 return IndirectSymbols.end();
1100 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1103 /// @name Linker Option List Access
1106 std::vector<std::vector<std::string> > &getLinkerOptions() {
1107 return LinkerOptions;
1111 /// @name Data Region List Access
1114 // FIXME: This is a total hack, this should not be here. Once things are
1115 // factored so that the streamer has direct access to the .o writer, it can
1117 std::vector<DataRegionData> &getDataRegions() {
1121 data_region_iterator data_region_begin() {
1122 return DataRegions.begin();
1124 const_data_region_iterator data_region_begin() const {
1125 return DataRegions.begin();
1128 data_region_iterator data_region_end() {
1129 return DataRegions.end();
1131 const_data_region_iterator data_region_end() const {
1132 return DataRegions.end();
1135 size_t data_region_size() const { return DataRegions.size(); }
1138 /// @name Backend Data Access
1141 MCSectionData &getSectionData(const MCSection &Section) const {
1142 MCSectionData *Entry = SectionMap.lookup(&Section);
1143 assert(Entry && "Missing section data!");
1147 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1148 bool *Created = 0) {
1149 MCSectionData *&Entry = SectionMap[&Section];
1151 if (Created) *Created = !Entry;
1153 Entry = new MCSectionData(Section, this);
1158 bool hasSymbolData(const MCSymbol &Symbol) const {
1159 return SymbolMap.lookup(&Symbol) != 0;
1162 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1163 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1164 assert(Entry && "Missing symbol data!");
1168 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1169 bool *Created = 0) {
1170 MCSymbolData *&Entry = SymbolMap[&Symbol];
1172 if (Created) *Created = !Entry;
1174 Entry = new MCSymbolData(Symbol, 0, 0, this);
1179 const_file_name_iterator file_names_begin() const {
1180 return FileNames.begin();
1183 const_file_name_iterator file_names_end() const {
1184 return FileNames.end();
1187 void addFileName(StringRef FileName) {
1188 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1190 FileNames.push_back(FileName);
1198 } // end namespace llvm