LLVM 22.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_LOWLEVEL_DWARFEXPRESSION_H
10#define LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFEXPRESSION_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/iterator.h"
17
18namespace llvm {
19class DWARFUnit;
20struct DIDumpOptions;
21class MCRegisterInfo;
22class raw_ostream;
23
25public:
26 class iterator;
27
28 /// This class represents an Operation in the Expression.
29 ///
30 /// An Operation can be in Error state (check with isError()). This
31 /// means that it couldn't be decoded successfully and if it is the
32 /// case, all others fields contain undefined values.
33 class Operation {
34 public:
35 /// Size and signedness of expression operations' operands.
37 Size1 = 0,
38 Size2 = 1,
39 Size4 = 2,
40 Size8 = 3,
44 SizeBlock = 7, ///< Preceding operand contains block size
46 /// The operand is a ULEB128 encoded SubOpcode. This is only valid
47 /// for the first operand of an operation.
50 SignBit = 0x80,
56 };
57
59 DwarfNA, ///< Serves as a marker for unused entries
60 Dwarf2 = 2,
63 Dwarf5
64 };
65
66 /// Description of the encoding of one expression Op.
67 struct Description {
68 DwarfVersion Version; ///< Dwarf version where the Op was introduced.
69 SmallVector<Encoding> Op; ///< Encoding for Op operands.
70
71 template <typename... Ts>
73 : Version(Version), Op{Op...} {}
75 ~Description() = default;
76 };
77
78 private:
80 friend class DWARFVerifier;
81
82 uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
84 bool Error = false;
85 uint64_t EndOffset;
87 SmallVector<uint64_t> OperandEndOffsets;
88
89 public:
90 const Description &getDescription() const { return Desc; }
91 uint8_t getCode() const { return Opcode; }
92 LLVM_ABI std::optional<unsigned> getSubCode() const;
93 uint64_t getNumOperands() const { return Operands.size(); }
95 uint64_t getRawOperand(unsigned Idx) const { return Operands[Idx]; }
97 return OperandEndOffsets;
98 }
100 return OperandEndOffsets[Idx];
101 }
102 uint64_t getEndOffset() const { return EndOffset; }
103 bool isError() const { return Error; }
104
105 private:
106 LLVM_ABI bool extract(DataExtractor Data, uint8_t AddressSize,
108 std::optional<dwarf::DwarfFormat> Format);
109 };
110
111 /// An iterator to go through the expression operations.
113 : public iterator_facade_base<iterator, std::forward_iterator_tag,
114 const Operation> {
115 friend class DWARFExpression;
116 const DWARFExpression *Expr;
120 : Expr(Expr), Offset(Offset) {
121 Op.Error =
122 Offset >= Expr->Data.getData().size() ||
123 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
124 }
125
126 public:
128 Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
129 Op.Error =
130 Offset >= Expr->Data.getData().size() ||
131 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
132 return *this;
133 }
134
135 const Operation &operator*() const { return Op; }
136
138 return iterator(Expr, Op.EndOffset + Add);
139 }
140
141 // Comparison operators are provided out of line.
142 friend bool operator==(const iterator &, const iterator &);
143 };
144
146 std::optional<dwarf::DwarfFormat> Format = std::nullopt)
147 : Data(Data), AddressSize(AddressSize), Format(Format) {
148 assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
149 }
150
151 iterator begin() const { return iterator(this, 0); }
152 iterator end() const { return iterator(this, Data.getData().size()); }
153
154 LLVM_ABI bool operator==(const DWARFExpression &RHS) const;
155
156 StringRef getData() const { return Data.getData(); }
157
158 friend class DWARFVerifier;
159
160private:
161 DataExtractor Data;
162 uint8_t AddressSize;
163 std::optional<dwarf::DwarfFormat> Format;
164};
165
168 return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
169}
170
171} // end namespace llvm
172
173#endif // LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFEXPRESSION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
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
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.
LLVM_ABI std::optional< unsigned > getSubCode() const
@ DwarfNA
Serves as a marker for unused entries.
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)
A class that verifies DWARF debug information given a DWARF Context.
Definition: DWARFVerifier.h:68
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:96
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
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 size_t size() const
size - Get the string size.
Definition: StringRef.h:154
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
@ Add
Sum of integers.
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.