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 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
52 /// These describe abbreviations that all blocks of the specified ID inherit.
55 std::vector<BitCodeAbbrev*> Abbrevs;
57 std::vector<BlockInfo> BlockInfoRecords;
59 /// FirstChar - This remembers the first byte of the stream.
60 const unsigned char *FirstChar;
63 NextChar = FirstChar = LastChar = 0;
69 BitstreamReader(const unsigned char *Start, const unsigned char *End) {
73 void init(const unsigned char *Start, const unsigned char *End) {
74 NextChar = FirstChar = Start;
76 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
83 // Abbrevs could still exist if the stream was broken. If so, don't leak
85 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
86 CurAbbrevs[i]->dropRef();
88 for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
89 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
90 for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
91 Abbrevs[i]->dropRef();
94 // Free the BlockInfoRecords.
95 while (!BlockInfoRecords.empty()) {
96 BlockInfo &Info = BlockInfoRecords.back();
97 // Free blockinfo abbrev info.
98 for (unsigned i = 0, e = Info.Abbrevs.size(); i != e; ++i)
99 Info.Abbrevs[i]->dropRef();
100 BlockInfoRecords.pop_back();
104 bool AtEndOfStream() const { return NextChar == LastChar; }
106 /// GetCurrentBitNo - Return the bit # of the bit we are reading.
107 uint64_t GetCurrentBitNo() const {
108 return (NextChar-FirstChar)*8 + ((32-BitsInCurWord) & 31);
111 /// JumpToBit - Reset the stream to the specified bit number.
112 void JumpToBit(uint64_t BitNo) {
113 unsigned ByteNo = unsigned(BitNo/8) & ~3;
114 unsigned WordBitNo = unsigned(BitNo) & 31;
115 assert(ByteNo < (unsigned)(LastChar-FirstChar) && "Invalid location");
117 // Move the cursor to the right word.
118 NextChar = FirstChar+ByteNo;
121 // Skip over any bits that are already consumed.
128 /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
129 unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
131 uint32_t Read(unsigned NumBits) {
132 // If the field is fully contained by CurWord, return it quickly.
133 if (BitsInCurWord >= NumBits) {
134 uint32_t R = CurWord & ((1U << NumBits)-1);
136 BitsInCurWord -= NumBits;
140 // If we run out of data, stop at the end of the stream.
141 if (LastChar == NextChar) {
147 unsigned R = CurWord;
149 // Read the next word from the stream.
150 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
151 (NextChar[2] << 16) | (NextChar[3] << 24);
154 // Extract NumBits-BitsInCurWord from what we just read.
155 unsigned BitsLeft = NumBits-BitsInCurWord;
157 // Be careful here, BitsLeft is in the range [1..32] inclusive.
158 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
160 // BitsLeft bits have just been used up from CurWord.
162 CurWord >>= BitsLeft;
165 BitsInCurWord = 32-BitsLeft;
169 uint64_t Read64(unsigned NumBits) {
170 if (NumBits <= 32) return Read(NumBits);
172 uint64_t V = Read(32);
173 return V | (uint64_t)Read(NumBits-32) << 32;
176 uint32_t ReadVBR(unsigned NumBits) {
177 uint32_t Piece = Read(NumBits);
178 if ((Piece & (1U << (NumBits-1))) == 0)
182 unsigned NextBit = 0;
184 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
186 if ((Piece & (1U << (NumBits-1))) == 0)
189 NextBit += NumBits-1;
190 Piece = Read(NumBits);
194 uint64_t ReadVBR64(unsigned NumBits) {
195 uint64_t Piece = Read(NumBits);
196 if ((Piece & (1U << (NumBits-1))) == 0)
200 unsigned NextBit = 0;
202 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
204 if ((Piece & (1U << (NumBits-1))) == 0)
207 NextBit += NumBits-1;
208 Piece = Read(NumBits);
218 unsigned ReadCode() {
219 return Read(CurCodeSize);
222 //===--------------------------------------------------------------------===//
223 // Block Manipulation
224 //===--------------------------------------------------------------------===//
227 /// getBlockInfo - If there is block info for the specified ID, return it,
228 /// otherwise return null.
229 BlockInfo *getBlockInfo(unsigned BlockID) {
230 // Common case, the most recent entry matches BlockID.
231 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
232 return &BlockInfoRecords.back();
234 for (unsigned i = 0, e = BlockInfoRecords.size(); i != e; ++i)
235 if (BlockInfoRecords[i].BlockID == BlockID)
236 return &BlockInfoRecords[i];
243 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
245 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
247 unsigned ReadSubBlockID() {
248 return ReadVBR(bitc::BlockIDWidth);
251 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
252 /// over the body of this block. If the block record is malformed, return
255 // Read and ignore the codelen value. Since we are skipping this block, we
256 // don't care what code widths are used inside of it.
257 ReadVBR(bitc::CodeLenWidth);
259 unsigned NumWords = Read(bitc::BlockSizeWidth);
261 // Check that the block wasn't partially defined, and that the offset isn't
263 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
266 NextChar += NumWords*4;
270 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
271 /// the block, returning the BlockID of the block we just entered.
272 bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = 0) {
273 // Save the current block's state on BlockScope.
274 BlockScope.push_back(Block(CurCodeSize));
275 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
277 // Add the abbrevs specific to this block to the CurAbbrevs list.
278 if (BlockInfo *Info = getBlockInfo(BlockID)) {
279 for (unsigned i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
280 CurAbbrevs.push_back(Info->Abbrevs[i]);
281 CurAbbrevs.back()->addRef();
285 // Get the codesize of this block.
286 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
288 unsigned NumWords = Read(bitc::BlockSizeWidth);
289 if (NumWordsP) *NumWordsP = NumWords;
291 // Validate that this block is sane.
292 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
298 bool ReadBlockEnd() {
299 if (BlockScope.empty()) return true;
302 // [END_BLOCK, <align4bytes>]
304 CurCodeSize = BlockScope.back().PrevCodeSize;
306 // Delete abbrevs from popped scope.
307 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
308 CurAbbrevs[i]->dropRef();
310 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
311 BlockScope.pop_back();
315 //===--------------------------------------------------------------------===//
317 //===--------------------------------------------------------------------===//
320 void ReadAbbreviatedField(const BitCodeAbbrevOp &Op,
321 SmallVectorImpl<uint64_t> &Vals) {
322 if (Op.isLiteral()) {
323 // If the abbrev specifies the literal value to use, use it.
324 Vals.push_back(Op.getLiteralValue());
326 // Decode the value as we are commanded.
327 switch (Op.getEncoding()) {
328 default: assert(0 && "Unknown encoding!");
329 case BitCodeAbbrevOp::Fixed:
330 Vals.push_back(Read((unsigned)Op.getEncodingData()));
332 case BitCodeAbbrevOp::VBR:
333 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
335 case BitCodeAbbrevOp::Char6:
336 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
342 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
343 if (AbbrevID == bitc::UNABBREV_RECORD) {
344 unsigned Code = ReadVBR(6);
345 unsigned NumElts = ReadVBR(6);
346 for (unsigned i = 0; i != NumElts; ++i)
347 Vals.push_back(ReadVBR64(6));
351 unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
352 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
353 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
355 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
356 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
357 if (Op.isLiteral() || Op.getEncoding() != BitCodeAbbrevOp::Array) {
358 ReadAbbreviatedField(Op, Vals);
360 // Array case. Read the number of elements as a vbr6.
361 unsigned NumElts = ReadVBR(6);
363 // Get the element encoding.
364 assert(i+2 == e && "array op not second to last?");
365 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
367 // Read all the elements.
368 for (; NumElts; --NumElts)
369 ReadAbbreviatedField(EltEnc, Vals);
373 unsigned Code = (unsigned)Vals[0];
374 Vals.erase(Vals.begin());
378 //===--------------------------------------------------------------------===//
380 //===--------------------------------------------------------------------===//
382 void ReadAbbrevRecord() {
383 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
384 unsigned NumOpInfo = ReadVBR(5);
385 for (unsigned i = 0; i != NumOpInfo; ++i) {
386 bool IsLiteral = Read(1);
388 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
392 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
393 if (BitCodeAbbrevOp::hasEncodingData(E))
394 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
396 Abbv->Add(BitCodeAbbrevOp(E));
398 CurAbbrevs.push_back(Abbv);
401 //===--------------------------------------------------------------------===//
402 // BlockInfo Block Reading
403 //===--------------------------------------------------------------------===//
406 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
407 if (BlockInfo *BI = getBlockInfo(BlockID))
410 // Otherwise, add a new record.
411 BlockInfoRecords.push_back(BlockInfo());
412 BlockInfoRecords.back().BlockID = BlockID;
413 return BlockInfoRecords.back();
418 bool ReadBlockInfoBlock() {
419 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
421 SmallVector<uint64_t, 64> Record;
422 BlockInfo *CurBlockInfo = 0;
424 // Read all the records for this module.
426 unsigned Code = ReadCode();
427 if (Code == bitc::END_BLOCK)
428 return ReadBlockEnd();
429 if (Code == bitc::ENTER_SUBBLOCK) {
431 if (SkipBlock()) return true;
435 // Read abbrev records, associate them with CurBID.
436 if (Code == bitc::DEFINE_ABBREV) {
437 if (!CurBlockInfo) return true;
440 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
441 // appropriate BlockInfo.
442 BitCodeAbbrev *Abbv = CurAbbrevs.back();
443 CurAbbrevs.pop_back();
444 CurBlockInfo->Abbrevs.push_back(Abbv);
450 switch (ReadRecord(Code, Record)) {
451 default: break; // Default behavior, ignore unknown content.
452 case bitc::BLOCKINFO_CODE_SETBID:
453 if (Record.size() < 1) return true;
454 CurBlockInfo = &getOrCreateBlockInfo((unsigned)Record[0]);
461 } // End llvm namespace