| OLD | NEW |
| 1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===// | 1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===// |
| 2 // Low-level bitstream reader interface | 2 // Low-level bitstream reader interface |
| 3 // | 3 // |
| 4 // The LLVM Compiler Infrastructure | 4 // The LLVM Compiler Infrastructure |
| 5 // | 5 // |
| 6 // This file is distributed under the University of Illinois Open Source | 6 // This file is distributed under the University of Illinois Open Source |
| 7 // License. See LICENSE.TXT for details. | 7 // License. See LICENSE.TXT for details. |
| 8 // | 8 // |
| 9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
| 10 // | 10 // |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 /// file. There may be multiple independent cursors reading within | 164 /// file. There may be multiple independent cursors reading within |
| 165 /// one bitstream, each maintaining their own local state. | 165 /// one bitstream, each maintaining their own local state. |
| 166 /// | 166 /// |
| 167 /// Unlike iterators, NaClBitstreamCursors are heavy-weight objects | 167 /// Unlike iterators, NaClBitstreamCursors are heavy-weight objects |
| 168 /// that should not be passed by value. | 168 /// that should not be passed by value. |
| 169 class NaClBitstreamCursor { | 169 class NaClBitstreamCursor { |
| 170 friend class Deserializer; | 170 friend class Deserializer; |
| 171 NaClBitstreamReader *BitStream; | 171 NaClBitstreamReader *BitStream; |
| 172 size_t NextChar; | 172 size_t NextChar; |
| 173 | 173 |
| 174 | |
| 175 /// CurWord/word_t - This is the current data we have pulled from the stream | 174 /// CurWord/word_t - This is the current data we have pulled from the stream |
| 176 /// but have not returned to the client. This is specifically and | 175 /// but have not returned to the client. This is specifically and |
| 177 /// intentionally defined to follow the word size of the host machine for | 176 /// intentionally defined to follow the word size of the host machine for |
| 178 /// efficiency. We use word_t in places that are aware of this to make it | 177 /// efficiency. We use word_t in places that are aware of this to make it |
| 179 /// perfectly explicit what is going on. | 178 /// perfectly explicit what is going on. |
| 180 typedef uint32_t word_t; | 179 typedef uint32_t word_t; |
| 181 word_t CurWord; | 180 word_t CurWord; |
| 182 | 181 |
| 183 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This | 182 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This |
| 184 /// is always from [0...31/63] inclusive (depending on word size). | 183 /// is always from [0...31/63] inclusive (depending on word size). |
| 185 unsigned BitsInCurWord; | 184 unsigned BitsInCurWord; |
| 186 | 185 |
| 187 // CurCodeSize - This is the declared size of code values used for the current | 186 // CurCodeSize - This is the declared size of code values used for the current |
| 188 // block, in bits. | 187 // block, in bits. |
| 189 unsigned CurCodeSize; | 188 NaClBitcodeSelectorAbbrev CurCodeSize; |
| 190 | 189 |
| 191 /// CurAbbrevs - Abbrevs installed at in this block. | 190 /// CurAbbrevs - Abbrevs installed at in this block. |
| 192 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; | 191 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; |
| 193 | 192 |
| 194 struct Block { | 193 struct Block { |
| 195 unsigned PrevCodeSize; | 194 NaClBitcodeSelectorAbbrev PrevCodeSize; |
| 196 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; | 195 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; |
| 197 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} | 196 explicit Block() : PrevCodeSize() {} |
| 197 explicit Block(const NaClBitcodeSelectorAbbrev& PCS) |
| 198 : PrevCodeSize(PCS) {} |
| 198 }; | 199 }; |
| 199 | 200 |
| 200 /// BlockScope - This tracks the codesize of parent blocks. | 201 /// BlockScope - This tracks the codesize of parent blocks. |
| 201 SmallVector<Block, 8> BlockScope; | 202 SmallVector<Block, 8> BlockScope; |
| 202 | 203 |
| 203 | |
| 204 public: | 204 public: |
| 205 NaClBitstreamCursor() : BitStream(0), NextChar(0) { | 205 NaClBitstreamCursor() : BitStream(0), NextChar(0) { |
| 206 } | 206 } |
| 207 NaClBitstreamCursor(const NaClBitstreamCursor &RHS) | 207 NaClBitstreamCursor(const NaClBitstreamCursor &RHS) |
| 208 : BitStream(0), NextChar(0) { | 208 : BitStream(0), NextChar(0) { |
| 209 operator=(RHS); | 209 operator=(RHS); |
| 210 } | 210 } |
| 211 | 211 |
| 212 explicit NaClBitstreamCursor(NaClBitstreamReader &R) : BitStream(&R) { | 212 explicit NaClBitstreamCursor(NaClBitstreamReader &R) : BitStream(&R) { |
| 213 NextChar = 0; | 213 NextChar = 0; |
| 214 CurWord = 0; | 214 CurWord = 0; |
| 215 BitsInCurWord = 0; | 215 BitsInCurWord = 0; |
| 216 CurCodeSize = 2; | |
| 217 } | 216 } |
| 218 | 217 |
| 219 void init(NaClBitstreamReader &R) { | 218 void init(NaClBitstreamReader &R) { |
| 220 freeState(); | 219 freeState(); |
| 221 | 220 |
| 222 BitStream = &R; | 221 BitStream = &R; |
| 223 NextChar = 0; | 222 NextChar = 0; |
| 224 CurWord = 0; | 223 CurWord = 0; |
| 225 BitsInCurWord = 0; | 224 BitsInCurWord = 0; |
| 226 CurCodeSize = 2; | |
| 227 } | 225 } |
| 228 | 226 |
| 229 ~NaClBitstreamCursor() { | 227 ~NaClBitstreamCursor() { |
| 230 freeState(); | 228 freeState(); |
| 231 } | 229 } |
| 232 | 230 |
| 233 void operator=(const NaClBitstreamCursor &RHS); | 231 void operator=(const NaClBitstreamCursor &RHS); |
| 234 | 232 |
| 235 void freeState(); | 233 void freeState(); |
| 236 | 234 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 248 uint8_t buf[4] = { 0xFF, 0xFF, 0xFF, 0xFF }; | 246 uint8_t buf[4] = { 0xFF, 0xFF, 0xFF, 0xFF }; |
| 249 BitStream->getBitcodeBytes().readBytes(pos, sizeof(buf), buf, NULL); | 247 BitStream->getBitcodeBytes().readBytes(pos, sizeof(buf), buf, NULL); |
| 250 return *reinterpret_cast<support::ulittle32_t *>(buf); | 248 return *reinterpret_cast<support::ulittle32_t *>(buf); |
| 251 } | 249 } |
| 252 | 250 |
| 253 bool AtEndOfStream() { | 251 bool AtEndOfStream() { |
| 254 return BitsInCurWord == 0 && isEndPos(NextChar); | 252 return BitsInCurWord == 0 && isEndPos(NextChar); |
| 255 } | 253 } |
| 256 | 254 |
| 257 /// getAbbrevIDWidth - Return the number of bits used to encode an abbrev #. | 255 /// getAbbrevIDWidth - Return the number of bits used to encode an abbrev #. |
| 258 unsigned getAbbrevIDWidth() const { return CurCodeSize; } | 256 unsigned getAbbrevIDWidth() const { return CurCodeSize.NumBits; } |
| 259 | 257 |
| 260 /// GetCurrentBitNo - Return the bit # of the bit we are reading. | 258 /// GetCurrentBitNo - Return the bit # of the bit we are reading. |
| 261 uint64_t GetCurrentBitNo() const { | 259 uint64_t GetCurrentBitNo() const { |
| 262 return NextChar*CHAR_BIT - BitsInCurWord; | 260 return NextChar*CHAR_BIT - BitsInCurWord; |
| 263 } | 261 } |
| 264 | 262 |
| 265 NaClBitstreamReader *getBitStreamReader() { | 263 NaClBitstreamReader *getBitStreamReader() { |
| 266 return BitStream; | 264 return BitStream; |
| 267 } | 265 } |
| 268 const NaClBitstreamReader *getBitStreamReader() const { | 266 const NaClBitstreamReader *getBitStreamReader() const { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 | 334 |
| 337 // Skip over any bits that are already consumed. | 335 // Skip over any bits that are already consumed. |
| 338 if (WordBitNo) { | 336 if (WordBitNo) { |
| 339 if (sizeof(word_t) > 4) | 337 if (sizeof(word_t) > 4) |
| 340 Read64(WordBitNo); | 338 Read64(WordBitNo); |
| 341 else | 339 else |
| 342 Read(WordBitNo); | 340 Read(WordBitNo); |
| 343 } | 341 } |
| 344 } | 342 } |
| 345 | 343 |
| 346 | |
| 347 uint32_t Read(unsigned NumBits) { | 344 uint32_t Read(unsigned NumBits) { |
| 348 assert(NumBits && NumBits <= 32 && | 345 assert(NumBits && NumBits <= 32 && |
| 349 "Cannot return zero or more than 32 bits!"); | 346 "Cannot return zero or more than 32 bits!"); |
| 350 | 347 |
| 351 // If the field is fully contained by CurWord, return it quickly. | 348 // If the field is fully contained by CurWord, return it quickly. |
| 352 if (BitsInCurWord >= NumBits) { | 349 if (BitsInCurWord >= NumBits) { |
| 353 uint32_t R = uint32_t(CurWord) & (~0U >> (32-NumBits)); | 350 uint32_t R = uint32_t(CurWord) & (~0U >> (32-NumBits)); |
| 354 CurWord >>= NumBits; | 351 CurWord >>= NumBits; |
| 355 BitsInCurWord -= NumBits; | 352 BitsInCurWord -= NumBits; |
| 356 return R; | 353 return R; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 BitsInCurWord = 32; | 449 BitsInCurWord = 32; |
| 453 return; | 450 return; |
| 454 } | 451 } |
| 455 | 452 |
| 456 BitsInCurWord = 0; | 453 BitsInCurWord = 0; |
| 457 CurWord = 0; | 454 CurWord = 0; |
| 458 } | 455 } |
| 459 public: | 456 public: |
| 460 | 457 |
| 461 unsigned ReadCode() { | 458 unsigned ReadCode() { |
| 462 return Read(CurCodeSize); | 459 return CurCodeSize.IsFixed |
| 460 ? Read(CurCodeSize.NumBits) |
| 461 : ReadVBR(CurCodeSize.NumBits); |
| 463 } | 462 } |
| 464 | 463 |
| 465 | |
| 466 // Block header: | 464 // Block header: |
| 467 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 465 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
| 468 | 466 |
| 469 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for | 467 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for |
| 470 /// the block. | 468 /// the block. |
| 471 unsigned ReadSubBlockID() { | 469 unsigned ReadSubBlockID() { |
| 472 return ReadVBR(naclbitc::BlockIDWidth); | 470 return ReadVBR(naclbitc::BlockIDWidth); |
| 473 } | 471 } |
| 474 | 472 |
| 475 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip | 473 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 // Abbrev Processing | 549 // Abbrev Processing |
| 552 //===--------------------------------------------------------------------===// | 550 //===--------------------------------------------------------------------===// |
| 553 void ReadAbbrevRecord(); | 551 void ReadAbbrevRecord(); |
| 554 | 552 |
| 555 bool ReadBlockInfoBlock(); | 553 bool ReadBlockInfoBlock(); |
| 556 }; | 554 }; |
| 557 | 555 |
| 558 } // End llvm namespace | 556 } // End llvm namespace |
| 559 | 557 |
| 560 #endif | 558 #endif |
| OLD | NEW |