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; |
73 public: | |
74 NaClBitstreamReader() : InitialAddress(0) {} | |
75 | 79 |
76 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, | 80 void initFromHeader(NaClBitcodeHeader &Header) { |
77 size_t MyInitialAddress=0) { | 81 InitialAddress = Header.getHeaderSize(); |
78 InitialAddress = MyInitialAddress; | 82 AlignBitcodeRecords = Header.getAlignBitcodeRecords(); |
79 init(Start, End); | |
80 } | 83 } |
81 | 84 |
82 NaClBitstreamReader(MemoryObject *Bytes, size_t MyInitialAddress=0) | 85 public: |
83 : InitialAddress(MyInitialAddress) { | 86 /// Read stream from sequence of bytes [Start .. End) after parsing |
84 BitcodeBytes.reset(Bytes); | 87 /// the given bitcode header. |
| 88 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, |
| 89 NaClBitcodeHeader &Header) |
| 90 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)) { |
| 91 initFromHeader(Header); |
85 } | 92 } |
86 | 93 |
87 void init(const unsigned char *Start, const unsigned char *End) { | 94 /// Read stream from Bytes, after parsing the given bitcode header. |
88 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes"); | 95 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header) |
89 BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End)); | 96 : BitcodeBytes(Bytes) { |
| 97 initFromHeader(Header); |
90 } | 98 } |
91 | 99 |
| 100 /// Read stream from bytes, starting at the given initial address. |
| 101 /// Provides simple API for unit testing. |
| 102 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress) |
| 103 : BitcodeBytes(Bytes), InitialAddress(InitialAddress) { |
| 104 } |
| 105 |
| 106 // Returns the memory object that is being read. |
92 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } | 107 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } |
93 | 108 |
94 ~NaClBitstreamReader() { | 109 ~NaClBitstreamReader() { |
95 // Free the BlockInfoRecords. | 110 // Free the BlockInfoRecords. |
96 while (!BlockInfoRecords.empty()) { | 111 while (!BlockInfoRecords.empty()) { |
97 BlockInfo &Info = BlockInfoRecords.back(); | 112 BlockInfo &Info = BlockInfoRecords.back(); |
98 // Free blockinfo abbrev info. | 113 // Free blockinfo abbrev info. |
99 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); | 114 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); |
100 i != e; ++i) | 115 i != e; ++i) |
101 Info.Abbrevs[i]->dropRef(); | 116 Info.Abbrevs[i]->dropRef(); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 | 524 |
510 if ((Piece & (1U << (NumBits-1))) == 0) | 525 if ((Piece & (1U << (NumBits-1))) == 0) |
511 return Result; | 526 return Result; |
512 | 527 |
513 NextBit += NumBits-1; | 528 NextBit += NumBits-1; |
514 Piece = Read(NumBits); | 529 Piece = Read(NumBits); |
515 } | 530 } |
516 } | 531 } |
517 | 532 |
518 private: | 533 private: |
| 534 void SkipToByteBoundary() { |
| 535 unsigned BitsToSkip = BitsInCurWord % CHAR_BIT; |
| 536 if (BitsToSkip) { |
| 537 CurWord >>= BitsToSkip; |
| 538 BitsInCurWord -= BitsToSkip; |
| 539 } |
| 540 } |
| 541 |
| 542 void SkipToByteBoundaryIfAligned() { |
| 543 if (BitStream->AlignBitcodeRecords) |
| 544 SkipToByteBoundary(); |
| 545 } |
| 546 |
519 void SkipToFourByteBoundary() { | 547 void SkipToFourByteBoundary() { |
520 // If word_t is 64-bits and if we've read less than 32 bits, just dump | 548 // 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. | 549 // the bits we have up to the next 32-bit boundary. |
522 if (sizeof(word_t) > 4 && | 550 if (sizeof(word_t) > 4 && |
523 BitsInCurWord >= 32) { | 551 BitsInCurWord >= 32) { |
524 CurWord >>= BitsInCurWord-32; | 552 CurWord >>= BitsInCurWord-32; |
525 BitsInCurWord = 32; | 553 BitsInCurWord = 32; |
526 return; | 554 return; |
527 } | 555 } |
528 | 556 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, | 679 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, |
652 // except that no abbreviation is built. | 680 // except that no abbreviation is built. |
653 void SkipAbbrevRecord(); | 681 void SkipAbbrevRecord(); |
654 | 682 |
655 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); | 683 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); |
656 }; | 684 }; |
657 | 685 |
658 } // End llvm namespace | 686 } // End llvm namespace |
659 | 687 |
660 #endif | 688 #endif |
OLD | NEW |