Chromium Code Reviews| 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 // |
| 11 // This header defines the BitstreamReader class. This class can be used to | 11 // This header defines the BitstreamReader class. This class can be used to |
| 12 // read an arbitrary bitstream, regardless of its contents. | 12 // read an arbitrary bitstream, regardless of its contents. |
| 13 // | 13 // |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 | 15 |
| 16 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H | 16 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H |
| 17 #define LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H | 17 #define LLVM_BITCODE_NACL_NACLBITSTREAMREADER_H |
| 18 | 18 |
| 19 #include "llvm/ADT/SmallVector.h" | 19 #include "llvm/ADT/SmallVector.h" |
| 20 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | |
| 20 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" | 21 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" |
| 21 #include "llvm/Support/Endian.h" | 22 #include "llvm/Support/Endian.h" |
| 22 #include "llvm/Support/StreamingMemoryObject.h" | 23 #include "llvm/Support/StreamingMemoryObject.h" |
| 23 #include <climits> | 24 #include <climits> |
| 24 #include <vector> | 25 #include <vector> |
| 25 | 26 |
| 26 namespace llvm { | 27 namespace llvm { |
| 27 | 28 |
| 28 class Deserializer; | 29 class Deserializer; |
| 29 | 30 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 54 /// the NaClBitstreamCursor class. | 55 /// the NaClBitstreamCursor class. |
| 55 class NaClBitstreamReader { | 56 class NaClBitstreamReader { |
| 56 public: | 57 public: |
| 57 /// This contains information emitted to BLOCKINFO_BLOCK blocks. These | 58 /// This contains information emitted to BLOCKINFO_BLOCK blocks. These |
| 58 /// describe abbreviations that all blocks of the specified ID inherit. | 59 /// describe abbreviations that all blocks of the specified ID inherit. |
| 59 struct BlockInfo { | 60 struct BlockInfo { |
| 60 unsigned BlockID; | 61 unsigned BlockID; |
| 61 std::vector<NaClBitCodeAbbrev*> Abbrevs; | 62 std::vector<NaClBitCodeAbbrev*> Abbrevs; |
| 62 }; | 63 }; |
| 63 private: | 64 private: |
| 65 friend class NaClBitstreamCursor; | |
| 66 | |
| 64 std::unique_ptr<MemoryObject> BitcodeBytes; | 67 std::unique_ptr<MemoryObject> BitcodeBytes; |
| 65 | 68 |
| 66 std::vector<BlockInfo> BlockInfoRecords; | 69 std::vector<BlockInfo> BlockInfoRecords; |
| 67 | 70 |
| 68 /// \brief Holds the offset of the first byte after the header. | 71 /// \brief Holds the offset of the first byte after the header. |
| 69 size_t InitialAddress; | 72 size_t InitialAddress; |
| 70 | 73 |
| 74 // True if filler should be added to byte align records. | |
| 75 bool AlignBitcodeRecords = false; | |
| 76 | |
| 71 NaClBitstreamReader(const NaClBitstreamReader&) LLVM_DELETED_FUNCTION; | 77 NaClBitstreamReader(const NaClBitstreamReader&) LLVM_DELETED_FUNCTION; |
| 72 void operator=(const NaClBitstreamReader&) LLVM_DELETED_FUNCTION; | 78 void operator=(const NaClBitstreamReader&) LLVM_DELETED_FUNCTION; |
| 79 | |
| 80 void initFromHeader(const NaClBitcodeHeader &Header) { | |
| 81 AlignBitcodeRecords = Header.getAlignBitcodeRecords(); | |
| 82 } | |
| 83 | |
| 73 public: | 84 public: |
| 74 NaClBitstreamReader() : InitialAddress(0) {} | 85 NaClBitstreamReader() : InitialAddress(0) {} |
| 75 | 86 |
| 76 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, | 87 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, |
| 77 size_t MyInitialAddress=0) { | 88 size_t MyInitialAddress=0) { |
| 78 InitialAddress = MyInitialAddress; | 89 InitialAddress = MyInitialAddress; |
|
jvoung (off chromium)
2015/05/07 18:11:17
This variant no longer calls init(Start, End) so B
Karl
2015/05/07 22:18:54
Bad refactoring on my part. Fixing to have both fo
| |
| 79 init(Start, End); | 90 } |
| 91 | |
| 92 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, | |
| 93 NaClBitcodeHeader &Header) { | |
|
jvoung (off chromium)
2015/05/07 18:11:17
Not a huge fan of having some many variants of the
Karl
2015/05/07 22:18:54
There are two reasons for multiple headers. The fi
jvoung (off chromium)
2015/05/08 17:55:01
Yeah it is definitely annoying to have to either s
Karl
2015/05/08 21:08:59
Looks reasonable, changing.
| |
| 94 InitialAddress = Header.getHeaderSize(); | |
| 95 init(Start, End, Header); | |
| 80 } | 96 } |
| 81 | 97 |
| 82 NaClBitstreamReader(MemoryObject *Bytes, size_t MyInitialAddress=0) | 98 NaClBitstreamReader(MemoryObject *Bytes, size_t MyInitialAddress=0) |
| 83 : InitialAddress(MyInitialAddress) { | 99 : InitialAddress(MyInitialAddress) { |
| 84 BitcodeBytes.reset(Bytes); | 100 BitcodeBytes.reset(Bytes); |
| 85 } | 101 } |
| 86 | 102 |
| 87 void init(const unsigned char *Start, const unsigned char *End) { | 103 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header) |
| 104 : InitialAddress(Header.getHeaderSize()) { | |
| 105 BitcodeBytes.reset(Bytes); | |
| 106 initFromHeader(Header); | |
| 107 } | |
| 108 | |
| 109 void init(const unsigned char *Start, const unsigned char *End, | |
| 110 NaClBitcodeHeader &Header) { | |
| 88 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes"); | 111 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes"); |
| 89 BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End)); | 112 BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End)); |
| 113 initFromHeader(Header); | |
| 90 } | 114 } |
| 91 | 115 |
| 92 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } | 116 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } |
| 93 | 117 |
| 94 ~NaClBitstreamReader() { | 118 ~NaClBitstreamReader() { |
| 95 // Free the BlockInfoRecords. | 119 // Free the BlockInfoRecords. |
| 96 while (!BlockInfoRecords.empty()) { | 120 while (!BlockInfoRecords.empty()) { |
| 97 BlockInfo &Info = BlockInfoRecords.back(); | 121 BlockInfo &Info = BlockInfoRecords.back(); |
| 98 // Free blockinfo abbrev info. | 122 // Free blockinfo abbrev info. |
| 99 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); | 123 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 | 533 |
| 510 if ((Piece & (1U << (NumBits-1))) == 0) | 534 if ((Piece & (1U << (NumBits-1))) == 0) |
| 511 return Result; | 535 return Result; |
| 512 | 536 |
| 513 NextBit += NumBits-1; | 537 NextBit += NumBits-1; |
| 514 Piece = Read(NumBits); | 538 Piece = Read(NumBits); |
| 515 } | 539 } |
| 516 } | 540 } |
| 517 | 541 |
| 518 private: | 542 private: |
| 543 void SkipToByteBoundary() { | |
| 544 unsigned BitsToSkip = BitsInCurWord % CHAR_BIT; | |
| 545 if (BitsToSkip) { | |
| 546 CurWord >>= BitsToSkip; | |
| 547 BitsInCurWord -= BitsToSkip; | |
| 548 } | |
| 549 } | |
| 550 | |
| 551 void SkipToByteBoundaryIfAligned() { | |
| 552 if (BitStream->AlignBitcodeRecords) | |
| 553 SkipToByteBoundary(); | |
| 554 } | |
| 555 | |
| 519 void SkipToFourByteBoundary() { | 556 void SkipToFourByteBoundary() { |
| 520 // If word_t is 64-bits and if we've read less than 32 bits, just dump | 557 // If word_t is 64-bits and if we've read less than 32 bits, just dump |
| 521 // the bits we have up to the next 32-bit boundary. | 558 // the bits we have up to the next 32-bit boundary. |
| 522 if (sizeof(word_t) > 4 && | 559 if (sizeof(word_t) > 4 && |
| 523 BitsInCurWord >= 32) { | 560 BitsInCurWord >= 32) { |
| 524 CurWord >>= BitsInCurWord-32; | 561 CurWord >>= BitsInCurWord-32; |
| 525 BitsInCurWord = 32; | 562 BitsInCurWord = 32; |
| 526 return; | 563 return; |
| 527 } | 564 } |
| 528 | 565 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, | 688 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, |
| 652 // except that no abbreviation is built. | 689 // except that no abbreviation is built. |
| 653 void SkipAbbrevRecord(); | 690 void SkipAbbrevRecord(); |
| 654 | 691 |
| 655 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); | 692 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); |
| 656 }; | 693 }; |
| 657 | 694 |
| 658 } // End llvm namespace | 695 } // End llvm namespace |
| 659 | 696 |
| 660 #endif | 697 #endif |
| OLD | NEW |