LLVM 23.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_SFrame: OS << "SFrame"; break;
57 case MCFragment::FT_LEB: OS << "LEB"; break;
58 case MCFragment::FT_BoundaryAlign: OS << "BoundaryAlign"; break;
59 case MCFragment::FT_PrefAlign: OS << "PrefAlign"; break;
60 case MCFragment::FT_SymbolId: OS << "SymbolId"; break;
61 case MCFragment::FT_CVInlineLines: OS << "CVInlineLineTable"; break;
62 case MCFragment::FT_CVDefRange: OS << "CVDefRangeTable"; break;
63 // clang-format on
64 }
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 if (F.isLinkerRelaxable())
74 OS << " LinkerRelaxable";
75 }
76 };
77
78 switch (getKind()) {
87 OS << " LinkerRelaxable";
88 auto Fixed = getContents();
89 auto Var = getVarContents();
90 OS << " Size:" << Fixed.size();
92 OS << '+' << Var.size();
93 // FT_Align uses getVarContents to track the size, but the content is
94 // ignored and not useful.
96 Var = {};
97 }
98 OS << " [";
99 for (unsigned i = 0, e = Fixed.size(); i != e; ++i) {
100 if (i) OS << ",";
101 OS << format("%02x", uint8_t(Fixed[i]));
102 }
103 for (unsigned i = 0, e = Var.size(); i != e; ++i) {
104 if (Fixed.size() || i)
105 OS << ",";
106 OS << format("%02x", uint8_t(Var[i]));
107 }
108 OS << ']';
109 switch (getKind()) {
111 break;
113 OS << ' ';
114 getInst().dump_pretty(OS);
115 break;
117 OS << "\n Align:" << getAlignment().value() << " Fill:" << getAlignFill()
118 << " FillLen:" << unsigned(getAlignFillLen())
119 << " MaxBytesToEmit:" << getAlignMaxBytesToEmit();
120 if (hasAlignEmitNops())
121 OS << " Nops";
122 break;
123 case MCFragment::FT_LEB: {
124 OS << " Value:";
125 getLEBValue().print(OS, nullptr);
126 OS << " Signed:" << isLEBSigned();
127 break;
128 }
130 OS << " AddrDelta:";
131 getDwarfAddrDelta().print(OS, nullptr);
132 OS << " LineDelta:" << getDwarfLineDelta();
133 break;
136 OS << " AddrDelta:";
137 getDwarfAddrDelta().print(OS, nullptr);
138 break;
139 default:
141 }
142 printFixups(getFixups());
143 printFixups(getVarFixups());
144 break;
145 }
146 case MCFragment::FT_Fill: {
147 const auto *FF = cast<MCFillFragment>(this);
148 OS << " Value:" << static_cast<unsigned>(FF->getValue())
149 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
150 << " NumValues:";
151 FF->getNumValues().print(OS, nullptr);
152 break;
153 }
154 case MCFragment::FT_Nops: {
155 const auto *NF = cast<MCNopsFragment>(this);
156 OS << " NumBytes:" << NF->getNumBytes()
157 << " ControlledNopLength:" << NF->getControlledNopLength();
158 break;
159 }
160 case MCFragment::FT_Org: {
161 const auto *OF = cast<MCOrgFragment>(this);
162 OS << " Offset:";
163 OF->getOffset().print(OS, nullptr);
164 OS << " Value:" << static_cast<unsigned>(OF->getValue());
165 break;
166 }
168 const auto *BF = cast<MCBoundaryAlignFragment>(this);
169 OS << " BoundarySize:" << BF->getAlignment().value()
170 << " LastFragment:" << BF->getLastFragment()
171 << " Size:" << BF->getSize();
172 break;
173 }
175 OS << " PrefAlign:" << getPrefAlignPreferred().value()
176 << " End:" << getPrefAlignEnd().getName()
177 << " ComputedAlign:" << getPrefAlignComputed().value();
178 break;
180 const auto *F = cast<MCSymbolIdFragment>(this);
181 OS << " Sym:" << F->getSymbol();
182 break;
183 }
185 const auto *F = cast<MCCVInlineLineTableFragment>(this);
186 OS << " Sym:" << *F->getFnStartSym();
187 break;
188 }
190 const auto *F = cast<MCCVDefRangeFragment>(this);
191 OS << "\n ";
192 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
193 F->getRanges()) {
194 OS << " RangeStart:" << RangeStartEnd.first;
195 OS << " RangeEnd:" << RangeStartEnd.second;
196 }
197 break;
198 }
199 }
200}
201#endif
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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:40
const MCExpr & getDwarfAddrDelta() const
Definition MCSection.h:345
MutableArrayRef< char > getContents()
Definition MCSection.h:689
Align getPrefAlignPreferred() const
Definition MCSection.h:303
FragmentType getKind() const
Definition MCSection.h:179
bool isLinkerRelaxable() const
Definition MCSection.h:206
LLVM_ABI MCFragment(FragmentType Kind=MCFragment::FT_Data, bool HasInstructions=false)
bool hasAlignEmitNops() const
Definition MCSection.h:280
MCInst getInst() const
Definition MCSection.h:730
LLVM_ABI const MCSymbol * getAtom() const
unsigned getAlignMaxBytesToEmit() const
Definition MCSection.h:276
int64_t getDwarfLineDelta() const
Definition MCSection.h:353
bool isLEBSigned() const
Definition MCSection.h:339
MutableArrayRef< MCFixup > getFixups()
Definition MCSection.h:707
const MCExpr & getLEBValue() const
Definition MCSection.h:331
LLVM_ABI void dump() const
Align getPrefAlignComputed() const
Definition MCSection.h:307
Align getAlignment() const
Definition MCSection.h:264
int64_t getAlignFill() const
Definition MCSection.h:268
uint8_t getAlignFillLen() const
Definition MCSection.h:272
const MCSymbol & getPrefAlignEnd() const
Definition MCSection.h:299
MutableArrayRef< char > getVarContents()
Definition MCSection.h:696
MutableArrayRef< MCFixup > getVarFixups()
Definition MCSection.h:716
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
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
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.
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:2554
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77