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