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 {
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 means
31 /// that it couldn't be decoded successfully. Some fields stay valid so that
32 /// a caller can report or copy the bytes it could not decode: getCode(),
33 /// getDescription(), getEndOffset(), which is the offset the operation
34 /// started at, and in some cases getSubCode(). The remaining operand values
35 /// are undefined.
36 class Operation {
37 public:
38 /// Size and signedness of expression operations' operands.
40 Size1 = 0,
41 Size2 = 1,
42 Size4 = 2,
43 Size8 = 3,
47 SizeBlock = 7, ///< Preceding operand contains block size
49 /// The operand is a ULEB128 encoded SubOpcode. This is only valid
50 /// for the first operand of an operation.
53 /// The operand is a ULEB128 encoded selector naming an NVIDIA specific
54 /// operation. The number and type of any operands that follow are
55 /// implied by the selector, so an unrecognized one cannot be skipped
56 /// and stops decoding.
58 SignBit = 0x80,
64 };
65
67 DwarfNA, ///< Serves as a marker for unused entries
68 Dwarf2 = 2,
72 };
73
74 /// Description of the encoding of one expression Op.
75 struct Description {
76 DwarfVersion Version; ///< Dwarf version where the Op was introduced.
77 SmallVector<Encoding> Op; ///< Encoding for Op operands.
78
79 template <typename... Ts>
83 ~Description() = default;
84 };
85
86 private:
88 friend class DWARFVerifier;
89
90 uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
91 Description Desc;
92 bool Error = false;
93 uint64_t EndOffset;
94 SmallVector<uint64_t> Operands;
95 SmallVector<uint64_t> OperandEndOffsets;
96
97 public:
98 const Description &getDescription() const { return Desc; }
99 uint8_t getCode() const { return Opcode; }
100 LLVM_ABI std::optional<unsigned> getSubCode() const;
101 uint64_t getNumOperands() const { return Operands.size(); }
102 ArrayRef<uint64_t> getRawOperands() const { return Operands; };
103 uint64_t getRawOperand(unsigned Idx) const { return Operands[Idx]; }
105 return OperandEndOffsets;
106 }
107 uint64_t getOperandEndOffset(unsigned Idx) const {
108 return OperandEndOffsets[Idx];
109 }
110 uint64_t getEndOffset() const { return EndOffset; }
111 bool isError() const { return Error; }
112
113 private:
114 LLVM_ABI bool extract(DataExtractor Data, uint8_t AddressSize,
116 std::optional<dwarf::DwarfFormat> Format);
117 };
118
119 /// An iterator to go through the expression operations.
120 class iterator
121 : public iterator_facade_base<iterator, std::forward_iterator_tag,
122 const Operation> {
123 friend class DWARFExpression;
124 const DWARFExpression *Expr;
125 uint64_t Offset;
126 Operation Op;
127 iterator(const DWARFExpression *Expr, uint64_t Offset)
128 : Expr(Expr), Offset(Offset) {
129 Op.Error =
130 Offset >= Expr->Data.getData().size() ||
131 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
132 }
133
134 public:
135 /// Get the byte offset of the current operation within the expression.
136 uint64_t getOffset() const { return Offset; }
137
138 iterator &operator++() {
139 Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
140 Op.Error =
141 Offset >= Expr->Data.getData().size() ||
142 !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
143 return *this;
144 }
145
146 const Operation &operator*() const { return Op; }
147
148 iterator skipBytes(uint64_t Add) const {
149 return iterator(Expr, Op.EndOffset + Add);
150 }
151
152 // Comparison operators are provided out of line.
153 friend bool operator==(const iterator &, const iterator &);
154 };
155
157 std::optional<dwarf::DwarfFormat> Format = std::nullopt)
158 : Data(Data), AddressSize(AddressSize), Format(Format) {
159 assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
160 }
161
162 iterator begin() const { return iterator(this, 0); }
163 iterator end() const { return iterator(this, Data.getData().size()); }
164
165 LLVM_ABI bool operator==(const DWARFExpression &RHS) 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!")
unsigned uint64_t
#define LLVM_ABI
Definition Compiler.h:215
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
iterator begin() const
DWARFExpression(DataExtractor Data, uint8_t AddressSize, std::optional< dwarf::DwarfFormat > Format=std::nullopt)
LLVM_ABI bool operator==(const DWARFExpression &RHS) const
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
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 class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
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:1669
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:196
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.