BitstreamReader.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This header defines the BitstreamReader class. This class can be used to
  15. // read an arbitrary bitstream, regardless of its contents.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
  19. #define LLVM_BITSTREAM_BITSTREAMREADER_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/Bitstream/BitCodes.h"
  23. #include "llvm/Support/Endian.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/MemoryBufferRef.h"
  27. #include <algorithm>
  28. #include <cassert>
  29. #include <climits>
  30. #include <cstddef>
  31. #include <cstdint>
  32. #include <memory>
  33. #include <string>
  34. #include <utility>
  35. #include <vector>
  36. namespace llvm {
  37. /// This class maintains the abbreviations read from a block info block.
  38. class BitstreamBlockInfo {
  39. public:
  40. /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
  41. /// describe abbreviations that all blocks of the specified ID inherit.
  42. struct BlockInfo {
  43. unsigned BlockID = 0;
  44. std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
  45. std::string Name;
  46. std::vector<std::pair<unsigned, std::string>> RecordNames;
  47. };
  48. private:
  49. std::vector<BlockInfo> BlockInfoRecords;
  50. public:
  51. /// If there is block info for the specified ID, return it, otherwise return
  52. /// null.
  53. const BlockInfo *getBlockInfo(unsigned BlockID) const {
  54. // Common case, the most recent entry matches BlockID.
  55. if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
  56. return &BlockInfoRecords.back();
  57. for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
  58. i != e; ++i)
  59. if (BlockInfoRecords[i].BlockID == BlockID)
  60. return &BlockInfoRecords[i];
  61. return nullptr;
  62. }
  63. BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
  64. if (const BlockInfo *BI = getBlockInfo(BlockID))
  65. return *const_cast<BlockInfo*>(BI);
  66. // Otherwise, add a new record.
  67. BlockInfoRecords.emplace_back();
  68. BlockInfoRecords.back().BlockID = BlockID;
  69. return BlockInfoRecords.back();
  70. }
  71. };
  72. /// This represents a position within a bitstream. There may be multiple
  73. /// independent cursors reading within one bitstream, each maintaining their
  74. /// own local state.
  75. class SimpleBitstreamCursor {
  76. ArrayRef<uint8_t> BitcodeBytes;
  77. size_t NextChar = 0;
  78. public:
  79. /// This is the current data we have pulled from the stream but have not
  80. /// returned to the client. This is specifically and intentionally defined to
  81. /// follow the word size of the host machine for efficiency. We use word_t in
  82. /// places that are aware of this to make it perfectly explicit what is going
  83. /// on.
  84. using word_t = size_t;
  85. private:
  86. word_t CurWord = 0;
  87. /// This is the number of bits in CurWord that are valid. This is always from
  88. /// [0...bits_of(size_t)-1] inclusive.
  89. unsigned BitsInCurWord = 0;
  90. public:
  91. static const constexpr size_t MaxChunkSize = sizeof(word_t) * 8;
  92. SimpleBitstreamCursor() = default;
  93. explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
  94. : BitcodeBytes(BitcodeBytes) {}
  95. explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
  96. : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {}
  97. explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
  98. : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
  99. bool canSkipToPos(size_t pos) const {
  100. // pos can be skipped to if it is a valid address or one byte past the end.
  101. return pos <= BitcodeBytes.size();
  102. }
  103. bool AtEndOfStream() {
  104. return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
  105. }
  106. /// Return the bit # of the bit we are reading.
  107. uint64_t GetCurrentBitNo() const {
  108. return NextChar*CHAR_BIT - BitsInCurWord;
  109. }
  110. // Return the byte # of the current bit.
  111. uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
  112. ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
  113. /// Reset the stream to the specified bit number.
  114. Error JumpToBit(uint64_t BitNo) {
  115. size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
  116. unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
  117. assert(canSkipToPos(ByteNo) && "Invalid location");
  118. // Move the cursor to the right word.
  119. NextChar = ByteNo;
  120. BitsInCurWord = 0;
  121. // Skip over any bits that are already consumed.
  122. if (WordBitNo) {
  123. if (Expected<word_t> Res = Read(WordBitNo))
  124. return Error::success();
  125. else
  126. return Res.takeError();
  127. }
  128. return Error::success();
  129. }
  130. /// Get a pointer into the bitstream at the specified byte offset.
  131. const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
  132. return BitcodeBytes.data() + ByteNo;
  133. }
  134. /// Get a pointer into the bitstream at the specified bit offset.
  135. ///
  136. /// The bit offset must be on a byte boundary.
  137. const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
  138. assert(!(BitNo % 8) && "Expected bit on byte boundary");
  139. return getPointerToByte(BitNo / 8, NumBytes);
  140. }
  141. Error fillCurWord() {
  142. if (NextChar >= BitcodeBytes.size())
  143. return createStringError(std::errc::io_error,
  144. "Unexpected end of file reading %u of %u bytes",
  145. NextChar, BitcodeBytes.size());
  146. // Read the next word from the stream.
  147. const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
  148. unsigned BytesRead;
  149. if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
  150. BytesRead = sizeof(word_t);
  151. CurWord =
  152. support::endian::read<word_t, support::little, support::unaligned>(
  153. NextCharPtr);
  154. } else {
  155. // Short read.
  156. BytesRead = BitcodeBytes.size() - NextChar;
  157. CurWord = 0;
  158. for (unsigned B = 0; B != BytesRead; ++B)
  159. CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
  160. }
  161. NextChar += BytesRead;
  162. BitsInCurWord = BytesRead * 8;
  163. return Error::success();
  164. }
  165. Expected<word_t> Read(unsigned NumBits) {
  166. static const unsigned BitsInWord = MaxChunkSize;
  167. assert(NumBits && NumBits <= BitsInWord &&
  168. "Cannot return zero or more than BitsInWord bits!");
  169. static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
  170. // If the field is fully contained by CurWord, return it quickly.
  171. if (BitsInCurWord >= NumBits) {
  172. word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
  173. // Use a mask to avoid undefined behavior.
  174. CurWord >>= (NumBits & Mask);
  175. BitsInCurWord -= NumBits;
  176. return R;
  177. }
  178. word_t R = BitsInCurWord ? CurWord : 0;
  179. unsigned BitsLeft = NumBits - BitsInCurWord;
  180. if (Error fillResult = fillCurWord())
  181. return std::move(fillResult);
  182. // If we run out of data, abort.
  183. if (BitsLeft > BitsInCurWord)
  184. return createStringError(std::errc::io_error,
  185. "Unexpected end of file reading %u of %u bits",
  186. BitsInCurWord, BitsLeft);
  187. word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
  188. // Use a mask to avoid undefined behavior.
  189. CurWord >>= (BitsLeft & Mask);
  190. BitsInCurWord -= BitsLeft;
  191. R |= R2 << (NumBits - BitsLeft);
  192. return R;
  193. }
  194. Expected<uint32_t> ReadVBR(unsigned NumBits) {
  195. Expected<unsigned> MaybeRead = Read(NumBits);
  196. if (!MaybeRead)
  197. return MaybeRead;
  198. uint32_t Piece = MaybeRead.get();
  199. if ((Piece & (1U << (NumBits-1))) == 0)
  200. return Piece;
  201. uint32_t Result = 0;
  202. unsigned NextBit = 0;
  203. while (true) {
  204. Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
  205. if ((Piece & (1U << (NumBits-1))) == 0)
  206. return Result;
  207. NextBit += NumBits-1;
  208. MaybeRead = Read(NumBits);
  209. if (!MaybeRead)
  210. return MaybeRead;
  211. Piece = MaybeRead.get();
  212. }
  213. }
  214. // Read a VBR that may have a value up to 64-bits in size. The chunk size of
  215. // the VBR must still be <= 32 bits though.
  216. Expected<uint64_t> ReadVBR64(unsigned NumBits) {
  217. Expected<uint64_t> MaybeRead = Read(NumBits);
  218. if (!MaybeRead)
  219. return MaybeRead;
  220. uint32_t Piece = MaybeRead.get();
  221. if ((Piece & (1U << (NumBits-1))) == 0)
  222. return uint64_t(Piece);
  223. uint64_t Result = 0;
  224. unsigned NextBit = 0;
  225. while (true) {
  226. Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
  227. if ((Piece & (1U << (NumBits-1))) == 0)
  228. return Result;
  229. NextBit += NumBits-1;
  230. MaybeRead = Read(NumBits);
  231. if (!MaybeRead)
  232. return MaybeRead;
  233. Piece = MaybeRead.get();
  234. }
  235. }
  236. void SkipToFourByteBoundary() {
  237. // If word_t is 64-bits and if we've read less than 32 bits, just dump
  238. // the bits we have up to the next 32-bit boundary.
  239. if (sizeof(word_t) > 4 &&
  240. BitsInCurWord >= 32) {
  241. CurWord >>= BitsInCurWord-32;
  242. BitsInCurWord = 32;
  243. return;
  244. }
  245. BitsInCurWord = 0;
  246. }
  247. /// Return the size of the stream in bytes.
  248. size_t SizeInBytes() const { return BitcodeBytes.size(); }
  249. /// Skip to the end of the file.
  250. void skipToEnd() { NextChar = BitcodeBytes.size(); }
  251. };
  252. /// When advancing through a bitstream cursor, each advance can discover a few
  253. /// different kinds of entries:
  254. struct BitstreamEntry {
  255. enum {
  256. Error, // Malformed bitcode was found.
  257. EndBlock, // We've reached the end of the current block, (or the end of the
  258. // file, which is treated like a series of EndBlock records.
  259. SubBlock, // This is the start of a new subblock of a specific ID.
  260. Record // This is a record with a specific AbbrevID.
  261. } Kind;
  262. unsigned ID;
  263. static BitstreamEntry getError() {
  264. BitstreamEntry E; E.Kind = Error; return E;
  265. }
  266. static BitstreamEntry getEndBlock() {
  267. BitstreamEntry E; E.Kind = EndBlock; return E;
  268. }
  269. static BitstreamEntry getSubBlock(unsigned ID) {
  270. BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
  271. }
  272. static BitstreamEntry getRecord(unsigned AbbrevID) {
  273. BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
  274. }
  275. };
  276. /// This represents a position within a bitcode file, implemented on top of a
  277. /// SimpleBitstreamCursor.
  278. ///
  279. /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
  280. /// be passed by value.
  281. class BitstreamCursor : SimpleBitstreamCursor {
  282. // This is the declared size of code values used for the current block, in
  283. // bits.
  284. unsigned CurCodeSize = 2;
  285. /// Abbrevs installed at in this block.
  286. std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
  287. struct Block {
  288. unsigned PrevCodeSize;
  289. std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
  290. explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
  291. };
  292. /// This tracks the codesize of parent blocks.
  293. SmallVector<Block, 8> BlockScope;
  294. BitstreamBlockInfo *BlockInfo = nullptr;
  295. public:
  296. static const size_t MaxChunkSize = sizeof(word_t) * 8;
  297. BitstreamCursor() = default;
  298. explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
  299. : SimpleBitstreamCursor(BitcodeBytes) {}
  300. explicit BitstreamCursor(StringRef BitcodeBytes)
  301. : SimpleBitstreamCursor(BitcodeBytes) {}
  302. explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
  303. : SimpleBitstreamCursor(BitcodeBytes) {}
  304. using SimpleBitstreamCursor::AtEndOfStream;
  305. using SimpleBitstreamCursor::canSkipToPos;
  306. using SimpleBitstreamCursor::fillCurWord;
  307. using SimpleBitstreamCursor::getBitcodeBytes;
  308. using SimpleBitstreamCursor::GetCurrentBitNo;
  309. using SimpleBitstreamCursor::getCurrentByteNo;
  310. using SimpleBitstreamCursor::getPointerToByte;
  311. using SimpleBitstreamCursor::JumpToBit;
  312. using SimpleBitstreamCursor::Read;
  313. using SimpleBitstreamCursor::ReadVBR;
  314. using SimpleBitstreamCursor::ReadVBR64;
  315. using SimpleBitstreamCursor::SizeInBytes;
  316. using SimpleBitstreamCursor::skipToEnd;
  317. /// Return the number of bits used to encode an abbrev #.
  318. unsigned getAbbrevIDWidth() const { return CurCodeSize; }
  319. /// Flags that modify the behavior of advance().
  320. enum {
  321. /// If this flag is used, the advance() method does not automatically pop
  322. /// the block scope when the end of a block is reached.
  323. AF_DontPopBlockAtEnd = 1,
  324. /// If this flag is used, abbrev entries are returned just like normal
  325. /// records.
  326. AF_DontAutoprocessAbbrevs = 2
  327. };
  328. /// Advance the current bitstream, returning the next entry in the stream.
  329. Expected<BitstreamEntry> advance(unsigned Flags = 0) {
  330. while (true) {
  331. if (AtEndOfStream())
  332. return BitstreamEntry::getError();
  333. Expected<unsigned> MaybeCode = ReadCode();
  334. if (!MaybeCode)
  335. return MaybeCode.takeError();
  336. unsigned Code = MaybeCode.get();
  337. if (Code == bitc::END_BLOCK) {
  338. // Pop the end of the block unless Flags tells us not to.
  339. if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
  340. return BitstreamEntry::getError();
  341. return BitstreamEntry::getEndBlock();
  342. }
  343. if (Code == bitc::ENTER_SUBBLOCK) {
  344. if (Expected<unsigned> MaybeSubBlock = ReadSubBlockID())
  345. return BitstreamEntry::getSubBlock(MaybeSubBlock.get());
  346. else
  347. return MaybeSubBlock.takeError();
  348. }
  349. if (Code == bitc::DEFINE_ABBREV &&
  350. !(Flags & AF_DontAutoprocessAbbrevs)) {
  351. // We read and accumulate abbrev's, the client can't do anything with
  352. // them anyway.
  353. if (Error Err = ReadAbbrevRecord())
  354. return std::move(Err);
  355. continue;
  356. }
  357. return BitstreamEntry::getRecord(Code);
  358. }
  359. }
  360. /// This is a convenience function for clients that don't expect any
  361. /// subblocks. This just skips over them automatically.
  362. Expected<BitstreamEntry> advanceSkippingSubblocks(unsigned Flags = 0) {
  363. while (true) {
  364. // If we found a normal entry, return it.
  365. Expected<BitstreamEntry> MaybeEntry = advance(Flags);
  366. if (!MaybeEntry)
  367. return MaybeEntry;
  368. BitstreamEntry Entry = MaybeEntry.get();
  369. if (Entry.Kind != BitstreamEntry::SubBlock)
  370. return Entry;
  371. // If we found a sub-block, just skip over it and check the next entry.
  372. if (Error Err = SkipBlock())
  373. return std::move(Err);
  374. }
  375. }
  376. Expected<unsigned> ReadCode() { return Read(CurCodeSize); }
  377. // Block header:
  378. // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
  379. /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
  380. Expected<unsigned> ReadSubBlockID() { return ReadVBR(bitc::BlockIDWidth); }
  381. /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
  382. /// of this block.
  383. Error SkipBlock() {
  384. // Read and ignore the codelen value.
  385. if (Expected<uint32_t> Res = ReadVBR(bitc::CodeLenWidth))
  386. ; // Since we are skipping this block, we don't care what code widths are
  387. // used inside of it.
  388. else
  389. return Res.takeError();
  390. SkipToFourByteBoundary();
  391. Expected<unsigned> MaybeNum = Read(bitc::BlockSizeWidth);
  392. if (!MaybeNum)
  393. return MaybeNum.takeError();
  394. size_t NumFourBytes = MaybeNum.get();
  395. // Check that the block wasn't partially defined, and that the offset isn't
  396. // bogus.
  397. size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8;
  398. if (AtEndOfStream())
  399. return createStringError(std::errc::illegal_byte_sequence,
  400. "can't skip block: already at end of stream");
  401. if (!canSkipToPos(SkipTo / 8))
  402. return createStringError(std::errc::illegal_byte_sequence,
  403. "can't skip to bit %zu from %" PRIu64, SkipTo,
  404. GetCurrentBitNo());
  405. if (Error Res = JumpToBit(SkipTo))
  406. return Res;
  407. return Error::success();
  408. }
  409. /// Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
  410. Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
  411. bool ReadBlockEnd() {
  412. if (BlockScope.empty()) return true;
  413. // Block tail:
  414. // [END_BLOCK, <align4bytes>]
  415. SkipToFourByteBoundary();
  416. popBlockScope();
  417. return false;
  418. }
  419. private:
  420. void popBlockScope() {
  421. CurCodeSize = BlockScope.back().PrevCodeSize;
  422. CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
  423. BlockScope.pop_back();
  424. }
  425. //===--------------------------------------------------------------------===//
  426. // Record Processing
  427. //===--------------------------------------------------------------------===//
  428. public:
  429. /// Return the abbreviation for the specified AbbrevId.
  430. const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
  431. unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
  432. if (AbbrevNo >= CurAbbrevs.size())
  433. report_fatal_error("Invalid abbrev number");
  434. return CurAbbrevs[AbbrevNo].get();
  435. }
  436. /// Read the current record and discard it, returning the code for the record.
  437. Expected<unsigned> skipRecord(unsigned AbbrevID);
  438. Expected<unsigned> readRecord(unsigned AbbrevID,
  439. SmallVectorImpl<uint64_t> &Vals,
  440. StringRef *Blob = nullptr);
  441. //===--------------------------------------------------------------------===//
  442. // Abbrev Processing
  443. //===--------------------------------------------------------------------===//
  444. Error ReadAbbrevRecord();
  445. /// Read and return a block info block from the bitstream. If an error was
  446. /// encountered, return None.
  447. ///
  448. /// \param ReadBlockInfoNames Whether to read block/record name information in
  449. /// the BlockInfo block. Only llvm-bcanalyzer uses this.
  450. Expected<Optional<BitstreamBlockInfo>>
  451. ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
  452. /// Set the block info to be used by this BitstreamCursor to interpret
  453. /// abbreviated records.
  454. void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
  455. };
  456. } // end llvm namespace
  457. #endif // LLVM_BITSTREAM_BITSTREAMREADER_H
  458. #ifdef __GNUC__
  459. #pragma GCC diagnostic pop
  460. #endif