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/MCDirectives.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/DataTypes.h"
25 #include <vector> // FIXME: Shouldn't be needed.
38 class MCSubtargetInfo;
44 class MCFragment : public ilist_node<MCFragment> {
45 friend class MCAsmLayout;
47 MCFragment(const MCFragment&) LLVM_DELETED_FUNCTION;
48 void operator=(const MCFragment&) LLVM_DELETED_FUNCTION;
55 FT_CompactEncodedInst,
67 /// Parent - The data for the section this fragment is in.
68 MCSectionData *Parent;
70 /// Atom - The atom this fragment is in, as represented by it's defining
74 /// @name Assembler Backend Data
77 // FIXME: This could all be kept private to the assembler implementation.
79 /// Offset - The offset of this fragment in its section. This is ~0 until
83 /// LayoutOrder - The layout order of this fragment.
89 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
94 virtual ~MCFragment();
96 FragmentType getKind() const { return Kind; }
98 MCSectionData *getParent() const { return Parent; }
99 void setParent(MCSectionData *Value) { Parent = Value; }
101 MCSymbolData *getAtom() const { return Atom; }
102 void setAtom(MCSymbolData *Value) { Atom = Value; }
104 unsigned getLayoutOrder() const { return LayoutOrder; }
105 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
107 /// \brief Does this fragment have instructions emitted into it? By default
108 /// this is false, but specific fragment types may set it to true.
109 virtual bool hasInstructions() const { return false; }
111 /// \brief Should this fragment be placed at the end of an aligned bundle?
112 virtual bool alignToBundleEnd() const { return false; }
113 virtual void setAlignToBundleEnd(bool V) { }
115 /// \brief Get the padding size that must be inserted before this fragment.
116 /// Used for bundling. By default, no padding is inserted.
117 /// Note that padding size is restricted to 8 bits. This is an optimization
118 /// to reduce the amount of space used for each fragment. In practice, larger
119 /// padding should never be required.
120 virtual uint8_t getBundlePadding() const {
124 /// \brief Set the padding size for this fragment. By default it's a no-op,
125 /// and only some fragments have a meaningful implementation.
126 virtual void setBundlePadding(uint8_t N) {
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 uint8_t getBundlePadding() const override {
150 return BundlePadding;
153 void setBundlePadding(uint8_t N) override {
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_Compressed:
165 case MCFragment::FT_Data:
171 /// Interface implemented by fragments that contain encoded instructions and/or
172 /// data and also have fixups registered.
174 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
175 void anchor() override;
178 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
179 MCSectionData *SD = 0)
180 : MCEncodedFragment(FType, SD)
184 virtual ~MCEncodedFragmentWithFixups();
186 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
187 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
189 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
190 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
192 virtual fixup_iterator fixup_begin() = 0;
193 virtual const_fixup_iterator fixup_begin() const = 0;
194 virtual fixup_iterator fixup_end() = 0;
195 virtual const_fixup_iterator fixup_end() const = 0;
197 static bool classof(const MCFragment *F) {
198 MCFragment::FragmentType Kind = F->getKind();
199 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data ||
200 Kind == MCFragment::FT_Compressed;
204 /// Fragment for data and encoded instructions.
206 class MCDataFragment : public MCEncodedFragmentWithFixups {
207 void anchor() override;
209 /// \brief Does this fragment contain encoded instructions anywhere in it?
210 bool HasInstructions;
212 /// \brief Should this fragment be aligned to the end of a bundle?
213 bool AlignToBundleEnd;
215 SmallVector<char, 32> Contents;
217 /// Fixups - The list of fixups in this fragment.
218 SmallVector<MCFixup, 4> Fixups;
220 MCDataFragment(MCFragment::FragmentType FType, MCSectionData *SD = 0)
221 : MCEncodedFragmentWithFixups(FType, SD), HasInstructions(false),
222 AlignToBundleEnd(false) {}
225 MCDataFragment(MCSectionData *SD = 0)
226 : MCEncodedFragmentWithFixups(FT_Data, SD),
227 HasInstructions(false), AlignToBundleEnd(false)
231 SmallVectorImpl<char> &getContents() override { return Contents; }
232 const SmallVectorImpl<char> &getContents() const override {
236 SmallVectorImpl<MCFixup> &getFixups() override {
240 const SmallVectorImpl<MCFixup> &getFixups() const override {
244 bool hasInstructions() const override { return HasInstructions; }
245 virtual void setHasInstructions(bool V) { HasInstructions = V; }
247 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
248 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
250 fixup_iterator fixup_begin() override { return Fixups.begin(); }
251 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
253 fixup_iterator fixup_end() override {return Fixups.end();}
254 const_fixup_iterator fixup_end() const override {return Fixups.end();}
256 static bool classof(const MCFragment *F) {
257 return F->getKind() == MCFragment::FT_Data ||
258 F->getKind() == MCFragment::FT_Compressed;
262 class MCCompressedFragment: public MCDataFragment {
263 mutable SmallVector<char, 32> CompressedContents;
265 MCCompressedFragment(MCSectionData *SD = nullptr)
266 : MCDataFragment(FT_Compressed, SD) {}
267 const SmallVectorImpl<char> &getCompressedContents() const;
268 using MCDataFragment::getContents;
269 SmallVectorImpl<char> &getContents() override;
272 /// This is a compact (memory-size-wise) fragment for holding an encoded
273 /// instruction (non-relaxable) that has no fixups registered. When applicable,
274 /// it can be used instead of MCDataFragment and lead to lower memory
277 class MCCompactEncodedInstFragment : public MCEncodedFragment {
278 void anchor() override;
280 /// \brief Should this fragment be aligned to the end of a bundle?
281 bool AlignToBundleEnd;
283 SmallVector<char, 4> Contents;
285 MCCompactEncodedInstFragment(MCSectionData *SD = 0)
286 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
290 bool hasInstructions() const override {
294 SmallVectorImpl<char> &getContents() override { return Contents; }
295 const SmallVectorImpl<char> &getContents() const override { return Contents; }
297 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
298 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
300 static bool classof(const MCFragment *F) {
301 return F->getKind() == MCFragment::FT_CompactEncodedInst;
305 /// A relaxable fragment holds on to its MCInst, since it may need to be
306 /// relaxed during the assembler layout and relaxation stage.
308 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
309 void anchor() override;
311 /// Inst - The instruction this is a fragment for.
314 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
315 /// Keep a copy instead of a reference to make sure that updates to STI
316 /// in the assembler are not seen here.
317 const MCSubtargetInfo STI;
319 /// Contents - Binary data for the currently encoded instruction.
320 SmallVector<char, 8> Contents;
322 /// Fixups - The list of fixups in this fragment.
323 SmallVector<MCFixup, 1> Fixups;
326 MCRelaxableFragment(const MCInst &_Inst,
327 const MCSubtargetInfo &_STI,
328 MCSectionData *SD = 0)
329 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(_Inst), STI(_STI) {
332 SmallVectorImpl<char> &getContents() override { return Contents; }
333 const SmallVectorImpl<char> &getContents() const override { return Contents; }
335 const MCInst &getInst() const { return Inst; }
336 void setInst(const MCInst& Value) { Inst = Value; }
338 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
340 SmallVectorImpl<MCFixup> &getFixups() override {
344 const SmallVectorImpl<MCFixup> &getFixups() const override {
348 bool hasInstructions() const override { return true; }
350 fixup_iterator fixup_begin() override { return Fixups.begin(); }
351 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
353 fixup_iterator fixup_end() override {return Fixups.end();}
354 const_fixup_iterator fixup_end() const override {return Fixups.end();}
356 static bool classof(const MCFragment *F) {
357 return F->getKind() == MCFragment::FT_Relaxable;
361 class MCAlignFragment : public MCFragment {
362 virtual void anchor();
364 /// Alignment - The alignment to ensure, in bytes.
367 /// Value - Value to use for filling padding bytes.
370 /// ValueSize - The size of the integer (in bytes) of \p Value.
373 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
374 /// cannot be satisfied in this width then this fragment is ignored.
375 unsigned MaxBytesToEmit;
377 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
378 /// of using the provided value. The exact interpretation of this flag is
379 /// target dependent.
383 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
384 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
385 : MCFragment(FT_Align, SD), Alignment(_Alignment),
386 Value(_Value),ValueSize(_ValueSize),
387 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
392 unsigned getAlignment() const { return Alignment; }
394 int64_t getValue() const { return Value; }
396 unsigned getValueSize() const { return ValueSize; }
398 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
400 bool hasEmitNops() const { return EmitNops; }
401 void setEmitNops(bool Value) { EmitNops = Value; }
405 static bool classof(const MCFragment *F) {
406 return F->getKind() == MCFragment::FT_Align;
410 class MCFillFragment : public MCFragment {
411 virtual void anchor();
413 /// Value - Value to use for filling bytes.
416 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
417 /// this is a virtual fill fragment.
420 /// Size - The number of bytes to insert.
424 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
425 MCSectionData *SD = 0)
426 : MCFragment(FT_Fill, SD),
427 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
428 assert((!ValueSize || (Size % ValueSize) == 0) &&
429 "Fill size must be a multiple of the value size!");
435 int64_t getValue() const { return Value; }
437 unsigned getValueSize() const { return ValueSize; }
439 uint64_t getSize() const { return Size; }
443 static bool classof(const MCFragment *F) {
444 return F->getKind() == MCFragment::FT_Fill;
448 class MCOrgFragment : public MCFragment {
449 virtual void anchor();
451 /// Offset - The offset this fragment should start at.
452 const MCExpr *Offset;
454 /// Value - Value to use for filling bytes.
458 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
459 : MCFragment(FT_Org, SD),
460 Offset(&_Offset), Value(_Value) {}
465 const MCExpr &getOffset() const { return *Offset; }
467 uint8_t getValue() const { return Value; }
471 static bool classof(const MCFragment *F) {
472 return F->getKind() == MCFragment::FT_Org;
476 class MCLEBFragment : public MCFragment {
477 virtual void anchor();
479 /// Value - The value this fragment should contain.
482 /// IsSigned - True if this is a sleb128, false if uleb128.
485 SmallString<8> Contents;
487 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD = 0)
488 : MCFragment(FT_LEB, SD),
489 Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
494 const MCExpr &getValue() const { return *Value; }
496 bool isSigned() const { return IsSigned; }
498 SmallString<8> &getContents() { return Contents; }
499 const SmallString<8> &getContents() const { return Contents; }
503 static bool classof(const MCFragment *F) {
504 return F->getKind() == MCFragment::FT_LEB;
508 class MCDwarfLineAddrFragment : public MCFragment {
509 virtual void anchor();
511 /// LineDelta - the value of the difference between the two line numbers
512 /// between two .loc dwarf directives.
515 /// AddrDelta - The expression for the difference of the two symbols that
516 /// make up the address delta between two .loc dwarf directives.
517 const MCExpr *AddrDelta;
519 SmallString<8> Contents;
522 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
523 MCSectionData *SD = 0)
524 : MCFragment(FT_Dwarf, SD),
525 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
530 int64_t getLineDelta() const { return LineDelta; }
532 const MCExpr &getAddrDelta() const { return *AddrDelta; }
534 SmallString<8> &getContents() { return Contents; }
535 const SmallString<8> &getContents() const { return Contents; }
539 static bool classof(const MCFragment *F) {
540 return F->getKind() == MCFragment::FT_Dwarf;
544 class MCDwarfCallFrameFragment : public MCFragment {
545 virtual void anchor();
547 /// AddrDelta - The expression for the difference of the two symbols that
548 /// make up the address delta between two .cfi_* dwarf directives.
549 const MCExpr *AddrDelta;
551 SmallString<8> Contents;
554 MCDwarfCallFrameFragment(const MCExpr &_AddrDelta, MCSectionData *SD = 0)
555 : MCFragment(FT_DwarfFrame, SD),
556 AddrDelta(&_AddrDelta) { Contents.push_back(0); }
561 const MCExpr &getAddrDelta() const { return *AddrDelta; }
563 SmallString<8> &getContents() { return Contents; }
564 const SmallString<8> &getContents() const { return Contents; }
568 static bool classof(const MCFragment *F) {
569 return F->getKind() == MCFragment::FT_DwarfFrame;
573 // FIXME: Should this be a separate class, or just merged into MCSection? Since
574 // we anticipate the fast path being through an MCAssembler, the only reason to
575 // keep it out is for API abstraction.
576 class MCSectionData : public ilist_node<MCSectionData> {
577 friend class MCAsmLayout;
579 MCSectionData(const MCSectionData&) LLVM_DELETED_FUNCTION;
580 void operator=(const MCSectionData&) LLVM_DELETED_FUNCTION;
583 typedef iplist<MCFragment> FragmentListType;
585 typedef FragmentListType::const_iterator const_iterator;
586 typedef FragmentListType::iterator iterator;
588 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
589 typedef FragmentListType::reverse_iterator reverse_iterator;
591 /// \brief Express the state of bundle locked groups while emitting code.
592 enum BundleLockStateType {
595 BundleLockedAlignToEnd
598 FragmentListType Fragments;
599 const MCSection *Section;
601 /// Ordinal - The section index in the assemblers section list.
604 /// LayoutOrder - The index of this section in the layout order.
605 unsigned LayoutOrder;
607 /// Alignment - The maximum alignment seen in this section.
610 /// \brief Keeping track of bundle-locked state.
611 BundleLockStateType BundleLockState;
613 /// \brief We've seen a bundle_lock directive but not its first instruction
615 bool BundleGroupBeforeFirstInst;
617 /// @name Assembler Backend Data
620 // FIXME: This could all be kept private to the assembler implementation.
622 /// HasInstructions - Whether this section has had instructions emitted into
624 unsigned HasInstructions : 1;
626 /// Mapping from subsection number to insertion point for subsection numbers
627 /// below that number.
628 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
633 // Only for use as sentinel.
635 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
637 const MCSection &getSection() const { return *Section; }
639 unsigned getAlignment() const { return Alignment; }
640 void setAlignment(unsigned Value) { Alignment = Value; }
642 bool hasInstructions() const { return HasInstructions; }
643 void setHasInstructions(bool Value) { HasInstructions = Value; }
645 unsigned getOrdinal() const { return Ordinal; }
646 void setOrdinal(unsigned Value) { Ordinal = Value; }
648 unsigned getLayoutOrder() const { return LayoutOrder; }
649 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
651 /// @name Fragment Access
654 const FragmentListType &getFragmentList() const { return Fragments; }
655 FragmentListType &getFragmentList() { return Fragments; }
657 iterator begin() { return Fragments.begin(); }
658 const_iterator begin() const { return Fragments.begin(); }
660 iterator end() { return Fragments.end(); }
661 const_iterator end() const { return Fragments.end(); }
663 reverse_iterator rbegin() { return Fragments.rbegin(); }
664 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
666 reverse_iterator rend() { return Fragments.rend(); }
667 const_reverse_iterator rend() const { return Fragments.rend(); }
669 size_t size() const { return Fragments.size(); }
671 bool empty() const { return Fragments.empty(); }
673 iterator getSubsectionInsertionPoint(unsigned Subsection);
675 bool isBundleLocked() const {
676 return BundleLockState != NotBundleLocked;
679 BundleLockStateType getBundleLockState() const {
680 return BundleLockState;
683 void setBundleLockState(BundleLockStateType NewState) {
684 BundleLockState = NewState;
687 bool isBundleGroupBeforeFirstInst() const {
688 return BundleGroupBeforeFirstInst;
691 void setBundleGroupBeforeFirstInst(bool IsFirst) {
692 BundleGroupBeforeFirstInst = IsFirst;
700 // FIXME: Same concerns as with SectionData.
701 class MCSymbolData : public ilist_node<MCSymbolData> {
703 const MCSymbol *Symbol;
705 /// Fragment - The fragment this symbol's value is relative to, if any.
706 MCFragment *Fragment;
708 /// Offset - The offset to apply to the fragment address to form this symbol's
712 /// IsExternal - True if this symbol is visible outside this translation
714 unsigned IsExternal : 1;
716 /// IsPrivateExtern - True if this symbol is private extern.
717 unsigned IsPrivateExtern : 1;
719 /// CommonSize - The size of the symbol, if it is 'common', or 0.
721 // FIXME: Pack this in with other fields? We could put it in offset, since a
722 // common symbol can never get a definition.
725 /// SymbolSize - An expression describing how to calculate the size of
726 /// a symbol. If a symbol has no size this field will be NULL.
727 const MCExpr *SymbolSize;
729 /// CommonAlign - The alignment of the symbol, if it is 'common'.
731 // FIXME: Pack this in with other fields?
732 unsigned CommonAlign;
734 /// Flags - The Flags field is used by object file implementations to store
735 /// additional per symbol information which is not easily classified.
738 /// Index - Index field, for use by the object file implementation.
742 // Only for use as sentinel.
744 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
750 const MCSymbol &getSymbol() const { return *Symbol; }
752 MCFragment *getFragment() const { return Fragment; }
753 void setFragment(MCFragment *Value) { Fragment = Value; }
755 uint64_t getOffset() const { return Offset; }
756 void setOffset(uint64_t Value) { Offset = Value; }
759 /// @name Symbol Attributes
762 bool isExternal() const { return IsExternal; }
763 void setExternal(bool Value) { IsExternal = Value; }
765 bool isPrivateExtern() const { return IsPrivateExtern; }
766 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
768 /// isCommon - Is this a 'common' symbol.
769 bool isCommon() const { return CommonSize != 0; }
771 /// setCommon - Mark this symbol as being 'common'.
773 /// \param Size - The size of the symbol.
774 /// \param Align - The alignment of the symbol.
775 void setCommon(uint64_t Size, unsigned Align) {
780 /// getCommonSize - Return the size of a 'common' symbol.
781 uint64_t getCommonSize() const {
782 assert(isCommon() && "Not a 'common' symbol!");
786 void setSize(const MCExpr *SS) {
790 const MCExpr *getSize() const {
795 /// getCommonAlignment - Return the alignment of a 'common' symbol.
796 unsigned getCommonAlignment() const {
797 assert(isCommon() && "Not a 'common' symbol!");
801 /// getFlags - Get the (implementation defined) symbol flags.
802 uint32_t getFlags() const { return Flags; }
804 /// setFlags - Set the (implementation defined) symbol flags.
805 void setFlags(uint32_t Value) { Flags = Value; }
807 /// modifyFlags - Modify the flags via a mask
808 void modifyFlags(uint32_t Value, uint32_t Mask) {
809 Flags = (Flags & ~Mask) | Value;
812 /// getIndex - Get the (implementation defined) index.
813 uint64_t getIndex() const { return Index; }
815 /// setIndex - Set the (implementation defined) index.
816 void setIndex(uint64_t Value) { Index = Value; }
823 // FIXME: This really doesn't belong here. See comments below.
824 struct IndirectSymbolData {
826 MCSectionData *SectionData;
829 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
831 struct DataRegionData {
832 // This enum should be kept in sync w/ the mach-o definition in
833 // llvm/Object/MachOFormat.h.
834 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
840 friend class MCAsmLayout;
843 typedef iplist<MCSectionData> SectionDataListType;
844 typedef iplist<MCSymbolData> SymbolDataListType;
846 typedef SectionDataListType::const_iterator const_iterator;
847 typedef SectionDataListType::iterator iterator;
849 typedef SymbolDataListType::const_iterator const_symbol_iterator;
850 typedef SymbolDataListType::iterator symbol_iterator;
852 typedef std::vector<std::string> FileNameVectorType;
853 typedef FileNameVectorType::const_iterator const_file_name_iterator;
855 typedef std::vector<IndirectSymbolData>::const_iterator
856 const_indirect_symbol_iterator;
857 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
859 typedef std::vector<DataRegionData>::const_iterator
860 const_data_region_iterator;
861 typedef std::vector<DataRegionData>::iterator data_region_iterator;
863 /// MachO specific deployment target version info.
864 // A Major version of 0 indicates that no version information was supplied
865 // and so the corresponding load command should not be emitted.
867 MCVersionMinType Kind;
871 } VersionMinInfoType;
873 MCAssembler(const MCAssembler&) LLVM_DELETED_FUNCTION;
874 void operator=(const MCAssembler&) LLVM_DELETED_FUNCTION;
878 MCAsmBackend &Backend;
880 MCCodeEmitter &Emitter;
882 MCObjectWriter &Writer;
886 iplist<MCSectionData> Sections;
888 iplist<MCSymbolData> Symbols;
890 /// The map of sections to their associated assembler backend data.
892 // FIXME: Avoid this indirection?
893 DenseMap<const MCSection*, MCSectionData*> SectionMap;
895 /// The map of symbols to their associated assembler backend data.
897 // FIXME: Avoid this indirection?
898 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
900 std::vector<IndirectSymbolData> IndirectSymbols;
902 std::vector<DataRegionData> DataRegions;
904 /// The list of linker options to propagate into the object file.
905 std::vector<std::vector<std::string> > LinkerOptions;
907 /// List of declared file names
908 FileNameVectorType FileNames;
910 /// The set of function symbols for which a .thumb_func directive has
913 // FIXME: We really would like this in target specific code rather than
914 // here. Maybe when the relocation stuff moves to target specific,
915 // this can go with it? The streamer would need some target specific
917 SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
919 /// \brief The bundle alignment size currently set in the assembler.
921 /// By default it's 0, which means bundling is disabled.
922 unsigned BundleAlignSize;
924 unsigned RelaxAll : 1;
925 unsigned NoExecStack : 1;
926 unsigned SubsectionsViaSymbols : 1;
928 /// ELF specific e_header flags
929 // It would be good if there were an MCELFAssembler class to hold this.
930 // ELF header flags are used both by the integrated and standalone assemblers.
931 // Access to the flags is necessary in cases where assembler directives affect
932 // which flags to be set.
933 unsigned ELFHeaderEFlags;
935 VersionMinInfoType VersionMinInfo;
937 /// Evaluate a fixup to a relocatable expression and the value which should be
938 /// placed into the fixup.
940 /// \param Layout The layout to use for evaluation.
941 /// \param Fixup The fixup to evaluate.
942 /// \param DF The fragment the fixup is inside.
943 /// \param Target [out] On return, the relocatable expression the fixup
945 /// \param Value [out] On return, the value of the fixup as currently laid
947 /// \return Whether the fixup value was fully resolved. This is true if the
948 /// \p Value result is fixed, otherwise the value may change due to
950 bool evaluateFixup(const MCAsmLayout &Layout,
951 const MCFixup &Fixup, const MCFragment *DF,
952 MCValue &Target, uint64_t &Value) const;
954 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
955 /// (increased in size, in order to hold its value correctly).
956 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
957 const MCAsmLayout &Layout) const;
959 /// Check whether the given fragment needs relaxation.
960 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
961 const MCAsmLayout &Layout) const;
963 /// \brief Perform one layout iteration and return true if any offsets
965 bool layoutOnce(MCAsmLayout &Layout);
967 /// \brief Perform one layout iteration of the given section and return true
968 /// if any offsets were adjusted.
969 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
971 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
973 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
975 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
976 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
977 MCDwarfCallFrameFragment &DF);
979 /// finishLayout - Finalize a layout, including fragment lowering.
980 void finishLayout(MCAsmLayout &Layout);
982 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
983 MCFragment &F, const MCFixup &Fixup);
986 /// Compute the effective fragment size assuming it is laid out at the given
987 /// \p SectionAddress and \p FragmentOffset.
988 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
989 const MCFragment &F) const;
991 /// Find the symbol which defines the atom containing the given symbol, or
992 /// null if there is no such symbol.
993 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
995 /// Check whether a particular symbol is visible to the linker and is required
996 /// in the symbol table, or whether it can be discarded by the assembler. This
997 /// also effects whether the assembler treats the label as potentially
998 /// defining a separate atom.
999 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
1001 /// Emit the section contents using the given object writer.
1002 void writeSectionData(const MCSectionData *Section,
1003 const MCAsmLayout &Layout) const;
1005 /// Check whether a given symbol has been flagged with .thumb_func.
1006 bool isThumbFunc(const MCSymbol *Func) const {
1007 return ThumbFuncs.count(Func);
1010 /// Flag a function symbol as the target of a .thumb_func directive.
1011 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1013 /// ELF e_header flags
1014 unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1015 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1017 /// MachO deployment target version information.
1018 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1019 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1021 VersionMinInfo.Kind = Kind;
1022 VersionMinInfo.Major = Major;
1023 VersionMinInfo.Minor = Minor;
1024 VersionMinInfo.Update = Update;
1028 /// Construct a new assembler instance.
1030 /// \param OS The stream to output to.
1032 // FIXME: How are we going to parameterize this? Two obvious options are stay
1033 // concrete and require clients to pass in a target like object. The other
1034 // option is to make this abstract, and have targets provide concrete
1035 // implementations as we do with AsmParser.
1036 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1037 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1041 /// Reuse an assembler instance
1045 MCContext &getContext() const { return Context; }
1047 MCAsmBackend &getBackend() const { return Backend; }
1049 MCCodeEmitter &getEmitter() const { return Emitter; }
1051 MCObjectWriter &getWriter() const { return Writer; }
1053 /// Finish - Do final processing and write the object to the output stream.
1054 /// \p Writer is used for custom object writer (as the MCJIT does),
1055 /// if not specified it is automatically created from backend.
1058 // FIXME: This does not belong here.
1059 bool getSubsectionsViaSymbols() const {
1060 return SubsectionsViaSymbols;
1062 void setSubsectionsViaSymbols(bool Value) {
1063 SubsectionsViaSymbols = Value;
1066 bool getRelaxAll() const { return RelaxAll; }
1067 void setRelaxAll(bool Value) { RelaxAll = Value; }
1069 bool getNoExecStack() const { return NoExecStack; }
1070 void setNoExecStack(bool Value) { NoExecStack = Value; }
1072 bool isBundlingEnabled() const {
1073 return BundleAlignSize != 0;
1076 unsigned getBundleAlignSize() const {
1077 return BundleAlignSize;
1080 void setBundleAlignSize(unsigned Size) {
1081 assert((Size == 0 || !(Size & (Size - 1))) &&
1082 "Expect a power-of-two bundle align size");
1083 BundleAlignSize = Size;
1086 /// @name Section List Access
1089 const SectionDataListType &getSectionList() const { return Sections; }
1090 SectionDataListType &getSectionList() { return Sections; }
1092 iterator begin() { return Sections.begin(); }
1093 const_iterator begin() const { return Sections.begin(); }
1095 iterator end() { return Sections.end(); }
1096 const_iterator end() const { return Sections.end(); }
1098 size_t size() const { return Sections.size(); }
1101 /// @name Symbol List Access
1104 const SymbolDataListType &getSymbolList() const { return Symbols; }
1105 SymbolDataListType &getSymbolList() { return Symbols; }
1107 symbol_iterator symbol_begin() { return Symbols.begin(); }
1108 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1110 symbol_iterator symbol_end() { return Symbols.end(); }
1111 const_symbol_iterator symbol_end() const { return Symbols.end(); }
1113 size_t symbol_size() const { return Symbols.size(); }
1116 /// @name Indirect Symbol List Access
1119 // FIXME: This is a total hack, this should not be here. Once things are
1120 // factored so that the streamer has direct access to the .o writer, it can
1122 std::vector<IndirectSymbolData> &getIndirectSymbols() {
1123 return IndirectSymbols;
1126 indirect_symbol_iterator indirect_symbol_begin() {
1127 return IndirectSymbols.begin();
1129 const_indirect_symbol_iterator indirect_symbol_begin() const {
1130 return IndirectSymbols.begin();
1133 indirect_symbol_iterator indirect_symbol_end() {
1134 return IndirectSymbols.end();
1136 const_indirect_symbol_iterator indirect_symbol_end() const {
1137 return IndirectSymbols.end();
1140 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1143 /// @name Linker Option List Access
1146 std::vector<std::vector<std::string> > &getLinkerOptions() {
1147 return LinkerOptions;
1151 /// @name Data Region List Access
1154 // FIXME: This is a total hack, this should not be here. Once things are
1155 // factored so that the streamer has direct access to the .o writer, it can
1157 std::vector<DataRegionData> &getDataRegions() {
1161 data_region_iterator data_region_begin() {
1162 return DataRegions.begin();
1164 const_data_region_iterator data_region_begin() const {
1165 return DataRegions.begin();
1168 data_region_iterator data_region_end() {
1169 return DataRegions.end();
1171 const_data_region_iterator data_region_end() const {
1172 return DataRegions.end();
1175 size_t data_region_size() const { return DataRegions.size(); }
1178 /// @name Backend Data Access
1181 MCSectionData &getSectionData(const MCSection &Section) const {
1182 MCSectionData *Entry = SectionMap.lookup(&Section);
1183 assert(Entry && "Missing section data!");
1187 MCSectionData &getOrCreateSectionData(const MCSection &Section,
1188 bool *Created = 0) {
1189 MCSectionData *&Entry = SectionMap[&Section];
1191 if (Created) *Created = !Entry;
1193 Entry = new MCSectionData(Section, this);
1198 bool hasSymbolData(const MCSymbol &Symbol) const {
1199 return SymbolMap.lookup(&Symbol) != 0;
1202 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1203 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1204 assert(Entry && "Missing symbol data!");
1208 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1209 bool *Created = 0) {
1210 MCSymbolData *&Entry = SymbolMap[&Symbol];
1212 if (Created) *Created = !Entry;
1214 Entry = new MCSymbolData(Symbol, 0, 0, this);
1219 const_file_name_iterator file_names_begin() const {
1220 return FileNames.begin();
1223 const_file_name_iterator file_names_end() const {
1224 return FileNames.end();
1227 void addFileName(StringRef FileName) {
1228 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1230 FileNames.push_back(FileName);
1238 } // end namespace llvm