ceae5afbe98c4232421e8757acb9086f4c216982
[oota-llvm.git] / include / llvm / Bitcode / BitstreamReader.h
1 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines the BitstreamReader class.  This class can be used to
11 // read an arbitrary bitstream, regardless of its contents.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef BITSTREAM_READER_H
16 #define BITSTREAM_READER_H
17
18 #include "llvm/Bitcode/BitCodes.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include <cassert>
21
22 namespace llvm {
23   
24 class BitstreamReader {
25   const unsigned char *NextChar;
26   const unsigned char *LastChar;
27   
28   /// CurWord - This is the current data we have pulled from the stream but have
29   /// not returned to the client.
30   uint32_t CurWord;
31   
32   /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
33   /// is always from [0...31] inclusive.
34   unsigned BitsInCurWord;
35   
36   // CurCodeSize - This is the declared size of code values used for the current
37   // block, in bits.
38   unsigned CurCodeSize;
39   
40   /// BlockScope - This tracks the codesize of parent blocks.
41   SmallVector<unsigned, 8> BlockScope;
42   
43 public:
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");
47     CurWord = 0;
48     BitsInCurWord = 0;
49     CurCodeSize = 2;
50   }
51   
52   bool AtEndOfStream() const { return NextChar == LastChar; }
53   
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);
58       CurWord >>= NumBits;
59       BitsInCurWord -= NumBits;
60       return R;
61     }
62
63     // If we run out of data, stop at the end of the stream.
64     if (LastChar == NextChar) {
65       CurWord = 0;
66       BitsInCurWord = 0;
67       return 0;
68     }
69     
70     unsigned R = CurWord;
71
72     // Read the next word from the stream.
73     CurWord = (NextChar[0] <<  0) | (NextChar[1] << 8) |
74               (NextChar[2] << 16) | (NextChar[3] << 24);
75     NextChar += 4;
76     
77     // Extract NumBits-BitsInCurWord from what we just read.
78     unsigned BitsLeft = NumBits-BitsInCurWord;
79     
80     // Be careful here, BitsLeft is in the range [1..32] inclusive.
81     R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
82     
83     // BitsLeft bits have just been used up from CurWord.
84     if (BitsLeft != 32)
85       CurWord >>= BitsLeft;
86     else
87       CurWord = 0;
88     BitsInCurWord = 32-BitsLeft;
89     return R;
90   }
91   
92   uint32_t ReadVBR(unsigned NumBits) {
93     uint32_t Piece = Read(NumBits);
94     if ((Piece & (1U << NumBits-1)) == 0)
95       return Piece;
96
97     uint32_t Result = 0;
98     unsigned NextBit = 0;
99     while (1) {
100       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
101
102       if ((Piece & (1U << NumBits-1)) == 0)
103         return Result;
104       
105       NextBit += NumBits-1;
106       Piece = Read(NumBits);
107     }
108   }
109   
110   uint64_t ReadVBR64(unsigned NumBits) {
111     uint64_t Piece = Read(NumBits);
112     if ((Piece & (1U << NumBits-1)) == 0)
113       return Piece;
114     
115     uint64_t Result = 0;
116     unsigned NextBit = 0;
117     while (1) {
118       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
119       
120       if ((Piece & (1U << NumBits-1)) == 0)
121         return Result;
122       
123       NextBit += NumBits-1;
124       Piece = Read(NumBits);
125     }
126   }
127
128   void SkipToWord() {
129     BitsInCurWord = 0;
130     CurWord = 0;
131   }
132
133   
134   unsigned ReadCode() {
135     return Read(CurCodeSize);
136   }
137
138   //===--------------------------------------------------------------------===//
139   // Block Manipulation
140   //===--------------------------------------------------------------------===//
141   
142   // Block header:
143   //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
144
145   /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
146   /// the block.
147   unsigned ReadSubBlockID() {
148     return ReadVBR(bitc::BlockIDWidth);
149   }
150   
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
153   /// true.
154   bool SkipBlock() {
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);
158     SkipToWord();
159     unsigned NumWords = Read(bitc::BlockSizeWidth);
160     
161     // Check that the block wasn't partially defined, and that the offset isn't
162     // bogus.
163     if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
164       return true;
165     
166     NextChar += NumWords*4;
167     return false;
168   }
169   
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);
174     
175     // Get the codesize of this block.
176     CurCodeSize = ReadVBR(bitc::CodeLenWidth);
177     SkipToWord();
178     unsigned NumWords = Read(bitc::BlockSizeWidth);
179     
180     // Validate that this block is sane.
181     if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
182       return true;
183     
184     return false;
185   }
186   
187   bool ReadBlockEnd() {
188     if (BlockScope.empty()) return true;
189     
190     // Block tail:
191     //    [END_BLOCK, <align4bytes>]
192     SkipToWord();
193     CurCodeSize = BlockScope.back();
194     BlockScope.pop_back();
195     return false;
196   }
197   
198   //===--------------------------------------------------------------------===//
199   // Record Processing
200   //===--------------------------------------------------------------------===//
201   
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));
208       return Code;
209     }
210     
211     assert(0 && "Reading with abbrevs not implemented!");
212   }
213   
214 };
215
216 } // End llvm namespace
217
218 #endif
219
220