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