| 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 21 matching lines...) Expand all Loading... |
| 32 /// bitcode wire format stream, maintaining information that is global | 32 /// bitcode wire format stream, maintaining information that is global |
| 33 /// to decoding the entire file. While a file is being read, multiple | 33 /// to decoding the entire file. While a file is being read, multiple |
| 34 /// cursors can be independently advanced or skipped around within the | 34 /// cursors can be independently advanced or skipped around within the |
| 35 /// file. These are represented by the NaClBitstreamCursor class. | 35 /// file. These are represented by the NaClBitstreamCursor class. |
| 36 class NaClBitstreamReader { | 36 class NaClBitstreamReader { |
| 37 public: | 37 public: |
| 38 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. | 38 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. |
| 39 /// These describe abbreviations that all blocks of the specified ID inherit. | 39 /// These describe abbreviations that all blocks of the specified ID inherit. |
| 40 struct BlockInfo { | 40 struct BlockInfo { |
| 41 unsigned BlockID; | 41 unsigned BlockID; |
| 42 std::vector<BitCodeAbbrev*> Abbrevs; | 42 std::vector<NaClBitCodeAbbrev*> Abbrevs; |
| 43 std::string Name; | 43 std::string Name; |
| 44 | 44 |
| 45 std::vector<std::pair<unsigned, std::string> > RecordNames; | 45 std::vector<std::pair<unsigned, std::string> > RecordNames; |
| 46 }; | 46 }; |
| 47 private: | 47 private: |
| 48 OwningPtr<StreamableMemoryObject> BitcodeBytes; | 48 OwningPtr<StreamableMemoryObject> BitcodeBytes; |
| 49 | 49 |
| 50 std::vector<BlockInfo> BlockInfoRecords; | 50 std::vector<BlockInfo> BlockInfoRecords; |
| 51 | 51 |
| 52 /// IgnoreBlockInfoNames - This is set to true if we don't care about the | 52 /// IgnoreBlockInfoNames - This is set to true if we don't care about the |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 182 |
| 183 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This | 183 /// 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). | 184 /// is always from [0...31/63] inclusive (depending on word size). |
| 185 unsigned BitsInCurWord; | 185 unsigned BitsInCurWord; |
| 186 | 186 |
| 187 // CurCodeSize - This is the declared size of code values used for the current | 187 // CurCodeSize - This is the declared size of code values used for the current |
| 188 // block, in bits. | 188 // block, in bits. |
| 189 unsigned CurCodeSize; | 189 unsigned CurCodeSize; |
| 190 | 190 |
| 191 /// CurAbbrevs - Abbrevs installed at in this block. | 191 /// CurAbbrevs - Abbrevs installed at in this block. |
| 192 std::vector<BitCodeAbbrev*> CurAbbrevs; | 192 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; |
| 193 | 193 |
| 194 struct Block { | 194 struct Block { |
| 195 unsigned PrevCodeSize; | 195 unsigned PrevCodeSize; |
| 196 std::vector<BitCodeAbbrev*> PrevAbbrevs; | 196 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; |
| 197 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} | 197 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} |
| 198 }; | 198 }; |
| 199 | 199 |
| 200 /// BlockScope - This tracks the codesize of parent blocks. | 200 /// BlockScope - This tracks the codesize of parent blocks. |
| 201 SmallVector<Block, 8> BlockScope; | 201 SmallVector<Block, 8> BlockScope; |
| 202 | 202 |
| 203 | 203 |
| 204 public: | 204 public: |
| 205 NaClBitstreamCursor() : BitStream(0), NextChar(0) { | 205 NaClBitstreamCursor() : BitStream(0), NextChar(0) { |
| 206 } | 206 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 /// AF_DontAutoprocessAbbrevs - If this flag is used, abbrev entries are | 279 /// AF_DontAutoprocessAbbrevs - If this flag is used, abbrev entries are |
| 280 /// returned just like normal records. | 280 /// returned just like normal records. |
| 281 AF_DontAutoprocessAbbrevs = 2 | 281 AF_DontAutoprocessAbbrevs = 2 |
| 282 }; | 282 }; |
| 283 | 283 |
| 284 /// advance - Advance the current bitstream, returning the next entry in the | 284 /// advance - Advance the current bitstream, returning the next entry in the |
| 285 /// stream. | 285 /// stream. |
| 286 NaClBitstreamEntry advance(unsigned Flags = 0) { | 286 NaClBitstreamEntry advance(unsigned Flags = 0) { |
| 287 while (1) { | 287 while (1) { |
| 288 unsigned Code = ReadCode(); | 288 unsigned Code = ReadCode(); |
| 289 if (Code == bitc::END_BLOCK) { | 289 if (Code == naclbitc::END_BLOCK) { |
| 290 // Pop the end of the block unless Flags tells us not to. | 290 // Pop the end of the block unless Flags tells us not to. |
| 291 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) | 291 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) |
| 292 return NaClBitstreamEntry::getError(); | 292 return NaClBitstreamEntry::getError(); |
| 293 return NaClBitstreamEntry::getEndBlock(); | 293 return NaClBitstreamEntry::getEndBlock(); |
| 294 } | 294 } |
| 295 | 295 |
| 296 if (Code == bitc::ENTER_SUBBLOCK) | 296 if (Code == naclbitc::ENTER_SUBBLOCK) |
| 297 return NaClBitstreamEntry::getSubBlock(ReadSubBlockID()); | 297 return NaClBitstreamEntry::getSubBlock(ReadSubBlockID()); |
| 298 | 298 |
| 299 if (Code == bitc::DEFINE_ABBREV && | 299 if (Code == naclbitc::DEFINE_ABBREV && |
| 300 !(Flags & AF_DontAutoprocessAbbrevs)) { | 300 !(Flags & AF_DontAutoprocessAbbrevs)) { |
| 301 // We read and accumulate abbrev's, the client can't do anything with | 301 // We read and accumulate abbrev's, the client can't do anything with |
| 302 // them anyway. | 302 // them anyway. |
| 303 ReadAbbrevRecord(); | 303 ReadAbbrevRecord(); |
| 304 continue; | 304 continue; |
| 305 } | 305 } |
| 306 | 306 |
| 307 return NaClBitstreamEntry::getRecord(Code); | 307 return NaClBitstreamEntry::getRecord(Code); |
| 308 } | 308 } |
| 309 } | 309 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 return Read(CurCodeSize); | 462 return Read(CurCodeSize); |
| 463 } | 463 } |
| 464 | 464 |
| 465 | 465 |
| 466 // Block header: | 466 // Block header: |
| 467 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 467 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
| 468 | 468 |
| 469 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for | 469 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for |
| 470 /// the block. | 470 /// the block. |
| 471 unsigned ReadSubBlockID() { | 471 unsigned ReadSubBlockID() { |
| 472 return ReadVBR(bitc::BlockIDWidth); | 472 return ReadVBR(naclbitc::BlockIDWidth); |
| 473 } | 473 } |
| 474 | 474 |
| 475 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip | 475 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip |
| 476 /// over the body of this block. If the block record is malformed, return | 476 /// over the body of this block. If the block record is malformed, return |
| 477 /// true. | 477 /// true. |
| 478 bool SkipBlock() { | 478 bool SkipBlock() { |
| 479 // Read and ignore the codelen value. Since we are skipping this block, we | 479 // Read and ignore the codelen value. Since we are skipping this block, we |
| 480 // don't care what code widths are used inside of it. | 480 // don't care what code widths are used inside of it. |
| 481 ReadVBR(bitc::CodeLenWidth); | 481 ReadVBR(naclbitc::CodeLenWidth); |
| 482 SkipToFourByteBoundary(); | 482 SkipToFourByteBoundary(); |
| 483 unsigned NumFourBytes = Read(bitc::BlockSizeWidth); | 483 unsigned NumFourBytes = Read(naclbitc::BlockSizeWidth); |
| 484 | 484 |
| 485 // Check that the block wasn't partially defined, and that the offset isn't | 485 // Check that the block wasn't partially defined, and that the offset isn't |
| 486 // bogus. | 486 // bogus. |
| 487 size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8; | 487 size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8; |
| 488 if (AtEndOfStream() || !canSkipToPos(SkipTo/8)) | 488 if (AtEndOfStream() || !canSkipToPos(SkipTo/8)) |
| 489 return true; | 489 return true; |
| 490 | 490 |
| 491 JumpToBit(SkipTo); | 491 JumpToBit(SkipTo); |
| 492 return false; | 492 return false; |
| 493 } | 493 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 519 | 519 |
| 520 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 520 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
| 521 BlockScope.pop_back(); | 521 BlockScope.pop_back(); |
| 522 } | 522 } |
| 523 | 523 |
| 524 //===--------------------------------------------------------------------===// | 524 //===--------------------------------------------------------------------===// |
| 525 // Record Processing | 525 // Record Processing |
| 526 //===--------------------------------------------------------------------===// | 526 //===--------------------------------------------------------------------===// |
| 527 | 527 |
| 528 private: | 528 private: |
| 529 void readAbbreviatedLiteral(const BitCodeAbbrevOp &Op, | 529 void readAbbreviatedLiteral(const NaClBitCodeAbbrevOp &Op, |
| 530 SmallVectorImpl<uint64_t> &Vals); | 530 SmallVectorImpl<uint64_t> &Vals); |
| 531 void readAbbreviatedField(const BitCodeAbbrevOp &Op, | 531 void readAbbreviatedField(const NaClBitCodeAbbrevOp &Op, |
| 532 SmallVectorImpl<uint64_t> &Vals); | 532 SmallVectorImpl<uint64_t> &Vals); |
| 533 void skipAbbreviatedField(const BitCodeAbbrevOp &Op); | 533 void skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op); |
| 534 | 534 |
| 535 public: | 535 public: |
| 536 | 536 |
| 537 /// getAbbrev - Return the abbreviation for the specified AbbrevId. | 537 /// getAbbrev - Return the abbreviation for the specified AbbrevId. |
| 538 const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) { | 538 const NaClBitCodeAbbrev *getAbbrev(unsigned AbbrevID) { |
| 539 unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV; | 539 unsigned AbbrevNo = AbbrevID-naclbitc::FIRST_APPLICATION_ABBREV; |
| 540 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!"); | 540 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!"); |
| 541 return CurAbbrevs[AbbrevNo]; | 541 return CurAbbrevs[AbbrevNo]; |
| 542 } | 542 } |
| 543 | 543 |
| 544 /// skipRecord - Read the current record and discard it. | 544 /// skipRecord - Read the current record and discard it. |
| 545 void skipRecord(unsigned AbbrevID); | 545 void skipRecord(unsigned AbbrevID); |
| 546 | 546 |
| 547 unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals, | 547 unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals, |
| 548 StringRef *Blob = 0); | 548 StringRef *Blob = 0); |
| 549 | 549 |
| 550 //===--------------------------------------------------------------------===// | 550 //===--------------------------------------------------------------------===// |
| 551 // Abbrev Processing | 551 // Abbrev Processing |
| 552 //===--------------------------------------------------------------------===// | 552 //===--------------------------------------------------------------------===// |
| 553 void ReadAbbrevRecord(); | 553 void ReadAbbrevRecord(); |
| 554 | 554 |
| 555 bool ReadBlockInfoBlock(); | 555 bool ReadBlockInfoBlock(); |
| 556 }; | 556 }; |
| 557 | 557 |
| 558 } // End llvm namespace | 558 } // End llvm namespace |
| 559 | 559 |
| 560 #endif | 560 #endif |
| OLD | NEW |