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;
61 /// Parent - The data for the section this fragment is in.
62 MCSectionData *Parent;
64 /// Atom - The atom this fragment is in, as represented by it's defining
65 /// symbol. Atom's are only used by backends which set
66 /// \see MCAsmBackend::hasReliableSymbolDifference().
69 /// @name Assembler Backend Data
72 // FIXME: This could all be kept private to the assembler implementation.
74 /// Offset - The offset of this fragment in its section. This is ~0 until
78 /// LayoutOrder - The layout order of this fragment.
84 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
89 virtual ~MCFragment();
91 FragmentType getKind() const { return Kind; }
93 MCSectionData *getParent() const { return Parent; }
94 void setParent(MCSectionData *Value) { Parent = Value; }
96 MCSymbolData *getAtom() const { return Atom; }
97 void setAtom(MCSymbolData *Value) { Atom = Value; }
99 unsigned getLayoutOrder() const { return LayoutOrder; }
100 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
102 /// \brief Does this fragment have instructions emitted into it? By default
103 /// this is false, but specific fragment types may set it to true.
104 virtual bool hasInstructions() const { return false; }
106 /// \brief Should this fragment be placed at the end of an aligned bundle?
107 virtual bool alignToBundleEnd() const { return false; }
109 /// \brief Get the padding size that must be inserted before this fragment.
110 /// Used for bundling. By default, no padding is inserted.
111 /// Note that padding size is restricted to 8 bits. This is an optimization
112 /// to reduce the amount of space used for each fragment. In practice, larger
113 /// padding should never be required.
114 virtual uint8_t getBundlePadding() const {
118 /// \brief Set the padding size for this fragment. By default it's a no-op,
119 /// and only some fragments have a meaningful implementation.
120 virtual void setBundlePadding(uint8_t N) {
126 class MCEncodedFragment : public MCFragment {
127 virtual void anchor();
129 uint8_t BundlePadding;
131 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
132 : MCFragment(FType, SD), BundlePadding(0)
135 virtual ~MCEncodedFragment();
137 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
138 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
140 virtual SmallVectorImpl<char> &getContents() = 0;
141 virtual const SmallVectorImpl<char> &getContents() const = 0;
143 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
144 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
146 virtual fixup_iterator fixup_begin() = 0;
147 virtual const_fixup_iterator fixup_begin() const = 0;
148 virtual fixup_iterator fixup_end() = 0;
149 virtual const_fixup_iterator fixup_end() const = 0;
151 virtual uint8_t getBundlePadding() const {
152 return BundlePadding;
155 virtual void setBundlePadding(uint8_t N) {
159 static bool classof(const MCFragment *F) {
160 MCFragment::FragmentType Kind = F->getKind();
161 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
165 /// Fragment for data and encoded instructions.
167 class MCDataFragment : public MCEncodedFragment {
168 virtual void anchor();
170 /// \brief Does this fragment contain encoded instructions anywhere in it?
171 bool HasInstructions;
173 /// \brief Should this fragment be aligned to the end of a bundle?
174 bool AlignToBundleEnd;
176 SmallVector<char, 32> Contents;
178 /// Fixups - The list of fixups in this fragment.
179 SmallVector<MCFixup, 4> Fixups;
181 MCDataFragment(MCSectionData *SD = 0)
182 : MCEncodedFragment(FT_Data, SD),
183 HasInstructions(false), AlignToBundleEnd(false)
187 virtual SmallVectorImpl<char> &getContents() { return Contents; }
188 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
190 SmallVectorImpl<MCFixup> &getFixups() {
194 const SmallVectorImpl<MCFixup> &getFixups() const {
198 virtual bool hasInstructions() const { return HasInstructions; }
199 virtual void setHasInstructions(bool V) { HasInstructions = V; }
201 virtual bool alignToBundleEnd() const { return AlignToBundleEnd; }
202 virtual void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
204 fixup_iterator fixup_begin() { return Fixups.begin(); }
205 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
207 fixup_iterator fixup_end() {return Fixups.end();}
208 const_fixup_iterator fixup_end() const {return Fixups.end();}
210 static bool classof(const MCFragment *F) {
211 return F->getKind() == MCFragment::FT_Data;
215 /// A relaxable fragment holds on to its MCInst, since it may need to be
216 /// relaxed during the assembler layout and relaxation stage.
218 class MCRelaxableFragment : public MCEncodedFragment {
219 virtual void anchor();
221 /// Inst - The instruction this is a fragment for.
224 /// Contents - Binary data for the currently encoded instruction.
225 SmallVector<char, 8> Contents;
227 /// Fixups - The list of fixups in this fragment.
228 SmallVector<MCFixup, 1> Fixups;
231 MCRelaxableFragment(const MCInst &_Inst, MCSectionData *SD = 0)
232 : MCEncodedFragment(FT_Relaxable, SD), Inst(_Inst) {
235 virtual SmallVectorImpl<char> &getContents() { return Contents; }
236 virtual const SmallVectorImpl<char> &getContents() const { return Contents; }
238 unsigned getInstSize() const { return Contents.size(); }
239 const MCInst &getInst() const { return Inst; }
240 void setInst(const MCInst& Value) { Inst = Value; }
242 SmallVectorImpl<MCFixup> &getFixups() {
246 const SmallVectorImpl<MCFixup> &getFixups() const {
250 virtual bool hasInstructions() const { return true; }
252 fixup_iterator fixup_begin() { return Fixups.begin(); }
253 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
255 fixup_iterator fixup_end() {return Fixups.end();}
256 const_fixup_iterator fixup_end() const {return Fixups.end();}
258 static bool classof(const MCFragment *F) {
259 return F->getKind() == MCFragment::FT_Relaxable;
263 class MCAlignFragment : public MCFragment {
264 virtual void anchor();
266 /// Alignment - The alignment to ensure, in bytes.
269 /// Value - Value to use for filling padding bytes.
272 /// ValueSize - The size of the integer (in bytes) of \p Value.
275 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
276 /// cannot be satisfied in this width then this fragment is ignored.
277 unsigned MaxBytesToEmit;
279 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
280 /// of using the provided value. The exact interpretation of this flag is
281 /// target dependent.
285 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
286 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
287 : MCFragment(FT_Align, SD), Alignment(_Alignment),
288 Value(_Value),ValueSize(_ValueSize),
289 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
294 unsigned getAlignment() const { return Alignment; }
296 int64_t getValue() const { return Value; }
298 unsigned getValueSize() const { return ValueSize; }
300 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
302 bool hasEmitNops() const { return EmitNops; }
303 void setEmitNops(bool Value) { EmitNops = Value; }
307 static bool classof(const MCFragment *F) {
308 return F->getKind() == MCFragment::FT_Align;
312 class MCFillFragment : public MCFragment {
313 virtual void anchor();
315 /// Value - Value to use for filling bytes.
318 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
319 /// this is a virtual fill fragment.
322 /// Size - The number of bytes to insert.
326 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
327 MCSectionData *SD = 0)
328 : MCFragment(FT_Fill, SD),
329 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
330 assert((!ValueSize || (Size % ValueSize) == 0) &&
331 "Fill size must be a multiple of the value size!");
337 int64_t getValue() const { return Value; }
339 unsigned getValueSize() const { return ValueSize; }
341 uint64_t getSize() const { return Size; }
345 static bool classof(const MCFragment *F) {
346 return F->getKind() == MCFragment::FT_Fill;
350 class MCOrgFragment : public MCFragment {
351 virtual void anchor();
353 /// Offset - The offset this fragment should start at.
354 const MCExpr *Offset;
356 /// Value - Value to use for filling bytes.
360 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
361 : MCFragment(FT_Org, SD),
362 Offset(&_Offset), Value(_Value) {}
367 const MCExpr &getOffset() const { return *Offset; }
369 uint8_t getValue() const { return Value; }
373 static bool classof(const MCFragment *F) {
374 return F->getKind() == MCFragment::FT_Org;
378 class MCLEBFragment : public MCFragment {
379 virtual void anchor();
381 /// Value - The value this fragment should contain.
384 /// IsSigned - True if this is a sleb128, false if uleb128.
387 SmallString<8> Contents;
389 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD)
390 : MCFragment(FT_LEB, SD),
391 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
396 const MCExpr &getValue() const { return *Value; }
398 bool isSigned() const { return IsSigned; }
400 SmallString<8> &getContents() { return Contents; }
401 const SmallString<8> &getContents() const { return Contents; }
405 static bool classof(const MCFragment *F) {
406 return F->getKind() == MCFragment::FT_LEB;
410 class MCDwarfLineAddrFragment : public MCFragment {
411 virtual void anchor();
413 /// LineDelta - the value of the difference between the two line numbers
414 /// between two .loc dwarf directives.
417 /// AddrDelta - The expression for the difference of the two symbols that
418 /// make up the address delta between two .loc dwarf directives.
419 const MCExpr *AddrDelta;
421 SmallString<8> Contents;
424 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
426 : MCFragment(FT_Dwarf, SD),
427 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
432 int64_t getLineDelta() const { return LineDelta; }
434 const MCExpr &getAddrDelta() const { return *AddrDelta; }
436 SmallString<8> &getContents() { return Contents; }
437 const SmallString<8> &getContents() const { return Contents; }
441 static bool classof(const MCFragment *F) {
442 return F->getKind() == MCFragment::FT_Dwarf;
446 class MCDwarfCallFrameFragment : public MCFragment {
447 virtual void anchor();
449 /// AddrDelta - The expression for the difference of the two symbols that
450 /// make up the address delta between two .cfi_* dwarf directives.
451 const MCExpr *AddrDelta;
453 SmallString<8> Contents;
456 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD)
457 : MCFragment(FT_DwarfFrame, SD),
458 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
463 const MCExpr &getAddrDelta() const { return *AddrDelta; }
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_DwarfFrame;
475 // FIXME: Should this be a separate class, or just merged into MCSection? Since
476 // we anticipate the fast path being through an MCAssembler, the only reason to
477 // keep it out is for API abstraction.
478 class MCSectionData : public ilist_node<MCSectionData> {
479 friend class MCAsmLayout;
481 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
482 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
485 typedef iplist<MCFragment> FragmentListType;
487 typedef FragmentListType::const_iterator const_iterator;
488 typedef FragmentListType::iterator iterator;
490 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
491 typedef FragmentListType::reverse_iterator reverse_iterator;
493 /// \brief Express the state of bundle locked groups while emitting code.
494 enum BundleLockStateType {
497 BundleLockedAlignToEnd
500 FragmentListType Fragments;
501 const MCSection *Section;
503 /// Ordinal - The section index in the assemblers section list.
506 /// LayoutOrder - The index of this section in the layout order.
507 unsigned LayoutOrder;
509 /// Alignment - The maximum alignment seen in this section.
512 /// \brief Keeping track of bundle-locked state.
513 BundleLockStateType BundleLockState;
515 /// \brief We've seen a bundle_lock directive but not its first instruction
517 bool BundleGroupBeforeFirstInst;
519 /// @name Assembler Backend Data
522 // FIXME: This could all be kept private to the assembler implementation.
524 /// HasInstructions - Whether this section has had instructions emitted into
526 unsigned HasInstructions : 1;
531 // Only for use as sentinel.
533 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
535 const MCSection &getSection() const { return *Section; }
537 unsigned getAlignment() const { return Alignment; }
538 void setAlignment(unsigned Value) { Alignment = Value; }
540 bool hasInstructions() const { return HasInstructions; }
541 void setHasInstructions(bool Value) { HasInstructions = Value; }
543 unsigned getOrdinal() const { return Ordinal; }
544 void setOrdinal(unsigned Value) { Ordinal = Value; }
546 unsigned getLayoutOrder() const { return LayoutOrder; }
547 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
549 /// @name Fragment Access
552 const FragmentListType &getFragmentList() const { return Fragments; }
553 FragmentListType &getFragmentList() { return Fragments; }
555 iterator begin() { return Fragments.begin(); }
556 const_iterator begin() const { return Fragments.begin(); }
558 iterator end() { return Fragments.end(); }
559 const_iterator end() const { return Fragments.end(); }
561 reverse_iterator rbegin() { return Fragments.rbegin(); }
562 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
564 reverse_iterator rend() { return Fragments.rend(); }
565 const_reverse_iterator rend() const { return Fragments.rend(); }
567 size_t size() const { return Fragments.size(); }
569 bool empty() const { return Fragments.empty(); }
571 bool isBundleLocked() const {
572 return BundleLockState != NotBundleLocked;
575 BundleLockStateType getBundleLockState() const {
576 return BundleLockState;
579 void setBundleLockState(BundleLockStateType NewState) {
580 BundleLockState = NewState;
583 bool isBundleGroupBeforeFirstInst() const {
584 return BundleGroupBeforeFirstInst;
587 void setBundleGroupBeforeFirstInst(bool IsFirst) {
588 BundleGroupBeforeFirstInst = IsFirst;
596 // FIXME: Same concerns as with SectionData.
597 class MCSymbolData : public ilist_node<MCSymbolData> {
599 const MCSymbol *Symbol;
601 /// Fragment - The fragment this symbol's value is relative to, if any.
602 MCFragment *Fragment;
604 /// Offset - The offset to apply to the fragment address to form this symbol's
608 /// IsExternal - True if this symbol is visible outside this translation
610 unsigned IsExternal : 1;
612 /// IsPrivateExtern - True if this symbol is private extern.
613 unsigned IsPrivateExtern : 1;
615 /// CommonSize - The size of the symbol, if it is 'common', or 0.
617 // FIXME: Pack this in with other fields? We could put it in offset, since a
618 // common symbol can never get a definition.
621 /// SymbolSize - An expression describing how to calculate the size of
622 /// a symbol. If a symbol has no size this field will be NULL.
623 const MCExpr *SymbolSize;
625 /// CommonAlign - The alignment of the symbol, if it is 'common'.
627 // FIXME: Pack this in with other fields?
628 unsigned CommonAlign;
630 /// Flags - The Flags field is used by object file implementations to store
631 /// additional per symbol information which is not easily classified.
634 /// Index - Index field, for use by the object file implementation.
638 // Only for use as sentinel.
640 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
646 const MCSymbol &getSymbol() const { return *Symbol; }
648 MCFragment *getFragment() const { return Fragment; }
649 void setFragment(MCFragment *Value) { Fragment = Value; }
651 uint64_t getOffset() const { return Offset; }
652 void setOffset(uint64_t Value) { Offset = Value; }
655 /// @name Symbol Attributes
658 bool isExternal() const { return IsExternal; }
659 void setExternal(bool Value) { IsExternal = Value; }
661 bool isPrivateExtern() const { return IsPrivateExtern; }
662 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
664 /// isCommon - Is this a 'common' symbol.
665 bool isCommon() const { return CommonSize != 0; }
667 /// setCommon - Mark this symbol as being 'common'.
669 /// \param Size - The size of the symbol.
670 /// \param Align - The alignment of the symbol.
671 void setCommon(uint64_t Size, unsigned Align) {
676 /// getCommonSize - Return the size of a 'common' symbol.
677 uint64_t getCommonSize() const {
678 assert(isCommon() && "Not a 'common' symbol!");
682 void setSize(const MCExpr *SS) {
686 const MCExpr *getSize() const {
691 /// getCommonAlignment - Return the alignment of a 'common' symbol.
692 unsigned getCommonAlignment() const {
693 assert(isCommon() && "Not a 'common' symbol!");
697 /// getFlags - Get the (implementation defined) symbol flags.
698 uint32_t getFlags() const { return Flags; }
700 /// setFlags - Set the (implementation defined) symbol flags.
701 void setFlags(uint32_t Value) { Flags = Value; }
703 /// modifyFlags - Modify the flags via a mask
704 void modifyFlags(uint32_t Value, uint32_t Mask) {
705 Flags = (Flags & ~Mask) | Value;
708 /// getIndex - Get the (implementation defined) index.
709 uint64_t getIndex() const { return Index; }
711 /// setIndex - Set the (implementation defined) index.
712 void setIndex(uint64_t Value) { Index = Value; }
719 // FIXME: This really doesn't belong here. See comments below.
720 struct IndirectSymbolData {
722 MCSectionData *SectionData;
725 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
727 struct DataRegionData {
728 // This enum should be kept in sync w/ the mach-o definition in
729 // llvm/Object/MachOFormat.h.
730 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
736 friend class MCAsmLayout;
739 typedef iplist<MCSectionData> SectionDataListType;
740 typedef iplist<MCSymbolData> SymbolDataListType;
742 typedef SectionDataListType::const_iterator const_iterator;
743 typedef SectionDataListType::iterator iterator;
745 typedef SymbolDataListType::const_iterator const_symbol_iterator;
746 typedef SymbolDataListType::iterator symbol_iterator;
748 typedef std::vector<IndirectSymbolData>::const_iterator
749 const_indirect_symbol_iterator;
750 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
752 typedef std::vector<DataRegionData>::const_iterator
753 const_data_region_iterator;
754 typedef std::vector<DataRegionData>::iterator data_region_iterator;
757 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
758 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
762 MCAsmBackend &Backend;
764 MCCodeEmitter &Emitter;
766 MCObjectWriter &Writer;
770 iplist<MCSectionData> Sections;
772 iplist<MCSymbolData> Symbols;
774 /// The map of sections to their associated assembler backend data.
776 // FIXME: Avoid this indirection?
777 DenseMap<const MCSection*, MCSectionData*> SectionMap;
779 /// The map of symbols to their associated assembler backend data.
781 // FIXME: Avoid this indirection?
782 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
784 std::vector<IndirectSymbolData> IndirectSymbols;
786 std::vector<DataRegionData> DataRegions;
787 /// The set of function symbols for which a .thumb_func directive has
790 // FIXME: We really would like this in target specific code rather than
791 // here. Maybe when the relocation stuff moves to target specific,
792 // this can go with it? The streamer would need some target specific
794 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
796 /// \brief The bundle alignment size currently set in the assembler.
798 /// By default it's 0, which means bundling is disabled.
799 unsigned BundleAlignSize;
801 unsigned RelaxAll : 1;
802 unsigned NoExecStack : 1;
803 unsigned SubsectionsViaSymbols : 1;
806 /// Evaluate a fixup to a relocatable expression and the value which should be
807 /// placed into the fixup.
809 /// \param Layout The layout to use for evaluation.
810 /// \param Fixup The fixup to evaluate.
811 /// \param DF The fragment the fixup is inside.
812 /// \param Target [out] On return, the relocatable expression the fixup
814 /// \param Value [out] On return, the value of the fixup as currently laid
816 /// \return Whether the fixup value was fully resolved. This is true if the
817 /// \p Value result is fixed, otherwise the value may change due to
819 bool evaluateFixup(const MCAsmLayout &Layout,
820 const MCFixup &Fixup, const MCFragment *DF,
821 MCValue &Target, uint64_t &Value) const;
823 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
824 /// (increased in size, in order to hold its value correctly).
825 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
826 const MCAsmLayout &Layout) const;
828 /// Check whether the given fragment needs relaxation.
829 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
830 const MCAsmLayout &Layout) const;
832 /// \brief Perform one layout iteration and return true if any offsets
834 bool layoutOnce(MCAsmLayout &Layout);
836 /// \brief Perform one layout iteration of the given section and return true
837 /// if any offsets were adjusted.
838 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
840 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
842 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
844 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
845 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
846 MCDwarfCallFrameFragment &DF);
848 /// finishLayout - Finalize a layout, including fragment lowering.
849 void finishLayout(MCAsmLayout &Layout);
851 uint64_t handleFixup(const MCAsmLayout &Layout,
852 MCFragment &F, const MCFixup &Fixup);
855 /// Compute the effective fragment size assuming it is laid out at the given
856 /// \p SectionAddress and \p FragmentOffset.
857 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
858 const MCFragment &F) const;
860 /// Find the symbol which defines the atom containing the given symbol, or
861 /// null if there is no such symbol.
862 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
864 /// Check whether a particular symbol is visible to the linker and is required
865 /// in the symbol table, or whether it can be discarded by the assembler. This
866 /// also effects whether the assembler treats the label as potentially
867 /// defining a separate atom.
868 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
870 /// Emit the section contents using the given object writer.
871 void writeSectionData(const MCSectionData *Section,
872 const MCAsmLayout &Layout) const;
874 /// Check whether a given symbol has been flagged with .thumb_func.
875 bool isThumbFunc(const MCSymbol *Func) const {
876 return ThumbFuncs.count(Func);
879 /// Flag a function symbol as the target of a .thumb_func directive.
880 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
883 /// Construct a new assembler instance.
885 /// \param OS The stream to output to.
887 // FIXME: How are we going to parameterize this? Two obvious options are stay
888 // concrete and require clients to pass in a target like object. The other
889 // option is to make this abstract, and have targets provide concrete
890 // implementations as we do with AsmParser.
891 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
892 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
896 /// Reuse an assembler instance
900 MCContext &getContext() const { return Context; }
902 MCAsmBackend &getBackend() const { return Backend; }
904 MCCodeEmitter &getEmitter() const { return Emitter; }
906 MCObjectWriter &getWriter() const { return Writer; }
908 /// Finish - Do final processing and write the object to the output stream.
909 /// \p Writer is used for custom object writer (as the MCJIT does),
910 /// if not specified it is automatically created from backend.
913 // FIXME: This does not belong here.
914 bool getSubsectionsViaSymbols() const {
915 return SubsectionsViaSymbols;
917 void setSubsectionsViaSymbols(bool Value) {
918 SubsectionsViaSymbols = Value;
921 bool getRelaxAll() const { return RelaxAll; }
922 void setRelaxAll(bool Value) { RelaxAll = Value; }
924 bool getNoExecStack() const { return NoExecStack; }
925 void setNoExecStack(bool Value) { NoExecStack = Value; }
927 bool isBundlingEnabled() const {
928 return BundleAlignSize != 0;
931 unsigned getBundleAlignSize() const {
932 return BundleAlignSize;
935 void setBundleAlignSize(unsigned Size) {
936 assert((Size == 0 || !(Size & (Size - 1))) &&
937 "Expect a power-of-two bundle align size");
938 BundleAlignSize = Size;
941 /// @name Section List Access
944 const SectionDataListType &getSectionList() const { return Sections; }
945 SectionDataListType &getSectionList() { return Sections; }
947 iterator begin() { return Sections.begin(); }
948 const_iterator begin() const { return Sections.begin(); }
950 iterator end() { return Sections.end(); }
951 const_iterator end() const { return Sections.end(); }
953 size_t size() const { return Sections.size(); }
956 /// @name Symbol List Access
959 const SymbolDataListType &getSymbolList() const { return Symbols; }
960 SymbolDataListType &getSymbolList() { return Symbols; }
962 symbol_iterator symbol_begin() { return Symbols.begin(); }
963 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
965 symbol_iterator symbol_end() { return Symbols.end(); }
966 const_symbol_iterator symbol_end() const { return Symbols.end(); }
968 size_t symbol_size() const { return Symbols.size(); }
971 /// @name Indirect Symbol List Access
974 // FIXME: This is a total hack, this should not be here. Once things are
975 // factored so that the streamer has direct access to the .o writer, it can
977 std::vector<IndirectSymbolData> &getIndirectSymbols() {
978 return IndirectSymbols;
981 indirect_symbol_iterator indirect_symbol_begin() {
982 return IndirectSymbols.begin();
984 const_indirect_symbol_iterator indirect_symbol_begin() const {
985 return IndirectSymbols.begin();
988 indirect_symbol_iterator indirect_symbol_end() {
989 return IndirectSymbols.end();
991 const_indirect_symbol_iterator indirect_symbol_end() const {
992 return IndirectSymbols.end();
995 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
998 /// @name Data Region List Access
1001 // FIXME: This is a total hack, this should not be here. Once things are
1002 // factored so that the streamer has direct access to the .o writer, it can
1004 std::vector<DataRegionData> &getDataRegions() {
1008 data_region_iterator data_region_begin() {
1009 return DataRegions.begin();
1011 const_data_region_iterator data_region_begin() const {
1012 return DataRegions.begin();
1015 data_region_iterator data_region_end() {
1016 return DataRegions.end();
1018 const_data_region_iterator data_region_end() const {
1019 return DataRegions.end();
1022 size_t data_region_size() const { return DataRegions.size(); }
1025 /// @name Backend Data Access
1028 MCSectionData &getSectionData(const MCSection &Section) const {
1029 MCSectionData *Entry = SectionMap.lookup(&Section);
1030 assert(Entry && "Missing section data!");
1034 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1035 bool *Created = 0) {
1036 MCSectionData *&Entry = SectionMap[&Section];
1038 if (Created) *Created = !Entry;
1040 Entry = new MCSectionData(Section, this);
1045 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1046 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1047 assert(Entry && "Missing symbol data!");
1051 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1052 bool *Created = 0) {
1053 MCSymbolData *&Entry = SymbolMap[&Symbol];
1055 if (Created) *Created = !Entry;
1057 Entry = new MCSymbolData(Symbol, 0, 0, this);
1067 } // end namespace llvm