1 //===- MCFragment.h - Fragment type hierarchy -------------------*- 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_MCFRAGMENT_H
11 #define LLVM_MC_MCFRAGMENT_H
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/MC/MCFixup.h"
18 #include "llvm/MC/MCInst.h"
23 class MCSubtargetInfo;
25 class MCFragment : public ilist_node_with_parent<MCFragment, MCSection> {
26 friend class MCAsmLayout;
28 MCFragment(const MCFragment &) = delete;
29 void operator=(const MCFragment &) = delete;
32 enum FragmentType : uint8_t {
35 FT_CompactEncodedInst,
53 /// \brief Should this fragment be aligned to the end of a bundle?
54 bool AlignToBundleEnd;
56 uint8_t BundlePadding;
58 /// LayoutOrder - The layout order of this fragment.
61 /// The data for the section this fragment is in.
64 /// Atom - The atom this fragment is in, as represented by it's defining
68 /// \name Assembler Backend Data
71 // FIXME: This could all be kept private to the assembler implementation.
73 /// Offset - The offset of this fragment in its section. This is ~0 until
80 MCFragment(FragmentType Kind, bool HasInstructions,
81 uint8_t BundlePadding, MCSection *Parent = nullptr);
86 // This is a friend so that the sentinal can be created.
87 friend struct ilist_sentinel_traits<MCFragment>;
91 /// Destroys the current fragment.
93 /// This must be used instead of delete as MCFragment is non-virtual.
94 /// This method will dispatch to the appropriate subclass.
97 FragmentType getKind() const { return Kind; }
99 MCSection *getParent() const { return Parent; }
100 void setParent(MCSection *Value) { Parent = Value; }
102 const MCSymbol *getAtom() const { return Atom; }
103 void setAtom(const MCSymbol *Value) { Atom = Value; }
105 unsigned getLayoutOrder() const { return LayoutOrder; }
106 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
108 /// \brief Does this fragment have instructions emitted into it? By default
109 /// this is false, but specific fragment types may set it to true.
110 bool hasInstructions() const { return HasInstructions; }
112 /// \brief Should this fragment be placed at the end of an aligned bundle?
113 bool alignToBundleEnd() const { return AlignToBundleEnd; }
114 void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
116 /// \brief Get the padding size that must be inserted before this fragment.
117 /// Used for bundling. By default, no padding is inserted.
118 /// Note that padding size is restricted to 8 bits. This is an optimization
119 /// to reduce the amount of space used for each fragment. In practice, larger
120 /// padding should never be required.
121 uint8_t getBundlePadding() const { return BundlePadding; }
123 /// \brief Set the padding size for this fragment. By default it's a no-op,
124 /// and only some fragments have a meaningful implementation.
125 void setBundlePadding(uint8_t N) { BundlePadding = N; }
127 /// \brief Return true if given frgment has FT_Dummy type.
128 bool isDummy() const { return Kind == FT_Dummy; }
133 class MCDummyFragment : public MCFragment {
135 explicit MCDummyFragment(MCSection *Sec)
136 : MCFragment(FT_Dummy, false, 0, Sec){};
137 static bool classof(const MCFragment *F) { return F->getKind() == FT_Dummy; }
140 /// Interface implemented by fragments that contain encoded instructions and/or
143 class MCEncodedFragment : public MCFragment {
145 MCEncodedFragment(MCFragment::FragmentType FType, bool HasInstructions,
147 : MCFragment(FType, HasInstructions, 0, Sec) {}
150 static bool classof(const MCFragment *F) {
151 MCFragment::FragmentType Kind = F->getKind();
155 case MCFragment::FT_Relaxable:
156 case MCFragment::FT_CompactEncodedInst:
157 case MCFragment::FT_Data:
163 /// Interface implemented by fragments that contain encoded instructions and/or
166 template<unsigned ContentsSize>
167 class MCEncodedFragmentWithContents : public MCEncodedFragment {
168 SmallVector<char, ContentsSize> Contents;
171 MCEncodedFragmentWithContents(MCFragment::FragmentType FType,
172 bool HasInstructions,
174 : MCEncodedFragment(FType, HasInstructions, Sec) {}
177 SmallVectorImpl<char> &getContents() { return Contents; }
178 const SmallVectorImpl<char> &getContents() const { return Contents; }
181 /// Interface implemented by fragments that contain encoded instructions and/or
182 /// data and also have fixups registered.
184 template<unsigned ContentsSize, unsigned FixupsSize>
185 class MCEncodedFragmentWithFixups :
186 public MCEncodedFragmentWithContents<ContentsSize> {
188 /// Fixups - The list of fixups in this fragment.
189 SmallVector<MCFixup, FixupsSize> Fixups;
192 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
193 bool HasInstructions,
195 : MCEncodedFragmentWithContents<ContentsSize>(FType, HasInstructions,
199 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
200 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
202 SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
203 const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
205 fixup_iterator fixup_begin() { return Fixups.begin(); }
206 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
208 fixup_iterator fixup_end() { return Fixups.end(); }
209 const_fixup_iterator fixup_end() const { return Fixups.end(); }
211 static bool classof(const MCFragment *F) {
212 MCFragment::FragmentType Kind = F->getKind();
213 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
217 /// Fragment for data and encoded instructions.
219 class MCDataFragment : public MCEncodedFragmentWithFixups<32, 4> {
221 MCDataFragment(MCSection *Sec = nullptr)
222 : MCEncodedFragmentWithFixups<32, 4>(FT_Data, false, Sec) {}
224 void setHasInstructions(bool V) { HasInstructions = V; }
226 static bool classof(const MCFragment *F) {
227 return F->getKind() == MCFragment::FT_Data;
231 /// This is a compact (memory-size-wise) fragment for holding an encoded
232 /// instruction (non-relaxable) that has no fixups registered. When applicable,
233 /// it can be used instead of MCDataFragment and lead to lower memory
236 class MCCompactEncodedInstFragment : public MCEncodedFragmentWithContents<4> {
238 MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
239 : MCEncodedFragmentWithContents(FT_CompactEncodedInst, true, Sec) {
242 static bool classof(const MCFragment *F) {
243 return F->getKind() == MCFragment::FT_CompactEncodedInst;
247 /// A relaxable fragment holds on to its MCInst, since it may need to be
248 /// relaxed during the assembler layout and relaxation stage.
250 class MCRelaxableFragment : public MCEncodedFragmentWithFixups<8, 1> {
252 /// Inst - The instruction this is a fragment for.
255 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
256 const MCSubtargetInfo &STI;
259 MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
260 MCSection *Sec = nullptr)
261 : MCEncodedFragmentWithFixups(FT_Relaxable, true, Sec),
262 Inst(Inst), STI(STI) {}
264 const MCInst &getInst() const { return Inst; }
265 void setInst(const MCInst &Value) { Inst = Value; }
267 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
269 static bool classof(const MCFragment *F) {
270 return F->getKind() == MCFragment::FT_Relaxable;
274 class MCAlignFragment : public MCFragment {
276 /// Alignment - The alignment to ensure, in bytes.
279 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
280 /// of using the provided value. The exact interpretation of this flag is
281 /// target dependent.
284 /// Value - Value to use for filling padding bytes.
287 /// ValueSize - The size of the integer (in bytes) of \p Value.
290 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
291 /// cannot be satisfied in this width then this fragment is ignored.
292 unsigned MaxBytesToEmit;
295 MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
296 unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
297 : MCFragment(FT_Align, false, 0, Sec), Alignment(Alignment),
298 EmitNops(false), Value(Value),
299 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit) {}
304 unsigned getAlignment() const { return Alignment; }
306 int64_t getValue() const { return Value; }
308 unsigned getValueSize() const { return ValueSize; }
310 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
312 bool hasEmitNops() const { return EmitNops; }
313 void setEmitNops(bool Value) { EmitNops = Value; }
317 static bool classof(const MCFragment *F) {
318 return F->getKind() == MCFragment::FT_Align;
322 class MCFillFragment : public MCFragment {
324 /// Value - Value to use for filling bytes.
327 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
328 /// this is a virtual fill fragment.
331 /// Size - The number of bytes to insert.
335 MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
336 MCSection *Sec = nullptr)
337 : MCFragment(FT_Fill, false, 0, Sec), Value(Value), ValueSize(ValueSize),
339 assert((!ValueSize || (Size % ValueSize) == 0) &&
340 "Fill size must be a multiple of the value size!");
346 int64_t getValue() const { return Value; }
348 unsigned getValueSize() const { return ValueSize; }
350 uint64_t getSize() const { return Size; }
354 static bool classof(const MCFragment *F) {
355 return F->getKind() == MCFragment::FT_Fill;
359 class MCOrgFragment : public MCFragment {
361 /// Offset - The offset this fragment should start at.
362 const MCExpr *Offset;
364 /// Value - Value to use for filling bytes.
368 MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSection *Sec = nullptr)
369 : MCFragment(FT_Org, false, 0, Sec), Offset(&Offset), Value(Value) {}
374 const MCExpr &getOffset() const { return *Offset; }
376 uint8_t getValue() const { return Value; }
380 static bool classof(const MCFragment *F) {
381 return F->getKind() == MCFragment::FT_Org;
385 class MCLEBFragment : public MCFragment {
387 /// Value - The value this fragment should contain.
390 /// IsSigned - True if this is a sleb128, false if uleb128.
393 SmallString<8> Contents;
396 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
397 : MCFragment(FT_LEB, false, 0, Sec), Value(&Value_), IsSigned(IsSigned_) {
398 Contents.push_back(0);
404 const MCExpr &getValue() const { return *Value; }
406 bool isSigned() const { return IsSigned; }
408 SmallString<8> &getContents() { return Contents; }
409 const SmallString<8> &getContents() const { return Contents; }
413 static bool classof(const MCFragment *F) {
414 return F->getKind() == MCFragment::FT_LEB;
418 class MCDwarfLineAddrFragment : public MCFragment {
420 /// LineDelta - the value of the difference between the two line numbers
421 /// between two .loc dwarf directives.
424 /// AddrDelta - The expression for the difference of the two symbols that
425 /// make up the address delta between two .loc dwarf directives.
426 const MCExpr *AddrDelta;
428 SmallString<8> Contents;
431 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
432 MCSection *Sec = nullptr)
433 : MCFragment(FT_Dwarf, false, 0, Sec), LineDelta(LineDelta),
434 AddrDelta(&AddrDelta) {
435 Contents.push_back(0);
441 int64_t getLineDelta() const { return LineDelta; }
443 const MCExpr &getAddrDelta() const { return *AddrDelta; }
445 SmallString<8> &getContents() { return Contents; }
446 const SmallString<8> &getContents() const { return Contents; }
450 static bool classof(const MCFragment *F) {
451 return F->getKind() == MCFragment::FT_Dwarf;
455 class MCDwarfCallFrameFragment : public MCFragment {
457 /// AddrDelta - The expression for the difference of the two symbols that
458 /// make up the address delta between two .cfi_* dwarf directives.
459 const MCExpr *AddrDelta;
461 SmallString<8> Contents;
464 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
465 : MCFragment(FT_DwarfFrame, false, 0, Sec), AddrDelta(&AddrDelta) {
466 Contents.push_back(0);
472 const MCExpr &getAddrDelta() const { return *AddrDelta; }
474 SmallString<8> &getContents() { return Contents; }
475 const SmallString<8> &getContents() const { return Contents; }
479 static bool classof(const MCFragment *F) {
480 return F->getKind() == MCFragment::FT_DwarfFrame;
484 class MCSafeSEHFragment : public MCFragment {
488 MCSafeSEHFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
489 : MCFragment(FT_SafeSEH, false, 0, Sec), Sym(Sym) {}
494 const MCSymbol *getSymbol() { return Sym; }
495 const MCSymbol *getSymbol() const { return Sym; }
499 static bool classof(const MCFragment *F) {
500 return F->getKind() == MCFragment::FT_SafeSEH;
504 } // end namespace llvm