LLVM 24.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 {
19
21public:
22 class iterator;
23
24 /// This class represents an Operation in the Expression.
25 ///
26 /// An Operation can be in Error state (check with isError()). This means
27 /// that it couldn't be decoded successfully. Some fields stay valid so that
28 /// a caller can report or copy the bytes it could not decode: getCode(),
29 /// getDescription(), getEndOffset(), which is the offset the operation
30 /// started at, and in some cases getSubCode(). The remaining operand values
31 /// are undefined.
32 class Operation {
33 public:
34 /// Size and signedness of expression operations' operands.
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 /// The operand is a ULEB128 encoded selector naming an NVIDIA specific
50 /// operation. The number and type of any operands that follow are
51 /// implied by the selector, so an unrecognized one cannot be skipped
52 /// and stops decoding.
54 SignBit = 0x80,
60 };
61
63 DwarfNA, ///< Serves as a marker for unused entries
64 Dwarf2 = 2,
68 };
69
70 /// Description of the encoding of one expression Op.
71 struct Description {
72 DwarfVersion Version; ///< Dwarf version where the Op was introduced.
73 SmallVector<Encoding> Op; ///< Encoding for Op operands.
74
75 template <typename... Ts>
79 ~Description() = default;
80 };
81
82 private:
84 friend class DWARFVerifier;
85
86 uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
87 Description Desc;
88 bool Error = false;
89 uint64_t EndOffset;
90 SmallVector<uint64_t> Operands;
91 SmallVector<uint64_t> OperandEndOffsets;
92
93 public:
94 const Description &getDescription() const { return Desc; }
95 uint8_t getCode() const { return Opcode; }
96 LLVM_ABI std::optional<unsigned> getSubCode() const;
97 uint64_t getNumOperands() const { return Operands.size(); }
98 ArrayRef<uint64_t> getRawOperands() const { return Operands; };
99 uint64_t getRawOperand(unsigned Idx) const { return Operands[Idx]; }
101 return OperandEndOffsets;
102 }
103 uint64_t getOperandEndOffset(unsigned Idx) const {
104 return OperandEndOffsets[Idx];
105 }
106 uint64_t getEndOffset() const { return EndOffset; }
107 bool isError() const { return Error; }
108
109 private:
110 LLVM_ABI bool extract(DataExtractor Data, uint8_t AddressSize,
112 std::optional<dwarf::DwarfFormat> Format);
113 };
114
115 /// An iterator to go through the expression operations.
116 class iterator
117 : public iterator_facade_base<iterator, std::forward_iterator_tag,
118 const Operation> {
119 friend class DWARFExpression;
120 const DWARFExpression *Expr;
121 uint64_t Offset;
122 Operation Op;
123 iterator(const DWARFExpression *Expr, uint64_t Offset)
124 : Expr(Expr), Offset(Offset) {
125 Op.Error =
126 Offset >= Expr->Data.getData().size() ||
127 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
128 }
129
130 public:
131 /// Get the byte offset of the current operation within the expression.
132 uint64_t getOffset() const { return Offset; }
133
134 iterator &operator++() {
135 Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
136 Op.Error =
137 Offset >= Expr->Data.getData().size() ||
138 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
139 return *this;
140 }
141
142 const Operation &operator*() const { return Op; }
143
144 iterator skipBytes(uint64_t Add) const {
145 return iterator(Expr, Op.EndOffset + Add);
146 }
147
148 // Comparison operators are provided out of line.
149 friend bool operator==(const iterator &, const iterator &);
150 };
151
153 std::optional<dwarf::DwarfFormat> Format = std::nullopt)
154 : Data(Data), AddressSize(AddressSize), Format(Format) {
155 assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
156 }
157
158 iterator begin() const { return iterator(this, 0); }
159 iterator end() const { return iterator(this, Data.getData().size()); }
160
161 LLVM_ABI bool operator==(const DWARFExpression &RHS) const;
162
163 /// Test if this location description, or one of its pieces, is a
164 /// memory location description.
165 LLVM_ABI bool isMemoryLocation() const;
166
167 StringRef getData() const { return Data.getData(); }
168
169 friend class DWARFVerifier;
170
171private:
172 DataExtractor Data;
173 uint8_t AddressSize;
174 std::optional<dwarf::DwarfFormat> Format;
175};
176
179 return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
180}
181
182} // end namespace llvm
183
184#endif // LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFEXPRESSION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
This file contains constants used for implementing Dwarf debug support.
Value * RHS
Value * LHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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.
@ NvidiaMuxArg
The operand is a ULEB128 encoded selector naming an NVIDIA specific operation.
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.
friend bool operator==(const iterator &, const iterator &)
const Operation & operator*() const
iterator skipBytes(uint64_t Add) const
uint64_t getOffset() const
Get the byte offset of the current operation within the expression.
StringRef getData() const
LLVM_ABI bool isMemoryLocation() const
Test if this location description, or one of its pieces, is a memory location description.
iterator begin() const
DWARFExpression(DataExtractor Data, uint8_t AddressSize, std::optional< dwarf::DwarfFormat > Format=std::nullopt)
LLVM_ABI bool operator==(const DWARFExpression &RHS) const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
@ Offset
Definition DWP.cpp:577
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:1685
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.