xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCFragment.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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,
40e8d8bef9SDimitry Andric     FT_Nops,
410b57cec5SDimitry Andric     FT_Relaxable,
420b57cec5SDimitry Andric     FT_Org,
430b57cec5SDimitry Andric     FT_Dwarf,
440b57cec5SDimitry Andric     FT_DwarfFrame,
450b57cec5SDimitry Andric     FT_LEB,
46480093f4SDimitry Andric     FT_BoundaryAlign,
470b57cec5SDimitry Andric     FT_SymbolId,
480b57cec5SDimitry Andric     FT_CVInlineLines,
490b57cec5SDimitry Andric     FT_CVDefRange,
50e8d8bef9SDimitry Andric     FT_PseudoProbe,
510b57cec5SDimitry Andric     FT_Dummy
520b57cec5SDimitry Andric   };
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric private:
55480093f4SDimitry Andric   /// The data for the section this fragment is in.
56480093f4SDimitry Andric   MCSection *Parent;
57480093f4SDimitry Andric 
58480093f4SDimitry Andric   /// The atom this fragment is in, as represented by its defining symbol.
59480093f4SDimitry Andric   const MCSymbol *Atom;
60480093f4SDimitry Andric 
61480093f4SDimitry Andric   /// The offset of this fragment in its section. This is ~0 until
62480093f4SDimitry Andric   /// initialized.
63480093f4SDimitry Andric   uint64_t Offset;
64480093f4SDimitry Andric 
65480093f4SDimitry Andric   /// The layout order of this fragment.
66480093f4SDimitry Andric   unsigned LayoutOrder;
67480093f4SDimitry Andric 
68e8d8bef9SDimitry Andric   /// The subsection this fragment belongs to. This is 0 if the fragment is not
69e8d8bef9SDimitry Andric   // in any subsection.
70e8d8bef9SDimitry Andric   unsigned SubsectionNumber = 0;
71e8d8bef9SDimitry Andric 
720b57cec5SDimitry Andric   FragmentType Kind;
730b57cec5SDimitry Andric 
745ffd83dbSDimitry Andric   /// Whether fragment is being laid out.
755ffd83dbSDimitry Andric   bool IsBeingLaidOut;
765ffd83dbSDimitry Andric 
770b57cec5SDimitry Andric protected:
780b57cec5SDimitry Andric   bool HasInstructions;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   MCFragment(FragmentType Kind, bool HasInstructions,
810b57cec5SDimitry Andric              MCSection *Parent = nullptr);
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric public:
840b57cec5SDimitry Andric   MCFragment() = delete;
850b57cec5SDimitry Andric   MCFragment(const MCFragment &) = delete;
860b57cec5SDimitry Andric   MCFragment &operator=(const MCFragment &) = delete;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   /// Destroys the current fragment.
890b57cec5SDimitry Andric   ///
900b57cec5SDimitry Andric   /// This must be used instead of delete as MCFragment is non-virtual.
910b57cec5SDimitry Andric   /// This method will dispatch to the appropriate subclass.
920b57cec5SDimitry Andric   void destroy();
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   FragmentType getKind() const { return Kind; }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   MCSection *getParent() const { return Parent; }
970b57cec5SDimitry Andric   void setParent(MCSection *Value) { Parent = Value; }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   const MCSymbol *getAtom() const { return Atom; }
1000b57cec5SDimitry Andric   void setAtom(const MCSymbol *Value) { Atom = Value; }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   unsigned getLayoutOrder() const { return LayoutOrder; }
1030b57cec5SDimitry Andric   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   /// Does this fragment have instructions emitted into it? By default
1060b57cec5SDimitry Andric   /// this is false, but specific fragment types may set it to true.
1070b57cec5SDimitry Andric   bool hasInstructions() const { return HasInstructions; }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   void dump() const;
110e8d8bef9SDimitry Andric 
111e8d8bef9SDimitry Andric   void setSubsectionNumber(unsigned Value) { SubsectionNumber = Value; }
112e8d8bef9SDimitry Andric   unsigned getSubsectionNumber() const { return SubsectionNumber; }
1130b57cec5SDimitry Andric };
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric class MCDummyFragment : public MCFragment {
1160b57cec5SDimitry Andric public:
1170b57cec5SDimitry Andric   explicit MCDummyFragment(MCSection *Sec) : MCFragment(FT_Dummy, false, Sec) {}
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   static bool classof(const MCFragment *F) { return F->getKind() == FT_Dummy; }
1200b57cec5SDimitry Andric };
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or
1230b57cec5SDimitry Andric /// data.
1240b57cec5SDimitry Andric ///
1250b57cec5SDimitry Andric class MCEncodedFragment : public MCFragment {
1260b57cec5SDimitry Andric   /// Should this fragment be aligned to the end of a bundle?
1270b57cec5SDimitry Andric   bool AlignToBundleEnd = false;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   uint8_t BundlePadding = 0;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric protected:
1320b57cec5SDimitry Andric   MCEncodedFragment(MCFragment::FragmentType FType, bool HasInstructions,
1330b57cec5SDimitry Andric                     MCSection *Sec)
1340b57cec5SDimitry Andric       : MCFragment(FType, HasInstructions, Sec) {}
1350b57cec5SDimitry Andric 
136480093f4SDimitry Andric   /// The MCSubtargetInfo in effect when the instruction was encoded.
137480093f4SDimitry Andric   /// It must be non-null for instructions.
1380b57cec5SDimitry Andric   const MCSubtargetInfo *STI = nullptr;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric public:
1410b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
1420b57cec5SDimitry Andric     MCFragment::FragmentType Kind = F->getKind();
1430b57cec5SDimitry Andric     switch (Kind) {
1440b57cec5SDimitry Andric     default:
1450b57cec5SDimitry Andric       return false;
1460b57cec5SDimitry Andric     case MCFragment::FT_Relaxable:
1470b57cec5SDimitry Andric     case MCFragment::FT_CompactEncodedInst:
1480b57cec5SDimitry Andric     case MCFragment::FT_Data:
1490b57cec5SDimitry Andric     case MCFragment::FT_Dwarf:
1500b57cec5SDimitry Andric     case MCFragment::FT_DwarfFrame:
151e8d8bef9SDimitry Andric     case MCFragment::FT_PseudoProbe:
1520b57cec5SDimitry Andric       return true;
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   /// Should this fragment be placed at the end of an aligned bundle?
1570b57cec5SDimitry Andric   bool alignToBundleEnd() const { return AlignToBundleEnd; }
1580b57cec5SDimitry Andric   void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   /// Get the padding size that must be inserted before this fragment.
1610b57cec5SDimitry Andric   /// Used for bundling. By default, no padding is inserted.
1620b57cec5SDimitry Andric   /// Note that padding size is restricted to 8 bits. This is an optimization
1630b57cec5SDimitry Andric   /// to reduce the amount of space used for each fragment. In practice, larger
1640b57cec5SDimitry Andric   /// padding should never be required.
1650b57cec5SDimitry Andric   uint8_t getBundlePadding() const { return BundlePadding; }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   /// Set the padding size for this fragment. By default it's a no-op,
1680b57cec5SDimitry Andric   /// and only some fragments have a meaningful implementation.
1690b57cec5SDimitry Andric   void setBundlePadding(uint8_t N) { BundlePadding = N; }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   /// Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
1720b57cec5SDimitry Andric   /// Guaranteed to be non-null if hasInstructions() == true
1730b57cec5SDimitry Andric   const MCSubtargetInfo *getSubtargetInfo() const { return STI; }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   /// Record that the fragment contains instructions with the MCSubtargetInfo in
1760b57cec5SDimitry Andric   /// effect when the instruction was encoded.
1770b57cec5SDimitry Andric   void setHasInstructions(const MCSubtargetInfo &STI) {
1780b57cec5SDimitry Andric     HasInstructions = true;
1790b57cec5SDimitry Andric     this->STI = &STI;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric };
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or
1840b57cec5SDimitry Andric /// data.
1850b57cec5SDimitry Andric ///
1860b57cec5SDimitry Andric template<unsigned ContentsSize>
1870b57cec5SDimitry Andric class MCEncodedFragmentWithContents : public MCEncodedFragment {
1880b57cec5SDimitry Andric   SmallVector<char, ContentsSize> Contents;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric protected:
1910b57cec5SDimitry Andric   MCEncodedFragmentWithContents(MCFragment::FragmentType FType,
1920b57cec5SDimitry Andric                                 bool HasInstructions,
1930b57cec5SDimitry Andric                                 MCSection *Sec)
1940b57cec5SDimitry Andric       : MCEncodedFragment(FType, HasInstructions, Sec) {}
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric public:
1970b57cec5SDimitry Andric   SmallVectorImpl<char> &getContents() { return Contents; }
1980b57cec5SDimitry Andric   const SmallVectorImpl<char> &getContents() const { return Contents; }
1990b57cec5SDimitry Andric };
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric /// Interface implemented by fragments that contain encoded instructions and/or
2020b57cec5SDimitry Andric /// data and also have fixups registered.
2030b57cec5SDimitry Andric ///
2040b57cec5SDimitry Andric template<unsigned ContentsSize, unsigned FixupsSize>
2050b57cec5SDimitry Andric class MCEncodedFragmentWithFixups :
2060b57cec5SDimitry Andric   public MCEncodedFragmentWithContents<ContentsSize> {
2070b57cec5SDimitry Andric 
208480093f4SDimitry Andric   /// The list of fixups in this fragment.
2090b57cec5SDimitry Andric   SmallVector<MCFixup, FixupsSize> Fixups;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric protected:
2120b57cec5SDimitry Andric   MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
2130b57cec5SDimitry Andric                               bool HasInstructions,
2140b57cec5SDimitry Andric                               MCSection *Sec)
2150b57cec5SDimitry Andric       : MCEncodedFragmentWithContents<ContentsSize>(FType, HasInstructions,
2160b57cec5SDimitry Andric                                                     Sec) {}
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric public:
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   using const_fixup_iterator = SmallVectorImpl<MCFixup>::const_iterator;
2210b57cec5SDimitry Andric   using fixup_iterator = SmallVectorImpl<MCFixup>::iterator;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
2240b57cec5SDimitry Andric   const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   fixup_iterator fixup_begin() { return Fixups.begin(); }
2270b57cec5SDimitry Andric   const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   fixup_iterator fixup_end() { return Fixups.end(); }
2300b57cec5SDimitry Andric   const_fixup_iterator fixup_end() const { return Fixups.end(); }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
2330b57cec5SDimitry Andric     MCFragment::FragmentType Kind = F->getKind();
2340b57cec5SDimitry Andric     return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data ||
2350b57cec5SDimitry Andric            Kind == MCFragment::FT_CVDefRange || Kind == MCFragment::FT_Dwarf ||
2360b57cec5SDimitry Andric            Kind == MCFragment::FT_DwarfFrame;
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric };
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric /// Fragment for data and encoded instructions.
2410b57cec5SDimitry Andric ///
2420b57cec5SDimitry Andric class MCDataFragment : public MCEncodedFragmentWithFixups<32, 4> {
2430b57cec5SDimitry Andric public:
2440b57cec5SDimitry Andric   MCDataFragment(MCSection *Sec = nullptr)
2450b57cec5SDimitry Andric       : MCEncodedFragmentWithFixups<32, 4>(FT_Data, false, Sec) {}
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
2480b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Data;
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric };
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric /// This is a compact (memory-size-wise) fragment for holding an encoded
2530b57cec5SDimitry Andric /// instruction (non-relaxable) that has no fixups registered. When applicable,
2540b57cec5SDimitry Andric /// it can be used instead of MCDataFragment and lead to lower memory
2550b57cec5SDimitry Andric /// consumption.
2560b57cec5SDimitry Andric ///
2570b57cec5SDimitry Andric class MCCompactEncodedInstFragment : public MCEncodedFragmentWithContents<4> {
2580b57cec5SDimitry Andric public:
2590b57cec5SDimitry Andric   MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
2600b57cec5SDimitry Andric       : MCEncodedFragmentWithContents(FT_CompactEncodedInst, true, Sec) {
2610b57cec5SDimitry Andric   }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
2640b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_CompactEncodedInst;
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric };
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric /// A relaxable fragment holds on to its MCInst, since it may need to be
2690b57cec5SDimitry Andric /// relaxed during the assembler layout and relaxation stage.
2700b57cec5SDimitry Andric ///
2710b57cec5SDimitry Andric class MCRelaxableFragment : public MCEncodedFragmentWithFixups<8, 1> {
2720b57cec5SDimitry Andric 
273480093f4SDimitry Andric   /// The instruction this is a fragment for.
2740b57cec5SDimitry Andric   MCInst Inst;
2755ffd83dbSDimitry Andric   /// Can we auto pad the instruction?
2765ffd83dbSDimitry Andric   bool AllowAutoPadding = false;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric public:
2790b57cec5SDimitry Andric   MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
2800b57cec5SDimitry Andric                       MCSection *Sec = nullptr)
2810b57cec5SDimitry Andric       : MCEncodedFragmentWithFixups(FT_Relaxable, true, Sec),
2820b57cec5SDimitry Andric         Inst(Inst) { this->STI = &STI; }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   const MCInst &getInst() const { return Inst; }
2850b57cec5SDimitry Andric   void setInst(const MCInst &Value) { Inst = Value; }
2860b57cec5SDimitry Andric 
2875ffd83dbSDimitry Andric   bool getAllowAutoPadding() const { return AllowAutoPadding; }
2885ffd83dbSDimitry Andric   void setAllowAutoPadding(bool V) { AllowAutoPadding = V; }
2895ffd83dbSDimitry Andric 
2900b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
2910b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Relaxable;
2920b57cec5SDimitry Andric   }
2930b57cec5SDimitry Andric };
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric class MCAlignFragment : public MCFragment {
296480093f4SDimitry Andric   /// The alignment to ensure, in bytes.
2970b57cec5SDimitry Andric   unsigned Alignment;
2980b57cec5SDimitry Andric 
299480093f4SDimitry Andric   /// Flag to indicate that (optimal) NOPs should be emitted instead
3000b57cec5SDimitry Andric   /// of using the provided value. The exact interpretation of this flag is
3010b57cec5SDimitry Andric   /// target dependent.
3020b57cec5SDimitry Andric   bool EmitNops : 1;
3030b57cec5SDimitry Andric 
304480093f4SDimitry Andric   /// Value to use for filling padding bytes.
3050b57cec5SDimitry Andric   int64_t Value;
3060b57cec5SDimitry Andric 
307480093f4SDimitry Andric   /// The size of the integer (in bytes) of \p Value.
3080b57cec5SDimitry Andric   unsigned ValueSize;
3090b57cec5SDimitry Andric 
310480093f4SDimitry Andric   /// The maximum number of bytes to emit; if the alignment
3110b57cec5SDimitry Andric   /// cannot be satisfied in this width then this fragment is ignored.
3120b57cec5SDimitry Andric   unsigned MaxBytesToEmit;
3130b57cec5SDimitry Andric 
314*349cc55cSDimitry Andric   /// When emitting Nops some subtargets have specific nop encodings.
315*349cc55cSDimitry Andric   const MCSubtargetInfo *STI;
316*349cc55cSDimitry Andric 
3170b57cec5SDimitry Andric public:
3180b57cec5SDimitry Andric   MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
3190b57cec5SDimitry Andric                   unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
3200b57cec5SDimitry Andric       : MCFragment(FT_Align, false, Sec), Alignment(Alignment), EmitNops(false),
3210b57cec5SDimitry Andric         Value(Value), ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit) {}
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   unsigned getAlignment() const { return Alignment; }
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   int64_t getValue() const { return Value; }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   unsigned getValueSize() const { return ValueSize; }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   bool hasEmitNops() const { return EmitNops; }
332*349cc55cSDimitry Andric   void setEmitNops(bool Value, const MCSubtargetInfo *STI) {
333*349cc55cSDimitry Andric     EmitNops = Value;
334*349cc55cSDimitry Andric     this->STI = STI;
335*349cc55cSDimitry Andric   }
336*349cc55cSDimitry Andric 
337*349cc55cSDimitry Andric   const MCSubtargetInfo *getSubtargetInfo() const { return STI; }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
3400b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Align;
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric };
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric class MCFillFragment : public MCFragment {
345480093f4SDimitry Andric   uint8_t ValueSize;
3460b57cec5SDimitry Andric   /// Value to use for filling bytes.
3470b57cec5SDimitry Andric   uint64_t Value;
3480b57cec5SDimitry Andric   /// The number of bytes to insert.
3490b57cec5SDimitry Andric   const MCExpr &NumValues;
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   /// Source location of the directive that this fragment was created for.
3520b57cec5SDimitry Andric   SMLoc Loc;
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric public:
3550b57cec5SDimitry Andric   MCFillFragment(uint64_t Value, uint8_t VSize, const MCExpr &NumValues,
3560b57cec5SDimitry Andric                  SMLoc Loc, MCSection *Sec = nullptr)
357480093f4SDimitry Andric       : MCFragment(FT_Fill, false, Sec), ValueSize(VSize), Value(Value),
3580b57cec5SDimitry Andric         NumValues(NumValues), Loc(Loc) {}
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   uint64_t getValue() const { return Value; }
3610b57cec5SDimitry Andric   uint8_t getValueSize() const { return ValueSize; }
3620b57cec5SDimitry Andric   const MCExpr &getNumValues() const { return NumValues; }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   SMLoc getLoc() const { return Loc; }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
3670b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Fill;
3680b57cec5SDimitry Andric   }
3690b57cec5SDimitry Andric };
3700b57cec5SDimitry Andric 
371e8d8bef9SDimitry Andric class MCNopsFragment : public MCFragment {
372e8d8bef9SDimitry Andric   /// The number of bytes to insert.
373e8d8bef9SDimitry Andric   int64_t Size;
374e8d8bef9SDimitry Andric   /// Maximum number of bytes allowed in each NOP instruction.
375e8d8bef9SDimitry Andric   int64_t ControlledNopLength;
376e8d8bef9SDimitry Andric 
377e8d8bef9SDimitry Andric   /// Source location of the directive that this fragment was created for.
378e8d8bef9SDimitry Andric   SMLoc Loc;
379e8d8bef9SDimitry Andric 
380*349cc55cSDimitry Andric   /// When emitting Nops some subtargets have specific nop encodings.
381*349cc55cSDimitry Andric   const MCSubtargetInfo &STI;
382*349cc55cSDimitry Andric 
383e8d8bef9SDimitry Andric public:
384e8d8bef9SDimitry Andric   MCNopsFragment(int64_t NumBytes, int64_t ControlledNopLength, SMLoc L,
385*349cc55cSDimitry Andric                  const MCSubtargetInfo &STI, MCSection *Sec = nullptr)
386e8d8bef9SDimitry Andric       : MCFragment(FT_Nops, false, Sec), Size(NumBytes),
387*349cc55cSDimitry Andric         ControlledNopLength(ControlledNopLength), Loc(L), STI(STI) {}
388e8d8bef9SDimitry Andric 
389e8d8bef9SDimitry Andric   int64_t getNumBytes() const { return Size; }
390e8d8bef9SDimitry Andric   int64_t getControlledNopLength() const { return ControlledNopLength; }
391e8d8bef9SDimitry Andric 
392e8d8bef9SDimitry Andric   SMLoc getLoc() const { return Loc; }
393e8d8bef9SDimitry Andric 
394*349cc55cSDimitry Andric   const MCSubtargetInfo *getSubtargetInfo() const { return &STI; }
395*349cc55cSDimitry Andric 
396e8d8bef9SDimitry Andric   static bool classof(const MCFragment *F) {
397e8d8bef9SDimitry Andric     return F->getKind() == MCFragment::FT_Nops;
398e8d8bef9SDimitry Andric   }
399e8d8bef9SDimitry Andric };
400e8d8bef9SDimitry Andric 
4010b57cec5SDimitry Andric class MCOrgFragment : public MCFragment {
4020b57cec5SDimitry Andric   /// Value to use for filling bytes.
4030b57cec5SDimitry Andric   int8_t Value;
4040b57cec5SDimitry Andric 
405480093f4SDimitry Andric   /// The offset this fragment should start at.
406480093f4SDimitry Andric   const MCExpr *Offset;
407480093f4SDimitry Andric 
4080b57cec5SDimitry Andric   /// Source location of the directive that this fragment was created for.
4090b57cec5SDimitry Andric   SMLoc Loc;
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric public:
4120b57cec5SDimitry Andric   MCOrgFragment(const MCExpr &Offset, int8_t Value, SMLoc Loc,
4130b57cec5SDimitry Andric                 MCSection *Sec = nullptr)
414480093f4SDimitry Andric       : MCFragment(FT_Org, false, Sec), Value(Value), Offset(&Offset),
415480093f4SDimitry Andric         Loc(Loc) {}
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   const MCExpr &getOffset() const { return *Offset; }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   uint8_t getValue() const { return Value; }
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   SMLoc getLoc() const { return Loc; }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
4240b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Org;
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric };
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric class MCLEBFragment : public MCFragment {
429480093f4SDimitry Andric   /// True if this is a sleb128, false if uleb128.
4300b57cec5SDimitry Andric   bool IsSigned;
4310b57cec5SDimitry Andric 
432480093f4SDimitry Andric   /// The value this fragment should contain.
433480093f4SDimitry Andric   const MCExpr *Value;
434480093f4SDimitry Andric 
4350b57cec5SDimitry Andric   SmallString<8> Contents;
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric public:
4380b57cec5SDimitry Andric   MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
439480093f4SDimitry Andric       : MCFragment(FT_LEB, false, Sec), IsSigned(IsSigned_), Value(&Value_) {
4400b57cec5SDimitry Andric     Contents.push_back(0);
4410b57cec5SDimitry Andric   }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   const MCExpr &getValue() const { return *Value; }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   bool isSigned() const { return IsSigned; }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   SmallString<8> &getContents() { return Contents; }
4480b57cec5SDimitry Andric   const SmallString<8> &getContents() const { return Contents; }
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   /// @}
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
4530b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_LEB;
4540b57cec5SDimitry Andric   }
4550b57cec5SDimitry Andric };
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric class MCDwarfLineAddrFragment : public MCEncodedFragmentWithFixups<8, 1> {
458480093f4SDimitry Andric   /// The value of the difference between the two line numbers
4590b57cec5SDimitry Andric   /// between two .loc dwarf directives.
4600b57cec5SDimitry Andric   int64_t LineDelta;
4610b57cec5SDimitry Andric 
462480093f4SDimitry Andric   /// The expression for the difference of the two symbols that
4630b57cec5SDimitry Andric   /// make up the address delta between two .loc dwarf directives.
4640b57cec5SDimitry Andric   const MCExpr *AddrDelta;
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric public:
4670b57cec5SDimitry Andric   MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
4680b57cec5SDimitry Andric                           MCSection *Sec = nullptr)
4690b57cec5SDimitry Andric       : MCEncodedFragmentWithFixups<8, 1>(FT_Dwarf, false, Sec),
4700b57cec5SDimitry Andric         LineDelta(LineDelta), AddrDelta(&AddrDelta) {}
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   int64_t getLineDelta() const { return LineDelta; }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   const MCExpr &getAddrDelta() const { return *AddrDelta; }
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
4770b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_Dwarf;
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric };
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric class MCDwarfCallFrameFragment : public MCEncodedFragmentWithFixups<8, 1> {
482480093f4SDimitry Andric   /// The expression for the difference of the two symbols that
4830b57cec5SDimitry Andric   /// make up the address delta between two .cfi_* dwarf directives.
4840b57cec5SDimitry Andric   const MCExpr *AddrDelta;
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric public:
4870b57cec5SDimitry Andric   MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
4880b57cec5SDimitry Andric       : MCEncodedFragmentWithFixups<8, 1>(FT_DwarfFrame, false, Sec),
4890b57cec5SDimitry Andric         AddrDelta(&AddrDelta) {}
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   const MCExpr &getAddrDelta() const { return *AddrDelta; }
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
4940b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_DwarfFrame;
4950b57cec5SDimitry Andric   }
4960b57cec5SDimitry Andric };
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric /// Represents a symbol table index fragment.
4990b57cec5SDimitry Andric class MCSymbolIdFragment : public MCFragment {
5000b57cec5SDimitry Andric   const MCSymbol *Sym;
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric public:
5030b57cec5SDimitry Andric   MCSymbolIdFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
5040b57cec5SDimitry Andric       : MCFragment(FT_SymbolId, false, Sec), Sym(Sym) {}
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   const MCSymbol *getSymbol() { return Sym; }
5070b57cec5SDimitry Andric   const MCSymbol *getSymbol() const { return Sym; }
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
5100b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_SymbolId;
5110b57cec5SDimitry Andric   }
5120b57cec5SDimitry Andric };
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric /// Fragment representing the binary annotations produced by the
5150b57cec5SDimitry Andric /// .cv_inline_linetable directive.
5160b57cec5SDimitry Andric class MCCVInlineLineTableFragment : public MCFragment {
5170b57cec5SDimitry Andric   unsigned SiteFuncId;
5180b57cec5SDimitry Andric   unsigned StartFileId;
5190b57cec5SDimitry Andric   unsigned StartLineNum;
5200b57cec5SDimitry Andric   const MCSymbol *FnStartSym;
5210b57cec5SDimitry Andric   const MCSymbol *FnEndSym;
5220b57cec5SDimitry Andric   SmallString<8> Contents;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   /// CodeViewContext has the real knowledge about this format, so let it access
5250b57cec5SDimitry Andric   /// our members.
5260b57cec5SDimitry Andric   friend class CodeViewContext;
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric public:
5290b57cec5SDimitry Andric   MCCVInlineLineTableFragment(unsigned SiteFuncId, unsigned StartFileId,
5300b57cec5SDimitry Andric                               unsigned StartLineNum, const MCSymbol *FnStartSym,
5310b57cec5SDimitry Andric                               const MCSymbol *FnEndSym,
5320b57cec5SDimitry Andric                               MCSection *Sec = nullptr)
5330b57cec5SDimitry Andric       : MCFragment(FT_CVInlineLines, false, Sec), SiteFuncId(SiteFuncId),
5340b57cec5SDimitry Andric         StartFileId(StartFileId), StartLineNum(StartLineNum),
5350b57cec5SDimitry Andric         FnStartSym(FnStartSym), FnEndSym(FnEndSym) {}
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric   const MCSymbol *getFnStartSym() const { return FnStartSym; }
5380b57cec5SDimitry Andric   const MCSymbol *getFnEndSym() const { return FnEndSym; }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   SmallString<8> &getContents() { return Contents; }
5410b57cec5SDimitry Andric   const SmallString<8> &getContents() const { return Contents; }
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
5440b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_CVInlineLines;
5450b57cec5SDimitry Andric   }
5460b57cec5SDimitry Andric };
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric /// Fragment representing the .cv_def_range directive.
5490b57cec5SDimitry Andric class MCCVDefRangeFragment : public MCEncodedFragmentWithFixups<32, 4> {
5500b57cec5SDimitry Andric   SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 2> Ranges;
5510b57cec5SDimitry Andric   SmallString<32> FixedSizePortion;
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   /// CodeViewContext has the real knowledge about this format, so let it access
5540b57cec5SDimitry Andric   /// our members.
5550b57cec5SDimitry Andric   friend class CodeViewContext;
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric public:
5580b57cec5SDimitry Andric   MCCVDefRangeFragment(
5590b57cec5SDimitry Andric       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
5600b57cec5SDimitry Andric       StringRef FixedSizePortion, MCSection *Sec = nullptr)
5610b57cec5SDimitry Andric       : MCEncodedFragmentWithFixups<32, 4>(FT_CVDefRange, false, Sec),
5620b57cec5SDimitry Andric         Ranges(Ranges.begin(), Ranges.end()),
5630b57cec5SDimitry Andric         FixedSizePortion(FixedSizePortion) {}
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> getRanges() const {
5660b57cec5SDimitry Andric     return Ranges;
5670b57cec5SDimitry Andric   }
5680b57cec5SDimitry Andric 
569fe6060f1SDimitry Andric   StringRef getFixedSizePortion() const { return FixedSizePortion.str(); }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   static bool classof(const MCFragment *F) {
5720b57cec5SDimitry Andric     return F->getKind() == MCFragment::FT_CVDefRange;
5730b57cec5SDimitry Andric   }
5740b57cec5SDimitry Andric };
5750b57cec5SDimitry Andric 
576480093f4SDimitry Andric /// Represents required padding such that a particular other set of fragments
577480093f4SDimitry Andric /// does not cross a particular power-of-two boundary. The other fragments must
578480093f4SDimitry Andric /// follow this one within the same section.
579480093f4SDimitry Andric class MCBoundaryAlignFragment : public MCFragment {
580480093f4SDimitry Andric   /// The alignment requirement of the branch to be aligned.
581480093f4SDimitry Andric   Align AlignBoundary;
5825ffd83dbSDimitry Andric   /// The last fragment in the set of fragments to be aligned.
5835ffd83dbSDimitry Andric   const MCFragment *LastFragment = nullptr;
584480093f4SDimitry Andric   /// The size of the fragment.  The size is lazily set during relaxation, and
585480093f4SDimitry Andric   /// is not meaningful before that.
586480093f4SDimitry Andric   uint64_t Size = 0;
587480093f4SDimitry Andric 
588*349cc55cSDimitry Andric   /// When emitting Nops some subtargets have specific nop encodings.
589*349cc55cSDimitry Andric   const MCSubtargetInfo &STI;
590*349cc55cSDimitry Andric 
591480093f4SDimitry Andric public:
592*349cc55cSDimitry Andric   MCBoundaryAlignFragment(Align AlignBoundary, const MCSubtargetInfo &STI,
593*349cc55cSDimitry Andric                           MCSection *Sec = nullptr)
594*349cc55cSDimitry Andric       : MCFragment(FT_BoundaryAlign, false, Sec), AlignBoundary(AlignBoundary),
595*349cc55cSDimitry Andric         STI(STI) {}
596480093f4SDimitry Andric 
597480093f4SDimitry Andric   uint64_t getSize() const { return Size; }
598480093f4SDimitry Andric   void setSize(uint64_t Value) { Size = Value; }
599480093f4SDimitry Andric 
600480093f4SDimitry Andric   Align getAlignment() const { return AlignBoundary; }
6015ffd83dbSDimitry Andric   void setAlignment(Align Value) { AlignBoundary = Value; }
602480093f4SDimitry Andric 
6035ffd83dbSDimitry Andric   const MCFragment *getLastFragment() const { return LastFragment; }
6045ffd83dbSDimitry Andric   void setLastFragment(const MCFragment *F) {
6055ffd83dbSDimitry Andric     assert(!F || getParent() == F->getParent());
6065ffd83dbSDimitry Andric     LastFragment = F;
6075ffd83dbSDimitry Andric   }
608480093f4SDimitry Andric 
609*349cc55cSDimitry Andric   const MCSubtargetInfo *getSubtargetInfo() const { return &STI; }
610*349cc55cSDimitry Andric 
611480093f4SDimitry Andric   static bool classof(const MCFragment *F) {
612480093f4SDimitry Andric     return F->getKind() == MCFragment::FT_BoundaryAlign;
613480093f4SDimitry Andric   }
614480093f4SDimitry Andric };
615e8d8bef9SDimitry Andric 
616e8d8bef9SDimitry Andric class MCPseudoProbeAddrFragment : public MCEncodedFragmentWithFixups<8, 1> {
617e8d8bef9SDimitry Andric   /// The expression for the difference of the two symbols that
618e8d8bef9SDimitry Andric   /// make up the address delta between two .pseudoprobe directives.
619e8d8bef9SDimitry Andric   const MCExpr *AddrDelta;
620e8d8bef9SDimitry Andric 
621e8d8bef9SDimitry Andric public:
622e8d8bef9SDimitry Andric   MCPseudoProbeAddrFragment(const MCExpr *AddrDelta, MCSection *Sec = nullptr)
623e8d8bef9SDimitry Andric       : MCEncodedFragmentWithFixups<8, 1>(FT_PseudoProbe, false, Sec),
624e8d8bef9SDimitry Andric         AddrDelta(AddrDelta) {}
625e8d8bef9SDimitry Andric 
626e8d8bef9SDimitry Andric   const MCExpr &getAddrDelta() const { return *AddrDelta; }
627e8d8bef9SDimitry Andric 
628e8d8bef9SDimitry Andric   static bool classof(const MCFragment *F) {
629e8d8bef9SDimitry Andric     return F->getKind() == MCFragment::FT_PseudoProbe;
630e8d8bef9SDimitry Andric   }
631e8d8bef9SDimitry Andric };
6320b57cec5SDimitry Andric } // end namespace llvm
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric #endif // LLVM_MC_MCFRAGMENT_H
635