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_SFrame: OS << "SFrame"; break;
57 case MCFragment::FT_LEB: OS << "LEB"; break;
58 case MCFragment::FT_BoundaryAlign: OS<<"BoundaryAlign"; break;
59 case MCFragment::FT_SymbolId: OS << "SymbolId"; break;
60 case MCFragment::FT_CVInlineLines: OS << "CVInlineLineTable"; break;
61 case MCFragment::FT_CVDefRange: OS << "CVDefRangeTable"; break;
62 // clang-format on
63 }
64
65 auto printFixups = [&](llvm::ArrayRef<MCFixup> Fixups) {
66 if (Fixups.empty())
67 return;
68 for (auto [I, F] : llvm::enumerate(Fixups)) {
69 OS << "\n Fixup @" << F.getOffset() << " Value:";
70 F.getValue()->print(OS, nullptr);
71 OS << " Kind:" << F.getKind();
72 if (F.isLinkerRelaxable())
73 OS << " LinkerRelaxable";
74 }
75 };
76
77 switch (getKind()) {
86 OS << " LinkerRelaxable";
87 auto Fixed = getContents();
88 auto Var = getVarContents();
89 OS << " Size:" << Fixed.size();
91 OS << '+' << Var.size();
92 // FT_Align uses getVarContents to track the size, but the content is
93 // ignored and not useful.
95 Var = {};
96 }
97 OS << " [";
98 for (unsigned i = 0, e = Fixed.size(); i != e; ++i) {
99 if (i) OS << ",";
100 OS << format("%02x", uint8_t(Fixed[i]));
101 }
102 for (unsigned i = 0, e = Var.size(); i != e; ++i) {
103 if (Fixed.size() || i)
104 OS << ",";
105 OS << format("%02x", uint8_t(Var[i]));
106 }
107 OS << ']';
108 switch (getKind()) {
110 break;
112 OS << ' ';
113 getInst().dump_pretty(OS);
114 break;
116 OS << "\n Align:" << getAlignment().value() << " Fill:" << getAlignFill()
117 << " FillLen:" << unsigned(getAlignFillLen())
118 << " MaxBytesToEmit:" << getAlignMaxBytesToEmit();
119 if (hasAlignEmitNops())
120 OS << " Nops";
121 break;
122 case MCFragment::FT_LEB: {
123 OS << " Value:";
124 getLEBValue().print(OS, nullptr);
125 OS << " Signed:" << isLEBSigned();
126 break;
127 }
129 OS << " AddrDelta:";
130 getDwarfAddrDelta().print(OS, nullptr);
131 OS << " LineDelta:" << getDwarfLineDelta();
132 break;
135 OS << " AddrDelta:";
136 getDwarfAddrDelta().print(OS, nullptr);
137 break;
138 default:
140 }
141 printFixups(getFixups());
142 printFixups(getVarFixups());
143 break;
144 }
145 case MCFragment::FT_Fill: {
146 const auto *FF = cast<MCFillFragment>(this);
147 OS << " Value:" << static_cast<unsigned>(FF->getValue())
148 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
149 << " NumValues:";
150 FF->getNumValues().print(OS, nullptr);
151 break;
152 }
153 case MCFragment::FT_Nops: {
154 const auto *NF = cast<MCNopsFragment>(this);
155 OS << " NumBytes:" << NF->getNumBytes()
156 << " ControlledNopLength:" << NF->getControlledNopLength();
157 break;
158 }
159 case MCFragment::FT_Org: {
160 const auto *OF = cast<MCOrgFragment>(this);
161 OS << " Offset:";
162 OF->getOffset().print(OS, nullptr);
163 OS << " Value:" << static_cast<unsigned>(OF->getValue());
164 break;
165 }
167 const auto *BF = cast<MCBoundaryAlignFragment>(this);
168 OS << " BoundarySize:" << BF->getAlignment().value()
169 << " LastFragment:" << BF->getLastFragment()
170 << " Size:" << BF->getSize();
171 break;
172 }
174 const auto *F = cast<MCSymbolIdFragment>(this);
175 OS << " Sym:" << F->getSymbol();
176 break;
177 }
179 const auto *F = cast<MCCVInlineLineTableFragment>(this);
180 OS << " Sym:" << *F->getFnStartSym();
181 break;
182 }
184 const auto *F = cast<MCCVDefRangeFragment>(this);
185 OS << "\n ";
186 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
187 F->getRanges()) {
188 OS << " RangeStart:" << RangeStartEnd.first;
189 OS << " RangeEnd:" << RangeStartEnd.second;
190 }
191 break;
192 }
193 }
194}
195#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
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:290
MutableArrayRef< char > getContents()
Definition MCSection.h:640
FragmentType getKind() const
Definition MCSection.h:163
bool isLinkerRelaxable() const
Definition MCSection.h:190
LLVM_ABI MCFragment(FragmentType Kind=MCFragment::FT_Data, bool HasInstructions=false)
bool hasAlignEmitNops() const
Definition MCSection.h:264
MCInst getInst() const
Definition MCSection.h:681
LLVM_ABI const MCSymbol * getAtom() const
unsigned getAlignMaxBytesToEmit() const
Definition MCSection.h:260
int64_t getDwarfLineDelta() const
Definition MCSection.h:298
bool isLEBSigned() const
Definition MCSection.h:284
MutableArrayRef< MCFixup > getFixups()
Definition MCSection.h:658
const MCExpr & getLEBValue() const
Definition MCSection.h:276
LLVM_ABI void dump() const
Align getAlignment() const
Definition MCSection.h:248
int64_t getAlignFill() const
Definition MCSection.h:252
uint8_t getAlignFillLen() const
Definition MCSection.h:256
MutableArrayRef< char > getVarContents()
Definition MCSection.h:647
MutableArrayRef< MCFixup > getVarFixups()
Definition MCSection.h:667
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.
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:2452
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.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:85