StackMapParser.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //===-------- StackMapParser.h - StackMap Parsing Support -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_CODEGEN_STACKMAPPARSER_H
  10. #define LLVM_CODEGEN_STACKMAPPARSER_H
  11. #include "llvm/Support/Debug.h"
  12. #include "llvm/Support/Endian.h"
  13. #include <map>
  14. #include <vector>
  15. namespace llvm {
  16. template <support::endianness Endianness>
  17. class StackMapV1Parser {
  18. public:
  19. template <typename AccessorT>
  20. class AccessorIterator {
  21. public:
  22. AccessorIterator(AccessorT A) : A(A) {}
  23. AccessorIterator& operator++() { A = A.next(); return *this; }
  24. AccessorIterator operator++(int) {
  25. auto tmp = *this;
  26. ++*this;
  27. return tmp;
  28. }
  29. bool operator==(const AccessorIterator &Other) {
  30. return A.P == Other.A.P;
  31. }
  32. bool operator!=(const AccessorIterator &Other) { return !(*this == Other); }
  33. AccessorT& operator*() { return A; }
  34. AccessorT* operator->() { return &A; }
  35. private:
  36. AccessorT A;
  37. };
  38. /// Accessor for function records.
  39. class FunctionAccessor {
  40. friend class StackMapV1Parser;
  41. public:
  42. /// Get the function address.
  43. uint64_t getFunctionAddress() const {
  44. return read<uint64_t>(P);
  45. }
  46. /// Get the function's stack size.
  47. uint32_t getStackSize() const {
  48. return read<uint64_t>(P + sizeof(uint64_t));
  49. }
  50. private:
  51. FunctionAccessor(const uint8_t *P) : P(P) {}
  52. const static int FunctionAccessorSize = 2 * sizeof(uint64_t);
  53. FunctionAccessor next() const {
  54. return FunctionAccessor(P + FunctionAccessorSize);
  55. }
  56. const uint8_t *P;
  57. };
  58. /// Accessor for constants.
  59. class ConstantAccessor {
  60. friend class StackMapV1Parser;
  61. public:
  62. /// Return the value of this constant.
  63. uint64_t getValue() const { return read<uint64_t>(P); }
  64. private:
  65. ConstantAccessor(const uint8_t *P) : P(P) {}
  66. const static int ConstantAccessorSize = sizeof(uint64_t);
  67. ConstantAccessor next() const {
  68. return ConstantAccessor(P + ConstantAccessorSize);
  69. }
  70. const uint8_t *P;
  71. };
  72. // Forward-declare RecordAccessor so we can friend it below.
  73. class RecordAccessor;
  74. enum class LocationKind : uint8_t {
  75. Register = 1, Direct = 2, Indirect = 3, Constant = 4, ConstantIndex = 5
  76. };
  77. /// Accessor for location records.
  78. class LocationAccessor {
  79. friend class StackMapV1Parser;
  80. friend class RecordAccessor;
  81. public:
  82. /// Get the Kind for this location.
  83. LocationKind getKind() const {
  84. return LocationKind(P[KindOffset]);
  85. }
  86. /// Get the Dwarf register number for this location.
  87. uint16_t getDwarfRegNum() const {
  88. return read<uint16_t>(P + DwarfRegNumOffset);
  89. }
  90. /// Get the small-constant for this location. (Kind must be Constant).
  91. uint32_t getSmallConstant() const {
  92. assert(getKind() == LocationKind::Constant && "Not a small constant.");
  93. return read<uint32_t>(P + SmallConstantOffset);
  94. }
  95. /// Get the constant-index for this location. (Kind must be ConstantIndex).
  96. uint32_t getConstantIndex() const {
  97. assert(getKind() == LocationKind::ConstantIndex &&
  98. "Not a constant-index.");
  99. return read<uint32_t>(P + SmallConstantOffset);
  100. }
  101. /// Get the offset for this location. (Kind must be Direct or Indirect).
  102. int32_t getOffset() const {
  103. assert((getKind() == LocationKind::Direct ||
  104. getKind() == LocationKind::Indirect) &&
  105. "Not direct or indirect.");
  106. return read<int32_t>(P + SmallConstantOffset);
  107. }
  108. private:
  109. LocationAccessor(const uint8_t *P) : P(P) {}
  110. LocationAccessor next() const {
  111. return LocationAccessor(P + LocationAccessorSize);
  112. }
  113. static const int KindOffset = 0;
  114. static const int DwarfRegNumOffset = KindOffset + sizeof(uint16_t);
  115. static const int SmallConstantOffset = DwarfRegNumOffset + sizeof(uint16_t);
  116. static const int LocationAccessorSize = sizeof(uint64_t);
  117. const uint8_t *P;
  118. };
  119. /// Accessor for stackmap live-out fields.
  120. class LiveOutAccessor {
  121. friend class StackMapV1Parser;
  122. friend class RecordAccessor;
  123. public:
  124. /// Get the Dwarf register number for this live-out.
  125. uint16_t getDwarfRegNum() const {
  126. return read<uint16_t>(P + DwarfRegNumOffset);
  127. }
  128. /// Get the size in bytes of live [sub]register.
  129. unsigned getSizeInBytes() const {
  130. return read<uint8_t>(P + SizeOffset);
  131. }
  132. private:
  133. LiveOutAccessor(const uint8_t *P) : P(P) {}
  134. LiveOutAccessor next() const {
  135. return LiveOutAccessor(P + LiveOutAccessorSize);
  136. }
  137. static const int DwarfRegNumOffset = 0;
  138. static const int SizeOffset =
  139. DwarfRegNumOffset + sizeof(uint16_t) + sizeof(uint8_t);
  140. static const int LiveOutAccessorSize = sizeof(uint32_t);
  141. const uint8_t *P;
  142. };
  143. /// Accessor for stackmap records.
  144. class RecordAccessor {
  145. friend class StackMapV1Parser;
  146. public:
  147. typedef AccessorIterator<LocationAccessor> location_iterator;
  148. typedef AccessorIterator<LiveOutAccessor> liveout_iterator;
  149. /// Get the patchpoint/stackmap ID for this record.
  150. uint64_t getID() const {
  151. return read<uint64_t>(P + PatchpointIDOffset);
  152. }
  153. /// Get the instruction offset (from the start of the containing function)
  154. /// for this record.
  155. uint32_t getInstructionOffset() const {
  156. return read<uint32_t>(P + InstructionOffsetOffset);
  157. }
  158. /// Get the number of locations contained in this record.
  159. uint16_t getNumLocations() const {
  160. return read<uint16_t>(P + NumLocationsOffset);
  161. }
  162. /// Get the location with the given index.
  163. LocationAccessor getLocation(unsigned LocationIndex) const {
  164. unsigned LocationOffset =
  165. LocationListOffset + LocationIndex * LocationSize;
  166. return LocationAccessor(P + LocationOffset);
  167. }
  168. /// Begin iterator for locations.
  169. location_iterator location_begin() const {
  170. return location_iterator(getLocation(0));
  171. }
  172. /// End iterator for locations.
  173. location_iterator location_end() const {
  174. return location_iterator(getLocation(getNumLocations()));
  175. }
  176. /// Iterator range for locations.
  177. iterator_range<location_iterator> locations() const {
  178. return make_range(location_begin(), location_end());
  179. }
  180. /// Get the number of liveouts contained in this record.
  181. uint16_t getNumLiveOuts() const {
  182. return read<uint16_t>(P + getNumLiveOutsOffset());
  183. }
  184. /// Get the live-out with the given index.
  185. LiveOutAccessor getLiveOut(unsigned LiveOutIndex) const {
  186. unsigned LiveOutOffset =
  187. getNumLiveOutsOffset() + sizeof(uint16_t) + LiveOutIndex * LiveOutSize;
  188. return LiveOutAccessor(P + LiveOutOffset);
  189. }
  190. /// Begin iterator for live-outs.
  191. liveout_iterator liveouts_begin() const {
  192. return liveout_iterator(getLiveOut(0));
  193. }
  194. /// End iterator for live-outs.
  195. liveout_iterator liveouts_end() const {
  196. return liveout_iterator(getLiveOut(getNumLiveOuts()));
  197. }
  198. /// Iterator range for live-outs.
  199. iterator_range<liveout_iterator> liveouts() const {
  200. return make_range(liveouts_begin(), liveouts_end());
  201. }
  202. private:
  203. RecordAccessor(const uint8_t *P) : P(P) {}
  204. unsigned getNumLiveOutsOffset() const {
  205. return LocationListOffset + LocationSize * getNumLocations() +
  206. sizeof(uint16_t);
  207. }
  208. unsigned getSizeInBytes() const {
  209. unsigned RecordSize =
  210. getNumLiveOutsOffset() + sizeof(uint16_t) + getNumLiveOuts() * LiveOutSize;
  211. return (RecordSize + 7) & ~0x7;
  212. }
  213. RecordAccessor next() const {
  214. return RecordAccessor(P + getSizeInBytes());
  215. }
  216. static const unsigned PatchpointIDOffset = 0;
  217. static const unsigned InstructionOffsetOffset =
  218. PatchpointIDOffset + sizeof(uint64_t);
  219. static const unsigned NumLocationsOffset =
  220. InstructionOffsetOffset + sizeof(uint32_t) + sizeof(uint16_t);
  221. static const unsigned LocationListOffset =
  222. NumLocationsOffset + sizeof(uint16_t);
  223. static const unsigned LocationSize = sizeof(uint64_t);
  224. static const unsigned LiveOutSize = sizeof(uint32_t);
  225. const uint8_t *P;
  226. };
  227. /// Construct a parser for a version-1 stackmap. StackMap data will be read
  228. /// from the given array.
  229. StackMapV1Parser(ArrayRef<uint8_t> StackMapSection)
  230. : StackMapSection(StackMapSection) {
  231. ConstantsListOffset = FunctionListOffset + getNumFunctions() * FunctionSize;
  232. assert(StackMapSection[0] == 1 &&
  233. "StackMapV1Parser can only parse version 1 stackmaps");
  234. unsigned CurrentRecordOffset =
  235. ConstantsListOffset + getNumConstants() * ConstantSize;
  236. for (unsigned I = 0, E = getNumRecords(); I != E; ++I) {
  237. StackMapRecordOffsets.push_back(CurrentRecordOffset);
  238. CurrentRecordOffset +=
  239. RecordAccessor(&StackMapSection[CurrentRecordOffset]).getSizeInBytes();
  240. }
  241. }
  242. typedef AccessorIterator<FunctionAccessor> function_iterator;
  243. typedef AccessorIterator<ConstantAccessor> constant_iterator;
  244. typedef AccessorIterator<RecordAccessor> record_iterator;
  245. /// Get the version number of this stackmap. (Always returns 1).
  246. unsigned getVersion() const { return 1; }
  247. /// Get the number of functions in the stack map.
  248. uint32_t getNumFunctions() const {
  249. return read<uint32_t>(&StackMapSection[NumFunctionsOffset]);
  250. }
  251. /// Get the number of large constants in the stack map.
  252. uint32_t getNumConstants() const {
  253. return read<uint32_t>(&StackMapSection[NumConstantsOffset]);
  254. }
  255. /// Get the number of stackmap records in the stackmap.
  256. uint32_t getNumRecords() const {
  257. return read<uint32_t>(&StackMapSection[NumRecordsOffset]);
  258. }
  259. /// Return an FunctionAccessor for the given function index.
  260. FunctionAccessor getFunction(unsigned FunctionIndex) const {
  261. return FunctionAccessor(StackMapSection.data() +
  262. getFunctionOffset(FunctionIndex));
  263. }
  264. /// Begin iterator for functions.
  265. function_iterator functions_begin() const {
  266. return function_iterator(getFunction(0));
  267. }
  268. /// End iterator for functions.
  269. function_iterator functions_end() const {
  270. return function_iterator(
  271. FunctionAccessor(StackMapSection.data() +
  272. getFunctionOffset(getNumFunctions())));
  273. }
  274. /// Iterator range for functions.
  275. iterator_range<function_iterator> functions() const {
  276. return make_range(functions_begin(), functions_end());
  277. }
  278. /// Return the large constant at the given index.
  279. ConstantAccessor getConstant(unsigned ConstantIndex) const {
  280. return ConstantAccessor(StackMapSection.data() +
  281. getConstantOffset(ConstantIndex));
  282. }
  283. /// Begin iterator for constants.
  284. constant_iterator constants_begin() const {
  285. return constant_iterator(getConstant(0));
  286. }
  287. /// End iterator for constants.
  288. constant_iterator constants_end() const {
  289. return constant_iterator(
  290. ConstantAccessor(StackMapSection.data() +
  291. getConstantOffset(getNumConstants())));
  292. }
  293. /// Iterator range for constants.
  294. iterator_range<constant_iterator> constants() const {
  295. return make_range(constants_begin(), constants_end());
  296. }
  297. /// Return a RecordAccessor for the given record index.
  298. RecordAccessor getRecord(unsigned RecordIndex) const {
  299. std::size_t RecordOffset = StackMapRecordOffsets[RecordIndex];
  300. return RecordAccessor(StackMapSection.data() + RecordOffset);
  301. }
  302. /// Begin iterator for records.
  303. record_iterator records_begin() const {
  304. if (getNumRecords() == 0)
  305. return record_iterator(RecordAccessor(nullptr));
  306. return record_iterator(getRecord(0));
  307. }
  308. /// End iterator for records.
  309. record_iterator records_end() const {
  310. // Records need to be handled specially, since we cache the start addresses
  311. // for them: We can't just compute the 1-past-the-end address, we have to
  312. // look at the last record and use the 'next' method.
  313. if (getNumRecords() == 0)
  314. return record_iterator(RecordAccessor(nullptr));
  315. return record_iterator(getRecord(getNumRecords() - 1).next());
  316. }
  317. /// Iterator range for records.
  318. iterator_range<record_iterator> records() const {
  319. return make_range(records_begin(), records_end());
  320. }
  321. private:
  322. template <typename T>
  323. static T read(const uint8_t *P) {
  324. return support::endian::read<T, Endianness, 1>(P);
  325. }
  326. static const unsigned HeaderOffset = 0;
  327. static const unsigned NumFunctionsOffset = HeaderOffset + sizeof(uint32_t);
  328. static const unsigned NumConstantsOffset = NumFunctionsOffset + sizeof(uint32_t);
  329. static const unsigned NumRecordsOffset = NumConstantsOffset + sizeof(uint32_t);
  330. static const unsigned FunctionListOffset = NumRecordsOffset + sizeof(uint32_t);
  331. static const unsigned FunctionSize = 2 * sizeof(uint64_t);
  332. static const unsigned ConstantSize = sizeof(uint64_t);
  333. std::size_t getFunctionOffset(unsigned FunctionIndex) const {
  334. return FunctionListOffset + FunctionIndex * FunctionSize;
  335. }
  336. std::size_t getConstantOffset(unsigned ConstantIndex) const {
  337. return ConstantsListOffset + ConstantIndex * ConstantSize;
  338. }
  339. ArrayRef<uint8_t> StackMapSection;
  340. unsigned ConstantsListOffset;
  341. std::vector<unsigned> StackMapRecordOffsets;
  342. };
  343. }
  344. #endif