1 //===-------- StackMapParser.h - StackMap Parsing Support -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CODEGEN_STACKMAPPARSER_H
11 #define LLVM_CODEGEN_STACKMAPPARSER_H
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/Endian.h"
20 template <support::endianness Endianness>
21 class StackMapV1Parser {
24 template <typename AccessorT>
25 class AccessorIterator {
28 AccessorIterator(AccessorT A) : A(A) {}
29 AccessorIterator& operator++() { A = A.next(); return *this; }
30 AccessorIterator operator++(int) {
36 bool operator==(const AccessorIterator &Other) {
37 return A.P == Other.A.P;
40 bool operator!=(const AccessorIterator &Other) { return !(*this == Other); }
42 AccessorT& operator*() { return A; }
43 AccessorT* operator->() { return &A; }
49 /// Accessor for function records.
50 class FunctionAccessor {
51 friend class StackMapV1Parser;
54 /// Get the function address.
55 uint64_t getFunctionAddress() const {
56 return read<uint64_t>(P);
59 /// Get the function's stack size.
60 uint32_t getStackSize() const {
61 return read<uint64_t>(P + sizeof(uint64_t));
65 FunctionAccessor(const uint8_t *P) : P(P) {}
67 const static int FunctionAccessorSize = 2 * sizeof(uint64_t);
69 FunctionAccessor next() const {
70 return FunctionAccessor(P + FunctionAccessorSize);
76 /// Accessor for constants.
77 class ConstantAccessor {
78 friend class StackMapV1Parser;
81 /// Return the value of this constant.
82 uint64_t getValue() const { return read<uint64_t>(P); }
86 ConstantAccessor(const uint8_t *P) : P(P) {}
88 const static int ConstantAccessorSize = sizeof(uint64_t);
90 ConstantAccessor next() const {
91 return ConstantAccessor(P + ConstantAccessorSize);
97 // Forward-declare RecordAccessor so we can friend it below.
100 enum class LocationKind : uint8_t {
101 Register = 1, Direct = 2, Indirect = 3, Constant = 4, ConstantIndex = 5
105 /// Accessor for location records.
106 class LocationAccessor {
107 friend class StackMapV1Parser;
108 friend class RecordAccessor;
111 /// Get the Kind for this location.
112 LocationKind getKind() const {
113 return LocationKind(P[KindOffset]);
116 /// Get the Dwarf register number for this location.
117 uint16_t getDwarfRegNum() const {
118 return read<uint16_t>(P + DwarfRegNumOffset);
121 /// Get the small-constant for this location. (Kind must be Constant).
122 uint32_t getSmallConstant() const {
123 assert(getKind() == LocationKind::Constant && "Not a small constant.");
124 return read<uint32_t>(P + SmallConstantOffset);
127 /// Get the constant-index for this location. (Kind must be ConstantIndex).
128 uint32_t getConstantIndex() const {
129 assert(getKind() == LocationKind::ConstantIndex &&
130 "Not a constant-index.");
131 return read<uint32_t>(P + SmallConstantOffset);
134 /// Get the offset for this location. (Kind must be Direct or Indirect).
135 int32_t getOffset() const {
136 assert((getKind() == LocationKind::Direct ||
137 getKind() == LocationKind::Indirect) &&
138 "Not direct or indirect.");
139 return read<int32_t>(P + SmallConstantOffset);
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);
158 /// Accessor for stackmap live-out fields.
159 class LiveOutAccessor {
160 friend class StackMapV1Parser;
161 friend class RecordAccessor;
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);
176 LiveOutAccessor(const uint8_t *P) : P(P) {}
178 LiveOutAccessor next() const {
179 return LiveOutAccessor(P + LiveOutAccessorSize);
182 static const int DwarfRegNumOffset = 0;
183 static const int SizeOffset =
184 DwarfRegNumOffset + sizeof(uint16_t) + sizeof(uint8_t);
185 static const int LiveOutAccessorSize = sizeof(uint32_t);
190 /// Accessor for stackmap records.
191 class RecordAccessor {
192 friend class StackMapV1Parser;
195 typedef AccessorIterator<LocationAccessor> location_iterator;
196 typedef AccessorIterator<LiveOutAccessor> liveout_iterator;
198 /// Get the patchpoint/stackmap ID for this record.
199 uint64_t getID() const {
200 return read<uint64_t>(P + PatchpointIDOffset);
203 /// Get the instruction offset (from the start of the containing function)
205 uint32_t getInstructionOffset() const {
206 return read<uint32_t>(P + InstructionOffsetOffset);
209 /// Get the number of locations contained in this record.
210 uint16_t getNumLocations() const {
211 return read<uint16_t>(P + NumLocationsOffset);
214 /// Get the location with the given index.
215 LocationAccessor getLocation(unsigned LocationIndex) const {
216 unsigned LocationOffset =
217 LocationListOffset + LocationIndex * LocationSize;
218 return LocationAccessor(P + LocationOffset);
221 /// Begin iterator for locations.
222 location_iterator location_begin() const {
223 return location_iterator(getLocation(0));
226 /// End iterator for locations.
227 location_iterator location_end() const {
228 return location_iterator(getLocation(getNumLocations()));
231 /// Iterator range for locations.
232 iterator_range<location_iterator> locations() const {
233 return make_range(location_begin(), location_end());
236 /// Get the number of liveouts contained in this record.
237 uint16_t getNumLiveOuts() const {
238 return read<uint16_t>(P + getNumLiveOutsOffset());
241 /// Get the live-out with the given index.
242 LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const {
243 unsigned LiveOutOffset =
244 getNumLiveOutsOffset() + sizeof(uint16_t) + LiveOutIndex * LiveOutSize;
245 return LiveOutAccessor(P + LiveOutOffset);
248 /// Begin iterator for live-outs.
249 liveout_iterator liveouts_begin() const {
250 return liveout_iterator(getLiveOut(0));
254 /// End iterator for live-outs.
255 liveout_iterator liveouts_end() const {
256 return liveout_iterator(getLiveOut(getNumLiveOuts()));
259 /// Iterator range for live-outs.
260 iterator_range<liveout_iterator> liveouts() const {
261 return make_range(liveouts_begin(), liveouts_end());
266 RecordAccessor(const uint8_t *P) : P(P) {}
268 unsigned getNumLiveOutsOffset() const {
269 return LocationListOffset + LocationSize * getNumLocations() +
273 unsigned getSizeInBytes() const {
274 unsigned RecordSize =
275 getNumLiveOutsOffset() + sizeof(uint16_t) + getNumLiveOuts() * LiveOutSize;
276 return (RecordSize + 7) & ~0x7;
279 RecordAccessor next() const {
280 return RecordAccessor(P + getSizeInBytes());
283 static const unsigned PatchpointIDOffset = 0;
284 static const unsigned InstructionOffsetOffset =
285 PatchpointIDOffset + sizeof(uint64_t);
286 static const unsigned NumLocationsOffset =
287 InstructionOffsetOffset + sizeof(uint32_t) + sizeof(uint16_t);
288 static const unsigned LocationListOffset =
289 NumLocationsOffset + sizeof(uint16_t);
290 static const unsigned LocationSize = sizeof(uint64_t);
291 static const unsigned LiveOutSize = sizeof(uint32_t);
296 /// Construct a parser for a version-1 stackmap. StackMap data will be read
297 /// from the given array.
298 StackMapV1Parser(ArrayRef<uint8_t> StackMapSection)
299 : StackMapSection(StackMapSection) {
300 ConstantsListOffset = FunctionListOffset + getNumFunctions() * FunctionSize;
302 assert(StackMapSection[0] == 1 &&
303 "StackMapV1Parser can only parse version 1 stackmaps");
305 unsigned CurrentRecordOffset =
306 ConstantsListOffset + getNumConstants() * ConstantSize;
308 for (unsigned I = 0, E = getNumRecords(); I != E; ++I) {
309 StackMapRecordOffsets.push_back(CurrentRecordOffset);
310 CurrentRecordOffset +=
311 RecordAccessor(&StackMapSection[CurrentRecordOffset]).getSizeInBytes();
315 typedef AccessorIterator<FunctionAccessor> function_iterator;
316 typedef AccessorIterator<ConstantAccessor> constant_iterator;
317 typedef AccessorIterator<RecordAccessor> record_iterator;
319 /// Get the version number of this stackmap. (Always returns 1).
320 unsigned getVersion() const { return 1; }
322 /// Get the number of functions in the stack map.
323 uint32_t getNumFunctions() const {
324 return read<uint32_t>(&StackMapSection[NumFunctionsOffset]);
327 /// Get the number of large constants in the stack map.
328 uint32_t getNumConstants() const {
329 return read<uint32_t>(&StackMapSection[NumConstantsOffset]);
332 /// Get the number of stackmap records in the stackmap.
333 uint32_t getNumRecords() const {
334 return read<uint32_t>(&StackMapSection[NumRecordsOffset]);
337 /// Return an FunctionAccessor for the given function index.
338 FunctionAccessor getFunction(unsigned FunctionIndex) const {
339 return FunctionAccessor(StackMapSection.data() +
340 getFunctionOffset(FunctionIndex));
343 /// Begin iterator for functions.
344 function_iterator functions_begin() const {
345 return function_iterator(getFunction(0));
348 /// End iterator for functions.
349 function_iterator functions_end() const {
350 return function_iterator(
351 FunctionAccessor(StackMapSection.data() +
352 getFunctionOffset(getNumFunctions())));
355 /// Iterator range for functions.
356 iterator_range<function_iterator> functions() const {
357 return make_range(functions_begin(), functions_end());
360 /// Return the large constant at the given index.
361 ConstantAccessor getConstant(unsigned ConstantIndex) const {
362 return ConstantAccessor(StackMapSection.data() +
363 getConstantOffset(ConstantIndex));
366 /// Begin iterator for constants.
367 constant_iterator constants_begin() const {
368 return constant_iterator(getConstant(0));
371 /// End iterator for constants.
372 constant_iterator constants_end() const {
373 return constant_iterator(
374 ConstantAccessor(StackMapSection.data() +
375 getConstantOffset(getNumConstants())));
378 /// Iterator range for constants.
379 iterator_range<constant_iterator> constants() const {
380 return make_range(constants_begin(), constants_end());
383 /// Return a RecordAccessor for the given record index.
384 RecordAccessor getRecord(unsigned RecordIndex) const {
385 std::size_t RecordOffset = StackMapRecordOffsets[RecordIndex];
386 return RecordAccessor(StackMapSection.data() + RecordOffset);
389 /// Begin iterator for records.
390 record_iterator records_begin() const {
391 if (getNumRecords() == 0)
392 return record_iterator(RecordAccessor(nullptr));
393 return record_iterator(getRecord(0));
396 /// End iterator for records.
397 record_iterator records_end() const {
398 // Records need to be handled specially, since we cache the start addresses
399 // for them: We can't just compute the 1-past-the-end address, we have to
400 // look at the last record and use the 'next' method.
401 if (getNumRecords() == 0)
402 return record_iterator(RecordAccessor(nullptr));
403 return record_iterator(getRecord(getNumRecords() - 1).next());
406 /// Iterator range for records.
407 iterator_range<record_iterator> records() const {
408 return make_range(records_begin(), records_end());
413 template <typename T>
414 static T read(const uint8_t *P) {
415 return support::endian::read<T, Endianness, 1>(P);
418 static const unsigned HeaderOffset = 0;
419 static const unsigned NumFunctionsOffset = HeaderOffset + sizeof(uint32_t);
420 static const unsigned NumConstantsOffset = NumFunctionsOffset + sizeof(uint32_t);
421 static const unsigned NumRecordsOffset = NumConstantsOffset + sizeof(uint32_t);
422 static const unsigned FunctionListOffset = NumRecordsOffset + sizeof(uint32_t);
424 static const unsigned FunctionSize = 2 * sizeof(uint64_t);
425 static const unsigned ConstantSize = sizeof(uint64_t);
427 std::size_t getFunctionOffset(unsigned FunctionIndex) const {
428 return FunctionListOffset + FunctionIndex * FunctionSize;
431 std::size_t getConstantOffset(unsigned ConstantIndex) const {
432 return ConstantsListOffset + ConstantIndex * ConstantSize;
435 ArrayRef<uint8_t> StackMapSection;
436 unsigned ConstantsListOffset;
437 std::vector<unsigned> StackMapRecordOffsets;