LLVM 20.0.0git
StackMapParser.h
Go to the documentation of this file.
1//===- StackMapParser.h - StackMap Parsing Support --------------*- 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_OBJECT_STACKMAPPARSER_H
10#define LLVM_OBJECT_STACKMAPPARSER_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include "llvm/Object/ELF.h"
15#include "llvm/Support/Endian.h"
16#include <cassert>
17#include <cstddef>
18#include <cstdint>
19#include <vector>
20
21namespace llvm {
22
23/// A parser for the latest stackmap format. At the moment, latest=V3.
24template <llvm::endianness Endianness> class StackMapParser {
25public:
26 template <typename AccessorT>
28 public:
29 AccessorIterator(AccessorT A) : A(A) {}
30
31 AccessorIterator& operator++() { A = A.next(); return *this; }
33 auto tmp = *this;
34 ++*this;
35 return tmp;
36 }
37
38 bool operator==(const AccessorIterator &Other) const {
39 return A.P == Other.A.P;
40 }
41
42 bool operator!=(const AccessorIterator &Other) const {
43 return !(*this == Other);
44 }
45
46 AccessorT& operator*() { return A; }
47 AccessorT* operator->() { return &A; }
48
49 private:
50 AccessorT A;
51 };
52
53 /// Accessor for function records.
55 friend class StackMapParser;
56
57 public:
58 /// Get the function address.
60 return read<uint64_t>(P);
61 }
62
63 /// Get the function's stack size.
65 return read<uint64_t>(P + sizeof(uint64_t));
66 }
67
68 /// Get the number of callsite records.
70 return read<uint64_t>(P + (2 * sizeof(uint64_t)));
71 }
72
73 private:
74 FunctionAccessor(const uint8_t *P) : P(P) {}
75
76 const static int FunctionAccessorSize = 3 * sizeof(uint64_t);
77
78 FunctionAccessor next() const {
79 return FunctionAccessor(P + FunctionAccessorSize);
80 }
81
82 const uint8_t *P;
83 };
84
85 /// Accessor for constants.
87 friend class StackMapParser;
88
89 public:
90 /// Return the value of this constant.
91 uint64_t getValue() const { return read<uint64_t>(P); }
92
93 private:
94 ConstantAccessor(const uint8_t *P) : P(P) {}
95
96 const static int ConstantAccessorSize = sizeof(uint64_t);
97
98 ConstantAccessor next() const {
99 return ConstantAccessor(P + ConstantAccessorSize);
100 }
101
102 const uint8_t *P;
103 };
104
105 enum class LocationKind : uint8_t {
106 Register = 1, Direct = 2, Indirect = 3, Constant = 4, ConstantIndex = 5
107 };
108
109 /// Accessor for location records.
111 friend class StackMapParser;
112 friend class RecordAccessor;
113
114 public:
115 /// Get the Kind for this location.
117 return LocationKind(P[KindOffset]);
118 }
119
120 /// Get the Size for this location.
121 unsigned getSizeInBytes() const {
122 return read<uint16_t>(P + SizeOffset);
123
124 }
125
126 /// Get the Dwarf register number for this location.
128 return read<uint16_t>(P + DwarfRegNumOffset);
129 }
130
131 /// Get the small-constant for this location. (Kind must be Constant).
133 assert(getKind() == LocationKind::Constant && "Not a small constant.");
134 return read<uint32_t>(P + SmallConstantOffset);
135 }
136
137 /// Get the constant-index for this location. (Kind must be ConstantIndex).
140 "Not a constant-index.");
141 return read<uint32_t>(P + SmallConstantOffset);
142 }
143
144 /// Get the offset for this location. (Kind must be Direct or Indirect).
145 int32_t getOffset() const {
148 "Not direct or indirect.");
149 return read<int32_t>(P + SmallConstantOffset);
150 }
151
152 private:
153 LocationAccessor(const uint8_t *P) : P(P) {}
154
155 LocationAccessor next() const {
156 return LocationAccessor(P + LocationAccessorSize);
157 }
158
159 static const int KindOffset = 0;
160 static const int SizeOffset = KindOffset + sizeof(uint16_t);
161 static const int DwarfRegNumOffset = SizeOffset + sizeof(uint16_t);
162 static const int SmallConstantOffset = DwarfRegNumOffset + sizeof(uint32_t);
163 static const int LocationAccessorSize = sizeof(uint64_t) + sizeof(uint32_t);
164
165 const uint8_t *P;
166 };
167
168 /// Accessor for stackmap live-out fields.
170 friend class StackMapParser;
171 friend class RecordAccessor;
172
173 public:
174 /// Get the Dwarf register number for this live-out.
176 return read<uint16_t>(P + DwarfRegNumOffset);
177 }
178
179 /// Get the size in bytes of live [sub]register.
180 unsigned getSizeInBytes() const {
181 return read<uint8_t>(P + SizeOffset);
182 }
183
184 private:
185 LiveOutAccessor(const uint8_t *P) : P(P) {}
186
187 LiveOutAccessor next() const {
188 return LiveOutAccessor(P + LiveOutAccessorSize);
189 }
190
191 static const int DwarfRegNumOffset = 0;
192 static const int SizeOffset =
193 DwarfRegNumOffset + sizeof(uint16_t) + sizeof(uint8_t);
194 static const int LiveOutAccessorSize = sizeof(uint32_t);
195
196 const uint8_t *P;
197 };
198
199 /// Accessor for stackmap records.
201 friend class StackMapParser;
202
203 public:
206
207 /// Get the patchpoint/stackmap ID for this record.
208 uint64_t getID() const {
209 return read<uint64_t>(P + PatchpointIDOffset);
210 }
211
212 /// Get the instruction offset (from the start of the containing function)
213 /// for this record.
215 return read<uint32_t>(P + InstructionOffsetOffset);
216 }
217
218 /// Get the number of locations contained in this record.
220 return read<uint16_t>(P + NumLocationsOffset);
221 }
222
223 /// Get the location with the given index.
224 LocationAccessor getLocation(unsigned LocationIndex) const {
225 unsigned LocationOffset =
226 LocationListOffset + LocationIndex * LocationSize;
227 return LocationAccessor(P + LocationOffset);
228 }
229
230 /// Begin iterator for locations.
233 }
234
235 /// End iterator for locations.
238 }
239
240 /// Iterator range for locations.
243 }
244
245 /// Get the number of liveouts contained in this record.
247 return read<uint16_t>(P + getNumLiveOutsOffset());
248 }
249
250 /// Get the live-out with the given index.
251 LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const {
252 unsigned LiveOutOffset =
253 getNumLiveOutsOffset() + sizeof(uint16_t) + LiveOutIndex * LiveOutSize;
254 return LiveOutAccessor(P + LiveOutOffset);
255 }
256
257 /// Begin iterator for live-outs.
259 return liveout_iterator(getLiveOut(0));
260 }
261
262 /// End iterator for live-outs.
265 }
266
267 /// Iterator range for live-outs.
270 }
271
272 private:
273 RecordAccessor(const uint8_t *P) : P(P) {}
274
275 unsigned getNumLiveOutsOffset() const {
276 unsigned LocOffset =
277 ((LocationListOffset + LocationSize * getNumLocations()) + 7) & ~0x7;
278 return LocOffset + sizeof(uint16_t);
279 }
280
281 unsigned getSizeInBytes() const {
282 unsigned RecordSize =
283 getNumLiveOutsOffset() + sizeof(uint16_t) + getNumLiveOuts() * LiveOutSize;
284 return (RecordSize + 7) & ~0x7;
285 }
286
287 RecordAccessor next() const {
288 return RecordAccessor(P + getSizeInBytes());
289 }
290
291 static const unsigned PatchpointIDOffset = 0;
292 static const unsigned InstructionOffsetOffset =
293 PatchpointIDOffset + sizeof(uint64_t);
294 static const unsigned NumLocationsOffset =
295 InstructionOffsetOffset + sizeof(uint32_t) + sizeof(uint16_t);
296 static const unsigned LocationListOffset =
297 NumLocationsOffset + sizeof(uint16_t);
298 static const unsigned LocationSize = sizeof(uint64_t) + sizeof(uint32_t);
299 static const unsigned LiveOutSize = sizeof(uint32_t);
300
301 const uint8_t *P;
302 };
303
304 /// Construct a parser for a version-3 stackmap. StackMap data will be read
305 /// from the given array.
307 : StackMapSection(StackMapSection) {
308 ConstantsListOffset = FunctionListOffset + getNumFunctions() * FunctionSize;
309
310 assert(StackMapSection[0] == 3 &&
311 "StackMapParser can only parse version 3 stackmaps");
312
313 unsigned CurrentRecordOffset =
314 ConstantsListOffset + getNumConstants() * ConstantSize;
315
316 for (unsigned I = 0, E = getNumRecords(); I != E; ++I) {
317 StackMapRecordOffsets.push_back(CurrentRecordOffset);
318 CurrentRecordOffset +=
319 RecordAccessor(&StackMapSection[CurrentRecordOffset]).getSizeInBytes();
320 }
321 }
322
323 /// Validates the header of the specified stack map section.
324 static Error validateHeader(ArrayRef<uint8_t> StackMapSection) {
325 // See the comment for StackMaps::emitStackmapHeader().
326 if (StackMapSection.size() < 16)
327 return object::createError(
328 "the stack map section size (" + Twine(StackMapSection.size()) +
329 ") is less than the minimum possible size of its header (16)");
330
331 unsigned Version = StackMapSection[0];
332 if (Version != 3)
333 return object::createError(
334 "the version (" + Twine(Version) +
335 ") of the stack map section is unsupported, the "
336 "supported version is 3");
337 return Error::success();
338 }
339
343
344 /// Get the version number of this stackmap. (Always returns 3).
345 unsigned getVersion() const { return 3; }
346
347 /// Get the number of functions in the stack map.
349 return read<uint32_t>(&StackMapSection[NumFunctionsOffset]);
350 }
351
352 /// Get the number of large constants in the stack map.
354 return read<uint32_t>(&StackMapSection[NumConstantsOffset]);
355 }
356
357 /// Get the number of stackmap records in the stackmap.
359 return read<uint32_t>(&StackMapSection[NumRecordsOffset]);
360 }
361
362 /// Return an FunctionAccessor for the given function index.
363 FunctionAccessor getFunction(unsigned FunctionIndex) const {
364 return FunctionAccessor(StackMapSection.data() +
365 getFunctionOffset(FunctionIndex));
366 }
367
368 /// Begin iterator for functions.
371 }
372
373 /// End iterator for functions.
375 return function_iterator(
376 FunctionAccessor(StackMapSection.data() +
377 getFunctionOffset(getNumFunctions())));
378 }
379
380 /// Iterator range for functions.
383 }
384
385 /// Return the large constant at the given index.
387 return ConstantAccessor(StackMapSection.data() +
388 getConstantOffset(ConstantIndex));
389 }
390
391 /// Begin iterator for constants.
394 }
395
396 /// End iterator for constants.
398 return constant_iterator(
399 ConstantAccessor(StackMapSection.data() +
400 getConstantOffset(getNumConstants())));
401 }
402
403 /// Iterator range for constants.
406 }
407
408 /// Return a RecordAccessor for the given record index.
409 RecordAccessor getRecord(unsigned RecordIndex) const {
410 std::size_t RecordOffset = StackMapRecordOffsets[RecordIndex];
411 return RecordAccessor(StackMapSection.data() + RecordOffset);
412 }
413
414 /// Begin iterator for records.
416 if (getNumRecords() == 0)
417 return record_iterator(RecordAccessor(nullptr));
418 return record_iterator(getRecord(0));
419 }
420
421 /// End iterator for records.
423 // Records need to be handled specially, since we cache the start addresses
424 // for them: We can't just compute the 1-past-the-end address, we have to
425 // look at the last record and use the 'next' method.
426 if (getNumRecords() == 0)
427 return record_iterator(RecordAccessor(nullptr));
428 return record_iterator(getRecord(getNumRecords() - 1).next());
429 }
430
431 /// Iterator range for records.
434 }
435
436private:
437 template <typename T>
438 static T read(const uint8_t *P) {
439 return support::endian::read<T, Endianness>(P);
440 }
441
442 static const unsigned HeaderOffset = 0;
443 static const unsigned NumFunctionsOffset = HeaderOffset + sizeof(uint32_t);
444 static const unsigned NumConstantsOffset = NumFunctionsOffset + sizeof(uint32_t);
445 static const unsigned NumRecordsOffset = NumConstantsOffset + sizeof(uint32_t);
446 static const unsigned FunctionListOffset = NumRecordsOffset + sizeof(uint32_t);
447
448 static const unsigned FunctionSize = 3 * sizeof(uint64_t);
449 static const unsigned ConstantSize = sizeof(uint64_t);
450
451 std::size_t getFunctionOffset(unsigned FunctionIndex) const {
452 return FunctionListOffset + FunctionIndex * FunctionSize;
453 }
454
455 std::size_t getConstantOffset(unsigned ConstantIndex) const {
456 return ConstantsListOffset + ConstantIndex * ConstantSize;
457 }
458
459 ArrayRef<uint8_t> StackMapSection;
460 unsigned ConstantsListOffset;
461 std::vector<unsigned> StackMapRecordOffsets;
462};
463
464} // end namespace llvm
465
466#endif // LLVM_OBJECT_STACKMAPPARSER_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition: MD5.cpp:58
static std::optional< uint64_t > getSizeInBytes(std::optional< uint64_t > SizeInBits)
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:168
const T * data() const
Definition: ArrayRef.h:165
This is an important base class in LLVM.
Definition: Constant.h:42
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool operator==(const AccessorIterator &Other) const
bool operator!=(const AccessorIterator &Other) const
uint64_t getValue() const
Return the value of this constant.
Accessor for function records.
uint64_t getRecordCount() const
Get the number of callsite records.
uint64_t getFunctionAddress() const
Get the function address.
uint64_t getStackSize() const
Get the function's stack size.
Accessor for stackmap live-out fields.
uint16_t getDwarfRegNum() const
Get the Dwarf register number for this live-out.
unsigned getSizeInBytes() const
Get the size in bytes of live [sub]register.
Accessor for location records.
unsigned getSizeInBytes() const
Get the Size for this location.
uint16_t getDwarfRegNum() const
Get the Dwarf register number for this location.
LocationKind getKind() const
Get the Kind for this location.
int32_t getOffset() const
Get the offset for this location. (Kind must be Direct or Indirect).
uint32_t getConstantIndex() const
Get the constant-index for this location. (Kind must be ConstantIndex).
uint32_t getSmallConstant() const
Get the small-constant for this location. (Kind must be Constant).
Accessor for stackmap records.
iterator_range< liveout_iterator > liveouts() const
Iterator range for live-outs.
uint16_t getNumLocations() const
Get the number of locations contained in this record.
LocationAccessor getLocation(unsigned LocationIndex) const
Get the location with the given index.
uint32_t getInstructionOffset() const
Get the instruction offset (from the start of the containing function) for this record.
liveout_iterator liveouts_end() const
End iterator for live-outs.
iterator_range< location_iterator > locations() const
Iterator range for locations.
LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const
Get the live-out with the given index.
AccessorIterator< LiveOutAccessor > liveout_iterator
AccessorIterator< LocationAccessor > location_iterator
location_iterator location_begin() const
Begin iterator for locations.
uint64_t getID() const
Get the patchpoint/stackmap ID for this record.
uint16_t getNumLiveOuts() const
Get the number of liveouts contained in this record.
liveout_iterator liveouts_begin() const
Begin iterator for live-outs.
location_iterator location_end() const
End iterator for locations.
A parser for the latest stackmap format. At the moment, latest=V3.
constant_iterator constants_end() const
End iterator for constants.
record_iterator records_end() const
End iterator for records.
FunctionAccessor getFunction(unsigned FunctionIndex) const
Return an FunctionAccessor for the given function index.
AccessorIterator< FunctionAccessor > function_iterator
AccessorIterator< ConstantAccessor > constant_iterator
function_iterator functions_end() const
End iterator for functions.
iterator_range< record_iterator > records() const
Iterator range for records.
iterator_range< constant_iterator > constants() const
Iterator range for constants.
unsigned getVersion() const
Get the version number of this stackmap. (Always returns 3).
uint32_t getNumConstants() const
Get the number of large constants in the stack map.
function_iterator functions_begin() const
Begin iterator for functions.
record_iterator records_begin() const
Begin iterator for records.
uint32_t getNumRecords() const
Get the number of stackmap records in the stackmap.
constant_iterator constants_begin() const
Begin iterator for constants.
iterator_range< function_iterator > functions() const
Iterator range for functions.
AccessorIterator< RecordAccessor > record_iterator
ConstantAccessor getConstant(unsigned ConstantIndex) const
Return the large constant at the given index.
StackMapParser(ArrayRef< uint8_t > StackMapSection)
Construct a parser for a version-3 stackmap.
RecordAccessor getRecord(unsigned RecordIndex) const
Return a RecordAccessor for the given record index.
static Error validateHeader(ArrayRef< uint8_t > StackMapSection)
Validates the header of the specified stack map section.
uint32_t getNumFunctions() const
Get the number of functions in the stack map.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
Error createError(const Twine &Err)
Definition: Error.h:84
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
@ Other
Any other memory.