1 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitstreamReader class. This class can be used to
11 // read an arbitrary bitstream, regardless of its contents.
13 //===----------------------------------------------------------------------===//
15 #ifndef BITSTREAM_READER_H
16 #define BITSTREAM_READER_H
18 #include "llvm/Bitcode/BitCodes.h"
22 class BitstreamReader {
23 const unsigned char *NextChar;
24 const unsigned char *LastChar;
26 /// CurWord - This is the current data we have pulled from the stream but have
27 /// not returned to the client.
30 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
31 /// is always from [0...31] inclusive.
32 unsigned BitsInCurWord;
34 // CurCodeSize - This is the declared size of code values used for the current
38 /// CurAbbrevs - Abbrevs installed at in this block.
39 std::vector<BitCodeAbbrev*> CurAbbrevs;
42 unsigned PrevCodeSize;
43 std::vector<BitCodeAbbrev*> PrevAbbrevs;
44 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
47 /// BlockScope - This tracks the codesize of parent blocks.
48 SmallVector<Block, 8> BlockScope;
51 BitstreamReader(const unsigned char *Start, const unsigned char *End)
52 : NextChar(Start), LastChar(End) {
53 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
60 // Abbrevs could still exist if the stream was broken. If so, don't leak
62 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
65 for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
66 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
67 for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
72 bool AtEndOfStream() const { return NextChar == LastChar; }
74 uint32_t Read(unsigned NumBits) {
75 // If the field is fully contained by CurWord, return it quickly.
76 if (BitsInCurWord >= NumBits) {
77 uint32_t R = CurWord & ((1U << NumBits)-1);
79 BitsInCurWord -= NumBits;
83 // If we run out of data, stop at the end of the stream.
84 if (LastChar == NextChar) {
92 // Read the next word from the stream.
93 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
94 (NextChar[2] << 16) | (NextChar[3] << 24);
97 // Extract NumBits-BitsInCurWord from what we just read.
98 unsigned BitsLeft = NumBits-BitsInCurWord;
100 // Be careful here, BitsLeft is in the range [1..32] inclusive.
101 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
103 // BitsLeft bits have just been used up from CurWord.
105 CurWord >>= BitsLeft;
108 BitsInCurWord = 32-BitsLeft;
112 uint64_t Read64(unsigned NumBits) {
113 if (NumBits <= 32) return Read(NumBits);
115 uint64_t V = Read(32);
116 return V | (uint64_t)Read(NumBits-32) << 32;
119 uint32_t ReadVBR(unsigned NumBits) {
120 uint32_t Piece = Read(NumBits);
121 if ((Piece & (1U << (NumBits-1))) == 0)
125 unsigned NextBit = 0;
127 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
129 if ((Piece & (1U << (NumBits-1))) == 0)
132 NextBit += NumBits-1;
133 Piece = Read(NumBits);
137 uint64_t ReadVBR64(unsigned NumBits) {
138 uint64_t Piece = Read(NumBits);
139 if ((Piece & (1U << (NumBits-1))) == 0)
143 unsigned NextBit = 0;
145 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
147 if ((Piece & (1U << (NumBits-1))) == 0)
150 NextBit += NumBits-1;
151 Piece = Read(NumBits);
161 unsigned ReadCode() {
162 return Read(CurCodeSize);
165 //===--------------------------------------------------------------------===//
166 // Block Manipulation
167 //===--------------------------------------------------------------------===//
170 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
172 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
174 unsigned ReadSubBlockID() {
175 return ReadVBR(bitc::BlockIDWidth);
178 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
179 /// over the body of this block. If the block record is malformed, return
182 // Read and ignore the codelen value. Since we are skipping this block, we
183 // don't care what code widths are used inside of it.
184 ReadVBR(bitc::CodeLenWidth);
186 unsigned NumWords = Read(bitc::BlockSizeWidth);
188 // Check that the block wasn't partially defined, and that the offset isn't
190 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
193 NextChar += NumWords*4;
197 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
198 /// the block, returning the BlockID of the block we just entered.
199 bool EnterSubBlock() {
200 BlockScope.push_back(Block(CurCodeSize));
201 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
203 // Get the codesize of this block.
204 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
206 unsigned NumWords = Read(bitc::BlockSizeWidth);
208 // Validate that this block is sane.
209 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
215 bool ReadBlockEnd() {
216 if (BlockScope.empty()) return true;
219 // [END_BLOCK, <align4bytes>]
221 CurCodeSize = BlockScope.back().PrevCodeSize;
223 // Delete abbrevs from popped scope.
224 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
225 delete CurAbbrevs[i];
227 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
228 BlockScope.pop_back();
232 //===--------------------------------------------------------------------===//
234 //===--------------------------------------------------------------------===//
236 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
237 if (AbbrevID == bitc::UNABBREV_RECORD) {
238 unsigned Code = ReadVBR(6);
239 unsigned NumElts = ReadVBR(6);
240 for (unsigned i = 0; i != NumElts; ++i)
241 Vals.push_back(ReadVBR64(6));
245 unsigned AbbrevNo = AbbrevID-bitc::FIRST_ABBREV;
246 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
247 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
249 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
250 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
251 if (Op.isLiteral()) {
252 // If the abbrev specifies the literal value to use, use it.
253 Vals.push_back(Op.getLiteralValue());
255 // Decode the value as we are commanded.
256 switch (Op.getEncoding()) {
257 default: assert(0 && "Unknown encoding!");
258 case BitCodeAbbrevOp::FixedWidth:
259 Vals.push_back(Read(Op.getEncodingData()));
261 case BitCodeAbbrevOp::VBR:
262 Vals.push_back(ReadVBR64(Op.getEncodingData()));
268 unsigned Code = Vals[0];
269 Vals.erase(Vals.begin());
273 //===--------------------------------------------------------------------===//
275 //===--------------------------------------------------------------------===//
277 void ReadAbbrevRecord() {
278 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
279 unsigned NumOpInfo = ReadVBR(5);
280 for (unsigned i = 0; i != NumOpInfo; ++i) {
281 bool IsLiteral = Read(1);
283 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
287 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
288 if (BitCodeAbbrevOp::hasEncodingData(E)) {
289 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
291 assert(0 && "unimp");
294 CurAbbrevs.push_back(Abbv);
298 } // End llvm namespace