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"
20 #include "llvm/Support/DataTypes.h"
25 class BitstreamReader {
26 const unsigned char *NextChar;
27 const unsigned char *LastChar;
29 /// CurWord - This is the current data we have pulled from the stream but have
30 /// not returned to the client.
33 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
34 /// is always from [0...31] inclusive.
35 unsigned BitsInCurWord;
37 // CurCodeSize - This is the declared size of code values used for the current
41 /// BlockScope - This tracks the codesize of parent blocks.
42 SmallVector<unsigned, 8> BlockScope;
45 BitstreamReader(const unsigned char *Start, const unsigned char *End)
46 : NextChar(Start), LastChar(End) {
47 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
53 bool AtEndOfStream() const { return NextChar == LastChar; }
55 uint32_t Read(unsigned NumBits) {
56 // If the field is fully contained by CurWord, return it quickly.
57 if (BitsInCurWord >= NumBits) {
58 uint32_t R = CurWord & ((1U << NumBits)-1);
60 BitsInCurWord -= NumBits;
64 // If we run out of data, stop at the end of the stream.
65 if (LastChar == NextChar) {
73 // Read the next word from the stream.
74 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
75 (NextChar[2] << 16) | (NextChar[3] << 24);
78 // Extract NumBits-BitsInCurWord from what we just read.
79 unsigned BitsLeft = NumBits-BitsInCurWord;
81 // Be careful here, BitsLeft is in the range [1..32] inclusive.
82 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
84 // BitsLeft bits have just been used up from CurWord.
89 BitsInCurWord = 32-BitsLeft;
93 uint32_t ReadVBR(unsigned NumBits) {
94 uint32_t Piece = Read(NumBits);
95 if ((Piece & (1U << (NumBits-1))) == 0)
101 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
103 if ((Piece & (1U << (NumBits-1))) == 0)
106 NextBit += NumBits-1;
107 Piece = Read(NumBits);
111 uint64_t ReadVBR64(unsigned NumBits) {
112 uint64_t Piece = Read(NumBits);
113 if ((Piece & (1U << (NumBits-1))) == 0)
117 unsigned NextBit = 0;
119 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
121 if ((Piece & (1U << (NumBits-1))) == 0)
124 NextBit += NumBits-1;
125 Piece = Read(NumBits);
135 unsigned ReadCode() {
136 return Read(CurCodeSize);
139 //===--------------------------------------------------------------------===//
140 // Block Manipulation
141 //===--------------------------------------------------------------------===//
144 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
146 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
148 unsigned ReadSubBlockID() {
149 return ReadVBR(bitc::BlockIDWidth);
152 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
153 /// over the body of this block. If the block record is malformed, return
156 // Read and ignore the codelen value. Since we are skipping this block, we
157 // don't care what code widths are used inside of it.
158 ReadVBR(bitc::CodeLenWidth);
160 unsigned NumWords = Read(bitc::BlockSizeWidth);
162 // Check that the block wasn't partially defined, and that the offset isn't
164 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
167 NextChar += NumWords*4;
171 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
172 /// the block, returning the BlockID of the block we just entered.
173 bool EnterSubBlock() {
174 BlockScope.push_back(CurCodeSize);
176 // Get the codesize of this block.
177 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
179 unsigned NumWords = Read(bitc::BlockSizeWidth);
181 // Validate that this block is sane.
182 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
188 bool ReadBlockEnd() {
189 if (BlockScope.empty()) return true;
192 // [END_BLOCK, <align4bytes>]
194 CurCodeSize = BlockScope.back();
195 BlockScope.pop_back();
199 //===--------------------------------------------------------------------===//
201 //===--------------------------------------------------------------------===//
203 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
204 if (AbbrevID == bitc::UNABBREV_RECORD) {
205 unsigned Code = ReadVBR(6);
206 unsigned NumElts = ReadVBR(6);
207 for (unsigned i = 0; i != NumElts; ++i)
208 Vals.push_back(ReadVBR64(6));
212 assert(0 && "Reading with abbrevs not implemented!");
218 } // End llvm namespace