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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit; | 432 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit; |
436 | 433 |
437 if ((Piece & (1U << (NumBits-1))) == 0) | 434 if ((Piece & (1U << (NumBits-1))) == 0) |
438 return Result; | 435 return Result; |
439 | 436 |
440 NextBit += NumBits-1; | 437 NextBit += NumBits-1; |
441 Piece = Read(NumBits); | 438 Piece = Read(NumBits); |
442 } | 439 } |
443 } | 440 } |
444 | 441 |
442 int32_t ReadIntVBR(unsigned NumBits) { | |
443 return (uint32_t) NaClDecodeSignRotatedValue(ReadVBR64(NumBits)); | |
jvoung (off chromium)
2013/05/21 17:24:17
doesn't look like this readintvbr is used
Karl
2013/05/21 23:06:52
Removed.
| |
444 } | |
445 | |
445 private: | 446 private: |
446 void SkipToFourByteBoundary() { | 447 void SkipToFourByteBoundary() { |
447 // If word_t is 64-bits and if we've read less than 32 bits, just dump | 448 // If word_t is 64-bits and if we've read less than 32 bits, just dump |
448 // the bits we have up to the next 32-bit boundary. | 449 // the bits we have up to the next 32-bit boundary. |
449 if (sizeof(word_t) > 4 && | 450 if (sizeof(word_t) > 4 && |
450 BitsInCurWord >= 32) { | 451 BitsInCurWord >= 32) { |
451 CurWord >>= BitsInCurWord-32; | 452 CurWord >>= BitsInCurWord-32; |
452 BitsInCurWord = 32; | 453 BitsInCurWord = 32; |
453 return; | 454 return; |
454 } | 455 } |
455 | 456 |
456 BitsInCurWord = 0; | 457 BitsInCurWord = 0; |
457 CurWord = 0; | 458 CurWord = 0; |
458 } | 459 } |
459 public: | 460 public: |
460 | 461 |
461 unsigned ReadCode() { | 462 unsigned ReadCode() { |
462 return Read(CurCodeSize); | 463 return CurCodeSize.IsFixed |
464 ? Read(CurCodeSize.NumBits) | |
465 : ReadVBR(CurCodeSize.NumBits); | |
463 } | 466 } |
464 | 467 |
465 | |
466 // Block header: | 468 // Block header: |
467 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 469 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
468 | 470 |
469 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for | 471 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for |
470 /// the block. | 472 /// the block. |
471 unsigned ReadSubBlockID() { | 473 unsigned ReadSubBlockID() { |
472 return ReadVBR(naclbitc::BlockIDWidth); | 474 return ReadVBR(naclbitc::BlockIDWidth); |
473 } | 475 } |
474 | 476 |
475 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip | 477 /// 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 | 553 // Abbrev Processing |
552 //===--------------------------------------------------------------------===// | 554 //===--------------------------------------------------------------------===// |
553 void ReadAbbrevRecord(); | 555 void ReadAbbrevRecord(); |
554 | 556 |
555 bool ReadBlockInfoBlock(); | 557 bool ReadBlockInfoBlock(); |
556 }; | 558 }; |
557 | 559 |
558 } // End llvm namespace | 560 } // End llvm namespace |
559 | 561 |
560 #endif | 562 #endif |
OLD | NEW |