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) {
125 virtual bool hasFixups() const {
132 /// Interface implemented by fragments that contain encoded instructions and/or
135 class MCEncodedFragment : public MCFragment {
136 virtual void anchor();
138 uint8_t BundlePadding;
140 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
141 : MCFragment(FType, SD), BundlePadding(0)
144 virtual ~MCEncodedFragment();
146 virtual SmallVectorImpl<char> &getContents() = 0;
147 virtual const SmallVectorImpl<char> &getContents() const = 0;
149 virtual uint8_t getBundlePadding() const {
150 return BundlePadding;
153 virtual void setBundlePadding(uint8_t N) {
157 static bool classof(const MCFragment *F) {
158 MCFragment::FragmentType Kind = F->getKind();
162 case MCFragment::FT_Relaxable:
163 case MCFragment::FT_CompactEncodedInst:
164 case MCFragment::FT_Data:
170 /// Interface implemented by fragments that contain encoded instructions and/or
171 /// data and also have fixups registered.
173 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
174 virtual void anchor();
177 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
178 MCSectionData *SD = 0)
179 : MCEncodedFragment(FType, SD)
183 virtual ~MCEncodedFragmentWithFixups();
185 virtual bool hasFixups() const {
189 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
190 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
192 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
193 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
195 virtual fixup_iterator fixup_begin() = 0;
196 virtual const_fixup_iterator fixup_begin() const = 0;
197 virtual fixup_iterator fixup_end() = 0;
198 virtual const_fixup_iterator fixup_end() const = 0;
200 static bool classof(const MCFragment *F) {
201 return isa<MCEncodedFragment>(F) && F->hasFixups();
205 /// Fragment for data and encoded instructions.
207 class MCDataFragment : public MCEncodedFragmentWithFixups {
208 virtual void anchor();
210 /// \brief Does this fragment contain encoded instructions anywhere in it?
211 bool HasInstructions;
213 /// \brief Should this fragment be aligned to the end of a bundle?
214 bool AlignToBundleEnd;
216 SmallVector<char, 32> Contents;
218 /// Fixups - The list of fixups in this fragment.
219 SmallVector<MCFixup, 4> Fixups;
221 MCDataFragment(MCSectionData *SD = 0)
222 : MCEncodedFragmentWithFixups(FT_Data, SD),
223 HasInstructions(false), AlignToBundleEnd(false)
227 virtual SmallVectorImpl<char> &getContents() { return Contents; }
228 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
230 SmallVectorImpl<MCFixup> &getFixups() {
234 const SmallVectorImpl<MCFixup> &getFixups() const {
238 virtual bool hasInstructions() const { return HasInstructions; }
239 virtual void setHasInstructions(bool V) { HasInstructions = V; }
241 virtual bool alignToBundleEnd() const { return AlignToBundleEnd; }
242 virtual void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
244 fixup_iterator fixup_begin() { return Fixups.begin(); }
245 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
247 fixup_iterator fixup_end() {return Fixups.end();}
248 const_fixup_iterator fixup_end() const {return Fixups.end();}
250 static bool classof(const MCFragment *F) {
251 return F->getKind() == MCFragment::FT_Data;
255 /// This is a compact (memory-size-wise) fragment for holding an encoded
256 /// instruction (non-relaxable) that has no fixups registered. When applicable,
257 /// it can be used instead of MCDataFragment and lead to lower memory
260 class MCCompactEncodedInstFragment : public MCEncodedFragment {
261 virtual void anchor();
263 /// \brief Should this fragment be aligned to the end of a bundle?
264 bool AlignToBundleEnd;
266 SmallVector<char, 4> Contents;
268 MCCompactEncodedInstFragment(MCSectionData *SD = 0)
269 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
273 virtual bool hasInstructions() const {
277 virtual SmallVectorImpl<char> &getContents() { return Contents; }
278 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
280 virtual bool alignToBundleEnd() const { return AlignToBundleEnd; }
281 virtual void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
283 static bool classof(const MCFragment *F) {
284 return F->getKind() == MCFragment::FT_CompactEncodedInst;
288 /// A relaxable fragment holds on to its MCInst, since it may need to be
289 /// relaxed during the assembler layout and relaxation stage.
291 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
292 virtual void anchor();
294 /// Inst - The instruction this is a fragment for.
297 /// Contents - Binary data for the currently encoded instruction.
298 SmallVector<char, 8> Contents;
300 /// Fixups - The list of fixups in this fragment.
301 SmallVector<MCFixup, 1> Fixups;
304 MCRelaxableFragment(const MCInst &_Inst, MCSectionData *SD = 0)
305 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst) {
308 virtual SmallVectorImpl<char> &getContents() { return Contents; }
309 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
311 const MCInst &getInst() const { return Inst; }
312 void setInst(const MCInst& Value) { Inst = Value; }
314 SmallVectorImpl<MCFixup> &getFixups() {
318 const SmallVectorImpl<MCFixup> &getFixups() const {
322 virtual bool hasInstructions() const { return true; }
324 fixup_iterator fixup_begin() { return Fixups.begin(); }
325 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
327 fixup_iterator fixup_end() {return Fixups.end();}
328 const_fixup_iterator fixup_end() const {return Fixups.end();}
330 static bool classof(const MCFragment *F) {
331 return F->getKind() == MCFragment::FT_Relaxable;
335 class MCAlignFragment : public MCFragment {
336 virtual void anchor();
338 /// Alignment - The alignment to ensure, in bytes.
341 /// Value - Value to use for filling padding bytes.
344 /// ValueSize - The size of the integer (in bytes) of \p Value.
347 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
348 /// cannot be satisfied in this width then this fragment is ignored.
349 unsigned MaxBytesToEmit;
351 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
352 /// of using the provided value. The exact interpretation of this flag is
353 /// target dependent.
357 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
358 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
359 : MCFragment(FT_Align, SD), Alignment(_Alignment),
360 Value(_Value),ValueSize(_ValueSize),
361 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
366 unsigned getAlignment() const { return Alignment; }
368 int64_t getValue() const { return Value; }
370 unsigned getValueSize() const { return ValueSize; }
372 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
374 bool hasEmitNops() const { return EmitNops; }
375 void setEmitNops(bool Value) { EmitNops = Value; }
379 static bool classof(const MCFragment *F) {
380 return F->getKind() == MCFragment::FT_Align;
384 class MCFillFragment : public MCFragment {
385 virtual void anchor();
387 /// Value - Value to use for filling bytes.
390 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
391 /// this is a virtual fill fragment.
394 /// Size - The number of bytes to insert.
398 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
399 MCSectionData *SD = 0)
400 : MCFragment(FT_Fill, SD),
401 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
402 assert((!ValueSize || (Size % ValueSize) == 0) &&
403 "Fill size must be a multiple of the value size!");
409 int64_t getValue() const { return Value; }
411 unsigned getValueSize() const { return ValueSize; }
413 uint64_t getSize() const { return Size; }
417 static bool classof(const MCFragment *F) {
418 return F->getKind() == MCFragment::FT_Fill;
422 class MCOrgFragment : public MCFragment {
423 virtual void anchor();
425 /// Offset - The offset this fragment should start at.
426 const MCExpr *Offset;
428 /// Value - Value to use for filling bytes.
432 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
433 : MCFragment(FT_Org, SD),
434 Offset(&_Offset), Value(_Value) {}
439 const MCExpr &getOffset() const { return *Offset; }
441 uint8_t getValue() const { return Value; }
445 static bool classof(const MCFragment *F) {
446 return F->getKind() == MCFragment::FT_Org;
450 class MCLEBFragment : public MCFragment {
451 virtual void anchor();
453 /// Value - The value this fragment should contain.
456 /// IsSigned - True if this is a sleb128, false if uleb128.
459 SmallString<8> Contents;
461 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD)
462 : MCFragment(FT_LEB, SD),
463 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
468 const MCExpr &getValue() const { return *Value; }
470 bool isSigned() const { return IsSigned; }
472 SmallString<8> &getContents() { return Contents; }
473 const SmallString<8> &getContents() const { return Contents; }
477 static bool classof(const MCFragment *F) {
478 return F->getKind() == MCFragment::FT_LEB;
482 class MCDwarfLineAddrFragment : public MCFragment {
483 virtual void anchor();
485 /// LineDelta - the value of the difference between the two line numbers
486 /// between two .loc dwarf directives.
489 /// AddrDelta - The expression for the difference of the two symbols that
490 /// make up the address delta between two .loc dwarf directives.
491 const MCExpr *AddrDelta;
493 SmallString<8> Contents;
496 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
498 : MCFragment(FT_Dwarf, SD),
499 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
504 int64_t getLineDelta() const { return LineDelta; }
506 const MCExpr &getAddrDelta() const { return *AddrDelta; }
508 SmallString<8> &getContents() { return Contents; }
509 const SmallString<8> &getContents() const { return Contents; }
513 static bool classof(const MCFragment *F) {
514 return F->getKind() == MCFragment::FT_Dwarf;
518 class MCDwarfCallFrameFragment : public MCFragment {
519 virtual void anchor();
521 /// AddrDelta - The expression for the difference of the two symbols that
522 /// make up the address delta between two .cfi_* dwarf directives.
523 const MCExpr *AddrDelta;
525 SmallString<8> Contents;
528 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD)
529 : MCFragment(FT_DwarfFrame, SD),
530 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
535 const MCExpr &getAddrDelta() const { return *AddrDelta; }
537 SmallString<8> &getContents() { return Contents; }
538 const SmallString<8> &getContents() const { return Contents; }
542 static bool classof(const MCFragment *F) {
543 return F->getKind() == MCFragment::FT_DwarfFrame;
547 // FIXME: Should this be a separate class, or just merged into MCSection? Since
548 // we anticipate the fast path being through an MCAssembler, the only reason to
549 // keep it out is for API abstraction.
550 class MCSectionData : public ilist_node<MCSectionData> {
551 friend class MCAsmLayout;
553 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
554 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
557 typedef iplist<MCFragment> FragmentListType;
559 typedef FragmentListType::const_iterator const_iterator;
560 typedef FragmentListType::iterator iterator;
562 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
563 typedef FragmentListType::reverse_iterator reverse_iterator;
565 /// \brief Express the state of bundle locked groups while emitting code.
566 enum BundleLockStateType {
569 BundleLockedAlignToEnd
572 FragmentListType Fragments;
573 const MCSection *Section;
575 /// Ordinal - The section index in the assemblers section list.
578 /// LayoutOrder - The index of this section in the layout order.
579 unsigned LayoutOrder;
581 /// Alignment - The maximum alignment seen in this section.
584 /// \brief Keeping track of bundle-locked state.
585 BundleLockStateType BundleLockState;
587 /// \brief We've seen a bundle_lock directive but not its first instruction
589 bool BundleGroupBeforeFirstInst;
591 /// @name Assembler Backend Data
594 // FIXME: This could all be kept private to the assembler implementation.
596 /// HasInstructions - Whether this section has had instructions emitted into
598 unsigned HasInstructions : 1;
603 // Only for use as sentinel.
605 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
607 const MCSection &getSection() const { return *Section; }
609 unsigned getAlignment() const { return Alignment; }
610 void setAlignment(unsigned Value) { Alignment = Value; }
612 bool hasInstructions() const { return HasInstructions; }
613 void setHasInstructions(bool Value) { HasInstructions = Value; }
615 unsigned getOrdinal() const { return Ordinal; }
616 void setOrdinal(unsigned Value) { Ordinal = Value; }
618 unsigned getLayoutOrder() const { return LayoutOrder; }
619 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
621 /// @name Fragment Access
624 const FragmentListType &getFragmentList() const { return Fragments; }
625 FragmentListType &getFragmentList() { return Fragments; }
627 iterator begin() { return Fragments.begin(); }
628 const_iterator begin() const { return Fragments.begin(); }
630 iterator end() { return Fragments.end(); }
631 const_iterator end() const { return Fragments.end(); }
633 reverse_iterator rbegin() { return Fragments.rbegin(); }
634 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
636 reverse_iterator rend() { return Fragments.rend(); }
637 const_reverse_iterator rend() const { return Fragments.rend(); }
639 size_t size() const { return Fragments.size(); }
641 bool empty() const { return Fragments.empty(); }
643 bool isBundleLocked() const {
644 return BundleLockState != NotBundleLocked;
647 BundleLockStateType getBundleLockState() const {
648 return BundleLockState;
651 void setBundleLockState(BundleLockStateType NewState) {
652 BundleLockState = NewState;
655 bool isBundleGroupBeforeFirstInst() const {
656 return BundleGroupBeforeFirstInst;
659 void setBundleGroupBeforeFirstInst(bool IsFirst) {
660 BundleGroupBeforeFirstInst = IsFirst;
668 // FIXME: Same concerns as with SectionData.
669 class MCSymbolData : public ilist_node<MCSymbolData> {
671 const MCSymbol *Symbol;
673 /// Fragment - The fragment this symbol's value is relative to, if any.
674 MCFragment *Fragment;
676 /// Offset - The offset to apply to the fragment address to form this symbol's
680 /// IsExternal - True if this symbol is visible outside this translation
682 unsigned IsExternal : 1;
684 /// IsPrivateExtern - True if this symbol is private extern.
685 unsigned IsPrivateExtern : 1;
687 /// CommonSize - The size of the symbol, if it is 'common', or 0.
689 // FIXME: Pack this in with other fields? We could put it in offset, since a
690 // common symbol can never get a definition.
693 /// SymbolSize - An expression describing how to calculate the size of
694 /// a symbol. If a symbol has no size this field will be NULL.
695 const MCExpr *SymbolSize;
697 /// CommonAlign - The alignment of the symbol, if it is 'common'.
699 // FIXME: Pack this in with other fields?
700 unsigned CommonAlign;
702 /// Flags - The Flags field is used by object file implementations to store
703 /// additional per symbol information which is not easily classified.
706 /// Index - Index field, for use by the object file implementation.
710 // Only for use as sentinel.
712 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
718 const MCSymbol &getSymbol() const { return *Symbol; }
720 MCFragment *getFragment() const { return Fragment; }
721 void setFragment(MCFragment *Value) { Fragment = Value; }
723 uint64_t getOffset() const { return Offset; }
724 void setOffset(uint64_t Value) { Offset = Value; }
727 /// @name Symbol Attributes
730 bool isExternal() const { return IsExternal; }
731 void setExternal(bool Value) { IsExternal = Value; }
733 bool isPrivateExtern() const { return IsPrivateExtern; }
734 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
736 /// isCommon - Is this a 'common' symbol.
737 bool isCommon() const { return CommonSize != 0; }
739 /// setCommon - Mark this symbol as being 'common'.
741 /// \param Size - The size of the symbol.
742 /// \param Align - The alignment of the symbol.
743 void setCommon(uint64_t Size, unsigned Align) {
748 /// getCommonSize - Return the size of a 'common' symbol.
749 uint64_t getCommonSize() const {
750 assert(isCommon() && "Not a 'common' symbol!");
754 void setSize(const MCExpr *SS) {
758 const MCExpr *getSize() const {
763 /// getCommonAlignment - Return the alignment of a 'common' symbol.
764 unsigned getCommonAlignment() const {
765 assert(isCommon() && "Not a 'common' symbol!");
769 /// getFlags - Get the (implementation defined) symbol flags.
770 uint32_t getFlags() const { return Flags; }
772 /// setFlags - Set the (implementation defined) symbol flags.
773 void setFlags(uint32_t Value) { Flags = Value; }
775 /// modifyFlags - Modify the flags via a mask
776 void modifyFlags(uint32_t Value, uint32_t Mask) {
777 Flags = (Flags & ~Mask) | Value;
780 /// getIndex - Get the (implementation defined) index.
781 uint64_t getIndex() const { return Index; }
783 /// setIndex - Set the (implementation defined) index.
784 void setIndex(uint64_t Value) { Index = Value; }
791 // FIXME: This really doesn't belong here. See comments below.
792 struct IndirectSymbolData {
794 MCSectionData *SectionData;
797 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
799 struct DataRegionData {
800 // This enum should be kept in sync w/ the mach-o definition in
801 // llvm/Object/MachOFormat.h.
802 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
808 friend class MCAsmLayout;
811 typedef iplist<MCSectionData> SectionDataListType;
812 typedef iplist<MCSymbolData> SymbolDataListType;
814 typedef SectionDataListType::const_iterator const_iterator;
815 typedef SectionDataListType::iterator iterator;
817 typedef SymbolDataListType::const_iterator const_symbol_iterator;
818 typedef SymbolDataListType::iterator symbol_iterator;
820 typedef std::vector<IndirectSymbolData>::const_iterator
821 const_indirect_symbol_iterator;
822 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
824 typedef std::vector<DataRegionData>::const_iterator
825 const_data_region_iterator;
826 typedef std::vector<DataRegionData>::iterator data_region_iterator;
829 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
830 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
834 MCAsmBackend &Backend;
836 MCCodeEmitter &Emitter;
838 MCObjectWriter &Writer;
842 iplist<MCSectionData> Sections;
844 iplist<MCSymbolData> Symbols;
846 /// The map of sections to their associated assembler backend data.
848 // FIXME: Avoid this indirection?
849 DenseMap<const MCSection*, MCSectionData*> SectionMap;
851 /// The map of symbols to their associated assembler backend data.
853 // FIXME: Avoid this indirection?
854 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
856 std::vector<IndirectSymbolData> IndirectSymbols;
858 std::vector<DataRegionData> DataRegions;
859 /// The set of function symbols for which a .thumb_func directive has
862 // FIXME: We really would like this in target specific code rather than
863 // here. Maybe when the relocation stuff moves to target specific,
864 // this can go with it? The streamer would need some target specific
866 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
868 /// \brief The bundle alignment size currently set in the assembler.
870 /// By default it's 0, which means bundling is disabled.
871 unsigned BundleAlignSize;
873 unsigned RelaxAll : 1;
874 unsigned NoExecStack : 1;
875 unsigned SubsectionsViaSymbols : 1;
878 /// Evaluate a fixup to a relocatable expression and the value which should be
879 /// placed into the fixup.
881 /// \param Layout The layout to use for evaluation.
882 /// \param Fixup The fixup to evaluate.
883 /// \param DF The fragment the fixup is inside.
884 /// \param Target [out] On return, the relocatable expression the fixup
886 /// \param Value [out] On return, the value of the fixup as currently laid
888 /// \return Whether the fixup value was fully resolved. This is true if the
889 /// \p Value result is fixed, otherwise the value may change due to
891 bool evaluateFixup(const MCAsmLayout &Layout,
892 const MCFixup &Fixup, const MCFragment *DF,
893 MCValue &Target, uint64_t &Value) const;
895 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
896 /// (increased in size, in order to hold its value correctly).
897 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
898 const MCAsmLayout &Layout) const;
900 /// Check whether the given fragment needs relaxation.
901 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
902 const MCAsmLayout &Layout) const;
904 /// \brief Perform one layout iteration and return true if any offsets
906 bool layoutOnce(MCAsmLayout &Layout);
908 /// \brief Perform one layout iteration of the given section and return true
909 /// if any offsets were adjusted.
910 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
912 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
914 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
916 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
917 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
918 MCDwarfCallFrameFragment &DF);
920 /// finishLayout - Finalize a layout, including fragment lowering.
921 void finishLayout(MCAsmLayout &Layout);
923 uint64_t handleFixup(const MCAsmLayout &Layout,
924 MCFragment &F, const MCFixup &Fixup);
927 /// Compute the effective fragment size assuming it is laid out at the given
928 /// \p SectionAddress and \p FragmentOffset.
929 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
930 const MCFragment &F) const;
932 /// Find the symbol which defines the atom containing the given symbol, or
933 /// null if there is no such symbol.
934 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
936 /// Check whether a particular symbol is visible to the linker and is required
937 /// in the symbol table, or whether it can be discarded by the assembler. This
938 /// also effects whether the assembler treats the label as potentially
939 /// defining a separate atom.
940 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
942 /// Emit the section contents using the given object writer.
943 void writeSectionData(const MCSectionData *Section,
944 const MCAsmLayout &Layout) const;
946 /// Check whether a given symbol has been flagged with .thumb_func.
947 bool isThumbFunc(const MCSymbol *Func) const {
948 return ThumbFuncs.count(Func);
951 /// Flag a function symbol as the target of a .thumb_func directive.
952 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
955 /// Construct a new assembler instance.
957 /// \param OS The stream to output to.
959 // FIXME: How are we going to parameterize this? Two obvious options are stay
960 // concrete and require clients to pass in a target like object. The other
961 // option is to make this abstract, and have targets provide concrete
962 // implementations as we do with AsmParser.
963 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
964 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
968 /// Reuse an assembler instance
972 MCContext &getContext() const { return Context; }
974 MCAsmBackend &getBackend() const { return Backend; }
976 MCCodeEmitter &getEmitter() const { return Emitter; }
978 MCObjectWriter &getWriter() const { return Writer; }
980 /// Finish - Do final processing and write the object to the output stream.
981 /// \p Writer is used for custom object writer (as the MCJIT does),
982 /// if not specified it is automatically created from backend.
985 // FIXME: This does not belong here.
986 bool getSubsectionsViaSymbols() const {
987 return SubsectionsViaSymbols;
989 void setSubsectionsViaSymbols(bool Value) {
990 SubsectionsViaSymbols = Value;
993 bool getRelaxAll() const { return RelaxAll; }
994 void setRelaxAll(bool Value) { RelaxAll = Value; }
996 bool getNoExecStack() const { return NoExecStack; }
997 void setNoExecStack(bool Value) { NoExecStack = Value; }
999 bool isBundlingEnabled() const {
1000 return BundleAlignSize != 0;
1003 unsigned getBundleAlignSize() const {
1004 return BundleAlignSize;
1007 void setBundleAlignSize(unsigned Size) {
1008 assert((Size == 0 || !(Size & (Size - 1))) &&
1009 "Expect a power-of-two bundle align size");
1010 BundleAlignSize = Size;
1013 /// @name Section List Access
1016 const SectionDataListType &getSectionList() const { return Sections; }
1017 SectionDataListType &getSectionList() { return Sections; }
1019 iterator begin() { return Sections.begin(); }
1020 const_iterator begin() const { return Sections.begin(); }
1022 iterator end() { return Sections.end(); }
1023 const_iterator end() const { return Sections.end(); }
1025 size_t size() const { return Sections.size(); }
1028 /// @name Symbol List Access
1031 const SymbolDataListType &getSymbolList() const { return Symbols; }
1032 SymbolDataListType &getSymbolList() { return Symbols; }
1034 symbol_iterator symbol_begin() { return Symbols.begin(); }
1035 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1037 symbol_iterator symbol_end() { return Symbols.end(); }
1038 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1040 size_t symbol_size() const { return Symbols.size(); }
1043 /// @name Indirect Symbol List Access
1046 // FIXME: This is a total hack, this should not be here. Once things are
1047 // factored so that the streamer has direct access to the .o writer, it can
1049 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1050 return IndirectSymbols;
1053 indirect_symbol_iterator indirect_symbol_begin() {
1054 return IndirectSymbols.begin();
1056 const_indirect_symbol_iterator indirect_symbol_begin() const {
1057 return IndirectSymbols.begin();
1060 indirect_symbol_iterator indirect_symbol_end() {
1061 return IndirectSymbols.end();
1063 const_indirect_symbol_iterator indirect_symbol_end() const {
1064 return IndirectSymbols.end();
1067 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1070 /// @name Data Region List Access
1073 // FIXME: This is a total hack, this should not be here. Once things are
1074 // factored so that the streamer has direct access to the .o writer, it can
1076 std::vector<DataRegionData> &getDataRegions() {
1080 data_region_iterator data_region_begin() {
1081 return DataRegions.begin();
1083 const_data_region_iterator data_region_begin() const {
1084 return DataRegions.begin();
1087 data_region_iterator data_region_end() {
1088 return DataRegions.end();
1090 const_data_region_iterator data_region_end() const {
1091 return DataRegions.end();
1094 size_t data_region_size() const { return DataRegions.size(); }
1097 /// @name Backend Data Access
1100 MCSectionData &getSectionData(const MCSection &Section) const {
1101 MCSectionData *Entry = SectionMap.lookup(&Section);
1102 assert(Entry && "Missing section data!");
1106 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1107 bool *Created = 0) {
1108 MCSectionData *&Entry = SectionMap[&Section];
1110 if (Created) *Created = !Entry;
1112 Entry = new MCSectionData(Section, this);
1117 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1118 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1119 assert(Entry && "Missing symbol data!");
1123 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1124 bool *Created = 0) {
1125 MCSymbolData *&Entry = SymbolMap[&Symbol];
1127 if (Created) *Created = !Entry;
1129 Entry = new MCSymbolData(Symbol, 0, 0, this);
1139 } // end namespace llvm