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"
23 class BitstreamReader {
24 const unsigned char *NextChar;
25 const unsigned char *LastChar;
27 /// CurWord - This is the current data we have pulled from the stream but have
28 /// not returned to the client.
31 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
32 /// is always from [0...31] inclusive.
33 unsigned BitsInCurWord;
35 // CurCodeSize - This is the declared size of code values used for the current
39 /// CurAbbrevs - Abbrevs installed at in this block.
40 std::vector<BitCodeAbbrev*> CurAbbrevs;
43 unsigned PrevCodeSize;
44 std::vector<BitCodeAbbrev*> PrevAbbrevs;
45 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
48 /// BlockScope - This tracks the codesize of parent blocks.
49 SmallVector<Block, 8> BlockScope;
51 /// FirstChar - This remembers the first byte of the stream.
52 const unsigned char *FirstChar;
54 BitstreamReader(const unsigned char *Start, const unsigned char *End)
55 : NextChar(Start), LastChar(End), FirstChar(Start) {
56 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
63 // Abbrevs could still exist if the stream was broken. If so, don't leak
65 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
68 for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
69 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
70 for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
75 bool AtEndOfStream() const { return NextChar == LastChar; }
77 /// GetCurrentBitNo - Return the bit # of the bit we are reading.
78 uint64_t GetCurrentBitNo() const {
79 return (NextChar-FirstChar)*8 + (32-BitsInCurWord);
82 /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
83 unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
85 uint32_t Read(unsigned NumBits) {
86 // If the field is fully contained by CurWord, return it quickly.
87 if (BitsInCurWord >= NumBits) {
88 uint32_t R = CurWord & ((1U << NumBits)-1);
90 BitsInCurWord -= NumBits;
94 // If we run out of data, stop at the end of the stream.
95 if (LastChar == NextChar) {
101 unsigned R = CurWord;
103 // Read the next word from the stream.
104 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
105 (NextChar[2] << 16) | (NextChar[3] << 24);
108 // Extract NumBits-BitsInCurWord from what we just read.
109 unsigned BitsLeft = NumBits-BitsInCurWord;
111 // Be careful here, BitsLeft is in the range [1..32] inclusive.
112 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
114 // BitsLeft bits have just been used up from CurWord.
116 CurWord >>= BitsLeft;
119 BitsInCurWord = 32-BitsLeft;
123 uint64_t Read64(unsigned NumBits) {
124 if (NumBits <= 32) return Read(NumBits);
126 uint64_t V = Read(32);
127 return V | (uint64_t)Read(NumBits-32) << 32;
130 uint32_t ReadVBR(unsigned NumBits) {
131 uint32_t Piece = Read(NumBits);
132 if ((Piece & (1U << (NumBits-1))) == 0)
136 unsigned NextBit = 0;
138 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
140 if ((Piece & (1U << (NumBits-1))) == 0)
143 NextBit += NumBits-1;
144 Piece = Read(NumBits);
148 uint64_t ReadVBR64(unsigned NumBits) {
149 uint64_t Piece = Read(NumBits);
150 if ((Piece & (1U << (NumBits-1))) == 0)
154 unsigned NextBit = 0;
156 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
158 if ((Piece & (1U << (NumBits-1))) == 0)
161 NextBit += NumBits-1;
162 Piece = Read(NumBits);
172 unsigned ReadCode() {
173 return Read(CurCodeSize);
176 //===--------------------------------------------------------------------===//
177 // Block Manipulation
178 //===--------------------------------------------------------------------===//
181 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
183 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
185 unsigned ReadSubBlockID() {
186 return ReadVBR(bitc::BlockIDWidth);
189 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
190 /// over the body of this block. If the block record is malformed, return
193 // Read and ignore the codelen value. Since we are skipping this block, we
194 // don't care what code widths are used inside of it.
195 ReadVBR(bitc::CodeLenWidth);
197 unsigned NumWords = Read(bitc::BlockSizeWidth);
199 // Check that the block wasn't partially defined, and that the offset isn't
201 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
204 NextChar += NumWords*4;
208 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
209 /// the block, returning the BlockID of the block we just entered.
210 bool EnterSubBlock(unsigned *NumWordsP = 0) {
211 BlockScope.push_back(Block(CurCodeSize));
212 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
214 // Get the codesize of this block.
215 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
217 unsigned NumWords = Read(bitc::BlockSizeWidth);
218 if (NumWordsP) *NumWordsP = NumWords;
220 // Validate that this block is sane.
221 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
227 bool ReadBlockEnd() {
228 if (BlockScope.empty()) return true;
231 // [END_BLOCK, <align4bytes>]
233 CurCodeSize = BlockScope.back().PrevCodeSize;
235 // Delete abbrevs from popped scope.
236 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
237 delete CurAbbrevs[i];
239 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
240 BlockScope.pop_back();
244 //===--------------------------------------------------------------------===//
246 //===--------------------------------------------------------------------===//
248 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
249 if (AbbrevID == bitc::UNABBREV_RECORD) {
250 unsigned Code = ReadVBR(6);
251 unsigned NumElts = ReadVBR(6);
252 for (unsigned i = 0; i != NumElts; ++i)
253 Vals.push_back(ReadVBR64(6));
257 unsigned AbbrevNo = AbbrevID-bitc::FIRST_ABBREV;
258 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
259 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
261 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
262 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
263 if (Op.isLiteral()) {
264 // If the abbrev specifies the literal value to use, use it.
265 Vals.push_back(Op.getLiteralValue());
267 // Decode the value as we are commanded.
268 switch (Op.getEncoding()) {
269 default: assert(0 && "Unknown encoding!");
270 case BitCodeAbbrevOp::FixedWidth:
271 Vals.push_back(Read(Op.getEncodingData()));
273 case BitCodeAbbrevOp::VBR:
274 Vals.push_back(ReadVBR64(Op.getEncodingData()));
280 unsigned Code = Vals[0];
281 Vals.erase(Vals.begin());
285 //===--------------------------------------------------------------------===//
287 //===--------------------------------------------------------------------===//
289 void ReadAbbrevRecord() {
290 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
291 unsigned NumOpInfo = ReadVBR(5);
292 for (unsigned i = 0; i != NumOpInfo; ++i) {
293 bool IsLiteral = Read(1);
295 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
299 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
300 if (BitCodeAbbrevOp::hasEncodingData(E)) {
301 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
303 assert(0 && "unimp");
306 CurAbbrevs.push_back(Abbv);
310 } // End llvm namespace