LLVM 19.0.0git
DWARFExpression.h
Go to the documentation of this file.
1//===--- DWARFExpression.h - DWARF Expression handling ----------*- C++ -*-===//
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#ifndef LLVM_DEBUGINFO_DWARF_DWARFEXPRESSION_H
10#define LLVM_DEBUGINFO_DWARF_DWARFEXPRESSION_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/iterator.h"
16
17namespace llvm {
18class DWARFUnit;
19struct DIDumpOptions;
20class MCRegisterInfo;
21class raw_ostream;
22
24public:
25 class iterator;
26
27 /// This class represents an Operation in the Expression.
28 ///
29 /// An Operation can be in Error state (check with isError()). This
30 /// means that it couldn't be decoded successfully and if it is the
31 /// case, all others fields contain undefined values.
32 class Operation {
33 public:
34 /// Size and signedness of expression operations' operands.
35 enum Encoding : uint8_t {
36 Size1 = 0,
37 Size2 = 1,
38 Size4 = 2,
39 Size8 = 3,
43 SizeBlock = 7, ///< Preceding operand contains block size
45 /// The operand is a ULEB128 encoded SubOpcode. This is only valid
46 /// for the first operand of an operation.
49 SignBit = 0x80,
55 };
56
57 enum DwarfVersion : uint8_t {
58 DwarfNA, ///< Serves as a marker for unused entries
59 Dwarf2 = 2,
62 Dwarf5
63 };
64
65 /// Description of the encoding of one expression Op.
66 struct Description {
67 DwarfVersion Version; ///< Dwarf version where the Op was introduced.
68 SmallVector<Encoding> Op; ///< Encoding for Op operands.
69
70 template <typename... Ts>
72 : Version(Version), Op{Op...} {}
74 ~Description() = default;
75 };
76
77 private:
79 uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
81 bool Error = false;
82 uint64_t EndOffset;
84 SmallVector<uint64_t> OperandEndOffsets;
85
86 public:
87 const Description &getDescription() const { return Desc; }
88 uint8_t getCode() const { return Opcode; }
89 std::optional<unsigned> getSubCode() const;
90 uint64_t getNumOperands() const { return Operands.size(); }
92 uint64_t getRawOperand(unsigned Idx) const { return Operands[Idx]; }
94 return OperandEndOffsets;
95 }
97 return OperandEndOffsets[Idx];
98 }
99 uint64_t getEndOffset() const { return EndOffset; }
100 bool isError() const { return Error; }
101 bool print(raw_ostream &OS, DIDumpOptions DumpOpts,
102 const DWARFExpression *Expr, DWARFUnit *U) const;
103
104 /// Verify \p Op. Does not affect the return of \a isError().
105 static bool verify(const Operation &Op, DWARFUnit *U);
106
107 private:
108 bool extract(DataExtractor Data, uint8_t AddressSize, uint64_t Offset,
109 std::optional<dwarf::DwarfFormat> Format);
110 };
111
112 /// An iterator to go through the expression operations.
114 : public iterator_facade_base<iterator, std::forward_iterator_tag,
115 const Operation> {
116 friend class DWARFExpression;
117 const DWARFExpression *Expr;
121 : Expr(Expr), Offset(Offset) {
122 Op.Error =
123 Offset >= Expr->Data.getData().size() ||
124 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
125 }
126
127 public:
129 Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
130 Op.Error =
131 Offset >= Expr->Data.getData().size() ||
132 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
133 return *this;
134 }
135
136 const Operation &operator*() const { return Op; }
137
139 return iterator(Expr, Op.EndOffset + Add);
140 }
141
142 // Comparison operators are provided out of line.
143 friend bool operator==(const iterator &, const iterator &);
144 };
145
146 DWARFExpression(DataExtractor Data, uint8_t AddressSize,
147 std::optional<dwarf::DwarfFormat> Format = std::nullopt)
148 : Data(Data), AddressSize(AddressSize), Format(Format) {
149 assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
150 }
151
152 iterator begin() const { return iterator(this, 0); }
153 iterator end() const { return iterator(this, Data.getData().size()); }
154
155 void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U,
156 bool IsEH = false) const;
157
158 /// Print the expression in a format intended to be compact and useful to a
159 /// user, but not perfectly unambiguous, or capable of representing every
160 /// valid DWARF expression. Returns true if the expression was sucessfully
161 /// printed.
163 std::function<StringRef(uint64_t RegNum, bool IsEH)>
164 GetNameForDWARFReg = nullptr);
165
166 bool verify(DWARFUnit *U);
167
168 bool operator==(const DWARFExpression &RHS) const;
169
170 StringRef getData() const { return Data.getData(); }
171
173 DIDumpOptions DumpOpts, uint8_t Opcode,
175
176private:
177 DataExtractor Data;
178 uint8_t AddressSize;
179 std::optional<dwarf::DwarfFormat> Format;
180};
181
184 return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
185}
186}
187#endif
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file contains constants used for implementing Dwarf debug support.
uint64_t Offset
Definition: ELF_riscv.cpp:478
loop extract
mir Rename Register Operands
ppc ctr loops verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Value * LHS
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.
std::optional< unsigned > getSubCode() const
@ DwarfNA
Serves as a marker for unused entries.
bool print(raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, DWARFUnit *U) const
ArrayRef< uint64_t > getRawOperands() const
Encoding
Size and signedness of expression operations' operands.
@ SizeSubOpLEB
The operand is a ULEB128 encoded SubOpcode.
@ SizeBlock
Preceding operand contains block size.
const Description & getDescription() const
ArrayRef< uint64_t > getOperandEndOffsets() const
uint64_t getOperandEndOffset(unsigned Idx) const
uint64_t getRawOperand(unsigned Idx) const
An iterator to go through the expression operations.
const Operation & operator*() const
friend bool operator==(const iterator &, const iterator &)
iterator skipBytes(uint64_t Add) const
iterator end() const
StringRef getData() const
iterator begin() const
DWARFExpression(DataExtractor Data, uint8_t AddressSize, std::optional< dwarf::DwarfFormat > Format=std::nullopt)
bool printCompact(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...
bool operator==(const DWARFExpression &RHS) const
void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false) const
static bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, uint8_t Opcode, const ArrayRef< uint64_t > Operands)
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:95
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
@ Add
Sum of integers.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:193
Description of the encoding of one expression Op.
Description(DwarfVersion Version, Ts... Op)
DwarfVersion Version
Dwarf version where the Op was introduced.
SmallVector< Encoding > Op
Encoding for Op operands.