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"
19 #include "llvm/ADT/SmallVector.h"
24 class BitstreamReader {
25 const unsigned char *NextChar;
26 const unsigned char *LastChar;
28 /// CurWord - This is the current data we have pulled from the stream but have
29 /// not returned to the client.
32 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
33 /// is always from [0...31] inclusive.
34 unsigned BitsInCurWord;
36 // CurCodeSize - This is the declared size of code values used for the current
40 /// BlockScope - This tracks the codesize of parent blocks.
41 SmallVector<unsigned, 8> BlockScope;
44 BitstreamReader(const unsigned char *Start, const unsigned char *End)
45 : NextChar(Start), LastChar(End) {
46 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
52 bool AtEndOfStream() const { return NextChar == LastChar; }
54 uint32_t Read(unsigned NumBits) {
55 // If the field is fully contained by CurWord, return it quickly.
56 if (BitsInCurWord >= NumBits) {
57 uint32_t R = CurWord & ((1U << NumBits)-1);
59 BitsInCurWord -= NumBits;
63 // If we run out of data, stop at the end of the stream.
64 if (LastChar == NextChar) {
72 // Read the next word from the stream.
73 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
74 (NextChar[2] << 16) | (NextChar[3] << 24);
77 // Extract NumBits-BitsInCurWord from what we just read.
78 unsigned BitsLeft = NumBits-BitsInCurWord;
80 // Be careful here, BitsLeft is in the range [1..32] inclusive.
81 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
83 // BitsLeft bits have just been used up from CurWord.
88 BitsInCurWord = 32-BitsLeft;
92 uint32_t ReadVBR(unsigned NumBits) {
93 uint32_t Piece = Read(NumBits);
94 if ((Piece & (1U << NumBits-1)) == 0)
100 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
102 if ((Piece & (1U << NumBits-1)) == 0)
105 NextBit += NumBits-1;
106 Piece = Read(NumBits);
110 uint64_t ReadVBR64(unsigned NumBits) {
111 uint64_t Piece = Read(NumBits);
112 if ((Piece & (1U << NumBits-1)) == 0)
116 unsigned NextBit = 0;
118 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
120 if ((Piece & (1U << NumBits-1)) == 0)
123 NextBit += NumBits-1;
124 Piece = Read(NumBits);
134 unsigned ReadCode() {
135 return Read(CurCodeSize);
138 //===--------------------------------------------------------------------===//
139 // Block Manipulation
140 //===--------------------------------------------------------------------===//
143 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
145 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
147 unsigned ReadSubBlockID() {
148 return ReadVBR(bitc::BlockIDWidth);
151 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
152 /// over the body of this block. If the block record is malformed, return
155 // Read and ignore the codelen value. Since we are skipping this block, we
156 // don't care what code widths are used inside of it.
157 ReadVBR(bitc::CodeLenWidth);
159 unsigned NumWords = Read(bitc::BlockSizeWidth);
161 // Check that the block wasn't partially defined, and that the offset isn't
163 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
166 NextChar += NumWords*4;
170 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
171 /// the block, returning the BlockID of the block we just entered.
172 bool EnterSubBlock() {
173 BlockScope.push_back(CurCodeSize);
175 // Get the codesize of this block.
176 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
178 unsigned NumWords = Read(bitc::BlockSizeWidth);
180 // Validate that this block is sane.
181 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
187 bool ReadBlockEnd() {
188 if (BlockScope.empty()) return true;
191 // [END_BLOCK, <align4bytes>]
193 CurCodeSize = BlockScope.back();
194 BlockScope.pop_back();
198 //===--------------------------------------------------------------------===//
200 //===--------------------------------------------------------------------===//
202 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
203 if (AbbrevID == bitc::UNABBREV_RECORD) {
204 unsigned Code = ReadVBR(6);
205 unsigned NumElts = ReadVBR(6);
206 for (unsigned i = 0; i != NumElts; ++i)
207 Vals.push_back(ReadVBR64(6));
211 assert(0 && "Reading with abbrevs not implemented!");
216 } // End llvm namespace