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/ArrayRef.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/ilist_node.h"
18 #include "llvm/MC/MCFixup.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/SMLoc.h"
28 class MCSubtargetInfo;
31 class MCFragment : public ilist_node_with_parent<MCFragment, MCSection> {
32 friend class MCAsmLayout;
35 enum FragmentType : uint8_t {
38 FT_CompactEncodedInst,
59 /// \brief Should this fragment be aligned to the end of a bundle?
60 bool AlignToBundleEnd;
62 uint8_t BundlePadding;
64 /// LayoutOrder - The layout order of this fragment.
67 /// The data for the section this fragment is in.
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
86 MCFragment(FragmentType Kind, bool HasInstructions,
87 uint8_t BundlePadding, MCSection *Parent = nullptr);
92 MCFragment() = delete;
93 MCFragment(const MCFragment &) = delete;
94 MCFragment &operator=(const MCFragment &) = delete;
96 /// Destroys the current fragment.
98 /// This must be used instead of delete as MCFragment is non-virtual.
99 /// This method will dispatch to the appropriate subclass.
102 FragmentType getKind() const { return Kind; }
104 MCSection *getParent() const { return Parent; }
105 void setParent(MCSection *Value) { Parent = Value; }
107 const MCSymbol *getAtom() const { return Atom; }
108 void setAtom(const MCSymbol *Value) { Atom = Value; }
110 unsigned getLayoutOrder() const { return LayoutOrder; }
111 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
113 /// \brief Does this fragment have instructions emitted into it? By default
114 /// this is false, but specific fragment types may set it to true.
115 bool hasInstructions() const { return HasInstructions; }
117 /// \brief Should this fragment be placed at the end of an aligned bundle?
118 bool alignToBundleEnd() const { return AlignToBundleEnd; }
119 void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
121 /// \brief Get the padding size that must be inserted before this fragment.
122 /// Used for bundling. By default, no padding is inserted.
123 /// Note that padding size is restricted to 8 bits. This is an optimization
124 /// to reduce the amount of space used for each fragment. In practice, larger
125 /// padding should never be required.
126 uint8_t getBundlePadding() const { return BundlePadding; }
128 /// \brief Set the padding size for this fragment. By default it's a no-op,
129 /// and only some fragments have a meaningful implementation.
130 void setBundlePadding(uint8_t N) { BundlePadding = N; }
132 /// \brief Return true if given frgment has FT_Dummy type.
133 bool isDummy() const { return Kind == FT_Dummy; }
138 class MCDummyFragment : public MCFragment {
140 explicit MCDummyFragment(MCSection *Sec)
141 : MCFragment(FT_Dummy, false, 0, Sec) {}
143 static bool classof(const MCFragment *F) { return F->getKind() == FT_Dummy; }
146 /// Interface implemented by fragments that contain encoded instructions and/or
149 class MCEncodedFragment : public MCFragment {
151 MCEncodedFragment(MCFragment::FragmentType FType, bool HasInstructions,
153 : MCFragment(FType, HasInstructions, 0, Sec) {}
156 static bool classof(const MCFragment *F) {
157 MCFragment::FragmentType Kind = F->getKind();
161 case MCFragment::FT_Relaxable:
162 case MCFragment::FT_CompactEncodedInst:
163 case MCFragment::FT_Data:
169 /// Interface implemented by fragments that contain encoded instructions and/or
172 template<unsigned ContentsSize>
173 class MCEncodedFragmentWithContents : public MCEncodedFragment {
174 SmallVector<char, ContentsSize> Contents;
177 MCEncodedFragmentWithContents(MCFragment::FragmentType FType,
178 bool HasInstructions,
180 : MCEncodedFragment(FType, HasInstructions, Sec) {}
183 SmallVectorImpl<char> &getContents() { return Contents; }
184 const SmallVectorImpl<char> &getContents() const { return Contents; }
187 /// Interface implemented by fragments that contain encoded instructions and/or
188 /// data and also have fixups registered.
190 template<unsigned ContentsSize, unsigned FixupsSize>
191 class MCEncodedFragmentWithFixups :
192 public MCEncodedFragmentWithContents<ContentsSize> {
194 /// Fixups - The list of fixups in this fragment.
195 SmallVector<MCFixup, FixupsSize> Fixups;
198 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
199 bool HasInstructions,
201 : MCEncodedFragmentWithContents<ContentsSize>(FType, HasInstructions,
205 using const_fixup_iterator = SmallVectorImpl<MCFixup>::const_iterator;
206 using fixup_iterator = SmallVectorImpl<MCFixup>::iterator;
208 SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
209 const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
211 fixup_iterator fixup_begin() { return Fixups.begin(); }
212 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
214 fixup_iterator fixup_end() { return Fixups.end(); }
215 const_fixup_iterator fixup_end() const { return Fixups.end(); }
217 static bool classof(const MCFragment *F) {
218 MCFragment::FragmentType Kind = F->getKind();
219 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data ||
220 Kind == MCFragment::FT_CVDefRange;
224 /// Fragment for data and encoded instructions.
226 class MCDataFragment : public MCEncodedFragmentWithFixups<32, 4> {
228 MCDataFragment(MCSection *Sec = nullptr)
229 : MCEncodedFragmentWithFixups<32, 4>(FT_Data, false, Sec) {}
231 void setHasInstructions(bool V) { HasInstructions = V; }
233 static bool classof(const MCFragment *F) {
234 return F->getKind() == MCFragment::FT_Data;
238 /// This is a compact (memory-size-wise) fragment for holding an encoded
239 /// instruction (non-relaxable) that has no fixups registered. When applicable,
240 /// it can be used instead of MCDataFragment and lead to lower memory
243 class MCCompactEncodedInstFragment : public MCEncodedFragmentWithContents<4> {
245 MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
246 : MCEncodedFragmentWithContents(FT_CompactEncodedInst, true, Sec) {
249 static bool classof(const MCFragment *F) {
250 return F->getKind() == MCFragment::FT_CompactEncodedInst;
254 /// A relaxable fragment holds on to its MCInst, since it may need to be
255 /// relaxed during the assembler layout and relaxation stage.
257 class MCRelaxableFragment : public MCEncodedFragmentWithFixups<8, 1> {
259 /// Inst - The instruction this is a fragment for.
262 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
263 const MCSubtargetInfo &STI;
266 MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
267 MCSection *Sec = nullptr)
268 : MCEncodedFragmentWithFixups(FT_Relaxable, true, Sec),
269 Inst(Inst), STI(STI) {}
271 const MCInst &getInst() const { return Inst; }
272 void setInst(const MCInst &Value) { Inst = Value; }
274 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
276 static bool classof(const MCFragment *F) {
277 return F->getKind() == MCFragment::FT_Relaxable;
281 class MCAlignFragment : public MCFragment {
282 /// Alignment - The alignment to ensure, in bytes.
285 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
286 /// of using the provided value. The exact interpretation of this flag is
287 /// target dependent.
290 /// Value - Value to use for filling padding bytes.
293 /// ValueSize - The size of the integer (in bytes) of \p Value.
296 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
297 /// cannot be satisfied in this width then this fragment is ignored.
298 unsigned MaxBytesToEmit;
301 MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
302 unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
303 : MCFragment(FT_Align, false, 0, Sec), Alignment(Alignment),
304 EmitNops(false), Value(Value),
305 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit) {}
310 unsigned getAlignment() const { return Alignment; }
312 int64_t getValue() const { return Value; }
314 unsigned getValueSize() const { return ValueSize; }
316 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
318 bool hasEmitNops() const { return EmitNops; }
319 void setEmitNops(bool Value) { EmitNops = Value; }
323 static bool classof(const MCFragment *F) {
324 return F->getKind() == MCFragment::FT_Align;
328 /// Fragment for adding required padding.
329 /// This fragment is always inserted before an instruction, and holds that
330 /// instruction as context information (as well as a mask of kinds) for
331 /// determining the padding size.
333 class MCPaddingFragment : public MCFragment {
334 /// A mask containing all the kinds relevant to this fragment. i.e. the i'th
335 /// bit will be set iff kind i is relevant to this fragment.
336 uint64_t PaddingPoliciesMask;
337 /// A boolean indicating if this fragment will actually hold padding. If its
338 /// value is false, then this fragment serves only as a placeholder,
339 /// containing data to assist other insertion point in their decision making.
340 bool IsInsertionPoint;
347 /// A boolean indicating whether the instruction pointed by this fragment is
348 /// a fixed size instruction or a relaxable instruction held by a
349 /// MCRelaxableFragment.
350 bool IsImmutableSizedInst;
352 /// If the instruction is a fixed size instruction, hold its size.
354 /// Otherwise, hold a pointer to the MCRelaxableFragment holding it.
355 MCRelaxableFragment *InstFragment;
361 static const uint64_t PFK_None = UINT64_C(0);
363 enum MCPaddingFragmentKind {
364 // values 0-7 are reserved for future target independet values.
366 FirstTargetPerfNopFragmentKind = 8,
368 /// Limit range of target MCPerfNopFragment kinds to fit in uint64_t
369 MaxTargetPerfNopFragmentKind = 63
372 MCPaddingFragment(MCSection *Sec = nullptr)
373 : MCFragment(FT_Padding, false, 0, Sec), PaddingPoliciesMask(PFK_None),
374 IsInsertionPoint(false), Size(UINT64_C(0)),
375 InstInfo({false, MCInst(), false, {0}}) {}
377 bool isInsertionPoint() const { return IsInsertionPoint; }
378 void setAsInsertionPoint() { IsInsertionPoint = true; }
379 uint64_t getPaddingPoliciesMask() const { return PaddingPoliciesMask; }
380 void setPaddingPoliciesMask(uint64_t Value) { PaddingPoliciesMask = Value; }
381 bool hasPaddingPolicy(uint64_t PolicyMask) const {
382 assert(isPowerOf2_64(PolicyMask) &&
383 "Policy mask must contain exactly one policy");
384 return (getPaddingPoliciesMask() & PolicyMask) != PFK_None;
386 const MCInst &getInst() const {
387 assert(isInstructionInitialized() && "Fragment has no instruction!");
388 return InstInfo.Inst;
390 size_t getInstSize() const {
391 assert(isInstructionInitialized() && "Fragment has no instruction!");
392 if (InstInfo.IsImmutableSizedInst)
393 return InstInfo.InstSize;
394 assert(InstInfo.InstFragment != nullptr &&
395 "Must have a valid InstFragment to retrieve InstSize from");
396 return InstInfo.InstFragment->getContents().size();
398 void setInstAndInstSize(const MCInst &Inst, size_t InstSize) {
399 InstInfo.IsInitialized = true;
400 InstInfo.IsImmutableSizedInst = true;
401 InstInfo.Inst = Inst;
402 InstInfo.InstSize = InstSize;
404 void setInstAndInstFragment(const MCInst &Inst,
405 MCRelaxableFragment *InstFragment) {
406 InstInfo.IsInitialized = true;
407 InstInfo.IsImmutableSizedInst = false;
408 InstInfo.Inst = Inst;
409 InstInfo.InstFragment = InstFragment;
411 uint64_t getSize() const { return Size; }
412 void setSize(uint64_t Value) { Size = Value; }
413 bool isInstructionInitialized() const { return InstInfo.IsInitialized; }
415 static bool classof(const MCFragment *F) {
416 return F->getKind() == MCFragment::FT_Padding;
420 class MCFillFragment : public MCFragment {
421 /// Value to use for filling bytes.
424 /// The number of bytes to insert.
427 /// Source location of the directive that this fragment was created for.
431 MCFillFragment(uint8_t Value, const MCExpr &Size, SMLoc Loc,
432 MCSection *Sec = nullptr)
433 : MCFragment(FT_Fill, false, 0, Sec), Value(Value), Size(Size), Loc(Loc) {
436 uint8_t getValue() const { return Value; }
437 const MCExpr &getSize() const { return Size; }
439 SMLoc getLoc() const { return Loc; }
441 static bool classof(const MCFragment *F) {
442 return F->getKind() == MCFragment::FT_Fill;
446 class MCOrgFragment : public MCFragment {
447 /// Offset - The offset this fragment should start at.
448 const MCExpr *Offset;
450 /// Value - Value to use for filling bytes.
453 /// Loc - Source location of the directive that this fragment was created for.
457 MCOrgFragment(const MCExpr &Offset, int8_t Value, SMLoc Loc,
458 MCSection *Sec = nullptr)
459 : MCFragment(FT_Org, false, 0, Sec), Offset(&Offset), Value(Value), Loc(Loc) {}
464 const MCExpr &getOffset() const { return *Offset; }
466 uint8_t getValue() const { return Value; }
468 SMLoc getLoc() const { return Loc; }
472 static bool classof(const MCFragment *F) {
473 return F->getKind() == MCFragment::FT_Org;
477 class MCLEBFragment : public MCFragment {
478 /// Value - The value this fragment should contain.
481 /// IsSigned - True if this is a sleb128, false if uleb128.
484 SmallString<8> Contents;
487 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
488 : MCFragment(FT_LEB, false, 0, Sec), Value(&Value_), IsSigned(IsSigned_) {
489 Contents.push_back(0);
495 const MCExpr &getValue() const { return *Value; }
497 bool isSigned() const { return IsSigned; }
499 SmallString<8> &getContents() { return Contents; }
500 const SmallString<8> &getContents() const { return Contents; }
504 static bool classof(const MCFragment *F) {
505 return F->getKind() == MCFragment::FT_LEB;
509 class MCDwarfLineAddrFragment : public MCFragment {
510 /// LineDelta - the value of the difference between the two line numbers
511 /// between two .loc dwarf directives.
514 /// AddrDelta - The expression for the difference of the two symbols that
515 /// make up the address delta between two .loc dwarf directives.
516 const MCExpr *AddrDelta;
518 SmallString<8> Contents;
521 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
522 MCSection *Sec = nullptr)
523 : MCFragment(FT_Dwarf, false, 0, Sec), LineDelta(LineDelta),
524 AddrDelta(&AddrDelta) {
525 Contents.push_back(0);
531 int64_t getLineDelta() const { return LineDelta; }
533 const MCExpr &getAddrDelta() const { return *AddrDelta; }
535 SmallString<8> &getContents() { return Contents; }
536 const SmallString<8> &getContents() const { return Contents; }
540 static bool classof(const MCFragment *F) {
541 return F->getKind() == MCFragment::FT_Dwarf;
545 class MCDwarfCallFrameFragment : public MCFragment {
546 /// AddrDelta - The expression for the difference of the two symbols that
547 /// make up the address delta between two .cfi_* dwarf directives.
548 const MCExpr *AddrDelta;
550 SmallString<8> Contents;
553 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
554 : MCFragment(FT_DwarfFrame, false, 0, Sec), AddrDelta(&AddrDelta) {
555 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 /// Represents a symbol table index fragment.
574 class MCSymbolIdFragment : public MCFragment {
578 MCSymbolIdFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
579 : MCFragment(FT_SymbolId, false, 0, Sec), Sym(Sym) {}
584 const MCSymbol *getSymbol() { return Sym; }
585 const MCSymbol *getSymbol() const { return Sym; }
589 static bool classof(const MCFragment *F) {
590 return F->getKind() == MCFragment::FT_SymbolId;
594 /// Fragment representing the binary annotations produced by the
595 /// .cv_inline_linetable directive.
596 class MCCVInlineLineTableFragment : public MCFragment {
598 unsigned StartFileId;
599 unsigned StartLineNum;
600 const MCSymbol *FnStartSym;
601 const MCSymbol *FnEndSym;
602 SmallString<8> Contents;
604 /// CodeViewContext has the real knowledge about this format, so let it access
606 friend class CodeViewContext;
609 MCCVInlineLineTableFragment(unsigned SiteFuncId, unsigned StartFileId,
610 unsigned StartLineNum, const MCSymbol *FnStartSym,
611 const MCSymbol *FnEndSym,
612 MCSection *Sec = nullptr)
613 : MCFragment(FT_CVInlineLines, false, 0, Sec), SiteFuncId(SiteFuncId),
614 StartFileId(StartFileId), StartLineNum(StartLineNum),
615 FnStartSym(FnStartSym), FnEndSym(FnEndSym) {}
620 const MCSymbol *getFnStartSym() const { return FnStartSym; }
621 const MCSymbol *getFnEndSym() const { return FnEndSym; }
623 SmallString<8> &getContents() { return Contents; }
624 const SmallString<8> &getContents() const { return Contents; }
628 static bool classof(const MCFragment *F) {
629 return F->getKind() == MCFragment::FT_CVInlineLines;
633 /// Fragment representing the .cv_def_range directive.
634 class MCCVDefRangeFragment : public MCEncodedFragmentWithFixups<32, 4> {
635 SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 2> Ranges;
636 SmallString<32> FixedSizePortion;
638 /// CodeViewContext has the real knowledge about this format, so let it access
640 friend class CodeViewContext;
643 MCCVDefRangeFragment(
644 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
645 StringRef FixedSizePortion, MCSection *Sec = nullptr)
646 : MCEncodedFragmentWithFixups<32, 4>(FT_CVDefRange, false, Sec),
647 Ranges(Ranges.begin(), Ranges.end()),
648 FixedSizePortion(FixedSizePortion) {}
652 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> getRanges() const {
656 StringRef getFixedSizePortion() const { return FixedSizePortion; }
659 static bool classof(const MCFragment *F) {
660 return F->getKind() == MCFragment::FT_CVDefRange;
664 } // end namespace llvm
666 #endif // LLVM_MC_MCFRAGMENT_H