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/Support/Casting.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <vector> // FIXME: Shouldn't be needed.
40 class MCFragment : public ilist_node<MCFragment> {
41 friend class MCAsmLayout;
43 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
44 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
50 FT_CompactEncodedInst,
62 /// Parent - The data for the section this fragment is in.
63 MCSectionData *Parent;
65 /// Atom - The atom this fragment is in, as represented by it's defining
66 /// symbol. Atom's are only used by backends which set
67 /// \see MCAsmBackend::hasReliableSymbolDifference().
70 /// @name Assembler Backend Data
73 // FIXME: This could all be kept private to the assembler implementation.
75 /// Offset - The offset of this fragment in its section. This is ~0 until
79 /// LayoutOrder - The layout order of this fragment.
85 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
90 virtual ~MCFragment();
92 FragmentType getKind() const { return Kind; }
94 MCSectionData *getParent() const { return Parent; }
95 void setParent(MCSectionData *Value) { Parent = Value; }
97 MCSymbolData *getAtom() const { return Atom; }
98 void setAtom(MCSymbolData *Value) { Atom = Value; }
100 unsigned getLayoutOrder() const { return LayoutOrder; }
101 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
103 /// \brief Does this fragment have instructions emitted into it? By default
104 /// this is false, but specific fragment types may set it to true.
105 virtual bool hasInstructions() const { return false; }
107 /// \brief Should this fragment be placed at the end of an aligned bundle?
108 virtual bool alignToBundleEnd() const { return false; }
109 virtual void setAlignToBundleEnd(bool V) { }
111 /// \brief Get the padding size that must be inserted before this fragment.
112 /// Used for bundling. By default, no padding is inserted.
113 /// Note that padding size is restricted to 8 bits. This is an optimization
114 /// to reduce the amount of space used for each fragment. In practice, larger
115 /// padding should never be required.
116 virtual uint8_t getBundlePadding() const {
120 /// \brief Set the padding size for this fragment. By default it's a no-op,
121 /// and only some fragments have a meaningful implementation.
122 virtual void setBundlePadding(uint8_t N) {
128 /// Interface implemented by fragments that contain encoded instructions and/or
131 class MCEncodedFragment : public MCFragment {
132 virtual void anchor();
134 uint8_t BundlePadding;
136 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
137 : MCFragment(FType, SD), BundlePadding(0)
140 virtual ~MCEncodedFragment();
142 virtual SmallVectorImpl<char> &getContents() = 0;
143 virtual const SmallVectorImpl<char> &getContents() const = 0;
145 virtual uint8_t getBundlePadding() const {
146 return BundlePadding;
149 virtual void setBundlePadding(uint8_t N) {
153 static bool classof(const MCFragment *F) {
154 MCFragment::FragmentType Kind = F->getKind();
158 case MCFragment::FT_Relaxable:
159 case MCFragment::FT_CompactEncodedInst:
160 case MCFragment::FT_Data:
166 /// Interface implemented by fragments that contain encoded instructions and/or
167 /// data and also have fixups registered.
169 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
170 virtual void anchor();
173 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
174 MCSectionData *SD = 0)
175 : MCEncodedFragment(FType, SD)
179 virtual ~MCEncodedFragmentWithFixups();
181 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
182 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
184 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
185 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
187 virtual fixup_iterator fixup_begin() = 0;
188 virtual const_fixup_iterator fixup_begin() const = 0;
189 virtual fixup_iterator fixup_end() = 0;
190 virtual const_fixup_iterator fixup_end() const = 0;
192 static bool classof(const MCFragment *F) {
193 MCFragment::FragmentType Kind = F->getKind();
194 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
198 /// Fragment for data and encoded instructions.
200 class MCDataFragment : public MCEncodedFragmentWithFixups {
201 virtual void anchor();
203 /// \brief Does this fragment contain encoded instructions anywhere in it?
204 bool HasInstructions;
206 /// \brief Should this fragment be aligned to the end of a bundle?
207 bool AlignToBundleEnd;
209 SmallVector<char, 32> Contents;
211 /// Fixups - The list of fixups in this fragment.
212 SmallVector<MCFixup, 4> Fixups;
214 MCDataFragment(MCSectionData *SD = 0)
215 : MCEncodedFragmentWithFixups(FT_Data, SD),
216 HasInstructions(false), AlignToBundleEnd(false)
220 virtual SmallVectorImpl<char> &getContents() { return Contents; }
221 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
223 SmallVectorImpl<MCFixup> &getFixups() {
227 const SmallVectorImpl<MCFixup> &getFixups() const {
231 virtual bool hasInstructions() const { return HasInstructions; }
232 virtual void setHasInstructions(bool V) { HasInstructions = V; }
234 virtual bool alignToBundleEnd() const { return AlignToBundleEnd; }
235 virtual void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
237 fixup_iterator fixup_begin() { return Fixups.begin(); }
238 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
240 fixup_iterator fixup_end() {return Fixups.end();}
241 const_fixup_iterator fixup_end() const {return Fixups.end();}
243 static bool classof(const MCFragment *F) {
244 return F->getKind() == MCFragment::FT_Data;
248 /// This is a compact (memory-size-wise) fragment for holding an encoded
249 /// instruction (non-relaxable) that has no fixups registered. When applicable,
250 /// it can be used instead of MCDataFragment and lead to lower memory
253 class MCCompactEncodedInstFragment : public MCEncodedFragment {
254 virtual void anchor();
256 /// \brief Should this fragment be aligned to the end of a bundle?
257 bool AlignToBundleEnd;
259 SmallVector<char, 4> Contents;
261 MCCompactEncodedInstFragment(MCSectionData *SD = 0)
262 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
266 virtual bool hasInstructions() const {
270 virtual SmallVectorImpl<char> &getContents() { return Contents; }
271 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
273 virtual bool alignToBundleEnd() const { return AlignToBundleEnd; }
274 virtual void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
276 static bool classof(const MCFragment *F) {
277 return F->getKind() == MCFragment::FT_CompactEncodedInst;
281 /// A relaxable fragment holds on to its MCInst, since it may need to be
282 /// relaxed during the assembler layout and relaxation stage.
284 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
285 virtual void anchor();
287 /// Inst - The instruction this is a fragment for.
290 /// Contents - Binary data for the currently encoded instruction.
291 SmallVector<char, 8> Contents;
293 /// Fixups - The list of fixups in this fragment.
294 SmallVector<MCFixup, 1> Fixups;
297 MCRelaxableFragment(const MCInst &_Inst, MCSectionData *SD = 0)
298 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst) {
301 virtual SmallVectorImpl<char> &getContents() { return Contents; }
302 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
304 const MCInst &getInst() const { return Inst; }
305 void setInst(const MCInst& Value) { Inst = Value; }
307 SmallVectorImpl<MCFixup> &getFixups() {
311 const SmallVectorImpl<MCFixup> &getFixups() const {
315 virtual bool hasInstructions() const { return true; }
317 fixup_iterator fixup_begin() { return Fixups.begin(); }
318 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
320 fixup_iterator fixup_end() {return Fixups.end();}
321 const_fixup_iterator fixup_end() const {return Fixups.end();}
323 static bool classof(const MCFragment *F) {
324 return F->getKind() == MCFragment::FT_Relaxable;
328 class MCAlignFragment : public MCFragment {
329 virtual void anchor();
331 /// Alignment - The alignment to ensure, in bytes.
334 /// Value - Value to use for filling padding bytes.
337 /// ValueSize - The size of the integer (in bytes) of \p Value.
340 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
341 /// cannot be satisfied in this width then this fragment is ignored.
342 unsigned MaxBytesToEmit;
344 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
345 /// of using the provided value. The exact interpretation of this flag is
346 /// target dependent.
350 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
351 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
352 : MCFragment(FT_Align, SD), Alignment(_Alignment),
353 Value(_Value),ValueSize(_ValueSize),
354 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
359 unsigned getAlignment() const { return Alignment; }
361 int64_t getValue() const { return Value; }
363 unsigned getValueSize() const { return ValueSize; }
365 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
367 bool hasEmitNops() const { return EmitNops; }
368 void setEmitNops(bool Value) { EmitNops = Value; }
372 static bool classof(const MCFragment *F) {
373 return F->getKind() == MCFragment::FT_Align;
377 class MCFillFragment : public MCFragment {
378 virtual void anchor();
380 /// Value - Value to use for filling bytes.
383 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
384 /// this is a virtual fill fragment.
387 /// Size - The number of bytes to insert.
391 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
392 MCSectionData *SD = 0)
393 : MCFragment(FT_Fill, SD),
394 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
395 assert((!ValueSize || (Size % ValueSize) == 0) &&
396 "Fill size must be a multiple of the value size!");
402 int64_t getValue() const { return Value; }
404 unsigned getValueSize() const { return ValueSize; }
406 uint64_t getSize() const { return Size; }
410 static bool classof(const MCFragment *F) {
411 return F->getKind() == MCFragment::FT_Fill;
415 class MCOrgFragment : public MCFragment {
416 virtual void anchor();
418 /// Offset - The offset this fragment should start at.
419 const MCExpr *Offset;
421 /// Value - Value to use for filling bytes.
425 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
426 : MCFragment(FT_Org, SD),
427 Offset(&_Offset), Value(_Value) {}
432 const MCExpr &getOffset() const { return *Offset; }
434 uint8_t getValue() const { return Value; }
438 static bool classof(const MCFragment *F) {
439 return F->getKind() == MCFragment::FT_Org;
443 class MCLEBFragment : public MCFragment {
444 virtual void anchor();
446 /// Value - The value this fragment should contain.
449 /// IsSigned - True if this is a sleb128, false if uleb128.
452 SmallString<8> Contents;
454 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD = 0)
455 : MCFragment(FT_LEB, SD),
456 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
461 const MCExpr &getValue() const { return *Value; }
463 bool isSigned() const { return IsSigned; }
465 SmallString<8> &getContents() { return Contents; }
466 const SmallString<8> &getContents() const { return Contents; }
470 static bool classof(const MCFragment *F) {
471 return F->getKind() == MCFragment::FT_LEB;
475 class MCDwarfLineAddrFragment : public MCFragment {
476 virtual void anchor();
478 /// LineDelta - the value of the difference between the two line numbers
479 /// between two .loc dwarf directives.
482 /// AddrDelta - The expression for the difference of the two symbols that
483 /// make up the address delta between two .loc dwarf directives.
484 const MCExpr *AddrDelta;
486 SmallString<8> Contents;
489 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
490 MCSectionData *SD = 0)
491 : MCFragment(FT_Dwarf, SD),
492 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
497 int64_t getLineDelta() const { return LineDelta; }
499 const MCExpr &getAddrDelta() const { return *AddrDelta; }
501 SmallString<8> &getContents() { return Contents; }
502 const SmallString<8> &getContents() const { return Contents; }
506 static bool classof(const MCFragment *F) {
507 return F->getKind() == MCFragment::FT_Dwarf;
511 class MCDwarfCallFrameFragment : public MCFragment {
512 virtual void anchor();
514 /// AddrDelta - The expression for the difference of the two symbols that
515 /// make up the address delta between two .cfi_* dwarf directives.
516 const MCExpr *AddrDelta;
518 SmallString<8> Contents;
521 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
522 : MCFragment(FT_DwarfFrame, SD),
523 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
528 const MCExpr &getAddrDelta() const { return *AddrDelta; }
530 SmallString<8> &getContents() { return Contents; }
531 const SmallString<8> &getContents() const { return Contents; }
535 static bool classof(const MCFragment *F) {
536 return F->getKind() == MCFragment::FT_DwarfFrame;
540 // FIXME: Should this be a separate class, or just merged into MCSection? Since
541 // we anticipate the fast path being through an MCAssembler, the only reason to
542 // keep it out is for API abstraction.
543 class MCSectionData : public ilist_node<MCSectionData> {
544 friend class MCAsmLayout;
546 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
547 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
550 typedef iplist<MCFragment> FragmentListType;
552 typedef FragmentListType::const_iterator const_iterator;
553 typedef FragmentListType::iterator iterator;
555 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
556 typedef FragmentListType::reverse_iterator reverse_iterator;
558 /// \brief Express the state of bundle locked groups while emitting code.
559 enum BundleLockStateType {
562 BundleLockedAlignToEnd
565 FragmentListType Fragments;
566 const MCSection *Section;
568 /// Ordinal - The section index in the assemblers section list.
571 /// LayoutOrder - The index of this section in the layout order.
572 unsigned LayoutOrder;
574 /// Alignment - The maximum alignment seen in this section.
577 /// \brief Keeping track of bundle-locked state.
578 BundleLockStateType BundleLockState;
580 /// \brief We've seen a bundle_lock directive but not its first instruction
582 bool BundleGroupBeforeFirstInst;
584 /// @name Assembler Backend Data
587 // FIXME: This could all be kept private to the assembler implementation.
589 /// HasInstructions - Whether this section has had instructions emitted into
591 unsigned HasInstructions : 1;
593 /// Mapping from subsection number to insertion point for subsection numbers
594 /// below that number.
595 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
600 // Only for use as sentinel.
602 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
604 const MCSection &getSection() const { return *Section; }
606 unsigned getAlignment() const { return Alignment; }
607 void setAlignment(unsigned Value) { Alignment = Value; }
609 bool hasInstructions() const { return HasInstructions; }
610 void setHasInstructions(bool Value) { HasInstructions = Value; }
612 unsigned getOrdinal() const { return Ordinal; }
613 void setOrdinal(unsigned Value) { Ordinal = Value; }
615 unsigned getLayoutOrder() const { return LayoutOrder; }
616 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
618 /// @name Fragment Access
621 const FragmentListType &getFragmentList() const { return Fragments; }
622 FragmentListType &getFragmentList() { return Fragments; }
624 iterator begin() { return Fragments.begin(); }
625 const_iterator begin() const { return Fragments.begin(); }
627 iterator end() { return Fragments.end(); }
628 const_iterator end() const { return Fragments.end(); }
630 reverse_iterator rbegin() { return Fragments.rbegin(); }
631 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
633 reverse_iterator rend() { return Fragments.rend(); }
634 const_reverse_iterator rend() const { return Fragments.rend(); }
636 size_t size() const { return Fragments.size(); }
638 bool empty() const { return Fragments.empty(); }
640 iterator getSubsectionInsertionPoint(unsigned Subsection);
642 bool isBundleLocked() const {
643 return BundleLockState != NotBundleLocked;
646 BundleLockStateType getBundleLockState() const {
647 return BundleLockState;
650 void setBundleLockState(BundleLockStateType NewState) {
651 BundleLockState = NewState;
654 bool isBundleGroupBeforeFirstInst() const {
655 return BundleGroupBeforeFirstInst;
658 void setBundleGroupBeforeFirstInst(bool IsFirst) {
659 BundleGroupBeforeFirstInst = IsFirst;
667 // FIXME: Same concerns as with SectionData.
668 class MCSymbolData : public ilist_node<MCSymbolData> {
670 const MCSymbol *Symbol;
672 /// Fragment - The fragment this symbol's value is relative to, if any.
673 MCFragment *Fragment;
675 /// Offset - The offset to apply to the fragment address to form this symbol's
679 /// IsExternal - True if this symbol is visible outside this translation
681 unsigned IsExternal : 1;
683 /// IsPrivateExtern - True if this symbol is private extern.
684 unsigned IsPrivateExtern : 1;
686 /// CommonSize - The size of the symbol, if it is 'common', or 0.
688 // FIXME: Pack this in with other fields? We could put it in offset, since a
689 // common symbol can never get a definition.
692 /// SymbolSize - An expression describing how to calculate the size of
693 /// a symbol. If a symbol has no size this field will be NULL.
694 const MCExpr *SymbolSize;
696 /// CommonAlign - The alignment of the symbol, if it is 'common'.
698 // FIXME: Pack this in with other fields?
699 unsigned CommonAlign;
701 /// Flags - The Flags field is used by object file implementations to store
702 /// additional per symbol information which is not easily classified.
705 /// Index - Index field, for use by the object file implementation.
709 // Only for use as sentinel.
711 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
717 const MCSymbol &getSymbol() const { return *Symbol; }
719 MCFragment *getFragment() const { return Fragment; }
720 void setFragment(MCFragment *Value) { Fragment = Value; }
722 uint64_t getOffset() const { return Offset; }
723 void setOffset(uint64_t Value) { Offset = Value; }
726 /// @name Symbol Attributes
729 bool isExternal() const { return IsExternal; }
730 void setExternal(bool Value) { IsExternal = Value; }
732 bool isPrivateExtern() const { return IsPrivateExtern; }
733 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
735 /// isCommon - Is this a 'common' symbol.
736 bool isCommon() const { return CommonSize != 0; }
738 /// setCommon - Mark this symbol as being 'common'.
740 /// \param Size - The size of the symbol.
741 /// \param Align - The alignment of the symbol.
742 void setCommon(uint64_t Size, unsigned Align) {
747 /// getCommonSize - Return the size of a 'common' symbol.
748 uint64_t getCommonSize() const {
749 assert(isCommon() && "Not a 'common' symbol!");
753 void setSize(const MCExpr *SS) {
757 const MCExpr *getSize() const {
762 /// getCommonAlignment - Return the alignment of a 'common' symbol.
763 unsigned getCommonAlignment() const {
764 assert(isCommon() && "Not a 'common' symbol!");
768 /// getFlags - Get the (implementation defined) symbol flags.
769 uint32_t getFlags() const { return Flags; }
771 /// setFlags - Set the (implementation defined) symbol flags.
772 void setFlags(uint32_t Value) { Flags = Value; }
774 /// modifyFlags - Modify the flags via a mask
775 void modifyFlags(uint32_t Value, uint32_t Mask) {
776 Flags = (Flags & ~Mask) | Value;
779 /// getIndex - Get the (implementation defined) index.
780 uint64_t getIndex() const { return Index; }
782 /// setIndex - Set the (implementation defined) index.
783 void setIndex(uint64_t Value) { Index = Value; }
790 // FIXME: This really doesn't belong here. See comments below.
791 struct IndirectSymbolData {
793 MCSectionData *SectionData;
796 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
798 struct DataRegionData {
799 // This enum should be kept in sync w/ the mach-o definition in
800 // llvm/Object/MachOFormat.h.
801 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
807 friend class MCAsmLayout;
810 typedef iplist<MCSectionData> SectionDataListType;
811 typedef iplist<MCSymbolData> SymbolDataListType;
813 typedef SectionDataListType::const_iterator const_iterator;
814 typedef SectionDataListType::iterator iterator;
816 typedef SymbolDataListType::const_iterator const_symbol_iterator;
817 typedef SymbolDataListType::iterator symbol_iterator;
819 typedef std::vector<IndirectSymbolData>::const_iterator
820 const_indirect_symbol_iterator;
821 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
823 typedef std::vector<DataRegionData>::const_iterator
824 const_data_region_iterator;
825 typedef std::vector<DataRegionData>::iterator data_region_iterator;
828 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
829 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
833 MCAsmBackend &Backend;
835 MCCodeEmitter &Emitter;
837 MCObjectWriter &Writer;
841 iplist<MCSectionData> Sections;
843 iplist<MCSymbolData> Symbols;
845 /// The map of sections to their associated assembler backend data.
847 // FIXME: Avoid this indirection?
848 DenseMap<const MCSection*, MCSectionData*> SectionMap;
850 /// The map of symbols to their associated assembler backend data.
852 // FIXME: Avoid this indirection?
853 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
855 std::vector<IndirectSymbolData> IndirectSymbols;
857 std::vector<DataRegionData> DataRegions;
859 /// The list of linker options to propagate into the object file.
860 std::vector<std::vector<std::string> > LinkerOptions;
862 /// The set of function symbols for which a .thumb_func directive has
865 // FIXME: We really would like this in target specific code rather than
866 // here. Maybe when the relocation stuff moves to target specific,
867 // this can go with it? The streamer would need some target specific
869 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
871 /// \brief The bundle alignment size currently set in the assembler.
873 /// By default it's 0, which means bundling is disabled.
874 unsigned BundleAlignSize;
876 unsigned RelaxAll : 1;
877 unsigned NoExecStack : 1;
878 unsigned SubsectionsViaSymbols : 1;
880 /// ELF specific e_header flags
881 // It would be good if there were an MCELFAssembler class to hold this.
882 // ELF header flags are used both by the integrated and standalone assemblers.
883 // Access to the flags is necessary in cases where assembler directives affect
884 // which flags to be set.
885 unsigned ELFHeaderEFlags;
887 /// Evaluate a fixup to a relocatable expression and the value which should be
888 /// placed into the fixup.
890 /// \param Layout The layout to use for evaluation.
891 /// \param Fixup The fixup to evaluate.
892 /// \param DF The fragment the fixup is inside.
893 /// \param Target [out] On return, the relocatable expression the fixup
895 /// \param Value [out] On return, the value of the fixup as currently laid
897 /// \return Whether the fixup value was fully resolved. This is true if the
898 /// \p Value result is fixed, otherwise the value may change due to
900 bool evaluateFixup(const MCAsmLayout &Layout,
901 const MCFixup &Fixup, const MCFragment *DF,
902 MCValue &Target, uint64_t &Value) const;
904 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
905 /// (increased in size, in order to hold its value correctly).
906 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
907 const MCAsmLayout &Layout) const;
909 /// Check whether the given fragment needs relaxation.
910 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
911 const MCAsmLayout &Layout) const;
913 /// \brief Perform one layout iteration and return true if any offsets
915 bool layoutOnce(MCAsmLayout &Layout);
917 /// \brief Perform one layout iteration of the given section and return true
918 /// if any offsets were adjusted.
919 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
921 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
923 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
925 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
926 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
927 MCDwarfCallFrameFragment &DF);
929 /// finishLayout - Finalize a layout, including fragment lowering.
930 void finishLayout(MCAsmLayout &Layout);
932 uint64_t handleFixup(const MCAsmLayout &Layout,
933 MCFragment &F, const MCFixup &Fixup);
936 /// Compute the effective fragment size assuming it is laid out at the given
937 /// \p SectionAddress and \p FragmentOffset.
938 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
939 const MCFragment &F) const;
941 /// Find the symbol which defines the atom containing the given symbol, or
942 /// null if there is no such symbol.
943 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
945 /// Check whether a particular symbol is visible to the linker and is required
946 /// in the symbol table, or whether it can be discarded by the assembler. This
947 /// also effects whether the assembler treats the label as potentially
948 /// defining a separate atom.
949 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
951 /// Emit the section contents using the given object writer.
952 void writeSectionData(const MCSectionData *Section,
953 const MCAsmLayout &Layout) const;
955 /// Check whether a given symbol has been flagged with .thumb_func.
956 bool isThumbFunc(const MCSymbol *Func) const {
957 return ThumbFuncs.count(Func);
960 /// Flag a function symbol as the target of a .thumb_func directive.
961 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
963 /// ELF e_header flags
964 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
965 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
968 /// Construct a new assembler instance.
970 /// \param OS The stream to output to.
972 // FIXME: How are we going to parameterize this? Two obvious options are stay
973 // concrete and require clients to pass in a target like object. The other
974 // option is to make this abstract, and have targets provide concrete
975 // implementations as we do with AsmParser.
976 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
977 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
981 /// Reuse an assembler instance
985 MCContext &getContext() const { return Context; }
987 MCAsmBackend &getBackend() const { return Backend; }
989 MCCodeEmitter &getEmitter() const { return Emitter; }
991 MCObjectWriter &getWriter() const { return Writer; }
993 /// Finish - Do final processing and write the object to the output stream.
994 /// \p Writer is used for custom object writer (as the MCJIT does),
995 /// if not specified it is automatically created from backend.
998 // FIXME: This does not belong here.
999 bool getSubsectionsViaSymbols() const {
1000 return SubsectionsViaSymbols;
1002 void setSubsectionsViaSymbols(bool Value) {
1003 SubsectionsViaSymbols = Value;
1006 bool getRelaxAll() const { return RelaxAll; }
1007 void setRelaxAll(bool Value) { RelaxAll = Value; }
1009 bool getNoExecStack() const { return NoExecStack; }
1010 void setNoExecStack(bool Value) { NoExecStack = Value; }
1012 bool isBundlingEnabled() const {
1013 return BundleAlignSize != 0;
1016 unsigned getBundleAlignSize() const {
1017 return BundleAlignSize;
1020 void setBundleAlignSize(unsigned Size) {
1021 assert((Size == 0 || !(Size & (Size - 1))) &&
1022 "Expect a power-of-two bundle align size");
1023 BundleAlignSize = Size;
1026 /// @name Section List Access
1029 const SectionDataListType &getSectionList() const { return Sections; }
1030 SectionDataListType &getSectionList() { return Sections; }
1032 iterator begin() { return Sections.begin(); }
1033 const_iterator begin() const { return Sections.begin(); }
1035 iterator end() { return Sections.end(); }
1036 const_iterator end() const { return Sections.end(); }
1038 size_t size() const { return Sections.size(); }
1041 /// @name Symbol List Access
1044 const SymbolDataListType &getSymbolList() const { return Symbols; }
1045 SymbolDataListType &getSymbolList() { return Symbols; }
1047 symbol_iterator symbol_begin() { return Symbols.begin(); }
1048 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1050 symbol_iterator symbol_end() { return Symbols.end(); }
1051 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1053 size_t symbol_size() const { return Symbols.size(); }
1056 /// @name Indirect Symbol List Access
1059 // FIXME: This is a total hack, this should not be here. Once things are
1060 // factored so that the streamer has direct access to the .o writer, it can
1062 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1063 return IndirectSymbols;
1066 indirect_symbol_iterator indirect_symbol_begin() {
1067 return IndirectSymbols.begin();
1069 const_indirect_symbol_iterator indirect_symbol_begin() const {
1070 return IndirectSymbols.begin();
1073 indirect_symbol_iterator indirect_symbol_end() {
1074 return IndirectSymbols.end();
1076 const_indirect_symbol_iterator indirect_symbol_end() const {
1077 return IndirectSymbols.end();
1080 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1083 /// @name Linker Option List Access
1086 std::vector<std::vector<std::string> > &getLinkerOptions() {
1087 return LinkerOptions;
1091 /// @name Data Region List Access
1094 // FIXME: This is a total hack, this should not be here. Once things are
1095 // factored so that the streamer has direct access to the .o writer, it can
1097 std::vector<DataRegionData> &getDataRegions() {
1101 data_region_iterator data_region_begin() {
1102 return DataRegions.begin();
1104 const_data_region_iterator data_region_begin() const {
1105 return DataRegions.begin();
1108 data_region_iterator data_region_end() {
1109 return DataRegions.end();
1111 const_data_region_iterator data_region_end() const {
1112 return DataRegions.end();
1115 size_t data_region_size() const { return DataRegions.size(); }
1118 /// @name Backend Data Access
1121 MCSectionData &getSectionData(const MCSection &Section) const {
1122 MCSectionData *Entry = SectionMap.lookup(&Section);
1123 assert(Entry && "Missing section data!");
1127 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1128 bool *Created = 0) {
1129 MCSectionData *&Entry = SectionMap[&Section];
1131 if (Created) *Created = !Entry;
1133 Entry = new MCSectionData(Section, this);
1138 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1139 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1140 assert(Entry && "Missing symbol data!");
1144 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1145 bool *Created = 0) {
1146 MCSymbolData *&Entry = SymbolMap[&Symbol];
1148 if (Created) *Created = !Entry;
1150 Entry = new MCSymbolData(Symbol, 0, 0, this);
1160 } // end namespace llvm