1//===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/SmallVector.h"
10#include "llvm/ADT/StringExtras.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/Config/llvm-config.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCFixup.h"
15#include "llvm/MC/MCSection.h"
16#include "llvm/MC/MCSectionMachO.h"
17#include "llvm/MC/MCSymbol.h"
18#include "llvm/Support/Casting.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cassert>
22#include <type_traits>
23#include <utility>
24
25using namespace llvm;
26
27static_assert(std::is_trivially_destructible_v<MCDataFragment>,
28 "fragment classes must be trivially destructible");
29
30MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
31 : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false),
32 LinkerRelaxable(false), AllowAutoPadding(false) {}
33
34const MCSymbol *MCFragment::getAtom() const {
35 return cast<MCSectionMachO>(Val: Parent)->getAtom(I: LayoutOrder);
36}
37
38#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
39LLVM_DUMP_METHOD void MCFragment::dump() const {
40 raw_ostream &OS = errs();
41
42 OS << Offset << ' ';
43 switch (getKind()) {
44 // clang-format off
45 case MCFragment::FT_Align: OS << "Align"; break;
46 case MCFragment::FT_Data: OS << "Data"; break;
47 case MCFragment::FT_Fill: OS << "Fill"; break;
48 case MCFragment::FT_Nops: OS << "Nops"; break;
49 case MCFragment::FT_Relaxable: OS << "Relaxable"; break;
50 case MCFragment::FT_Org: OS << "Org"; break;
51 case MCFragment::FT_Dwarf: OS << "Dwarf"; break;
52 case MCFragment::FT_DwarfFrame: OS << "DwarfCallFrame"; break;
53 case MCFragment::FT_LEB: OS << "LEB"; break;
54 case MCFragment::FT_BoundaryAlign: OS<<"BoundaryAlign"; break;
55 case MCFragment::FT_SymbolId: OS << "SymbolId"; break;
56 case MCFragment::FT_CVInlineLines: OS << "CVInlineLineTable"; break;
57 case MCFragment::FT_CVDefRange: OS << "CVDefRangeTable"; break;
58 case MCFragment::FT_PseudoProbe: OS << "PseudoProbe"; break;
59 // clang-format on
60 }
61
62 if (const auto *EF = dyn_cast<MCEncodedFragment>(this))
63 if (auto Pad = static_cast<unsigned>(EF->getBundlePadding()))
64 OS << " BundlePadding:" << Pad;
65
66 auto printFixups = [&](llvm::ArrayRef<MCFixup> Fixups) {
67 if (Fixups.empty())
68 return;
69 for (auto [I, F] : llvm::enumerate(Fixups)) {
70 OS << "\n Fixup @" << F.getOffset() << " Value:";
71 F.getValue()->print(OS, nullptr);
72 OS << " Kind:" << F.getKind();
73 }
74 };
75
76 switch (getKind()) {
77 case MCFragment::FT_Align: {
78 const auto *AF = cast<MCAlignFragment>(this);
79 OS << " Align:" << AF->getAlignment().value() << " Value:" << AF->getValue()
80 << " ValueSize:" << AF->getValueSize()
81 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit();
82 if (AF->hasEmitNops())
83 OS << " Nops";
84 break;
85 }
86 case MCFragment::FT_Data: {
87 const auto *F = cast<MCDataFragment>(this);
88 if (F->isLinkerRelaxable())
89 OS << " LinkerRelaxable";
90 auto Contents = F->getContents();
91 OS << " Size:" << Contents.size() << " [";
92 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
93 if (i) OS << ",";
94 OS << format("%02x", uint8_t(Contents[i]));
95 }
96 OS << ']';
97 printFixups(F->getFixups());
98 break;
99 }
100 case MCFragment::FT_Fill: {
101 const auto *FF = cast<MCFillFragment>(this);
102 OS << " Value:" << static_cast<unsigned>(FF->getValue())
103 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
104 << " NumValues:";
105 FF->getNumValues().print(OS, nullptr);
106 break;
107 }
108 case MCFragment::FT_Nops: {
109 const auto *NF = cast<MCNopsFragment>(this);
110 OS << " NumBytes:" << NF->getNumBytes()
111 << " ControlledNopLength:" << NF->getControlledNopLength();
112 break;
113 }
114 case MCFragment::FT_Relaxable: {
115 const auto *F = cast<MCRelaxableFragment>(this);
116 OS << " Size:" << F->getContents().size() << ' ';
117 F->getInst().dump_pretty(OS);
118 printFixups(F->getFixups());
119 break;
120 }
121 case MCFragment::FT_Org: {
122 const auto *OF = cast<MCOrgFragment>(this);
123 OS << " Offset:";
124 OF->getOffset().print(OS, nullptr);
125 OS << " Value:" << static_cast<unsigned>(OF->getValue());
126 break;
127 }
128 case MCFragment::FT_Dwarf: {
129 const auto *OF = cast<MCDwarfLineAddrFragment>(this);
130 OS << " AddrDelta:";
131 OF->getAddrDelta().print(OS, nullptr);
132 OS << " LineDelta:" << OF->getLineDelta();
133 break;
134 }
135 case MCFragment::FT_DwarfFrame: {
136 const auto *CF = cast<MCDwarfCallFrameFragment>(this);
137 OS << " AddrDelta:";
138 CF->getAddrDelta().print(OS, nullptr);
139 break;
140 }
141 case MCFragment::FT_LEB: {
142 const auto *LF = cast<MCLEBFragment>(this);
143 OS << " Value:";
144 LF->getValue().print(OS, nullptr);
145 OS << " Signed:" << LF->isSigned();
146 break;
147 }
148 case MCFragment::FT_BoundaryAlign: {
149 const auto *BF = cast<MCBoundaryAlignFragment>(this);
150 OS << " BoundarySize:" << BF->getAlignment().value()
151 << " LastFragment:" << BF->getLastFragment()
152 << " Size:" << BF->getSize();
153 break;
154 }
155 case MCFragment::FT_SymbolId: {
156 const auto *F = cast<MCSymbolIdFragment>(this);
157 OS << " Sym:" << F->getSymbol();
158 break;
159 }
160 case MCFragment::FT_CVInlineLines: {
161 const auto *F = cast<MCCVInlineLineTableFragment>(this);
162 OS << " Sym:" << *F->getFnStartSym();
163 break;
164 }
165 case MCFragment::FT_CVDefRange: {
166 const auto *F = cast<MCCVDefRangeFragment>(this);
167 OS << "\n ";
168 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
169 F->getRanges()) {
170 OS << " RangeStart:" << RangeStartEnd.first;
171 OS << " RangeEnd:" << RangeStartEnd.second;
172 }
173 break;
174 }
175 case MCFragment::FT_PseudoProbe: {
176 const auto *OF = cast<MCPseudoProbeAddrFragment>(this);
177 OS << " AddrDelta:";
178 OF->getAddrDelta().print(OS, nullptr);
179 break;
180 }
181 }
182}
183#endif
184