LLVM 22.0.0git
DWARFExpressionPrinter.cpp
Go to the documentation of this file.
1//===-- DWARFExpression.cpp -----------------------------------------------===//
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
13#include "llvm/Support/Format.h"
14#include <cassert>
15#include <cstdint>
16
17using namespace llvm;
18using namespace dwarf;
19
20namespace llvm {
21
24
26 DIDumpOptions DumpOpts,
28 unsigned Operand) {
29 assert(Operand < Operands.size() && "operand out of bounds");
30 if (!U) {
31 OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
32 return;
33 }
34 auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
35 if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
36 OS << " (";
37 if (DumpOpts.Verbose)
38 OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
39 OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
40 if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
41 OS << " \"" << *Name << "\"";
42 } else {
43 OS << format(" <invalid base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
44 }
45}
46
48 DIDumpOptions DumpOpts, const DWARFExpression *Expr,
49 DWARFUnit *U) {
50 if (Op->isError()) {
51 if (!DumpOpts.PrintRegisterOnly)
52 OS << "<decoding error>";
53 return false;
54 }
55
56 // In "register-only" mode, still show simple constant-valued locations.
57 // This lets clients print annotations like "i = 0" when the location is
58 // a constant (e.g. DW_OP_constu/consts ... DW_OP_stack_value).
59 // We continue to suppress all other non-register ops in this mode.
60 if (DumpOpts.PrintRegisterOnly) {
61 // First, try pretty-printing registers (existing behavior below also does
62 // this, but we need to short-circuit here to avoid printing opcode names).
63 if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) ||
64 (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) ||
65 Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx ||
66 Op->getCode() == DW_OP_regval_type) {
67 if (prettyPrintRegisterOp(U, OS, DumpOpts, Op->getCode(),
69 return true;
70 // If we couldn't pretty-print, fall through and suppress.
71 }
72
73 // Show constants (decimal), suppress everything else.
74 if (Op->getCode() == DW_OP_constu) {
76 return true;
77 }
78 if (Op->getCode() == DW_OP_consts) {
79 OS << (int64_t)Op->getRawOperand(0);
80 return true;
81 }
82 if (Op->getCode() >= DW_OP_lit0 && Op->getCode() <= DW_OP_lit31) {
83 OS << (unsigned)(Op->getCode() - DW_OP_lit0);
84 return true;
85 }
86 if (Op->getCode() == DW_OP_stack_value)
87 return true; // metadata; don't print a token
88
89 return true; // suppress other opcodes silently in register-only mode
90 }
91
92 if (!DumpOpts.PrintRegisterOnly) {
94 assert(!Name.empty() && "DW_OP has no name!");
95 OS << Name;
96 }
97
98 if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) ||
99 (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) ||
100 Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx ||
101 Op->getCode() == DW_OP_regval_type)
102 if (prettyPrintRegisterOp(U, OS, DumpOpts, Op->getCode(),
103 Op->getRawOperands()))
104 return true;
105
106 if (!DumpOpts.PrintRegisterOnly) {
107 for (unsigned Operand = 0; Operand < Op->getDescription().Op.size();
108 ++Operand) {
109 unsigned Size = Op->getDescription().Op[Operand];
111
114 Op->getCode(), Op->getRawOperand(Operand));
115 assert(!SubName.empty() && "DW_OP SubOp has no name!");
116 OS << " " << SubName;
118 // For DW_OP_convert the operand may be 0 to indicate that conversion to
119 // the generic type should be done. The same holds for
120 // DW_OP_reinterpret, which is currently not supported.
121 if (Op->getCode() == DW_OP_convert && Op->getRawOperand(Operand) == 0)
122 OS << " 0x0";
123 else
125 Operand);
127 assert(Operand == 1);
128 switch (Op->getRawOperand(0)) {
129 case 0:
130 case 1:
131 case 2:
132 case 3: // global as uint32
133 case 4:
134 OS << format(" 0x%" PRIx64, Op->getRawOperand(Operand));
135 break;
136 default:
137 assert(false);
138 }
140 uint64_t Offset = Op->getRawOperand(Operand);
141 for (unsigned i = 0; i < Op->getRawOperand(Operand - 1); ++i)
142 OS << format(" 0x%02x",
143 static_cast<uint8_t>(Expr->getData()[Offset++]));
144 } else {
145 if (Signed)
146 OS << format(" %+" PRId64, (int64_t)Op->getRawOperand(Operand));
147 else if (Op->getCode() != DW_OP_entry_value &&
148 Op->getCode() != DW_OP_GNU_entry_value)
149 OS << format(" 0x%" PRIx64, Op->getRawOperand(Operand));
150 }
151 }
152 }
153 return true;
154}
155
157 DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH) {
158 uint32_t EntryValExprSize = 0;
159 uint64_t EntryValStartOffset = 0;
160 if (E->getData().empty())
161 OS << "<empty>";
162
163 for (auto &Op : *E) {
164 DumpOpts.IsEH = IsEH;
165 if (!printOp(&Op, OS, DumpOpts, E, U) && !DumpOpts.PrintRegisterOnly) {
166 uint64_t FailOffset = Op.getEndOffset();
167 while (FailOffset < E->getData().size())
168 OS << format(" %02x", static_cast<uint8_t>(E->getData()[FailOffset++]));
169 return;
170 }
171 if (!DumpOpts.PrintRegisterOnly) {
172 if (Op.getCode() == DW_OP_entry_value ||
173 Op.getCode() == DW_OP_GNU_entry_value) {
174 OS << "(";
175 EntryValExprSize = Op.getRawOperand(0);
176 EntryValStartOffset = Op.getEndOffset();
177 continue;
178 }
179
180 if (EntryValExprSize) {
181 EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;
182 if (EntryValExprSize == 0)
183 OS << ")";
184 }
185
186 if (Op.getEndOffset() < E->getData().size())
187 OS << ", ";
188 }
189 }
190}
191
192/// A user-facing string representation of a DWARF expression. This might be an
193/// Address expression, in which case it will be implicitly dereferenced, or a
194/// Value expression.
196 enum ExprKind {
199 };
202
204};
205
209 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg =
210 nullptr) {
212
213 while (I != E) {
215 uint8_t Opcode = Op.getCode();
216 switch (Opcode) {
217 case dwarf::DW_OP_regx: {
218 // DW_OP_regx: A register, with the register num given as an operand.
219 // Printed as the plain register name.
220 uint64_t DwarfRegNum = Op.getRawOperand(0);
221 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
222 if (RegName.empty())
223 return false;
224 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
225 S << RegName;
226 break;
227 }
228 case dwarf::DW_OP_bregx: {
229 int DwarfRegNum = Op.getRawOperand(0);
230 int64_t Offset = Op.getRawOperand(1);
231 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
232 if (RegName.empty())
233 return false;
234 raw_svector_ostream S(Stack.emplace_back().String);
235 S << RegName;
236 if (Offset)
237 S << format("%+" PRId64, Offset);
238 break;
239 }
240 case dwarf::DW_OP_entry_value:
241 case dwarf::DW_OP_GNU_entry_value: {
242 // DW_OP_entry_value contains a sub-expression which must be rendered
243 // separately.
244 uint64_t SubExprLength = Op.getRawOperand(0);
245 DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);
246 ++I;
247 raw_svector_ostream S(Stack.emplace_back().String);
248 S << "entry(";
249 printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg);
250 S << ")";
251 I = SubExprEnd;
252 continue;
253 }
254 case dwarf::DW_OP_stack_value: {
255 // The top stack entry should be treated as the actual value of tne
256 // variable, rather than the address of the variable in memory.
257 assert(!Stack.empty());
258 Stack.back().Kind = PrintedExpr::Value;
259 break;
260 }
261 case dwarf::DW_OP_nop: {
262 break;
263 }
264 case dwarf::DW_OP_LLVM_user: {
265 assert(Op.getSubCode() == dwarf::DW_OP_LLVM_nop);
266 break;
267 }
268 default:
269 if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {
270 // DW_OP_reg<N>: A register, with the register num implied by the
271 // opcode. Printed as the plain register name.
272 uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;
273 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
274 if (RegName.empty())
275 return false;
276 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
277 S << RegName;
278 } else if (Opcode >= dwarf::DW_OP_breg0 &&
279 Opcode <= dwarf::DW_OP_breg31) {
280 int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;
281 int64_t Offset = Op.getRawOperand(0);
282 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
283 if (RegName.empty())
284 return false;
285 raw_svector_ostream S(Stack.emplace_back().String);
286 S << RegName;
287 if (Offset)
288 S << format("%+" PRId64, Offset);
289 } else {
290 // If we hit an unknown operand, we don't know its effect on the stack,
291 // so bail out on the whole expression.
292 OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("
293 << (int)Opcode << ")>";
294 return false;
295 }
296 break;
297 }
298 ++I;
299 }
300
301 if (Stack.size() != 1) {
302 OS << "<stack of size " << Stack.size() << ", expected 1>";
303 return false;
304 }
305
306 if (Stack.front().Kind == PrintedExpr::Address)
307 OS << "[" << Stack.front().String << "]";
308 else
309 OS << Stack.front().String;
310
311 return true;
312}
313
315 const DWARFExpression *E, raw_ostream &OS,
316 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
317 return printCompactDWARFExpr(OS, E->begin(), E->end(), GetNameForDWARFReg);
318}
319
321 DIDumpOptions DumpOpts, uint8_t Opcode,
323 if (!DumpOpts.GetNameForDWARFReg)
324 return false;
325
326 uint64_t DwarfRegNum;
327 unsigned OpNum = 0;
328
329 if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
330 Opcode == DW_OP_regval_type)
331 DwarfRegNum = Operands[OpNum++];
332 else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
333 DwarfRegNum = Opcode - DW_OP_breg0;
334 else
335 DwarfRegNum = Opcode - DW_OP_reg0;
336
337 auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH);
338 if (!RegName.empty()) {
339 if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
340 Opcode == DW_OP_bregx)
341 OS << ' ' << RegName << format("%+" PRId64, Operands[OpNum]);
342 else
343 OS << ' ' << RegName.data();
344
345 if (Opcode == DW_OP_regval_type)
346 prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);
347 return true;
348 }
349
350 return false;
351}
352
353} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::string Name
uint64_t Size
#define RegName(no)
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
raw_pwrite_stream & OS
This file defines the SmallString class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents an Operation in the Expression.
LLVM_ABI std::optional< unsigned > getSubCode() const
ArrayRef< uint64_t > getRawOperands() const
@ SizeSubOpLEB
The operand is a ULEB128 encoded SubOpcode.
@ SizeBlock
Preceding operand contains block size.
const Description & getDescription() const
uint64_t getRawOperand(unsigned Idx) const
An iterator to go through the expression operations.
iterator end() const
StringRef getData() const
iterator begin() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:692
LLVM_ABI StringRef SubOperationEncodingString(unsigned OpEncoding, unsigned SubOpEncoding)
Definition: Dwarf.cpp:202
LLVM_ABI StringRef OperationEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:138
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1702
static bool printOp(const DWARFExpression::Operation *Op, raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, DWARFUnit *U)
LLVM_ABI void printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false)
Print a Dwarf expression/.
Op::Description Desc
static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, const DWARFExpression::iterator E, std::function< StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg=nullptr)
static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, ArrayRef< uint64_t > Operands, unsigned Operand)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126
DWARFExpression::Operation Op
LLVM_ABI bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, uint8_t Opcode, ArrayRef< uint64_t > Operands)
Pretty print a register opcode and operands.
LLVM_ABI bool printDwarfExpressionCompact(const DWARFExpression *E, raw_ostream &OS, std::function< StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg=nullptr)
Print the expression in a format intended to be compact and useful to a user, but not perfectly unamb...
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition: DIContext.h:215
Description of the encoding of one expression Op.
SmallVector< Encoding > Op
Encoding for Op operands.
A user-facing string representation of a DWARF expression.
PrintedExpr(ExprKind K=Address)