10b57cec5SDimitry Andric //===- MCFragment.h - Fragment type hierarchy -------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLVM_MC_MCFRAGMENT_H 100b57cec5SDimitry Andric #define LLVM_MC_MCFRAGMENT_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 130b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 140b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 150b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 160b57cec5SDimitry Andric #include "llvm/ADT/ilist_node.h" 170b57cec5SDimitry Andric #include "llvm/MC/MCFixup.h" 180b57cec5SDimitry Andric #include "llvm/MC/MCInst.h" 19480093f4SDimitry Andric #include "llvm/Support/Alignment.h" 200b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 210b57cec5SDimitry Andric #include "llvm/Support/SMLoc.h" 220b57cec5SDimitry Andric #include <cstdint> 230b57cec5SDimitry Andric #include <utility> 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace llvm { 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric class MCSection; 280b57cec5SDimitry Andric class MCSubtargetInfo; 290b57cec5SDimitry Andric class MCSymbol; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric class MCFragment : public ilist_node_with_parent<MCFragment, MCSection> { 320b57cec5SDimitry Andric friend class MCAsmLayout; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric public: 350b57cec5SDimitry Andric enum FragmentType : uint8_t { 360b57cec5SDimitry Andric FT_Align, 370b57cec5SDimitry Andric FT_Data, 380b57cec5SDimitry Andric FT_CompactEncodedInst, 390b57cec5SDimitry Andric FT_Fill, 400b57cec5SDimitry Andric FT_Relaxable, 410b57cec5SDimitry Andric FT_Org, 420b57cec5SDimitry Andric FT_Dwarf, 430b57cec5SDimitry Andric FT_DwarfFrame, 440b57cec5SDimitry Andric FT_LEB, 45480093f4SDimitry Andric FT_BoundaryAlign, 460b57cec5SDimitry Andric FT_SymbolId, 470b57cec5SDimitry Andric FT_CVInlineLines, 480b57cec5SDimitry Andric FT_CVDefRange, 490b57cec5SDimitry Andric FT_Dummy 500b57cec5SDimitry Andric }; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric private: 53480093f4SDimitry Andric /// The data for the section this fragment is in. 54480093f4SDimitry Andric MCSection *Parent; 55480093f4SDimitry Andric 56480093f4SDimitry Andric /// The atom this fragment is in, as represented by its defining symbol. 57480093f4SDimitry Andric const MCSymbol *Atom; 58480093f4SDimitry Andric 59480093f4SDimitry Andric /// The offset of this fragment in its section. This is ~0 until 60480093f4SDimitry Andric /// initialized. 61480093f4SDimitry Andric uint64_t Offset; 62480093f4SDimitry Andric 63480093f4SDimitry Andric /// The layout order of this fragment. 64480093f4SDimitry Andric unsigned LayoutOrder; 65480093f4SDimitry Andric 660b57cec5SDimitry Andric FragmentType Kind; 670b57cec5SDimitry Andric 68*5ffd83dbSDimitry Andric /// Whether fragment is being laid out. 69*5ffd83dbSDimitry Andric bool IsBeingLaidOut; 70*5ffd83dbSDimitry Andric 710b57cec5SDimitry Andric protected: 720b57cec5SDimitry Andric bool HasInstructions; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric MCFragment(FragmentType Kind, bool HasInstructions, 750b57cec5SDimitry Andric MCSection *Parent = nullptr); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric public: 780b57cec5SDimitry Andric MCFragment() = delete; 790b57cec5SDimitry Andric MCFragment(const MCFragment &) = delete; 800b57cec5SDimitry Andric MCFragment &operator=(const MCFragment &) = delete; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric /// Destroys the current fragment. 830b57cec5SDimitry Andric /// 840b57cec5SDimitry Andric /// This must be used instead of delete as MCFragment is non-virtual. 850b57cec5SDimitry Andric /// This method will dispatch to the appropriate subclass. 860b57cec5SDimitry Andric void destroy(); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric FragmentType getKind() const { return Kind; } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric MCSection *getParent() const { return Parent; } 910b57cec5SDimitry Andric void setParent(MCSection *Value) { Parent = Value; } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric const MCSymbol *getAtom() const { return Atom; } 940b57cec5SDimitry Andric void setAtom(const MCSymbol *Value) { Atom = Value; } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric unsigned getLayoutOrder() const { return LayoutOrder; } 970b57cec5SDimitry Andric void setLayoutOrder(unsigned Value) { LayoutOrder = Value; } 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric /// Does this fragment have instructions emitted into it? By default 1000b57cec5SDimitry Andric /// this is false, but specific fragment types may set it to true. 1010b57cec5SDimitry Andric bool hasInstructions() const { return HasInstructions; } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric void dump() const; 1040b57cec5SDimitry Andric }; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric class MCDummyFragment : public MCFragment { 1070b57cec5SDimitry Andric public: 1080b57cec5SDimitry Andric explicit MCDummyFragment(MCSection *Sec) : MCFragment(FT_Dummy, false, Sec) {} 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric static bool classof(const MCFragment *F) { return F->getKind() == FT_Dummy; } 1110b57cec5SDimitry Andric }; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or 1140b57cec5SDimitry Andric /// data. 1150b57cec5SDimitry Andric /// 1160b57cec5SDimitry Andric class MCEncodedFragment : public MCFragment { 1170b57cec5SDimitry Andric /// Should this fragment be aligned to the end of a bundle? 1180b57cec5SDimitry Andric bool AlignToBundleEnd = false; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric uint8_t BundlePadding = 0; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric protected: 1230b57cec5SDimitry Andric MCEncodedFragment(MCFragment::FragmentType FType, bool HasInstructions, 1240b57cec5SDimitry Andric MCSection *Sec) 1250b57cec5SDimitry Andric : MCFragment(FType, HasInstructions, Sec) {} 1260b57cec5SDimitry Andric 127480093f4SDimitry Andric /// The MCSubtargetInfo in effect when the instruction was encoded. 128480093f4SDimitry Andric /// It must be non-null for instructions. 1290b57cec5SDimitry Andric const MCSubtargetInfo *STI = nullptr; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric public: 1320b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 1330b57cec5SDimitry Andric MCFragment::FragmentType Kind = F->getKind(); 1340b57cec5SDimitry Andric switch (Kind) { 1350b57cec5SDimitry Andric default: 1360b57cec5SDimitry Andric return false; 1370b57cec5SDimitry Andric case MCFragment::FT_Relaxable: 1380b57cec5SDimitry Andric case MCFragment::FT_CompactEncodedInst: 1390b57cec5SDimitry Andric case MCFragment::FT_Data: 1400b57cec5SDimitry Andric case MCFragment::FT_Dwarf: 1410b57cec5SDimitry Andric case MCFragment::FT_DwarfFrame: 1420b57cec5SDimitry Andric return true; 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric /// Should this fragment be placed at the end of an aligned bundle? 1470b57cec5SDimitry Andric bool alignToBundleEnd() const { return AlignToBundleEnd; } 1480b57cec5SDimitry Andric void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; } 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric /// Get the padding size that must be inserted before this fragment. 1510b57cec5SDimitry Andric /// Used for bundling. By default, no padding is inserted. 1520b57cec5SDimitry Andric /// Note that padding size is restricted to 8 bits. This is an optimization 1530b57cec5SDimitry Andric /// to reduce the amount of space used for each fragment. In practice, larger 1540b57cec5SDimitry Andric /// padding should never be required. 1550b57cec5SDimitry Andric uint8_t getBundlePadding() const { return BundlePadding; } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric /// Set the padding size for this fragment. By default it's a no-op, 1580b57cec5SDimitry Andric /// and only some fragments have a meaningful implementation. 1590b57cec5SDimitry Andric void setBundlePadding(uint8_t N) { BundlePadding = N; } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric /// Retrieve the MCSubTargetInfo in effect when the instruction was encoded. 1620b57cec5SDimitry Andric /// Guaranteed to be non-null if hasInstructions() == true 1630b57cec5SDimitry Andric const MCSubtargetInfo *getSubtargetInfo() const { return STI; } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric /// Record that the fragment contains instructions with the MCSubtargetInfo in 1660b57cec5SDimitry Andric /// effect when the instruction was encoded. 1670b57cec5SDimitry Andric void setHasInstructions(const MCSubtargetInfo &STI) { 1680b57cec5SDimitry Andric HasInstructions = true; 1690b57cec5SDimitry Andric this->STI = &STI; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric }; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or 1740b57cec5SDimitry Andric /// data. 1750b57cec5SDimitry Andric /// 1760b57cec5SDimitry Andric template<unsigned ContentsSize> 1770b57cec5SDimitry Andric class MCEncodedFragmentWithContents : public MCEncodedFragment { 1780b57cec5SDimitry Andric SmallVector<char, ContentsSize> Contents; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric protected: 1810b57cec5SDimitry Andric MCEncodedFragmentWithContents(MCFragment::FragmentType FType, 1820b57cec5SDimitry Andric bool HasInstructions, 1830b57cec5SDimitry Andric MCSection *Sec) 1840b57cec5SDimitry Andric : MCEncodedFragment(FType, HasInstructions, Sec) {} 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric public: 1870b57cec5SDimitry Andric SmallVectorImpl<char> &getContents() { return Contents; } 1880b57cec5SDimitry Andric const SmallVectorImpl<char> &getContents() const { return Contents; } 1890b57cec5SDimitry Andric }; 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or 1920b57cec5SDimitry Andric /// data and also have fixups registered. 1930b57cec5SDimitry Andric /// 1940b57cec5SDimitry Andric template<unsigned ContentsSize, unsigned FixupsSize> 1950b57cec5SDimitry Andric class MCEncodedFragmentWithFixups : 1960b57cec5SDimitry Andric public MCEncodedFragmentWithContents<ContentsSize> { 1970b57cec5SDimitry Andric 198480093f4SDimitry Andric /// The list of fixups in this fragment. 1990b57cec5SDimitry Andric SmallVector<MCFixup, FixupsSize> Fixups; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric protected: 2020b57cec5SDimitry Andric MCEncodedFragmentWithFixups(MCFragment::FragmentType FType, 2030b57cec5SDimitry Andric bool HasInstructions, 2040b57cec5SDimitry Andric MCSection *Sec) 2050b57cec5SDimitry Andric : MCEncodedFragmentWithContents<ContentsSize>(FType, HasInstructions, 2060b57cec5SDimitry Andric Sec) {} 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric public: 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric using const_fixup_iterator = SmallVectorImpl<MCFixup>::const_iterator; 2110b57cec5SDimitry Andric using fixup_iterator = SmallVectorImpl<MCFixup>::iterator; 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &getFixups() { return Fixups; } 2140b57cec5SDimitry Andric const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric fixup_iterator fixup_begin() { return Fixups.begin(); } 2170b57cec5SDimitry Andric const_fixup_iterator fixup_begin() const { return Fixups.begin(); } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric fixup_iterator fixup_end() { return Fixups.end(); } 2200b57cec5SDimitry Andric const_fixup_iterator fixup_end() const { return Fixups.end(); } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 2230b57cec5SDimitry Andric MCFragment::FragmentType Kind = F->getKind(); 2240b57cec5SDimitry Andric return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data || 2250b57cec5SDimitry Andric Kind == MCFragment::FT_CVDefRange || Kind == MCFragment::FT_Dwarf || 2260b57cec5SDimitry Andric Kind == MCFragment::FT_DwarfFrame; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric /// Fragment for data and encoded instructions. 2310b57cec5SDimitry Andric /// 2320b57cec5SDimitry Andric class MCDataFragment : public MCEncodedFragmentWithFixups<32, 4> { 2330b57cec5SDimitry Andric public: 2340b57cec5SDimitry Andric MCDataFragment(MCSection *Sec = nullptr) 2350b57cec5SDimitry Andric : MCEncodedFragmentWithFixups<32, 4>(FT_Data, false, Sec) {} 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 2380b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Data; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric }; 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric /// This is a compact (memory-size-wise) fragment for holding an encoded 2430b57cec5SDimitry Andric /// instruction (non-relaxable) that has no fixups registered. When applicable, 2440b57cec5SDimitry Andric /// it can be used instead of MCDataFragment and lead to lower memory 2450b57cec5SDimitry Andric /// consumption. 2460b57cec5SDimitry Andric /// 2470b57cec5SDimitry Andric class MCCompactEncodedInstFragment : public MCEncodedFragmentWithContents<4> { 2480b57cec5SDimitry Andric public: 2490b57cec5SDimitry Andric MCCompactEncodedInstFragment(MCSection *Sec = nullptr) 2500b57cec5SDimitry Andric : MCEncodedFragmentWithContents(FT_CompactEncodedInst, true, Sec) { 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 2540b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_CompactEncodedInst; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric }; 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric /// A relaxable fragment holds on to its MCInst, since it may need to be 2590b57cec5SDimitry Andric /// relaxed during the assembler layout and relaxation stage. 2600b57cec5SDimitry Andric /// 2610b57cec5SDimitry Andric class MCRelaxableFragment : public MCEncodedFragmentWithFixups<8, 1> { 2620b57cec5SDimitry Andric 263480093f4SDimitry Andric /// The instruction this is a fragment for. 2640b57cec5SDimitry Andric MCInst Inst; 265*5ffd83dbSDimitry Andric /// Can we auto pad the instruction? 266*5ffd83dbSDimitry Andric bool AllowAutoPadding = false; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric public: 2690b57cec5SDimitry Andric MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI, 2700b57cec5SDimitry Andric MCSection *Sec = nullptr) 2710b57cec5SDimitry Andric : MCEncodedFragmentWithFixups(FT_Relaxable, true, Sec), 2720b57cec5SDimitry Andric Inst(Inst) { this->STI = &STI; } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric const MCInst &getInst() const { return Inst; } 2750b57cec5SDimitry Andric void setInst(const MCInst &Value) { Inst = Value; } 2760b57cec5SDimitry Andric 277*5ffd83dbSDimitry Andric bool getAllowAutoPadding() const { return AllowAutoPadding; } 278*5ffd83dbSDimitry Andric void setAllowAutoPadding(bool V) { AllowAutoPadding = V; } 279*5ffd83dbSDimitry Andric 2800b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 2810b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Relaxable; 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric }; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric class MCAlignFragment : public MCFragment { 286480093f4SDimitry Andric /// The alignment to ensure, in bytes. 2870b57cec5SDimitry Andric unsigned Alignment; 2880b57cec5SDimitry Andric 289480093f4SDimitry Andric /// Flag to indicate that (optimal) NOPs should be emitted instead 2900b57cec5SDimitry Andric /// of using the provided value. The exact interpretation of this flag is 2910b57cec5SDimitry Andric /// target dependent. 2920b57cec5SDimitry Andric bool EmitNops : 1; 2930b57cec5SDimitry Andric 294480093f4SDimitry Andric /// Value to use for filling padding bytes. 2950b57cec5SDimitry Andric int64_t Value; 2960b57cec5SDimitry Andric 297480093f4SDimitry Andric /// The size of the integer (in bytes) of \p Value. 2980b57cec5SDimitry Andric unsigned ValueSize; 2990b57cec5SDimitry Andric 300480093f4SDimitry Andric /// The maximum number of bytes to emit; if the alignment 3010b57cec5SDimitry Andric /// cannot be satisfied in this width then this fragment is ignored. 3020b57cec5SDimitry Andric unsigned MaxBytesToEmit; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric public: 3050b57cec5SDimitry Andric MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize, 3060b57cec5SDimitry Andric unsigned MaxBytesToEmit, MCSection *Sec = nullptr) 3070b57cec5SDimitry Andric : MCFragment(FT_Align, false, Sec), Alignment(Alignment), EmitNops(false), 3080b57cec5SDimitry Andric Value(Value), ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit) {} 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric unsigned getAlignment() const { return Alignment; } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric int64_t getValue() const { return Value; } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric unsigned getValueSize() const { return ValueSize; } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric bool hasEmitNops() const { return EmitNops; } 3190b57cec5SDimitry Andric void setEmitNops(bool Value) { EmitNops = Value; } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 3220b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Align; 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric }; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric class MCFillFragment : public MCFragment { 327480093f4SDimitry Andric uint8_t ValueSize; 3280b57cec5SDimitry Andric /// Value to use for filling bytes. 3290b57cec5SDimitry Andric uint64_t Value; 3300b57cec5SDimitry Andric /// The number of bytes to insert. 3310b57cec5SDimitry Andric const MCExpr &NumValues; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric /// Source location of the directive that this fragment was created for. 3340b57cec5SDimitry Andric SMLoc Loc; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric public: 3370b57cec5SDimitry Andric MCFillFragment(uint64_t Value, uint8_t VSize, const MCExpr &NumValues, 3380b57cec5SDimitry Andric SMLoc Loc, MCSection *Sec = nullptr) 339480093f4SDimitry Andric : MCFragment(FT_Fill, false, Sec), ValueSize(VSize), Value(Value), 3400b57cec5SDimitry Andric NumValues(NumValues), Loc(Loc) {} 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric uint64_t getValue() const { return Value; } 3430b57cec5SDimitry Andric uint8_t getValueSize() const { return ValueSize; } 3440b57cec5SDimitry Andric const MCExpr &getNumValues() const { return NumValues; } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric SMLoc getLoc() const { return Loc; } 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 3490b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Fill; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric }; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric class MCOrgFragment : public MCFragment { 3540b57cec5SDimitry Andric /// Value to use for filling bytes. 3550b57cec5SDimitry Andric int8_t Value; 3560b57cec5SDimitry Andric 357480093f4SDimitry Andric /// The offset this fragment should start at. 358480093f4SDimitry Andric const MCExpr *Offset; 359480093f4SDimitry Andric 3600b57cec5SDimitry Andric /// Source location of the directive that this fragment was created for. 3610b57cec5SDimitry Andric SMLoc Loc; 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric public: 3640b57cec5SDimitry Andric MCOrgFragment(const MCExpr &Offset, int8_t Value, SMLoc Loc, 3650b57cec5SDimitry Andric MCSection *Sec = nullptr) 366480093f4SDimitry Andric : MCFragment(FT_Org, false, Sec), Value(Value), Offset(&Offset), 367480093f4SDimitry Andric Loc(Loc) {} 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric const MCExpr &getOffset() const { return *Offset; } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric uint8_t getValue() const { return Value; } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric SMLoc getLoc() const { return Loc; } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 3760b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Org; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric }; 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric class MCLEBFragment : public MCFragment { 381480093f4SDimitry Andric /// True if this is a sleb128, false if uleb128. 3820b57cec5SDimitry Andric bool IsSigned; 3830b57cec5SDimitry Andric 384480093f4SDimitry Andric /// The value this fragment should contain. 385480093f4SDimitry Andric const MCExpr *Value; 386480093f4SDimitry Andric 3870b57cec5SDimitry Andric SmallString<8> Contents; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric public: 3900b57cec5SDimitry Andric MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr) 391480093f4SDimitry Andric : MCFragment(FT_LEB, false, Sec), IsSigned(IsSigned_), Value(&Value_) { 3920b57cec5SDimitry Andric Contents.push_back(0); 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric const MCExpr &getValue() const { return *Value; } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric bool isSigned() const { return IsSigned; } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric SmallString<8> &getContents() { return Contents; } 4000b57cec5SDimitry Andric const SmallString<8> &getContents() const { return Contents; } 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric /// @} 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 4050b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_LEB; 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric }; 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric class MCDwarfLineAddrFragment : public MCEncodedFragmentWithFixups<8, 1> { 410480093f4SDimitry Andric /// The value of the difference between the two line numbers 4110b57cec5SDimitry Andric /// between two .loc dwarf directives. 4120b57cec5SDimitry Andric int64_t LineDelta; 4130b57cec5SDimitry Andric 414480093f4SDimitry Andric /// The expression for the difference of the two symbols that 4150b57cec5SDimitry Andric /// make up the address delta between two .loc dwarf directives. 4160b57cec5SDimitry Andric const MCExpr *AddrDelta; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric public: 4190b57cec5SDimitry Andric MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta, 4200b57cec5SDimitry Andric MCSection *Sec = nullptr) 4210b57cec5SDimitry Andric : MCEncodedFragmentWithFixups<8, 1>(FT_Dwarf, false, Sec), 4220b57cec5SDimitry Andric LineDelta(LineDelta), AddrDelta(&AddrDelta) {} 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric int64_t getLineDelta() const { return LineDelta; } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric const MCExpr &getAddrDelta() const { return *AddrDelta; } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 4290b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_Dwarf; 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric }; 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric class MCDwarfCallFrameFragment : public MCEncodedFragmentWithFixups<8, 1> { 434480093f4SDimitry Andric /// The expression for the difference of the two symbols that 4350b57cec5SDimitry Andric /// make up the address delta between two .cfi_* dwarf directives. 4360b57cec5SDimitry Andric const MCExpr *AddrDelta; 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric public: 4390b57cec5SDimitry Andric MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr) 4400b57cec5SDimitry Andric : MCEncodedFragmentWithFixups<8, 1>(FT_DwarfFrame, false, Sec), 4410b57cec5SDimitry Andric AddrDelta(&AddrDelta) {} 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric const MCExpr &getAddrDelta() const { return *AddrDelta; } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 4460b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_DwarfFrame; 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric }; 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric /// Represents a symbol table index fragment. 4510b57cec5SDimitry Andric class MCSymbolIdFragment : public MCFragment { 4520b57cec5SDimitry Andric const MCSymbol *Sym; 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric public: 4550b57cec5SDimitry Andric MCSymbolIdFragment(const MCSymbol *Sym, MCSection *Sec = nullptr) 4560b57cec5SDimitry Andric : MCFragment(FT_SymbolId, false, Sec), Sym(Sym) {} 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric const MCSymbol *getSymbol() { return Sym; } 4590b57cec5SDimitry Andric const MCSymbol *getSymbol() const { return Sym; } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 4620b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_SymbolId; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric }; 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric /// Fragment representing the binary annotations produced by the 4670b57cec5SDimitry Andric /// .cv_inline_linetable directive. 4680b57cec5SDimitry Andric class MCCVInlineLineTableFragment : public MCFragment { 4690b57cec5SDimitry Andric unsigned SiteFuncId; 4700b57cec5SDimitry Andric unsigned StartFileId; 4710b57cec5SDimitry Andric unsigned StartLineNum; 4720b57cec5SDimitry Andric const MCSymbol *FnStartSym; 4730b57cec5SDimitry Andric const MCSymbol *FnEndSym; 4740b57cec5SDimitry Andric SmallString<8> Contents; 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric /// CodeViewContext has the real knowledge about this format, so let it access 4770b57cec5SDimitry Andric /// our members. 4780b57cec5SDimitry Andric friend class CodeViewContext; 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric public: 4810b57cec5SDimitry Andric MCCVInlineLineTableFragment(unsigned SiteFuncId, unsigned StartFileId, 4820b57cec5SDimitry Andric unsigned StartLineNum, const MCSymbol *FnStartSym, 4830b57cec5SDimitry Andric const MCSymbol *FnEndSym, 4840b57cec5SDimitry Andric MCSection *Sec = nullptr) 4850b57cec5SDimitry Andric : MCFragment(FT_CVInlineLines, false, Sec), SiteFuncId(SiteFuncId), 4860b57cec5SDimitry Andric StartFileId(StartFileId), StartLineNum(StartLineNum), 4870b57cec5SDimitry Andric FnStartSym(FnStartSym), FnEndSym(FnEndSym) {} 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric const MCSymbol *getFnStartSym() const { return FnStartSym; } 4900b57cec5SDimitry Andric const MCSymbol *getFnEndSym() const { return FnEndSym; } 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric SmallString<8> &getContents() { return Contents; } 4930b57cec5SDimitry Andric const SmallString<8> &getContents() const { return Contents; } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 4960b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_CVInlineLines; 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric }; 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric /// Fragment representing the .cv_def_range directive. 5010b57cec5SDimitry Andric class MCCVDefRangeFragment : public MCEncodedFragmentWithFixups<32, 4> { 5020b57cec5SDimitry Andric SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 2> Ranges; 5030b57cec5SDimitry Andric SmallString<32> FixedSizePortion; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric /// CodeViewContext has the real knowledge about this format, so let it access 5060b57cec5SDimitry Andric /// our members. 5070b57cec5SDimitry Andric friend class CodeViewContext; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric public: 5100b57cec5SDimitry Andric MCCVDefRangeFragment( 5110b57cec5SDimitry Andric ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 5120b57cec5SDimitry Andric StringRef FixedSizePortion, MCSection *Sec = nullptr) 5130b57cec5SDimitry Andric : MCEncodedFragmentWithFixups<32, 4>(FT_CVDefRange, false, Sec), 5140b57cec5SDimitry Andric Ranges(Ranges.begin(), Ranges.end()), 5150b57cec5SDimitry Andric FixedSizePortion(FixedSizePortion) {} 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> getRanges() const { 5180b57cec5SDimitry Andric return Ranges; 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric StringRef getFixedSizePortion() const { return FixedSizePortion; } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric static bool classof(const MCFragment *F) { 5240b57cec5SDimitry Andric return F->getKind() == MCFragment::FT_CVDefRange; 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric }; 5270b57cec5SDimitry Andric 528480093f4SDimitry Andric /// Represents required padding such that a particular other set of fragments 529480093f4SDimitry Andric /// does not cross a particular power-of-two boundary. The other fragments must 530480093f4SDimitry Andric /// follow this one within the same section. 531480093f4SDimitry Andric class MCBoundaryAlignFragment : public MCFragment { 532480093f4SDimitry Andric /// The alignment requirement of the branch to be aligned. 533480093f4SDimitry Andric Align AlignBoundary; 534*5ffd83dbSDimitry Andric /// The last fragment in the set of fragments to be aligned. 535*5ffd83dbSDimitry Andric const MCFragment *LastFragment = nullptr; 536480093f4SDimitry Andric /// The size of the fragment. The size is lazily set during relaxation, and 537480093f4SDimitry Andric /// is not meaningful before that. 538480093f4SDimitry Andric uint64_t Size = 0; 539480093f4SDimitry Andric 540480093f4SDimitry Andric public: 541*5ffd83dbSDimitry Andric MCBoundaryAlignFragment(Align AlignBoundary, MCSection *Sec = nullptr) 542*5ffd83dbSDimitry Andric : MCFragment(FT_BoundaryAlign, false, Sec), AlignBoundary(AlignBoundary) { 543*5ffd83dbSDimitry Andric } 544480093f4SDimitry Andric 545480093f4SDimitry Andric uint64_t getSize() const { return Size; } 546480093f4SDimitry Andric void setSize(uint64_t Value) { Size = Value; } 547480093f4SDimitry Andric 548480093f4SDimitry Andric Align getAlignment() const { return AlignBoundary; } 549*5ffd83dbSDimitry Andric void setAlignment(Align Value) { AlignBoundary = Value; } 550480093f4SDimitry Andric 551*5ffd83dbSDimitry Andric const MCFragment *getLastFragment() const { return LastFragment; } 552*5ffd83dbSDimitry Andric void setLastFragment(const MCFragment *F) { 553*5ffd83dbSDimitry Andric assert(!F || getParent() == F->getParent()); 554*5ffd83dbSDimitry Andric LastFragment = F; 555*5ffd83dbSDimitry Andric } 556480093f4SDimitry Andric 557480093f4SDimitry Andric static bool classof(const MCFragment *F) { 558480093f4SDimitry Andric return F->getKind() == MCFragment::FT_BoundaryAlign; 559480093f4SDimitry Andric } 560480093f4SDimitry Andric }; 5610b57cec5SDimitry Andric } // end namespace llvm 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric #endif // LLVM_MC_MCFRAGMENT_H 564