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
72 /// @name Assembler Backend Data
75 // FIXME: This could all be kept private to the assembler implementation.
77 /// Offset - The offset of this fragment in its section. This is ~0 until
81 /// LayoutOrder - The layout order of this fragment.
87 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
92 virtual ~MCFragment();
94 FragmentType getKind() const { return Kind; }
96 MCSectionData *getParent() const { return Parent; }
97 void setParent(MCSectionData *Value) { Parent = Value; }
99 MCSymbolData *getAtom() const { return Atom; }
100 void setAtom(MCSymbolData *Value) { Atom = Value; }
102 unsigned getLayoutOrder() const { return LayoutOrder; }
103 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
105 /// \brief Does this fragment have instructions emitted into it? By default
106 /// this is false, but specific fragment types may set it to true.
107 virtual bool hasInstructions() const { return false; }
109 /// \brief Should this fragment be placed at the end of an aligned bundle?
110 virtual bool alignToBundleEnd() const { return false; }
111 virtual void setAlignToBundleEnd(bool V) { }
113 /// \brief Get the padding size that must be inserted before this fragment.
114 /// Used for bundling. By default, no padding is inserted.
115 /// Note that padding size is restricted to 8 bits. This is an optimization
116 /// to reduce the amount of space used for each fragment. In practice, larger
117 /// padding should never be required.
118 virtual uint8_t getBundlePadding() const {
122 /// \brief Set the padding size for this fragment. By default it's a no-op,
123 /// and only some fragments have a meaningful implementation.
124 virtual void setBundlePadding(uint8_t N) {
130 /// Interface implemented by fragments that contain encoded instructions and/or
133 class MCEncodedFragment : public MCFragment {
134 virtual void anchor();
136 uint8_t BundlePadding;
138 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
139 : MCFragment(FType, SD), BundlePadding(0)
142 virtual ~MCEncodedFragment();
144 virtual SmallVectorImpl<char> &getContents() = 0;
145 virtual const SmallVectorImpl<char> &getContents() const = 0;
147 uint8_t getBundlePadding() const override {
148 return BundlePadding;
151 void setBundlePadding(uint8_t N) override {
155 static bool classof(const MCFragment *F) {
156 MCFragment::FragmentType Kind = F->getKind();
160 case MCFragment::FT_Relaxable:
161 case MCFragment::FT_CompactEncodedInst:
162 case MCFragment::FT_Data:
168 /// Interface implemented by fragments that contain encoded instructions and/or
169 /// data and also have fixups registered.
171 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
172 void anchor() override;
175 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
176 MCSectionData *SD = 0)
177 : MCEncodedFragment(FType, SD)
181 virtual ~MCEncodedFragmentWithFixups();
183 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
184 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
186 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
187 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
189 virtual fixup_iterator fixup_begin() = 0;
190 virtual const_fixup_iterator fixup_begin() const = 0;
191 virtual fixup_iterator fixup_end() = 0;
192 virtual const_fixup_iterator fixup_end() const = 0;
194 static bool classof(const MCFragment *F) {
195 MCFragment::FragmentType Kind = F->getKind();
196 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
200 /// Fragment for data and encoded instructions.
202 class MCDataFragment : public MCEncodedFragmentWithFixups {
203 void anchor() override;
205 /// \brief Does this fragment contain encoded instructions anywhere in it?
206 bool HasInstructions;
208 /// \brief Should this fragment be aligned to the end of a bundle?
209 bool AlignToBundleEnd;
211 SmallVector<char, 32> Contents;
213 /// Fixups - The list of fixups in this fragment.
214 SmallVector<MCFixup, 4> Fixups;
216 MCDataFragment(MCSectionData *SD = 0)
217 : MCEncodedFragmentWithFixups(FT_Data, SD),
218 HasInstructions(false), AlignToBundleEnd(false)
222 SmallVectorImpl<char> &getContents() override { return Contents; }
223 const SmallVectorImpl<char> &getContents() const override {
227 SmallVectorImpl<MCFixup> &getFixups() override {
231 const SmallVectorImpl<MCFixup> &getFixups() const override {
235 bool hasInstructions() const override { return HasInstructions; }
236 virtual void setHasInstructions(bool V) { HasInstructions = V; }
238 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
239 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
241 fixup_iterator fixup_begin() override { return Fixups.begin(); }
242 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
244 fixup_iterator fixup_end() override {return Fixups.end();}
245 const_fixup_iterator fixup_end() const override {return Fixups.end();}
247 static bool classof(const MCFragment *F) {
248 return F->getKind() == MCFragment::FT_Data;
252 /// This is a compact (memory-size-wise) fragment for holding an encoded
253 /// instruction (non-relaxable) that has no fixups registered. When applicable,
254 /// it can be used instead of MCDataFragment and lead to lower memory
257 class MCCompactEncodedInstFragment : public MCEncodedFragment {
258 void anchor() override;
260 /// \brief Should this fragment be aligned to the end of a bundle?
261 bool AlignToBundleEnd;
263 SmallVector<char, 4> Contents;
265 MCCompactEncodedInstFragment(MCSectionData *SD = 0)
266 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
270 bool hasInstructions() const override {
274 SmallVectorImpl<char> &getContents() override { return Contents; }
275 const SmallVectorImpl<char> &getContents() const override { return Contents; }
277 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
278 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
280 static bool classof(const MCFragment *F) {
281 return F->getKind() == MCFragment::FT_CompactEncodedInst;
285 /// A relaxable fragment holds on to its MCInst, since it may need to be
286 /// relaxed during the assembler layout and relaxation stage.
288 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
289 void anchor() override;
291 /// Inst - The instruction this is a fragment for.
294 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
295 /// Keep a copy instead of a reference to make sure that updates to STI
296 /// in the assembler are not seen here.
297 const MCSubtargetInfo STI;
299 /// Contents - Binary data for the currently encoded instruction.
300 SmallVector<char, 8> Contents;
302 /// Fixups - The list of fixups in this fragment.
303 SmallVector<MCFixup, 1> Fixups;
306 MCRelaxableFragment(const MCInst &_Inst,
307 const MCSubtargetInfo &_STI,
308 MCSectionData *SD = 0)
309 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
312 SmallVectorImpl<char> &getContents() override { return Contents; }
313 const SmallVectorImpl<char> &getContents() const override { return Contents; }
315 const MCInst &getInst() const { return Inst; }
316 void setInst(const MCInst& Value) { Inst = Value; }
318 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
320 SmallVectorImpl<MCFixup> &getFixups() override {
324 const SmallVectorImpl<MCFixup> &getFixups() const override {
328 bool hasInstructions() const override { return true; }
330 fixup_iterator fixup_begin() override { return Fixups.begin(); }
331 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
333 fixup_iterator fixup_end() override {return Fixups.end();}
334 const_fixup_iterator fixup_end() const override {return Fixups.end();}
336 static bool classof(const MCFragment *F) {
337 return F->getKind() == MCFragment::FT_Relaxable;
341 class MCAlignFragment : public MCFragment {
342 virtual void anchor();
344 /// Alignment - The alignment to ensure, in bytes.
347 /// Value - Value to use for filling padding bytes.
350 /// ValueSize - The size of the integer (in bytes) of \p Value.
353 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
354 /// cannot be satisfied in this width then this fragment is ignored.
355 unsigned MaxBytesToEmit;
357 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
358 /// of using the provided value. The exact interpretation of this flag is
359 /// target dependent.
363 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
364 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
365 : MCFragment(FT_Align, SD), Alignment(_Alignment),
366 Value(_Value),ValueSize(_ValueSize),
367 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
372 unsigned getAlignment() const { return Alignment; }
374 int64_t getValue() const { return Value; }
376 unsigned getValueSize() const { return ValueSize; }
378 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
380 bool hasEmitNops() const { return EmitNops; }
381 void setEmitNops(bool Value) { EmitNops = Value; }
385 static bool classof(const MCFragment *F) {
386 return F->getKind() == MCFragment::FT_Align;
390 class MCFillFragment : public MCFragment {
391 virtual void anchor();
393 /// Value - Value to use for filling bytes.
396 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
397 /// this is a virtual fill fragment.
400 /// Size - The number of bytes to insert.
404 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
405 MCSectionData *SD = 0)
406 : MCFragment(FT_Fill, SD),
407 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
408 assert((!ValueSize || (Size % ValueSize) == 0) &&
409 "Fill size must be a multiple of the value size!");
415 int64_t getValue() const { return Value; }
417 unsigned getValueSize() const { return ValueSize; }
419 uint64_t getSize() const { return Size; }
423 static bool classof(const MCFragment *F) {
424 return F->getKind() == MCFragment::FT_Fill;
428 class MCOrgFragment : public MCFragment {
429 virtual void anchor();
431 /// Offset - The offset this fragment should start at.
432 const MCExpr *Offset;
434 /// Value - Value to use for filling bytes.
438 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
439 : MCFragment(FT_Org, SD),
440 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_, MCSectionData *SD = 0)
468 : MCFragment(FT_LEB, SD),
469 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
474 const MCExpr &getValue() const { return *Value; }
476 bool isSigned() const { return IsSigned; }
478 SmallString<8> &getContents() { return Contents; }
479 const SmallString<8> &getContents() const { return Contents; }
483 static bool classof(const MCFragment *F) {
484 return F->getKind() == MCFragment::FT_LEB;
488 class MCDwarfLineAddrFragment : public MCFragment {
489 virtual void anchor();
491 /// LineDelta - the value of the difference between the two line numbers
492 /// between two .loc dwarf directives.
495 /// AddrDelta - The expression for the difference of the two symbols that
496 /// make up the address delta between two .loc dwarf directives.
497 const MCExpr *AddrDelta;
499 SmallString<8> Contents;
502 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
503 MCSectionData *SD = 0)
504 : MCFragment(FT_Dwarf, SD),
505 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
510 int64_t getLineDelta() const { return LineDelta; }
512 const MCExpr &getAddrDelta() const { return *AddrDelta; }
514 SmallString<8> &getContents() { return Contents; }
515 const SmallString<8> &getContents() const { return Contents; }
519 static bool classof(const MCFragment *F) {
520 return F->getKind() == MCFragment::FT_Dwarf;
524 class MCDwarfCallFrameFragment : public MCFragment {
525 virtual void anchor();
527 /// AddrDelta - The expression for the difference of the two symbols that
528 /// make up the address delta between two .cfi_* dwarf directives.
529 const MCExpr *AddrDelta;
531 SmallString<8> Contents;
534 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
535 : MCFragment(FT_DwarfFrame, SD),
536 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
541 const MCExpr &getAddrDelta() const { return *AddrDelta; }
543 SmallString<8> &getContents() { return Contents; }
544 const SmallString<8> &getContents() const { return Contents; }
548 static bool classof(const MCFragment *F) {
549 return F->getKind() == MCFragment::FT_DwarfFrame;
553 // FIXME: Should this be a separate class, or just merged into MCSection? Since
554 // we anticipate the fast path being through an MCAssembler, the only reason to
555 // keep it out is for API abstraction.
556 class MCSectionData : public ilist_node<MCSectionData> {
557 friend class MCAsmLayout;
559 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
560 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
563 typedef iplist<MCFragment> FragmentListType;
565 typedef FragmentListType::const_iterator const_iterator;
566 typedef FragmentListType::iterator iterator;
568 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
569 typedef FragmentListType::reverse_iterator reverse_iterator;
571 /// \brief Express the state of bundle locked groups while emitting code.
572 enum BundleLockStateType {
575 BundleLockedAlignToEnd
578 FragmentListType Fragments;
579 const MCSection *Section;
581 /// Ordinal - The section index in the assemblers section list.
584 /// LayoutOrder - The index of this section in the layout order.
585 unsigned LayoutOrder;
587 /// Alignment - The maximum alignment seen in this section.
590 /// \brief Keeping track of bundle-locked state.
591 BundleLockStateType BundleLockState;
593 /// \brief We've seen a bundle_lock directive but not its first instruction
595 bool BundleGroupBeforeFirstInst;
597 /// @name Assembler Backend Data
600 // FIXME: This could all be kept private to the assembler implementation.
602 /// HasInstructions - Whether this section has had instructions emitted into
604 unsigned HasInstructions : 1;
606 /// Mapping from subsection number to insertion point for subsection numbers
607 /// below that number.
608 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
613 // Only for use as sentinel.
615 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
617 const MCSection &getSection() const { return *Section; }
619 unsigned getAlignment() const { return Alignment; }
620 void setAlignment(unsigned Value) { Alignment = Value; }
622 bool hasInstructions() const { return HasInstructions; }
623 void setHasInstructions(bool Value) { HasInstructions = Value; }
625 unsigned getOrdinal() const { return Ordinal; }
626 void setOrdinal(unsigned Value) { Ordinal = Value; }
628 unsigned getLayoutOrder() const { return LayoutOrder; }
629 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
631 /// @name Fragment Access
634 const FragmentListType &getFragmentList() const { return Fragments; }
635 FragmentListType &getFragmentList() { return Fragments; }
637 iterator begin() { return Fragments.begin(); }
638 const_iterator begin() const { return Fragments.begin(); }
640 iterator end() { return Fragments.end(); }
641 const_iterator end() const { return Fragments.end(); }
643 reverse_iterator rbegin() { return Fragments.rbegin(); }
644 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
646 reverse_iterator rend() { return Fragments.rend(); }
647 const_reverse_iterator rend() const { return Fragments.rend(); }
649 size_t size() const { return Fragments.size(); }
651 bool empty() const { return Fragments.empty(); }
653 iterator getSubsectionInsertionPoint(unsigned Subsection);
655 bool isBundleLocked() const {
656 return BundleLockState != NotBundleLocked;
659 BundleLockStateType getBundleLockState() const {
660 return BundleLockState;
663 void setBundleLockState(BundleLockStateType NewState) {
664 BundleLockState = NewState;
667 bool isBundleGroupBeforeFirstInst() const {
668 return BundleGroupBeforeFirstInst;
671 void setBundleGroupBeforeFirstInst(bool IsFirst) {
672 BundleGroupBeforeFirstInst = IsFirst;
680 // FIXME: Same concerns as with SectionData.
681 class MCSymbolData : public ilist_node<MCSymbolData> {
683 const MCSymbol *Symbol;
685 /// Fragment - The fragment this symbol's value is relative to, if any.
686 MCFragment *Fragment;
688 /// Offset - The offset to apply to the fragment address to form this symbol's
692 /// IsExternal - True if this symbol is visible outside this translation
694 unsigned IsExternal : 1;
696 /// IsPrivateExtern - True if this symbol is private extern.
697 unsigned IsPrivateExtern : 1;
699 /// CommonSize - The size of the symbol, if it is 'common', or 0.
701 // FIXME: Pack this in with other fields? We could put it in offset, since a
702 // common symbol can never get a definition.
705 /// SymbolSize - An expression describing how to calculate the size of
706 /// a symbol. If a symbol has no size this field will be NULL.
707 const MCExpr *SymbolSize;
709 /// CommonAlign - The alignment of the symbol, if it is 'common'.
711 // FIXME: Pack this in with other fields?
712 unsigned CommonAlign;
714 /// Flags - The Flags field is used by object file implementations to store
715 /// additional per symbol information which is not easily classified.
718 /// Index - Index field, for use by the object file implementation.
722 // Only for use as sentinel.
724 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
730 const MCSymbol &getSymbol() const { return *Symbol; }
732 MCFragment *getFragment() const { return Fragment; }
733 void setFragment(MCFragment *Value) { Fragment = Value; }
735 uint64_t getOffset() const { return Offset; }
736 void setOffset(uint64_t Value) { Offset = Value; }
739 /// @name Symbol Attributes
742 bool isExternal() const { return IsExternal; }
743 void setExternal(bool Value) { IsExternal = Value; }
745 bool isPrivateExtern() const { return IsPrivateExtern; }
746 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
748 /// isCommon - Is this a 'common' symbol.
749 bool isCommon() const { return CommonSize != 0; }
751 /// setCommon - Mark this symbol as being 'common'.
753 /// \param Size - The size of the symbol.
754 /// \param Align - The alignment of the symbol.
755 void setCommon(uint64_t Size, unsigned Align) {
760 /// getCommonSize - Return the size of a 'common' symbol.
761 uint64_t getCommonSize() const {
762 assert(isCommon() && "Not a 'common' symbol!");
766 void setSize(const MCExpr *SS) {
770 const MCExpr *getSize() const {
775 /// getCommonAlignment - Return the alignment of a 'common' symbol.
776 unsigned getCommonAlignment() const {
777 assert(isCommon() && "Not a 'common' symbol!");
781 /// getFlags - Get the (implementation defined) symbol flags.
782 uint32_t getFlags() const { return Flags; }
784 /// setFlags - Set the (implementation defined) symbol flags.
785 void setFlags(uint32_t Value) { Flags = Value; }
787 /// modifyFlags - Modify the flags via a mask
788 void modifyFlags(uint32_t Value, uint32_t Mask) {
789 Flags = (Flags & ~Mask) | Value;
792 /// getIndex - Get the (implementation defined) index.
793 uint64_t getIndex() const { return Index; }
795 /// setIndex - Set the (implementation defined) index.
796 void setIndex(uint64_t Value) { Index = Value; }
803 // FIXME: This really doesn't belong here. See comments below.
804 struct IndirectSymbolData {
806 MCSectionData *SectionData;
809 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
811 struct DataRegionData {
812 // This enum should be kept in sync w/ the mach-o definition in
813 // llvm/Object/MachOFormat.h.
814 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
820 friend class MCAsmLayout;
823 typedef iplist<MCSectionData> SectionDataListType;
824 typedef iplist<MCSymbolData> SymbolDataListType;
826 typedef SectionDataListType::const_iterator const_iterator;
827 typedef SectionDataListType::iterator iterator;
829 typedef SymbolDataListType::const_iterator const_symbol_iterator;
830 typedef SymbolDataListType::iterator symbol_iterator;
832 typedef std::vector<std::string> FileNameVectorType;
833 typedef FileNameVectorType::const_iterator const_file_name_iterator;
835 typedef std::vector<IndirectSymbolData>::const_iterator
836 const_indirect_symbol_iterator;
837 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
839 typedef std::vector<DataRegionData>::const_iterator
840 const_data_region_iterator;
841 typedef std::vector<DataRegionData>::iterator data_region_iterator;
844 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
845 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
849 MCAsmBackend &Backend;
851 MCCodeEmitter &Emitter;
853 MCObjectWriter &Writer;
857 iplist<MCSectionData> Sections;
859 iplist<MCSymbolData> Symbols;
861 /// The map of sections to their associated assembler backend data.
863 // FIXME: Avoid this indirection?
864 DenseMap<const MCSection*, MCSectionData*> SectionMap;
866 /// The map of symbols to their associated assembler backend data.
868 // FIXME: Avoid this indirection?
869 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
871 std::vector<IndirectSymbolData> IndirectSymbols;
873 std::vector<DataRegionData> DataRegions;
875 /// The list of linker options to propagate into the object file.
876 std::vector<std::vector<std::string> > LinkerOptions;
878 /// List of declared file names
879 FileNameVectorType FileNames;
881 /// The set of function symbols for which a .thumb_func directive has
884 // FIXME: We really would like this in target specific code rather than
885 // here. Maybe when the relocation stuff moves to target specific,
886 // this can go with it? The streamer would need some target specific
888 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
890 /// \brief The bundle alignment size currently set in the assembler.
892 /// By default it's 0, which means bundling is disabled.
893 unsigned BundleAlignSize;
895 unsigned RelaxAll : 1;
896 unsigned NoExecStack : 1;
897 unsigned SubsectionsViaSymbols : 1;
899 /// ELF specific e_header flags
900 // It would be good if there were an MCELFAssembler class to hold this.
901 // ELF header flags are used both by the integrated and standalone assemblers.
902 // Access to the flags is necessary in cases where assembler directives affect
903 // which flags to be set.
904 unsigned ELFHeaderEFlags;
906 /// Evaluate a fixup to a relocatable expression and the value which should be
907 /// placed into the fixup.
909 /// \param Layout The layout to use for evaluation.
910 /// \param Fixup The fixup to evaluate.
911 /// \param DF The fragment the fixup is inside.
912 /// \param Target [out] On return, the relocatable expression the fixup
914 /// \param Value [out] On return, the value of the fixup as currently laid
916 /// \return Whether the fixup value was fully resolved. This is true if the
917 /// \p Value result is fixed, otherwise the value may change due to
919 bool evaluateFixup(const MCAsmLayout &Layout,
920 const MCFixup &Fixup, const MCFragment *DF,
921 MCValue &Target, uint64_t &Value) const;
923 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
924 /// (increased in size, in order to hold its value correctly).
925 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
926 const MCAsmLayout &Layout) const;
928 /// Check whether the given fragment needs relaxation.
929 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
930 const MCAsmLayout &Layout) const;
932 /// \brief Perform one layout iteration and return true if any offsets
934 bool layoutOnce(MCAsmLayout &Layout);
936 /// \brief Perform one layout iteration of the given section and return true
937 /// if any offsets were adjusted.
938 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
940 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
942 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
944 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
945 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
946 MCDwarfCallFrameFragment &DF);
948 /// finishLayout - Finalize a layout, including fragment lowering.
949 void finishLayout(MCAsmLayout &Layout);
951 uint64_t handleFixup(const MCAsmLayout &Layout,
952 MCFragment &F, const MCFixup &Fixup);
955 /// Compute the effective fragment size assuming it is laid out at the given
956 /// \p SectionAddress and \p FragmentOffset.
957 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
958 const MCFragment &F) const;
960 /// Find the symbol which defines the atom containing the given symbol, or
961 /// null if there is no such symbol.
962 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
964 /// Check whether a particular symbol is visible to the linker and is required
965 /// in the symbol table, or whether it can be discarded by the assembler. This
966 /// also effects whether the assembler treats the label as potentially
967 /// defining a separate atom.
968 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
970 /// Emit the section contents using the given object writer.
971 void writeSectionData(const MCSectionData *Section,
972 const MCAsmLayout &Layout) const;
974 /// Check whether a given symbol has been flagged with .thumb_func.
975 bool isThumbFunc(const MCSymbol *Func) const {
976 return ThumbFuncs.count(Func);
979 /// Flag a function symbol as the target of a .thumb_func directive.
980 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
982 /// ELF e_header flags
983 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
984 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
987 /// Construct a new assembler instance.
989 /// \param OS The stream to output to.
991 // FIXME: How are we going to parameterize this? Two obvious options are stay
992 // concrete and require clients to pass in a target like object. The other
993 // option is to make this abstract, and have targets provide concrete
994 // implementations as we do with AsmParser.
995 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
996 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1000 /// Reuse an assembler instance
1004 MCContext &getContext() const { return Context; }
1006 MCAsmBackend &getBackend() const { return Backend; }
1008 MCCodeEmitter &getEmitter() const { return Emitter; }
1010 MCObjectWriter &getWriter() const { return Writer; }
1012 /// Finish - Do final processing and write the object to the output stream.
1013 /// \p Writer is used for custom object writer (as the MCJIT does),
1014 /// if not specified it is automatically created from backend.
1017 // FIXME: This does not belong here.
1018 bool getSubsectionsViaSymbols() const {
1019 return SubsectionsViaSymbols;
1021 void setSubsectionsViaSymbols(bool Value) {
1022 SubsectionsViaSymbols = Value;
1025 bool getRelaxAll() const { return RelaxAll; }
1026 void setRelaxAll(bool Value) { RelaxAll = Value; }
1028 bool getNoExecStack() const { return NoExecStack; }
1029 void setNoExecStack(bool Value) { NoExecStack = Value; }
1031 bool isBundlingEnabled() const {
1032 return BundleAlignSize != 0;
1035 unsigned getBundleAlignSize() const {
1036 return BundleAlignSize;
1039 void setBundleAlignSize(unsigned Size) {
1040 assert((Size == 0 || !(Size & (Size - 1))) &&
1041 "Expect a power-of-two bundle align size");
1042 BundleAlignSize = Size;
1045 /// @name Section List Access
1048 const SectionDataListType &getSectionList() const { return Sections; }
1049 SectionDataListType &getSectionList() { return Sections; }
1051 iterator begin() { return Sections.begin(); }
1052 const_iterator begin() const { return Sections.begin(); }
1054 iterator end() { return Sections.end(); }
1055 const_iterator end() const { return Sections.end(); }
1057 size_t size() const { return Sections.size(); }
1060 /// @name Symbol List Access
1063 const SymbolDataListType &getSymbolList() const { return Symbols; }
1064 SymbolDataListType &getSymbolList() { return Symbols; }
1066 symbol_iterator symbol_begin() { return Symbols.begin(); }
1067 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1069 symbol_iterator symbol_end() { return Symbols.end(); }
1070 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1072 size_t symbol_size() const { return Symbols.size(); }
1075 /// @name Indirect Symbol List Access
1078 // FIXME: This is a total hack, this should not be here. Once things are
1079 // factored so that the streamer has direct access to the .o writer, it can
1081 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1082 return IndirectSymbols;
1085 indirect_symbol_iterator indirect_symbol_begin() {
1086 return IndirectSymbols.begin();
1088 const_indirect_symbol_iterator indirect_symbol_begin() const {
1089 return IndirectSymbols.begin();
1092 indirect_symbol_iterator indirect_symbol_end() {
1093 return IndirectSymbols.end();
1095 const_indirect_symbol_iterator indirect_symbol_end() const {
1096 return IndirectSymbols.end();
1099 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1102 /// @name Linker Option List Access
1105 std::vector<std::vector<std::string> > &getLinkerOptions() {
1106 return LinkerOptions;
1110 /// @name Data Region List Access
1113 // FIXME: This is a total hack, this should not be here. Once things are
1114 // factored so that the streamer has direct access to the .o writer, it can
1116 std::vector<DataRegionData> &getDataRegions() {
1120 data_region_iterator data_region_begin() {
1121 return DataRegions.begin();
1123 const_data_region_iterator data_region_begin() const {
1124 return DataRegions.begin();
1127 data_region_iterator data_region_end() {
1128 return DataRegions.end();
1130 const_data_region_iterator data_region_end() const {
1131 return DataRegions.end();
1134 size_t data_region_size() const { return DataRegions.size(); }
1137 /// @name Backend Data Access
1140 MCSectionData &getSectionData(const MCSection &Section) const {
1141 MCSectionData *Entry = SectionMap.lookup(&Section);
1142 assert(Entry && "Missing section data!");
1146 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1147 bool *Created = 0) {
1148 MCSectionData *&Entry = SectionMap[&Section];
1150 if (Created) *Created = !Entry;
1152 Entry = new MCSectionData(Section, this);
1157 bool hasSymbolData(const MCSymbol &Symbol) const {
1158 return SymbolMap.lookup(&Symbol) != 0;
1161 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1162 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1163 assert(Entry && "Missing symbol data!");
1167 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1168 bool *Created = 0) {
1169 MCSymbolData *&Entry = SymbolMap[&Symbol];
1171 if (Created) *Created = !Entry;
1173 Entry = new MCSymbolData(Symbol, 0, 0, this);
1178 const_file_name_iterator file_names_begin() const {
1179 return FileNames.begin();
1182 const_file_name_iterator file_names_end() const {
1183 return FileNames.end();
1186 void addFileName(StringRef FileName) {
1187 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1189 FileNames.push_back(FileName);
1197 } // end namespace llvm