LLVM 22.0.0git
DWARFExpression.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
10#include <cassert>
11#include <cstdint>
12#include <vector>
13
14using namespace llvm;
15using namespace dwarf;
16
17namespace llvm {
18
20typedef Op::Description Desc;
21
22static std::vector<Desc> getOpDescriptions() {
23 std::vector<Desc> Descriptions;
24 Descriptions.resize(0xff);
25 Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr);
26 Descriptions[DW_OP_deref] = Desc(Op::Dwarf2);
27 Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1);
28 Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1);
29 Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2);
30 Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2);
31 Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4);
32 Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4);
33 Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8);
34 Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8);
35 Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB);
36 Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
37 Descriptions[DW_OP_dup] = Desc(Op::Dwarf2);
38 Descriptions[DW_OP_drop] = Desc(Op::Dwarf2);
39 Descriptions[DW_OP_over] = Desc(Op::Dwarf2);
40 Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1);
41 Descriptions[DW_OP_swap] = Desc(Op::Dwarf2);
42 Descriptions[DW_OP_rot] = Desc(Op::Dwarf2);
43 Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2);
44 Descriptions[DW_OP_abs] = Desc(Op::Dwarf2);
45 Descriptions[DW_OP_and] = Desc(Op::Dwarf2);
46 Descriptions[DW_OP_div] = Desc(Op::Dwarf2);
47 Descriptions[DW_OP_minus] = Desc(Op::Dwarf2);
48 Descriptions[DW_OP_mod] = Desc(Op::Dwarf2);
49 Descriptions[DW_OP_mul] = Desc(Op::Dwarf2);
50 Descriptions[DW_OP_neg] = Desc(Op::Dwarf2);
51 Descriptions[DW_OP_not] = Desc(Op::Dwarf2);
52 Descriptions[DW_OP_or] = Desc(Op::Dwarf2);
53 Descriptions[DW_OP_plus] = Desc(Op::Dwarf2);
54 Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB);
55 Descriptions[DW_OP_shl] = Desc(Op::Dwarf2);
56 Descriptions[DW_OP_shr] = Desc(Op::Dwarf2);
57 Descriptions[DW_OP_shra] = Desc(Op::Dwarf2);
58 Descriptions[DW_OP_xor] = Desc(Op::Dwarf2);
59 Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2);
60 Descriptions[DW_OP_eq] = Desc(Op::Dwarf2);
61 Descriptions[DW_OP_ge] = Desc(Op::Dwarf2);
62 Descriptions[DW_OP_gt] = Desc(Op::Dwarf2);
63 Descriptions[DW_OP_le] = Desc(Op::Dwarf2);
64 Descriptions[DW_OP_lt] = Desc(Op::Dwarf2);
65 Descriptions[DW_OP_ne] = Desc(Op::Dwarf2);
66 Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2);
67 for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA)
68 Descriptions[LA] = Desc(Op::Dwarf2);
69 for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA)
70 Descriptions[LA] = Desc(Op::Dwarf2);
71 for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA)
72 Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
73 Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB);
74 Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
75 Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB);
76 Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB);
77 Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1);
78 Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1);
79 Descriptions[DW_OP_nop] = Desc(Op::Dwarf2);
80 Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3);
81 Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2);
82 Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4);
83 Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr);
84 Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3);
85 Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3);
86 Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB);
87 Descriptions[DW_OP_implicit_value] =
89 Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf4);
90 Descriptions[DW_OP_implicit_pointer] =
92 Descriptions[DW_OP_addrx] = Desc(Op::Dwarf5, Op::SizeLEB);
93 Descriptions[DW_OP_constx] = Desc(Op::Dwarf5, Op::SizeLEB);
94 Descriptions[DW_OP_entry_value] = Desc(Op::Dwarf5, Op::SizeLEB);
95 Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef);
96 Descriptions[DW_OP_regval_type] =
98 Descriptions[DW_OP_WASM_location] =
100 Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);
101 Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);
102 Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB);
103 Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB);
104 Descriptions[DW_OP_GNU_implicit_pointer] =
106 // This Description acts as a marker that getSubOpDesc must be called
107 // to fetch the final Description for the operation. Each such final
108 // Description must share the same first SizeSubOpLEB operand.
109 Descriptions[DW_OP_LLVM_user] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);
110 return Descriptions;
111}
112
113static Desc getDescImpl(ArrayRef<Desc> Descriptions, unsigned Opcode) {
114 // Handle possible corrupted or unsupported operation.
115 if (Opcode >= Descriptions.size())
116 return {};
117 return Descriptions[Opcode];
118}
119
120static Desc getOpDesc(unsigned Opcode) {
121 static std::vector<Desc> Descriptions = getOpDescriptions();
122 return getDescImpl(Descriptions, Opcode);
123}
124
125static std::vector<Desc> getSubOpDescriptions() {
126 static constexpr unsigned LlvmUserDescriptionsSize = 1
127#define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) +1
128#include "llvm/BinaryFormat/Dwarf.def"
129 ;
130 std::vector<Desc> Descriptions;
131 Descriptions.resize(LlvmUserDescriptionsSize);
132 Descriptions[DW_OP_LLVM_nop] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);
133 return Descriptions;
134}
135
136static Desc getSubOpDesc(unsigned Opcode, unsigned SubOpcode) {
137 assert(Opcode == DW_OP_LLVM_user);
138 static std::vector<Desc> Descriptions = getSubOpDescriptions();
139 return getDescImpl(Descriptions, SubOpcode);
140}
141
142bool DWARFExpression::Operation::extract(DataExtractor Data,
143 uint8_t AddressSize, uint64_t Offset,
144 std::optional<DwarfFormat> Format) {
145 EndOffset = Offset;
146 Opcode = Data.getU8(&Offset);
147
148 Desc = getOpDesc(Opcode);
150 return false;
151
152 Operands.resize(Desc.Op.size());
153 OperandEndOffsets.resize(Desc.Op.size());
154 for (unsigned Operand = 0; Operand < Desc.Op.size(); ++Operand) {
155 unsigned Size = Desc.Op[Operand];
156 unsigned Signed = Size & Operation::SignBit;
157
158 switch (Size & ~Operation::SignBit) {
160 assert(Operand == 0 && "SubOp operand must be the first operand");
161 Operands[Operand] = Data.getULEB128(&Offset);
162 Desc = getSubOpDesc(Opcode, Operands[Operand]);
164 return false;
166 "SizeSubOpLEB Description must begin with SizeSubOpLEB operand");
167 break;
168 case Operation::Size1:
169 Operands[Operand] = Data.getU8(&Offset);
170 if (Signed)
171 Operands[Operand] = (int8_t)Operands[Operand];
172 break;
173 case Operation::Size2:
174 Operands[Operand] = Data.getU16(&Offset);
175 if (Signed)
176 Operands[Operand] = (int16_t)Operands[Operand];
177 break;
178 case Operation::Size4:
179 Operands[Operand] = Data.getU32(&Offset);
180 if (Signed)
181 Operands[Operand] = (int32_t)Operands[Operand];
182 break;
183 case Operation::Size8:
184 Operands[Operand] = Data.getU64(&Offset);
185 break;
187 Operands[Operand] = Data.getUnsigned(&Offset, AddressSize);
188 break;
190 if (!Format)
191 return false;
192 Operands[Operand] =
193 Data.getUnsigned(&Offset, dwarf::getDwarfOffsetByteSize(*Format));
194 break;
196 if (Signed)
197 Operands[Operand] = Data.getSLEB128(&Offset);
198 else
199 Operands[Operand] = Data.getULEB128(&Offset);
200 break;
202 Operands[Operand] = Data.getULEB128(&Offset);
203 break;
205 assert(Operand == 1);
206 switch (Operands[0]) {
207 case 0:
208 case 1:
209 case 2:
210 case 4:
211 Operands[Operand] = Data.getULEB128(&Offset);
212 break;
213 case 3: // global as uint32
214 Operands[Operand] = Data.getU32(&Offset);
215 break;
216 default:
217 return false; // Unknown Wasm location
218 }
219 break;
221 // We need a size, so this cannot be the first operand
222 if (Operand == 0)
223 return false;
224 // Store the offset of the block as the value.
225 Operands[Operand] = Offset;
226 Offset += Operands[Operand - 1];
227 break;
228 default:
229 llvm_unreachable("Unknown DWARFExpression Op size");
230 }
231
232 OperandEndOffsets[Operand] = Offset;
233 }
234
235 EndOffset = Offset;
236 return true;
237}
238
239std::optional<unsigned> DWARFExpression::Operation::getSubCode() const {
240 if (!Desc.Op.size() || Desc.Op[0] != Operation::SizeSubOpLEB)
241 return std::nullopt;
242 return Operands[0];
243}
244
246 if (AddressSize != RHS.AddressSize || Format != RHS.Format)
247 return false;
248 return Data.getData() == RHS.Data.getData();
249}
250
251} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Size
mir Rename Register Operands
Value * RHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
This class represents an Operation in the Expression.
LLVM_ABI std::optional< unsigned > getSubCode() const
@ DwarfNA
Serves as a marker for unused entries.
@ SizeSubOpLEB
The operand is a ULEB128 encoded SubOpcode.
@ SizeBlock
Preceding operand contains block size.
LLVM_ABI bool operator==(const DWARFExpression &RHS) const
size_t size() const
Definition: SmallVector.h:79
void resize(size_type N)
Definition: SmallVector.h:639
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:1080
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
static Desc getSubOpDesc(unsigned Opcode, unsigned SubOpcode)
static std::vector< Desc > getOpDescriptions()
Op::Description Desc
static Desc getDescImpl(ArrayRef< Desc > Descriptions, unsigned Opcode)
DWARFExpression::Operation Op
static Desc getOpDesc(unsigned Opcode)
static std::vector< Desc > getSubOpDescriptions()
Description of the encoding of one expression Op.
DwarfVersion Version
Dwarf version where the Op was introduced.
SmallVector< Encoding > Op
Encoding for Op operands.