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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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, Cursor.GetCurrentBitNo()) |
| 53 << ErrorMessage; | 53 << ErrorMessage; |
| 54 report_fatal_error(StrBuf.str()); | 54 report_fatal_error(StrBuf.str()); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void NaClBitstreamCursor::freeState() { | |
| 58 // Free all the Abbrevs. | |
| 59 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) | |
| 60 CurAbbrevs[i]->dropRef(); | |
| 61 CurAbbrevs.clear(); | |
| 62 | |
| 63 // Free all the Abbrevs in the block scope. | |
| 64 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) { | |
| 65 std::vector<NaClBitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs; | |
| 66 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i) | |
| 67 Abbrevs[i]->dropRef(); | |
| 68 } | |
| 69 BlockScope.clear(); | |
| 70 } | |
| 71 | |
| 72 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const { | 57 void NaClBitstreamCursor::reportInvalidAbbrevNumber(unsigned AbbrevNo) const { |
| 73 std::string Buffer; | 58 std::string Buffer; |
| 74 raw_string_ostream StrBuf(Buffer); | 59 raw_string_ostream StrBuf(Buffer); |
| 75 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record"; | 60 StrBuf << "Invalid abbreviation # " << AbbrevNo << " defined for record"; |
| 76 ErrHandler->Fatal(StrBuf.str()); | 61 ErrHandler->Fatal(StrBuf.str()); |
| 77 } | 62 } |
| 78 | 63 |
| 79 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const { | 64 void NaClBitstreamCursor::reportInvalidJumpToBit(uint64_t BitNo) const { |
| 80 std::string Buffer; | 65 std::string Buffer; |
| 81 raw_string_ostream StrBuf(Buffer); | 66 raw_string_ostream StrBuf(Buffer); |
| 82 StrBuf << "Invalid jump to bit " << BitNo; | 67 StrBuf << "Invalid jump to bit " << BitNo; |
| 83 ErrHandler->Fatal(StrBuf.str()); | 68 ErrHandler->Fatal(StrBuf.str()); |
| 84 } | 69 } |
| 85 | 70 |
| 86 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter | 71 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter |
| 87 /// the block, and return true if the block has an error. | 72 /// the block, and return true if the block has an error. |
| 88 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { | 73 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { |
| 89 // Save the current block's state on BlockScope. | 74 constexpr bool IsFixed = true; |
|
Jim Stichnoth
2016/03/14 22:46:49
I think LLVM is still generally not using constexp
Karl
2016/03/15 20:29:41
Done.
| |
| 90 BlockScope.push_back(Block(CurCodeSize)); | 75 NaClBitcodeSelectorAbbrev |
| 91 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 76 CodeAbbrev(IsFixed, ReadVBR(naclbitc::CodeLenWidth)); |
| 92 | 77 BlockScope.push_back(Block(&BitStream->getOrCreateBlockInfo(BlockID), |
| 93 // Add the abbrevs specific to this block to the CurAbbrevs list. | 78 CodeAbbrev)); |
| 94 if (const NaClBitstreamReader::BlockInfo *Info = | |
| 95 BitStream->getBlockInfo(BlockID)) { | |
| 96 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) { | |
| 97 CurAbbrevs.push_back(Info->Abbrevs[i]); | |
| 98 CurAbbrevs.back()->addRef(); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Get the codesize of this block. | |
| 103 CurCodeSize.IsFixed = true; | |
| 104 CurCodeSize.NumBits = ReadVBR(naclbitc::CodeLenWidth); | |
| 105 SkipToFourByteBoundary(); | 79 SkipToFourByteBoundary(); |
| 106 unsigned NumWords = Read(naclbitc::BlockSizeWidth); | 80 unsigned NumWords = Read(naclbitc::BlockSizeWidth); |
| 107 if (NumWordsP) *NumWordsP = NumWords; | 81 if (NumWordsP) *NumWordsP = NumWords; |
| 108 | 82 |
| 109 // Validate that this block is sane. | 83 // Validate that this block is sane. |
| 110 if (CurCodeSize.NumBits == 0 || AtEndOfStream()) | 84 if (BlockScope.back().getCodeAbbrev().NumBits == 0 || AtEndOfStream()) |
| 111 return true; | 85 return true; |
| 112 | 86 |
| 113 return false; | 87 return false; |
| 114 } | 88 } |
| 115 | 89 |
| 116 void NaClBitstreamCursor::skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op) { | 90 void NaClBitstreamCursor::skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op) { |
| 117 // Decode the value as we are commanded. | 91 // Decode the value as we are commanded. |
| 118 switch (Op.getEncoding()) { | 92 switch (Op.getEncoding()) { |
| 119 case NaClBitCodeAbbrevOp::Literal: | 93 case NaClBitCodeAbbrevOp::Literal: |
| 120 // No read necessary for literal. | 94 // No read necessary for literal. |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 StrBuf << "Invalid abbreviation encoding (" | 299 StrBuf << "Invalid abbreviation encoding (" |
| 326 << NaClBitCodeAbbrevOp::getEncodingName(E) << ")"; | 300 << NaClBitCodeAbbrevOp::getEncodingName(E) << ")"; |
| 327 ErrHandler->Fatal(StrBuf.str()); | 301 ErrHandler->Fatal(StrBuf.str()); |
| 328 } | 302 } |
| 329 Abbv->Add(NaClBitCodeAbbrevOp(E)); | 303 Abbv->Add(NaClBitCodeAbbrevOp(E)); |
| 330 } | 304 } |
| 331 } | 305 } |
| 332 SkipToByteBoundaryIfAligned(); | 306 SkipToByteBoundaryIfAligned(); |
| 333 if (!Abbv->isValid()) | 307 if (!Abbv->isValid()) |
| 334 ErrHandler->Fatal("Invalid abbreviation specified in bitcode file"); | 308 ErrHandler->Fatal("Invalid abbreviation specified in bitcode file"); |
| 335 CurAbbrevs.push_back(Abbv); | 309 BlockScope.back().addNewLocalAbbrev(Abbv); |
| 336 if (Listener) { | 310 if (Listener) { |
| 337 Listener->ProcessAbbreviation(Abbv, IsLocal); | 311 Listener->ProcessAbbreviation(Abbv, IsLocal); |
| 338 // Reset record information of the listener. | 312 // Reset record information of the listener. |
| 339 Listener->Values.clear(); | 313 Listener->Values.clear(); |
| 340 Listener->StartBit = GetCurrentBitNo(); | 314 Listener->StartBit = GetCurrentBitNo(); |
| 341 } | 315 } |
| 342 } | 316 } |
| 343 | 317 |
| 344 void NaClBitstreamCursor::SkipAbbrevRecord() { | 318 void NaClBitstreamCursor::SkipAbbrevRecord() { |
| 345 unsigned NumOpInfo = ReadVBR(5); | 319 unsigned NumOpInfo = ReadVBR(5); |
| 346 for (unsigned i = 0; i != NumOpInfo; ++i) { | 320 for (unsigned i = 0; i != NumOpInfo; ++i) { |
| 347 bool IsLiteral = Read(1) ? true : false; | 321 bool IsLiteral = Read(1) ? true : false; |
| 348 if (IsLiteral) { | 322 if (IsLiteral) { |
| 349 ReadVBR64(8); | 323 ReadVBR64(8); |
| 350 continue; | 324 continue; |
| 351 } | 325 } |
| 352 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); | 326 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); |
| 353 if (NaClBitCodeAbbrevOp::hasValue(E)) { | 327 if (NaClBitCodeAbbrevOp::hasValue(E)) { |
| 354 ReadVBR64(5); | 328 ReadVBR64(5); |
| 355 } | 329 } |
| 356 } | 330 } |
| 357 SkipToByteBoundaryIfAligned(); | 331 SkipToByteBoundaryIfAligned(); |
| 358 } | 332 } |
| 359 | 333 |
| 360 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { | 334 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { |
| 361 // If this is the second stream to get to the block info block, skip it. | 335 // If this is the second stream to get to the block info block, skip it. |
| 362 if (BitStream->hasBlockInfoRecords()) | 336 if (BitStream->HasReadBlockInfoBlock) |
| 363 return SkipBlock(); | 337 return SkipBlock(); |
| 364 | 338 |
| 339 BitStream->HasReadBlockInfoBlock = true; | |
| 340 | |
| 365 unsigned NumWords; | 341 unsigned NumWords; |
| 366 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; | 342 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; |
| 367 | 343 |
| 368 if (Listener) Listener->BeginBlockInfoBlock(NumWords); | 344 if (Listener) Listener->BeginBlockInfoBlock(NumWords); |
| 369 | 345 |
| 370 NaClBitcodeRecordVector Record; | 346 NaClBitcodeRecordVector Record; |
| 371 NaClBitstreamReader::BlockInfo *CurBlockInfo = 0; | 347 Block &CurBlock = BlockScope.back(); |
| 348 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = | |
| 349 &CurBlock.GlobalAbbrevs->getAbbrevs(); | |
| 350 bool FoundSetBID = false; | |
| 372 | 351 |
| 373 // Read records of the BlockInfo block. | 352 // Read records of the BlockInfo block. |
| 374 while (1) { | 353 while (1) { |
| 375 if (Listener) Listener->StartBit = GetCurrentBitNo(); | 354 if (Listener) Listener->StartBit = GetCurrentBitNo(); |
| 376 NaClBitstreamEntry Entry = advance(AF_DontAutoprocessAbbrevs, Listener); | 355 NaClBitstreamEntry Entry = advance(AF_DontAutoprocessAbbrevs, Listener); |
| 377 | 356 |
| 378 switch (Entry.Kind) { | 357 switch (Entry.Kind) { |
| 379 case llvm::NaClBitstreamEntry::SubBlock: // PNaCl doesn't allow! | 358 case llvm::NaClBitstreamEntry::SubBlock: // PNaCl doesn't allow! |
| 380 case llvm::NaClBitstreamEntry::Error: | 359 case llvm::NaClBitstreamEntry::Error: |
| 381 return true; | 360 return true; |
| 382 case llvm::NaClBitstreamEntry::EndBlock: | 361 case llvm::NaClBitstreamEntry::EndBlock: |
| 383 if (Listener) Listener->EndBlockInfoBlock(); | 362 if (Listener) Listener->EndBlockInfoBlock(); |
| 384 return false; | 363 return false; |
| 385 case llvm::NaClBitstreamEntry::Record: | 364 case llvm::NaClBitstreamEntry::Record: |
| 386 // The interesting case. | 365 // The interesting case. |
| 387 break; | 366 break; |
| 388 } | 367 } |
| 389 | 368 |
| 390 // Read abbrev records, associate them with CurBID. | 369 // Read abbrev records, associate them with CurBID. |
| 391 if (Entry.ID == naclbitc::DEFINE_ABBREV) { | 370 if (Entry.ID == naclbitc::DEFINE_ABBREV) { |
| 392 if (!CurBlockInfo) return true; | |
| 393 ReadAbbrevRecord(false, Listener); | 371 ReadAbbrevRecord(false, Listener); |
| 394 | 372 |
| 395 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the | 373 // ReadAbbrevRecord installs as local abbreviation. Move it to |
|
Jim Stichnoth
2016/03/14 22:46:49
s/as/a/ ?
Karl
2016/03/15 20:29:41
Done.
| |
| 396 // appropriate BlockInfo. | 374 // the appropriate BlockInfo if the corresponding SetBID record |
|
Jim Stichnoth
2016/03/14 22:46:49
reflow comment to 80-col
Karl
2016/03/15 20:29:41
Done.
| |
| 397 NaClBitCodeAbbrev *Abbv = CurAbbrevs.back(); | 375 // has been found. |
| 398 CurAbbrevs.pop_back(); | 376 if (FoundSetBID) { |
| 399 CurBlockInfo->Abbrevs.push_back(Abbv); | 377 NaClBitstreamReader::AbbrevListVector &LocalAbbrevs = |
| 400 continue; | 378 CurBlock.LocalAbbrevs.Abbrevs; |
| 379 NaClBitCodeAbbrev *Abbv = LocalAbbrevs.back(); | |
| 380 LocalAbbrevs.pop_back(); | |
| 381 UpdateAbbrevs->push_back(Abbv); | |
| 382 continue; | |
| 383 } | |
| 401 } | 384 } |
| 402 | 385 |
| 403 // Read a record. | 386 // Read a record. |
| 404 Record.clear(); | 387 Record.clear(); |
| 405 switch (readRecord(Entry.ID, Record)) { | 388 switch (readRecord(Entry.ID, Record)) { |
| 406 default: | 389 default: |
| 407 // No other records should be found! | 390 // No other records should be found! |
| 408 return true; | 391 return true; |
| 409 case naclbitc::BLOCKINFO_CODE_SETBID: | 392 case naclbitc::BLOCKINFO_CODE_SETBID: |
| 410 if (Record.size() < 1) return true; | 393 if (Record.size() < 1) return true; |
| 411 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); | 394 FoundSetBID = true; |
| 395 UpdateAbbrevs = | |
| 396 &BitStream->getOrCreateBlockInfo((unsigned)Record[0]).getAbbrevs(); | |
| 412 if (Listener) { | 397 if (Listener) { |
| 413 Listener->Values = Record; | 398 Listener->Values = Record; |
| 414 Listener->SetBID(); | 399 Listener->SetBID(); |
| 415 } | 400 } |
| 416 break; | 401 break; |
| 417 } | 402 } |
| 418 } | 403 } |
| 419 } | 404 } |
| OLD | NEW |