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 { | |
jvoung (off chromium)
2015/05/11 17:51:39
nit: seems like the other cases put the { on the p
Karl
2015/05/11 19:41:09
Done.
| |
98 initFromHeader(Header); | |
90 } | 99 } |
91 | 100 |
101 /// Read stream from bytes, starting at the given initial address. | |
102 /// Provides simple API for unit testing. | |
103 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress) | |
104 : BitcodeBytes(Bytes), InitialAddress(InitialAddress) { | |
105 } | |
106 | |
107 // Read stream from Bytes, starting at the given initial address | |
jvoung (off chromium)
2015/05/11 17:51:39
ctor-like comment for non-ctor?
Karl
2015/05/11 19:41:09
Done.
| |
92 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } | 108 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } |
93 | 109 |
94 ~NaClBitstreamReader() { | 110 ~NaClBitstreamReader() { |
95 // Free the BlockInfoRecords. | 111 // Free the BlockInfoRecords. |
96 while (!BlockInfoRecords.empty()) { | 112 while (!BlockInfoRecords.empty()) { |
97 BlockInfo &Info = BlockInfoRecords.back(); | 113 BlockInfo &Info = BlockInfoRecords.back(); |
98 // Free blockinfo abbrev info. | 114 // Free blockinfo abbrev info. |
99 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); | 115 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); |
100 i != e; ++i) | 116 i != e; ++i) |
101 Info.Abbrevs[i]->dropRef(); | 117 Info.Abbrevs[i]->dropRef(); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
509 | 525 |
510 if ((Piece & (1U << (NumBits-1))) == 0) | 526 if ((Piece & (1U << (NumBits-1))) == 0) |
511 return Result; | 527 return Result; |
512 | 528 |
513 NextBit += NumBits-1; | 529 NextBit += NumBits-1; |
514 Piece = Read(NumBits); | 530 Piece = Read(NumBits); |
515 } | 531 } |
516 } | 532 } |
517 | 533 |
518 private: | 534 private: |
535 void SkipToByteBoundary() { | |
536 unsigned BitsToSkip = BitsInCurWord % CHAR_BIT; | |
537 if (BitsToSkip) { | |
538 CurWord >>= BitsToSkip; | |
539 BitsInCurWord -= BitsToSkip; | |
540 } | |
541 } | |
542 | |
543 void SkipToByteBoundaryIfAligned() { | |
544 if (BitStream->AlignBitcodeRecords) | |
545 SkipToByteBoundary(); | |
546 } | |
547 | |
519 void SkipToFourByteBoundary() { | 548 void SkipToFourByteBoundary() { |
520 // If word_t is 64-bits and if we've read less than 32 bits, just dump | 549 // 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. | 550 // the bits we have up to the next 32-bit boundary. |
522 if (sizeof(word_t) > 4 && | 551 if (sizeof(word_t) > 4 && |
523 BitsInCurWord >= 32) { | 552 BitsInCurWord >= 32) { |
524 CurWord >>= BitsInCurWord-32; | 553 CurWord >>= BitsInCurWord-32; |
525 BitsInCurWord = 32; | 554 BitsInCurWord = 32; |
526 return; | 555 return; |
527 } | 556 } |
528 | 557 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
651 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, | 680 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, |
652 // except that no abbreviation is built. | 681 // except that no abbreviation is built. |
653 void SkipAbbrevRecord(); | 682 void SkipAbbrevRecord(); |
654 | 683 |
655 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); | 684 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); |
656 }; | 685 }; |
657 | 686 |
658 } // End llvm namespace | 687 } // End llvm namespace |
659 | 688 |
660 #endif | 689 #endif |
OLD | NEW |