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 } // end of anonymous namespace |
| 345 |
| 346 NaClBitstreamReader::BlockInfoRecordsMap:: |
| 347 BlockInfoRecordsMap() : IsFrozen(false) { |
| 348 for (size_t BlockID : ValidBlockIDs) |
| 349 Infos.emplace(BlockID, BlockInfo(BlockID)); |
| 350 } |
| 351 |
| 352 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock:: |
| 353 UpdateLock(BlockInfoRecordsMap &BlockInfoRecords) |
| 354 : BlockInfoRecords(BlockInfoRecords), Lock(BlockInfoRecords.Lock) |
| 355 {} |
| 356 |
| 357 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock:: |
| 358 ~UpdateLock() { |
| 359 if (BlockInfoRecords.freeze()) |
| 360 report_fatal_error("Global abbreviations block frozen while building."); |
| 361 } |
| 362 |
333 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { | 363 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { |
334 // If this is the second stream to get to the block info block, skip it. | 364 // If this is the second read of the block info block, skip it. |
335 if (BitStream->HasReadBlockInfoBlock) | 365 if (BitStream->BlockInfoRecords->isFrozen()) |
336 return SkipBlock(); | 366 return SkipBlock(); |
337 | 367 |
338 BitStream->HasReadBlockInfoBlock = true; | 368 NaClBitstreamReader::BlockInfoRecordsMap::UpdateLock |
339 | 369 Lock(*BitStream->BlockInfoRecords); |
340 unsigned NumWords; | 370 unsigned NumWords; |
341 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; | 371 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; |
342 | 372 |
343 if (Listener) Listener->BeginBlockInfoBlock(NumWords); | 373 if (Listener) Listener->BeginBlockInfoBlock(NumWords); |
344 | 374 |
345 NaClBitcodeRecordVector Record; | 375 NaClBitcodeRecordVector Record; |
346 Block &CurBlock = BlockScope.back(); | 376 Block &CurBlock = BlockScope.back(); |
347 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = | 377 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = |
348 &CurBlock.GlobalAbbrevs->getAbbrevs(); | 378 &CurBlock.GlobalAbbrevs->getAbbrevs(); |
349 bool FoundSetBID = false; | 379 bool FoundSetBID = false; |
(...skipping 30 matching lines...) Expand all Loading... |
380 // Read a record. | 410 // Read a record. |
381 Record.clear(); | 411 Record.clear(); |
382 switch (readRecord(Entry.ID, Record)) { | 412 switch (readRecord(Entry.ID, Record)) { |
383 default: | 413 default: |
384 // No other records should be found! | 414 // No other records should be found! |
385 return true; | 415 return true; |
386 case naclbitc::BLOCKINFO_CODE_SETBID: | 416 case naclbitc::BLOCKINFO_CODE_SETBID: |
387 if (Record.size() < 1) return true; | 417 if (Record.size() < 1) return true; |
388 FoundSetBID = true; | 418 FoundSetBID = true; |
389 UpdateAbbrevs = | 419 UpdateAbbrevs = |
390 &BitStream->getOrCreateBlockInfo((unsigned)Record[0]).getAbbrevs(); | 420 &BitStream->getBlockInfo((unsigned)Record[0]).getAbbrevs(); |
391 if (Listener) { | 421 if (Listener) { |
392 Listener->Values = Record; | 422 Listener->Values = Record; |
393 Listener->SetBID(); | 423 Listener->SetBID(); |
394 } | 424 } |
395 break; | 425 break; |
396 } | 426 } |
397 } | 427 } |
398 } | 428 } |
OLD | NEW |