| 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 const bool IsFixed = true; |
| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 raw_string_ostream StrBuf(Buffer); | 249 raw_string_ostream StrBuf(Buffer); |
| 276 StrBuf << "Invalid abbreviation encoding specified in bitcode file: " | 250 StrBuf << "Invalid abbreviation encoding specified in bitcode file: " |
| 277 << Value; | 251 << Value; |
| 278 ErrHandler->Fatal(StrBuf.str()); | 252 ErrHandler->Fatal(StrBuf.str()); |
| 279 } | 253 } |
| 280 return NaClBitCodeAbbrevOp::Encoding(Value); | 254 return NaClBitCodeAbbrevOp::Encoding(Value); |
| 281 } | 255 } |
| 282 | 256 |
| 283 void NaClBitstreamCursor::ReadAbbrevRecord(bool IsLocal, | 257 void NaClBitstreamCursor::ReadAbbrevRecord(bool IsLocal, |
| 284 NaClAbbrevListener *Listener) { | 258 NaClAbbrevListener *Listener) { |
| 285 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); | 259 NaClBitCodeAbbrev *Abbv = BlockScope.back().appendLocalCreate(); |
| 286 unsigned NumOpInfo = ReadVBR(5); | 260 unsigned NumOpInfo = ReadVBR(5); |
| 287 if (Listener) Listener->Values.push_back(NumOpInfo); | 261 if (Listener) Listener->Values.push_back(NumOpInfo); |
| 288 for (unsigned i = 0; i != NumOpInfo; ++i) { | 262 for (unsigned i = 0; i != NumOpInfo; ++i) { |
| 289 bool IsLiteral = Read(1) ? true : false; | 263 bool IsLiteral = Read(1) ? true : false; |
| 290 if (Listener) Listener->Values.push_back(IsLiteral); | 264 if (Listener) Listener->Values.push_back(IsLiteral); |
| 291 if (IsLiteral) { | 265 if (IsLiteral) { |
| 292 uint64_t Value = ReadVBR64(8); | 266 uint64_t Value = ReadVBR64(8); |
| 293 if (Listener) Listener->Values.push_back(Value); | 267 if (Listener) Listener->Values.push_back(Value); |
| 294 Abbv->Add(NaClBitCodeAbbrevOp(Value)); | 268 Abbv->Add(NaClBitCodeAbbrevOp(Value)); |
| 295 continue; | 269 continue; |
| (...skipping 29 matching lines...) Expand all 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); | |
| 336 if (Listener) { | 309 if (Listener) { |
| 337 Listener->ProcessAbbreviation(Abbv, IsLocal); | 310 Listener->ProcessAbbreviation(Abbv, IsLocal); |
| 338 // Reset record information of the listener. | 311 // Reset record information of the listener. |
| 339 Listener->Values.clear(); | 312 Listener->Values.clear(); |
| 340 Listener->StartBit = GetCurrentBitNo(); | 313 Listener->StartBit = GetCurrentBitNo(); |
| 341 } | 314 } |
| 342 } | 315 } |
| 343 | 316 |
| 344 void NaClBitstreamCursor::SkipAbbrevRecord() { | 317 void NaClBitstreamCursor::SkipAbbrevRecord() { |
| 345 unsigned NumOpInfo = ReadVBR(5); | 318 unsigned NumOpInfo = ReadVBR(5); |
| 346 for (unsigned i = 0; i != NumOpInfo; ++i) { | 319 for (unsigned i = 0; i != NumOpInfo; ++i) { |
| 347 bool IsLiteral = Read(1) ? true : false; | 320 bool IsLiteral = Read(1) ? true : false; |
| 348 if (IsLiteral) { | 321 if (IsLiteral) { |
| 349 ReadVBR64(8); | 322 ReadVBR64(8); |
| 350 continue; | 323 continue; |
| 351 } | 324 } |
| 352 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); | 325 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); |
| 353 if (NaClBitCodeAbbrevOp::hasValue(E)) { | 326 if (NaClBitCodeAbbrevOp::hasValue(E)) { |
| 354 ReadVBR64(5); | 327 ReadVBR64(5); |
| 355 } | 328 } |
| 356 } | 329 } |
| 357 SkipToByteBoundaryIfAligned(); | 330 SkipToByteBoundaryIfAligned(); |
| 358 } | 331 } |
| 359 | 332 |
| 360 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { | 333 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { |
| 361 // If this is the second stream to get to the block info block, skip it. | 334 // If this is the second stream to get to the block info block, skip it. |
| 362 if (BitStream->hasBlockInfoRecords()) | 335 if (BitStream->HasReadBlockInfoBlock) |
| 363 return SkipBlock(); | 336 return SkipBlock(); |
| 364 | 337 |
| 338 BitStream->HasReadBlockInfoBlock = true; |
| 339 |
| 365 unsigned NumWords; | 340 unsigned NumWords; |
| 366 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; | 341 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; |
| 367 | 342 |
| 368 if (Listener) Listener->BeginBlockInfoBlock(NumWords); | 343 if (Listener) Listener->BeginBlockInfoBlock(NumWords); |
| 369 | 344 |
| 370 NaClBitcodeRecordVector Record; | 345 NaClBitcodeRecordVector Record; |
| 371 NaClBitstreamReader::BlockInfo *CurBlockInfo = 0; | 346 Block &CurBlock = BlockScope.back(); |
| 347 NaClBitstreamReader::AbbrevList *UpdateAbbrevs = |
| 348 &CurBlock.GlobalAbbrevs->getAbbrevs(); |
| 349 bool FoundSetBID = false; |
| 372 | 350 |
| 373 // Read records of the BlockInfo block. | 351 // Read records of the BlockInfo block. |
| 374 while (1) { | 352 while (1) { |
| 375 if (Listener) Listener->StartBit = GetCurrentBitNo(); | 353 if (Listener) Listener->StartBit = GetCurrentBitNo(); |
| 376 NaClBitstreamEntry Entry = advance(AF_DontAutoprocessAbbrevs, Listener); | 354 NaClBitstreamEntry Entry = advance(AF_DontAutoprocessAbbrevs, Listener); |
| 377 | 355 |
| 378 switch (Entry.Kind) { | 356 switch (Entry.Kind) { |
| 379 case llvm::NaClBitstreamEntry::SubBlock: // PNaCl doesn't allow! | 357 case llvm::NaClBitstreamEntry::SubBlock: // PNaCl doesn't allow! |
| 380 case llvm::NaClBitstreamEntry::Error: | 358 case llvm::NaClBitstreamEntry::Error: |
| 381 return true; | 359 return true; |
| 382 case llvm::NaClBitstreamEntry::EndBlock: | 360 case llvm::NaClBitstreamEntry::EndBlock: |
| 383 if (Listener) Listener->EndBlockInfoBlock(); | 361 if (Listener) Listener->EndBlockInfoBlock(); |
| 384 return false; | 362 return false; |
| 385 case llvm::NaClBitstreamEntry::Record: | 363 case llvm::NaClBitstreamEntry::Record: |
| 386 // The interesting case. | 364 // The interesting case. |
| 387 break; | 365 break; |
| 388 } | 366 } |
| 389 | 367 |
| 390 // Read abbrev records, associate them with CurBID. | 368 // Read abbrev records, associate them with CurBID. |
| 391 if (Entry.ID == naclbitc::DEFINE_ABBREV) { | 369 if (Entry.ID == naclbitc::DEFINE_ABBREV) { |
| 392 if (!CurBlockInfo) return true; | |
| 393 ReadAbbrevRecord(false, Listener); | 370 ReadAbbrevRecord(false, Listener); |
| 394 | 371 |
| 395 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the | 372 // ReadAbbrevRecord installs a local abbreviation. Move it to the |
| 396 // appropriate BlockInfo. | 373 // appropriate BlockInfo if the corresponding SetBID record has been |
| 397 NaClBitCodeAbbrev *Abbv = CurAbbrevs.back(); | 374 // found. |
| 398 CurAbbrevs.pop_back(); | 375 if (FoundSetBID) |
| 399 CurBlockInfo->Abbrevs.push_back(Abbv); | 376 CurBlock.moveLocalAbbrevToAbbrevList(UpdateAbbrevs); |
| 400 continue; | 377 continue; |
| 401 } | 378 } |
| 402 | 379 |
| 403 // Read a record. | 380 // Read a record. |
| 404 Record.clear(); | 381 Record.clear(); |
| 405 switch (readRecord(Entry.ID, Record)) { | 382 switch (readRecord(Entry.ID, Record)) { |
| 406 default: | 383 default: |
| 407 // No other records should be found! | 384 // No other records should be found! |
| 408 return true; | 385 return true; |
| 409 case naclbitc::BLOCKINFO_CODE_SETBID: | 386 case naclbitc::BLOCKINFO_CODE_SETBID: |
| 410 if (Record.size() < 1) return true; | 387 if (Record.size() < 1) return true; |
| 411 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); | 388 FoundSetBID = true; |
| 389 UpdateAbbrevs = |
| 390 &BitStream->getOrCreateBlockInfo((unsigned)Record[0]).getAbbrevs(); |
| 412 if (Listener) { | 391 if (Listener) { |
| 413 Listener->Values = Record; | 392 Listener->Values = Record; |
| 414 Listener->SetBID(); | 393 Listener->SetBID(); |
| 415 } | 394 } |
| 416 break; | 395 break; |
| 417 } | 396 } |
| 418 } | 397 } |
| 419 } | 398 } |
| OLD | NEW |