Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Object / StackMapParser.h
blob8817516eb2128f1c14ee9ec21270548db60712a3
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_STACKMAPPARSER_H
10 #define LLVM_CODEGEN_STACKMAPPARSER_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/iterator_range.h"
14 #include "llvm/Support/Endian.h"
15 #include <cassert>
16 #include <cstddef>
17 #include <cstdint>
18 #include <vector>
20 namespace llvm {
22 template <support::endianness Endianness>
23 class StackMapV2Parser {
24 public:
25 template <typename AccessorT>
26 class AccessorIterator {
27 public:
28 AccessorIterator(AccessorT A) : A(A) {}
30 AccessorIterator& operator++() { A = A.next(); return *this; }
31 AccessorIterator operator++(int) {
32 auto tmp = *this;
33 ++*this;
34 return tmp;
37 bool operator==(const AccessorIterator &Other) {
38 return A.P == Other.A.P;
41 bool operator!=(const AccessorIterator &Other) { return !(*this == Other); }
43 AccessorT& operator*() { return A; }
44 AccessorT* operator->() { return &A; }
46 private:
47 AccessorT A;
50 /// Accessor for function records.
51 class FunctionAccessor {
52 friend class StackMapV2Parser;
54 public:
55 /// Get the function address.
56 uint64_t getFunctionAddress() const {
57 return read<uint64_t>(P);
60 /// Get the function's stack size.
61 uint64_t getStackSize() const {
62 return read<uint64_t>(P + sizeof(uint64_t));
65 /// Get the number of callsite records.
66 uint64_t getRecordCount() const {
67 return read<uint64_t>(P + (2 * sizeof(uint64_t)));
70 private:
71 FunctionAccessor(const uint8_t *P) : P(P) {}
73 const static int FunctionAccessorSize = 3 * sizeof(uint64_t);
75 FunctionAccessor next() const {
76 return FunctionAccessor(P + FunctionAccessorSize);
79 const uint8_t *P;
82 /// Accessor for constants.
83 class ConstantAccessor {
84 friend class StackMapV2Parser;
86 public:
87 /// Return the value of this constant.
88 uint64_t getValue() const { return read<uint64_t>(P); }
90 private:
91 ConstantAccessor(const uint8_t *P) : P(P) {}
93 const static int ConstantAccessorSize = sizeof(uint64_t);
95 ConstantAccessor next() const {
96 return ConstantAccessor(P + ConstantAccessorSize);
99 const uint8_t *P;
102 enum class LocationKind : uint8_t {
103 Register = 1, Direct = 2, Indirect = 3, Constant = 4, ConstantIndex = 5
106 /// Accessor for location records.
107 class LocationAccessor {
108 friend class StackMapV2Parser;
109 friend class RecordAccessor;
111 public:
112 /// Get the Kind for this location.
113 LocationKind getKind() const {
114 return LocationKind(P[KindOffset]);
117 /// Get the Dwarf register number for this location.
118 uint16_t getDwarfRegNum() const {
119 return read<uint16_t>(P + DwarfRegNumOffset);
122 /// Get the small-constant for this location. (Kind must be Constant).
123 uint32_t getSmallConstant() const {
124 assert(getKind() == LocationKind::Constant && "Not a small constant.");
125 return read<uint32_t>(P + SmallConstantOffset);
128 /// Get the constant-index for this location. (Kind must be ConstantIndex).
129 uint32_t getConstantIndex() const {
130 assert(getKind() == LocationKind::ConstantIndex &&
131 "Not a constant-index.");
132 return read<uint32_t>(P + SmallConstantOffset);
135 /// Get the offset for this location. (Kind must be Direct or Indirect).
136 int32_t getOffset() const {
137 assert((getKind() == LocationKind::Direct ||
138 getKind() == LocationKind::Indirect) &&
139 "Not direct or indirect.");
140 return read<int32_t>(P + SmallConstantOffset);
143 private:
144 LocationAccessor(const uint8_t *P) : P(P) {}
146 LocationAccessor next() const {
147 return LocationAccessor(P + LocationAccessorSize);
150 static const int KindOffset = 0;
151 static const int DwarfRegNumOffset = KindOffset + sizeof(uint16_t);
152 static const int SmallConstantOffset = DwarfRegNumOffset + sizeof(uint16_t);
153 static const int LocationAccessorSize = sizeof(uint64_t);
155 const uint8_t *P;
158 /// Accessor for stackmap live-out fields.
159 class LiveOutAccessor {
160 friend class StackMapV2Parser;
161 friend class RecordAccessor;
163 public:
164 /// Get the Dwarf register number for this live-out.
165 uint16_t getDwarfRegNum() const {
166 return read<uint16_t>(P + DwarfRegNumOffset);
169 /// Get the size in bytes of live [sub]register.
170 unsigned getSizeInBytes() const {
171 return read<uint8_t>(P + SizeOffset);
174 private:
175 LiveOutAccessor(const uint8_t *P) : P(P) {}
177 LiveOutAccessor next() const {
178 return LiveOutAccessor(P + LiveOutAccessorSize);
181 static const int DwarfRegNumOffset = 0;
182 static const int SizeOffset =
183 DwarfRegNumOffset + sizeof(uint16_t) + sizeof(uint8_t);
184 static const int LiveOutAccessorSize = sizeof(uint32_t);
186 const uint8_t *P;
189 /// Accessor for stackmap records.
190 class RecordAccessor {
191 friend class StackMapV2Parser;
193 public:
194 using location_iterator = AccessorIterator<LocationAccessor>;
195 using liveout_iterator = AccessorIterator<LiveOutAccessor>;
197 /// Get the patchpoint/stackmap ID for this record.
198 uint64_t getID() const {
199 return read<uint64_t>(P + PatchpointIDOffset);
202 /// Get the instruction offset (from the start of the containing function)
203 /// for this record.
204 uint32_t getInstructionOffset() const {
205 return read<uint32_t>(P + InstructionOffsetOffset);
208 /// Get the number of locations contained in this record.
209 uint16_t getNumLocations() const {
210 return read<uint16_t>(P + NumLocationsOffset);
213 /// Get the location with the given index.
214 LocationAccessor getLocation(unsigned LocationIndex) const {
215 unsigned LocationOffset =
216 LocationListOffset + LocationIndex * LocationSize;
217 return LocationAccessor(P + LocationOffset);
220 /// Begin iterator for locations.
221 location_iterator location_begin() const {
222 return location_iterator(getLocation(0));
225 /// End iterator for locations.
226 location_iterator location_end() const {
227 return location_iterator(getLocation(getNumLocations()));
230 /// Iterator range for locations.
231 iterator_range<location_iterator> locations() const {
232 return make_range(location_begin(), location_end());
235 /// Get the number of liveouts contained in this record.
236 uint16_t getNumLiveOuts() const {
237 return read<uint16_t>(P + getNumLiveOutsOffset());
240 /// Get the live-out with the given index.
241 LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const {
242 unsigned LiveOutOffset =
243 getNumLiveOutsOffset() + sizeof(uint16_t) + LiveOutIndex * LiveOutSize;
244 return LiveOutAccessor(P + LiveOutOffset);
247 /// Begin iterator for live-outs.
248 liveout_iterator liveouts_begin() const {
249 return liveout_iterator(getLiveOut(0));
252 /// End iterator for live-outs.
253 liveout_iterator liveouts_end() const {
254 return liveout_iterator(getLiveOut(getNumLiveOuts()));
257 /// Iterator range for live-outs.
258 iterator_range<liveout_iterator> liveouts() const {
259 return make_range(liveouts_begin(), liveouts_end());
262 private:
263 RecordAccessor(const uint8_t *P) : P(P) {}
265 unsigned getNumLiveOutsOffset() const {
266 return LocationListOffset + LocationSize * getNumLocations() +
267 sizeof(uint16_t);
270 unsigned getSizeInBytes() const {
271 unsigned RecordSize =
272 getNumLiveOutsOffset() + sizeof(uint16_t) + getNumLiveOuts() * LiveOutSize;
273 return (RecordSize + 7) & ~0x7;
276 RecordAccessor next() const {
277 return RecordAccessor(P + getSizeInBytes());
280 static const unsigned PatchpointIDOffset = 0;
281 static const unsigned InstructionOffsetOffset =
282 PatchpointIDOffset + sizeof(uint64_t);
283 static const unsigned NumLocationsOffset =
284 InstructionOffsetOffset + sizeof(uint32_t) + sizeof(uint16_t);
285 static const unsigned LocationListOffset =
286 NumLocationsOffset + sizeof(uint16_t);
287 static const unsigned LocationSize = sizeof(uint64_t);
288 static const unsigned LiveOutSize = sizeof(uint32_t);
290 const uint8_t *P;
293 /// Construct a parser for a version-2 stackmap. StackMap data will be read
294 /// from the given array.
295 StackMapV2Parser(ArrayRef<uint8_t> StackMapSection)
296 : StackMapSection(StackMapSection) {
297 ConstantsListOffset = FunctionListOffset + getNumFunctions() * FunctionSize;
299 assert(StackMapSection[0] == 2 &&
300 "StackMapV2Parser can only parse version 2 stackmaps");
302 unsigned CurrentRecordOffset =
303 ConstantsListOffset + getNumConstants() * ConstantSize;
305 for (unsigned I = 0, E = getNumRecords(); I != E; ++I) {
306 StackMapRecordOffsets.push_back(CurrentRecordOffset);
307 CurrentRecordOffset +=
308 RecordAccessor(&StackMapSection[CurrentRecordOffset]).getSizeInBytes();
312 using function_iterator = AccessorIterator<FunctionAccessor>;
313 using constant_iterator = AccessorIterator<ConstantAccessor>;
314 using record_iterator = AccessorIterator<RecordAccessor>;
316 /// Get the version number of this stackmap. (Always returns 2).
317 unsigned getVersion() const { return 2; }
319 /// Get the number of functions in the stack map.
320 uint32_t getNumFunctions() const {
321 return read<uint32_t>(&StackMapSection[NumFunctionsOffset]);
324 /// Get the number of large constants in the stack map.
325 uint32_t getNumConstants() const {
326 return read<uint32_t>(&StackMapSection[NumConstantsOffset]);
329 /// Get the number of stackmap records in the stackmap.
330 uint32_t getNumRecords() const {
331 return read<uint32_t>(&StackMapSection[NumRecordsOffset]);
334 /// Return an FunctionAccessor for the given function index.
335 FunctionAccessor getFunction(unsigned FunctionIndex) const {
336 return FunctionAccessor(StackMapSection.data() +
337 getFunctionOffset(FunctionIndex));
340 /// Begin iterator for functions.
341 function_iterator functions_begin() const {
342 return function_iterator(getFunction(0));
345 /// End iterator for functions.
346 function_iterator functions_end() const {
347 return function_iterator(
348 FunctionAccessor(StackMapSection.data() +
349 getFunctionOffset(getNumFunctions())));
352 /// Iterator range for functions.
353 iterator_range<function_iterator> functions() const {
354 return make_range(functions_begin(), functions_end());
357 /// Return the large constant at the given index.
358 ConstantAccessor getConstant(unsigned ConstantIndex) const {
359 return ConstantAccessor(StackMapSection.data() +
360 getConstantOffset(ConstantIndex));
363 /// Begin iterator for constants.
364 constant_iterator constants_begin() const {
365 return constant_iterator(getConstant(0));
368 /// End iterator for constants.
369 constant_iterator constants_end() const {
370 return constant_iterator(
371 ConstantAccessor(StackMapSection.data() +
372 getConstantOffset(getNumConstants())));
375 /// Iterator range for constants.
376 iterator_range<constant_iterator> constants() const {
377 return make_range(constants_begin(), constants_end());
380 /// Return a RecordAccessor for the given record index.
381 RecordAccessor getRecord(unsigned RecordIndex) const {
382 std::size_t RecordOffset = StackMapRecordOffsets[RecordIndex];
383 return RecordAccessor(StackMapSection.data() + RecordOffset);
386 /// Begin iterator for records.
387 record_iterator records_begin() const {
388 if (getNumRecords() == 0)
389 return record_iterator(RecordAccessor(nullptr));
390 return record_iterator(getRecord(0));
393 /// End iterator for records.
394 record_iterator records_end() const {
395 // Records need to be handled specially, since we cache the start addresses
396 // for them: We can't just compute the 1-past-the-end address, we have to
397 // look at the last record and use the 'next' method.
398 if (getNumRecords() == 0)
399 return record_iterator(RecordAccessor(nullptr));
400 return record_iterator(getRecord(getNumRecords() - 1).next());
403 /// Iterator range for records.
404 iterator_range<record_iterator> records() const {
405 return make_range(records_begin(), records_end());
408 private:
409 template <typename T>
410 static T read(const uint8_t *P) {
411 return support::endian::read<T, Endianness, 1>(P);
414 static const unsigned HeaderOffset = 0;
415 static const unsigned NumFunctionsOffset = HeaderOffset + sizeof(uint32_t);
416 static const unsigned NumConstantsOffset = NumFunctionsOffset + sizeof(uint32_t);
417 static const unsigned NumRecordsOffset = NumConstantsOffset + sizeof(uint32_t);
418 static const unsigned FunctionListOffset = NumRecordsOffset + sizeof(uint32_t);
420 static const unsigned FunctionSize = 3 * sizeof(uint64_t);
421 static const unsigned ConstantSize = sizeof(uint64_t);
423 std::size_t getFunctionOffset(unsigned FunctionIndex) const {
424 return FunctionListOffset + FunctionIndex * FunctionSize;
427 std::size_t getConstantOffset(unsigned ConstantIndex) const {
428 return ConstantsListOffset + ConstantIndex * ConstantSize;
431 ArrayRef<uint8_t> StackMapSection;
432 unsigned ConstantsListOffset;
433 std::vector<unsigned> StackMapRecordOffsets;
436 } // end namespace llvm
438 #endif // LLVM_CODEGEN_STACKMAPPARSER_H