LLVM 22.0.0git
MCFragment.cpp
Go to the documentation of this file.
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
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"
17#include "llvm/MC/MCSymbol.h"
21#include <cassert>
22#include <type_traits>
23#include <utility>
24
25using namespace llvm;
26
27static_assert(std::is_trivially_destructible_v<MCFragment>,
28 "fragment classes must be trivially destructible");
29
30MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
31 : Kind(Kind), LinkerRelaxable(false), HasInstructions(HasInstructions),
32 AllowAutoPadding(false) {
33 static_assert(sizeof(MCFragment::Tail) <= 16,
34 "Keep the variable-size tail small");
35}
36
38 return static_cast<const MCSectionMachO *>(Parent)->getAtom(LayoutOrder);
39}
40
41#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
43 raw_ostream &OS = errs();
44
45 OS << Offset << ' ';
46 switch (getKind()) {
47 // clang-format off
48 case MCFragment::FT_Align: OS << "Align"; break;
49 case MCFragment::FT_Data: OS << "Data"; break;
50 case MCFragment::FT_Fill: OS << "Fill"; break;
51 case MCFragment::FT_Nops: OS << "Nops"; break;
52 case MCFragment::FT_Relaxable: OS << "Relaxable"; break;
53 case MCFragment::FT_Org: OS << "Org"; break;
54 case MCFragment::FT_Dwarf: OS << "Dwarf"; break;
55 case MCFragment::FT_DwarfFrame: OS << "DwarfCallFrame"; break;
56 case MCFragment::FT_LEB: OS << "LEB"; break;
57 case MCFragment::FT_BoundaryAlign: OS<<"BoundaryAlign"; break;
58 case MCFragment::FT_SymbolId: OS << "SymbolId"; break;
59 case MCFragment::FT_CVInlineLines: OS << "CVInlineLineTable"; break;
60 case MCFragment::FT_CVDefRange: OS << "CVDefRangeTable"; break;
61 // clang-format on
62 }
63
64 auto printFixups = [&](llvm::ArrayRef<MCFixup> Fixups) {
65 if (Fixups.empty())
66 return;
67 for (auto [I, F] : llvm::enumerate(Fixups)) {
68 OS << "\n Fixup @" << F.getOffset() << " Value:";
69 F.getValue()->print(OS, nullptr);
70 OS << " Kind:" << F.getKind();
71 if (F.isLinkerRelaxable())
72 OS << " LinkerRelaxable";
73 }
74 };
75
76 switch (getKind()) {
84 OS << " LinkerRelaxable";
85 auto Fixed = getContents();
86 auto Var = getVarContents();
87 OS << " Size:" << Fixed.size();
89 OS << '+' << Var.size();
90 // FT_Align uses getVarContents to track the size, but the content is
91 // ignored and not useful.
93 Var = {};
94 }
95 OS << " [";
96 for (unsigned i = 0, e = Fixed.size(); i != e; ++i) {
97 if (i) OS << ",";
98 OS << format("%02x", uint8_t(Fixed[i]));
99 }
100 for (unsigned i = 0, e = Var.size(); i != e; ++i) {
101 if (Fixed.size() || i)
102 OS << ",";
103 OS << format("%02x", uint8_t(Var[i]));
104 }
105 OS << ']';
106 switch (getKind()) {
108 break;
110 OS << ' ';
112 break;
114 OS << "\n Align:" << getAlignment().value() << " Fill:" << getAlignFill()
115 << " FillLen:" << unsigned(getAlignFillLen())
116 << " MaxBytesToEmit:" << getAlignMaxBytesToEmit();
117 if (hasAlignEmitNops())
118 OS << " Nops";
119 break;
120 case MCFragment::FT_LEB: {
121 OS << " Value:";
122 getLEBValue().print(OS, nullptr);
123 OS << " Signed:" << isLEBSigned();
124 break;
125 }
127 OS << " AddrDelta:";
128 getDwarfAddrDelta().print(OS, nullptr);
129 OS << " LineDelta:" << getDwarfLineDelta();
130 break;
132 OS << " AddrDelta:";
133 getDwarfAddrDelta().print(OS, nullptr);
134 break;
135 default:
137 }
138 printFixups(getFixups());
139 printFixups(getVarFixups());
140 break;
141 }
142 case MCFragment::FT_Fill: {
143 const auto *FF = cast<MCFillFragment>(this);
144 OS << " Value:" << static_cast<unsigned>(FF->getValue())
145 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
146 << " NumValues:";
147 FF->getNumValues().print(OS, nullptr);
148 break;
149 }
150 case MCFragment::FT_Nops: {
151 const auto *NF = cast<MCNopsFragment>(this);
152 OS << " NumBytes:" << NF->getNumBytes()
153 << " ControlledNopLength:" << NF->getControlledNopLength();
154 break;
155 }
156 case MCFragment::FT_Org: {
157 const auto *OF = cast<MCOrgFragment>(this);
158 OS << " Offset:";
159 OF->getOffset().print(OS, nullptr);
160 OS << " Value:" << static_cast<unsigned>(OF->getValue());
161 break;
162 }
164 const auto *BF = cast<MCBoundaryAlignFragment>(this);
165 OS << " BoundarySize:" << BF->getAlignment().value()
166 << " LastFragment:" << BF->getLastFragment()
167 << " Size:" << BF->getSize();
168 break;
169 }
171 const auto *F = cast<MCSymbolIdFragment>(this);
172 OS << " Sym:" << F->getSymbol();
173 break;
174 }
176 const auto *F = cast<MCCVInlineLineTableFragment>(this);
177 OS << " Sym:" << *F->getFnStartSym();
178 break;
179 }
181 const auto *F = cast<MCCVDefRangeFragment>(this);
182 OS << "\n ";
183 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
184 F->getRanges()) {
185 OS << " RangeStart:" << RangeStartEnd.first;
186 OS << " RangeEnd:" << RangeStartEnd.second;
187 }
188 break;
189 }
190 }
191}
192#endif
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const MCExpr & getDwarfAddrDelta() const
Definition: MCSection.h:283
MutableArrayRef< char > getContents()
Definition: MCSection.h:615
FragmentType getKind() const
Definition: MCSection.h:156
bool isLinkerRelaxable() const
Definition: MCSection.h:183
LLVM_ABI MCFragment(FragmentType Kind=MCFragment::FT_Data, bool HasInstructions=false)
Definition: MCFragment.cpp:30
bool hasAlignEmitNops() const
Definition: MCSection.h:257
MCInst getInst() const
Definition: MCSection.h:656
LLVM_ABI const MCSymbol * getAtom() const
Definition: MCFragment.cpp:37
unsigned getAlignMaxBytesToEmit() const
Definition: MCSection.h:253
int64_t getDwarfLineDelta() const
Definition: MCSection.h:291
bool isLEBSigned() const
Definition: MCSection.h:277
MutableArrayRef< MCFixup > getFixups()
Definition: MCSection.h:633
const MCExpr & getLEBValue() const
Definition: MCSection.h:269
LLVM_ABI void dump() const
Definition: MCFragment.cpp:42
Align getAlignment() const
Definition: MCSection.h:241
int64_t getAlignFill() const
Definition: MCSection.h:245
uint8_t getAlignFillLen() const
Definition: MCSection.h:249
MutableArrayRef< char > getVarContents()
Definition: MCSection.h:622
MutableArrayRef< MCFixup > getVarFixups()
Definition: MCSection.h:642
LLVM_ABI void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ", const MCContext *Ctx=nullptr) const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
Definition: MCInst.cpp:90
This represents a section on a Mach-O system (used by Mac OS X).
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85