Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- NaClBitstreamReader.cpp --------------------------------------------===// | 1 //===- NaClBitstreamReader.cpp --------------------------------------------===// |
| 2 // NaClBitstreamReader implementation | 2 // NaClBitstreamReader implementation |
| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 //===----------------------------------------------------------------------===// | 42 //===----------------------------------------------------------------------===// |
| 43 // NaClBitstreamCursor implementation | 43 // NaClBitstreamCursor implementation |
| 44 //===----------------------------------------------------------------------===// | 44 //===----------------------------------------------------------------------===// |
| 45 | 45 |
| 46 void NaClBitstreamCursor::ErrorHandler:: | 46 void NaClBitstreamCursor::ErrorHandler:: |
| 47 Fatal(const std::string &ErrorMessage) const { | 47 Fatal(const std::string &ErrorMessage) const { |
| 48 // Default implementation is simply print message, and the bit where | 48 // Default implementation is simply print message, and the bit where |
| 49 // the error occurred. | 49 // the error occurred. |
| 50 std::string Buffer; | 50 std::string Buffer; |
| 51 raw_string_ostream StrBuf(Buffer); | 51 raw_string_ostream StrBuf(Buffer); |
| 52 naclbitc::ErrorAt(StrBuf, naclbitc::Fatal, Cursor.GetCurrentBitNo()) | 52 naclbitc::ErrorAt(StrBuf, naclbitc::Fatal, getCurrentBitNo()) << ErrorMessage; |
| 53 << ErrorMessage; | |
| 54 report_fatal_error(StrBuf.str()); | 53 report_fatal_error(StrBuf.str()); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const { | 56 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const { |
| 58 std::string Buffer; | 57 std::string Buffer; |
| 59 raw_string_ostream StrBuf(Buffer); | 58 raw_string_ostream StrBuf(Buffer); |
| 60 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record"; | 59 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record"; |
| 61 ErrHandler->Fatal(StrBuf.str()); | 60 ErrHandler->Fatal(StrBuf.str()); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const { | 63 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const { |
| 65 std::string Buffer; | 64 std::string Buffer; |
| 66 raw_string_ostream StrBuf(Buffer); | 65 raw_string_ostream StrBuf(Buffer); |
| 67 StrBuf << "Invalid jump to bit " << BitNo; | 66 StrBuf << "Invalid jump to bit " << BitNo; |
| 68 ErrHandler->Fatal(StrBuf.str()); | 67 ErrHandler->Fatal(StrBuf.str()); |
| 69 } | 68 } |
| 70 | 69 |
| 71 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter | 70 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter |
| 72 /// the block, and return true if the block has an error. | 71 /// the block, and return true if the block has an error. |
| 73 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { | 72 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { |
| 74 const bool IsFixed = true; | 73 const bool IsFixed = true; |
| 75 NaClBitcodeSelectorAbbrev | 74 NaClBitcodeSelectorAbbrev |
| 76 CodeAbbrev(IsFixed, ReadVBR(naclbitc::CodeLenWidth)); | 75 CodeAbbrev(IsFixed, ReadVBR(naclbitc::CodeLenWidth)); |
| 77 BlockScope.push_back(Block(&BitStream->getOrCreateBlockInfo(BlockID), | 76 BlockScope.push_back(Block(&BitStream->getBlockInfo(BlockID), CodeAbbrev)); |
| 78 CodeAbbrev)); | |
| 79 SkipToFourByteBoundary(); | 77 SkipToFourByteBoundary(); |
| 80 unsigned NumWords = Read(naclbitc::BlockSizeWidth); | 78 unsigned NumWords = Read(naclbitc::BlockSizeWidth); |
| 81 if (NumWordsP) *NumWordsP = NumWords; | 79 if (NumWordsP) *NumWordsP = NumWords; |
| 82 | 80 |
| 83 // Validate that this block is sane. | 81 // Validate that this block is sane. |
| 84 if (BlockScope.back().getCodeAbbrev().NumBits == 0 || AtEndOfStream()) | 82 if (BlockScope.back().getCodeAbbrev().NumBits == 0 || AtEndOfStream()) |
| 85 return true; | 83 return true; |
| 86 | 84 |
| 87 return false; | 85 return false; |
| 88 } | 86 } |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 continue; | 321 continue; |
| 324 } | 322 } |
| 325 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); | 323 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); |
| 326 if (NaClBitCodeAbbrevOp::hasValue(E)) { | 324 if (NaClBitCodeAbbrevOp::hasValue(E)) { |
| 327 ReadVBR64(5); | 325 ReadVBR64(5); |
| 328 } | 326 } |
| 329 } | 327 } |
| 330 SkipToByteBoundaryIfAligned(); | 328 SkipToByteBoundaryIfAligned(); |
| 331 } | 329 } |
| 332 | 330 |
| 331 namespace { | |
| 332 | |
| 333 unsigned ValidBlockIDs[] = { | |
| 334 naclbitc::BLOCKINFO_BLOCK_ID, | |
| 335 naclbitc::CONSTANTS_BLOCK_ID, | |
| 336 naclbitc::FUNCTION_BLOCK_ID, | |
| 337 naclbitc::GLOBALVAR_BLOCK_ID, | |
| 338 naclbitc::MODULE_BLOCK_ID, | |
| 339 naclbitc::TOP_LEVEL_BLOCKID, | |
| 340 naclbitc::TYPE_BLOCK_ID_NEW, | |
| 341 naclbitc::VALUE_SYMTAB_BLOCK_ID | |
| 342 }; | |
| 343 | |
| 344 const size_t InfosBucketSize = 23; | |
| 345 | |
| 346 } // end of anonymous namespace | |
|
John
2016/03/29 16:27:10
is this annotation part of the llvm coding style?
Karl
2016/03/29 20:10:47
There is a similar comment at the beginning of the
| |
| 347 | |
| 348 NaClBitstreamReader::BlockInfoRecordsMap:: | |
| 349 BlockInfoRecordsMap() : Infos(InfosBucketSize), IsFrozen(false) { | |
| 350 for (size_t i = 0; i < array_lengthof(ValidBlockIDs); ++i) { | |
|
John
2016/03/29 16:27:10
you can use for each loops for static array !!!! (
Karl
2016/03/29 20:10:47
Done.
| |
| 351 unsigned BlockID = ValidBlockIDs[i]; | |
| 352 Infos.emplace(BlockID, BlockInfo(BlockID)); | |
| 353 } | |
| 354 } | |
| 355 | |
| 356 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock:: | |
| 357 UpdateLock(BlockInfoRecordsMap &BlockInfoRecords) | |
| 358 : BlockInfoRecords(BlockInfoRecords), Lock(BlockInfoRecords.Lock) | |
| 359 {} | |
| 360 | |
| 361 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock:: | |
| 362 ~UpdateLock() { | |
| 363 if (BlockInfoRecords.freeze()) | |
| 364 report_fatal_error("Global abbreviations block frozen while building."); | |
| 365 } | |
| 366 | |
| 333 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { | 367 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { |
| 334 // If this is the second stream to get to the block info block, skip it. | 368 // If this is the second read of the block info block, skip it. |
| 335 if (BitStream->HasReadBlockInfoBlock) | 369 if (BitStream->BlockInfoRecords->isFrozen()) |
| 336 return SkipBlock(); | 370 return SkipBlock(); |
| 337 | 371 |
| 338 BitStream->HasReadBlockInfoBlock = true; | 372 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock |
| 339 | 373 Lock(*BitStream->BlockInfoRecords); |
| 340 unsigned NumWords; | 374 unsigned NumWords; |
| 341 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; | 375 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; |
| 342 | 376 |
| 343 if (Listener) Listener->BeginBlockInfoBlock(NumWords); | 377 if (Listener) Listener->BeginBlockInfoBlock(NumWords); |
| 344 | 378 |
| 345 NaClBitcodeRecordVector Record; | 379 NaClBitcodeRecordVector Record; |
| 346 Block &CurBlock = BlockScope.back(); | 380 Block &CurBlock = BlockScope.back(); |
| 347 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = | 381 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = |
| 348 &CurBlock.GlobalAbbrevs->getAbbrevs(); | 382 &CurBlock.GlobalAbbrevs->getAbbrevs(); |
| 349 bool FoundSetBID = false; | 383 bool FoundSetBID = false; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 380 // Read a record. | 414 // Read a record. |
| 381 Record.clear(); | 415 Record.clear(); |
| 382 switch (readRecord(Entry.ID, Record)) { | 416 switch (readRecord(Entry.ID, Record)) { |
| 383 default: | 417 default: |
| 384 // No other records should be found! | 418 // No other records should be found! |
| 385 return true; | 419 return true; |
| 386 case naclbitc::BLOCKINFO_CODE_SETBID: | 420 case naclbitc::BLOCKINFO_CODE_SETBID: |
| 387 if (Record.size() < 1) return true; | 421 if (Record.size() < 1) return true; |
| 388 FoundSetBID = true; | 422 FoundSetBID = true; |
| 389 UpdateAbbrevs = | 423 UpdateAbbrevs = |
| 390 &BitStream->getOrCreateBlockInfo((unsigned)Record[0]).getAbbrevs(); | 424 &BitStream->getBlockInfo((unsigned)Record[0]).getAbbrevs(); |
| 391 if (Listener) { | 425 if (Listener) { |
| 392 Listener->Values = Record; | 426 Listener->Values = Record; |
| 393 Listener->SetBID(); | 427 Listener->SetBID(); |
| 394 } | 428 } |
| 395 break; | 429 break; |
| 396 } | 430 } |
| 397 } | 431 } |
| 398 } | 432 } |
| OLD | NEW |