Added hook hasReservedCallFrame(). It returns true if the call frame is
[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 <vector>
20
21 namespace llvm {
22   
23 class BitstreamReader {
24   const unsigned char *NextChar;
25   const unsigned char *LastChar;
26   
27   /// CurWord - This is the current data we have pulled from the stream but have
28   /// not returned to the client.
29   uint32_t CurWord;
30   
31   /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
32   /// is always from [0...31] inclusive.
33   unsigned BitsInCurWord;
34   
35   // CurCodeSize - This is the declared size of code values used for the current
36   // block, in bits.
37   unsigned CurCodeSize;
38
39   /// CurAbbrevs - Abbrevs installed at in this block.
40   std::vector<BitCodeAbbrev*> CurAbbrevs;
41   
42   struct Block {
43     unsigned PrevCodeSize;
44     std::vector<BitCodeAbbrev*> PrevAbbrevs;
45     explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
46   };
47   
48   /// BlockScope - This tracks the codesize of parent blocks.
49   SmallVector<Block, 8> BlockScope;
50
51   /// FirstChar - This remembers the first byte of the stream.
52   const unsigned char *FirstChar;
53 public:
54   BitstreamReader(const unsigned char *Start, const unsigned char *End)
55     : NextChar(Start), LastChar(End), FirstChar(Start) {
56     assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
57     CurWord = 0;
58     BitsInCurWord = 0;
59     CurCodeSize = 2;
60   }
61   
62   ~BitstreamReader() {
63     // Abbrevs could still exist if the stream was broken.  If so, don't leak
64     // them.
65     for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
66       delete CurAbbrevs[i];
67
68     for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
69       std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
70       for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
71         delete Abbrevs[i];
72     }
73   }
74   
75   bool AtEndOfStream() const { return NextChar == LastChar; }
76   
77   /// GetCurrentBitNo - Return the bit # of the bit we are reading.
78   uint64_t GetCurrentBitNo() const {
79     return (NextChar-FirstChar)*8 + (32-BitsInCurWord);
80   }
81   
82   /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
83   unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
84   
85   uint32_t Read(unsigned NumBits) {
86     // If the field is fully contained by CurWord, return it quickly.
87     if (BitsInCurWord >= NumBits) {
88       uint32_t R = CurWord & ((1U << NumBits)-1);
89       CurWord >>= NumBits;
90       BitsInCurWord -= NumBits;
91       return R;
92     }
93
94     // If we run out of data, stop at the end of the stream.
95     if (LastChar == NextChar) {
96       CurWord = 0;
97       BitsInCurWord = 0;
98       return 0;
99     }
100     
101     unsigned R = CurWord;
102
103     // Read the next word from the stream.
104     CurWord = (NextChar[0] <<  0) | (NextChar[1] << 8) |
105               (NextChar[2] << 16) | (NextChar[3] << 24);
106     NextChar += 4;
107     
108     // Extract NumBits-BitsInCurWord from what we just read.
109     unsigned BitsLeft = NumBits-BitsInCurWord;
110     
111     // Be careful here, BitsLeft is in the range [1..32] inclusive.
112     R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
113     
114     // BitsLeft bits have just been used up from CurWord.
115     if (BitsLeft != 32)
116       CurWord >>= BitsLeft;
117     else
118       CurWord = 0;
119     BitsInCurWord = 32-BitsLeft;
120     return R;
121   }
122   
123   uint64_t Read64(unsigned NumBits) {
124     if (NumBits <= 32) return Read(NumBits);
125     
126     uint64_t V = Read(32);
127     return V | (uint64_t)Read(NumBits-32) << 32;
128   }
129   
130   uint32_t ReadVBR(unsigned NumBits) {
131     uint32_t Piece = Read(NumBits);
132     if ((Piece & (1U << (NumBits-1))) == 0)
133       return Piece;
134
135     uint32_t Result = 0;
136     unsigned NextBit = 0;
137     while (1) {
138       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
139
140       if ((Piece & (1U << (NumBits-1))) == 0)
141         return Result;
142       
143       NextBit += NumBits-1;
144       Piece = Read(NumBits);
145     }
146   }
147   
148   uint64_t ReadVBR64(unsigned NumBits) {
149     uint64_t Piece = Read(NumBits);
150     if ((Piece & (1U << (NumBits-1))) == 0)
151       return Piece;
152     
153     uint64_t Result = 0;
154     unsigned NextBit = 0;
155     while (1) {
156       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
157       
158       if ((Piece & (1U << (NumBits-1))) == 0)
159         return Result;
160       
161       NextBit += NumBits-1;
162       Piece = Read(NumBits);
163     }
164   }
165
166   void SkipToWord() {
167     BitsInCurWord = 0;
168     CurWord = 0;
169   }
170
171   
172   unsigned ReadCode() {
173     return Read(CurCodeSize);
174   }
175
176   //===--------------------------------------------------------------------===//
177   // Block Manipulation
178   //===--------------------------------------------------------------------===//
179   
180   // Block header:
181   //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
182
183   /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
184   /// the block.
185   unsigned ReadSubBlockID() {
186     return ReadVBR(bitc::BlockIDWidth);
187   }
188   
189   /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
190   /// over the body of this block.  If the block record is malformed, return
191   /// true.
192   bool SkipBlock() {
193     // Read and ignore the codelen value.  Since we are skipping this block, we
194     // don't care what code widths are used inside of it.
195     ReadVBR(bitc::CodeLenWidth);
196     SkipToWord();
197     unsigned NumWords = Read(bitc::BlockSizeWidth);
198     
199     // Check that the block wasn't partially defined, and that the offset isn't
200     // bogus.
201     if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
202       return true;
203     
204     NextChar += NumWords*4;
205     return false;
206   }
207   
208   /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
209   /// the block, returning the BlockID of the block we just entered.
210   bool EnterSubBlock(unsigned *NumWordsP = 0) {
211     BlockScope.push_back(Block(CurCodeSize));
212     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
213     
214     // Get the codesize of this block.
215     CurCodeSize = ReadVBR(bitc::CodeLenWidth);
216     SkipToWord();
217     unsigned NumWords = Read(bitc::BlockSizeWidth);
218     if (NumWordsP) *NumWordsP = NumWords;
219     
220     // Validate that this block is sane.
221     if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
222       return true;
223     
224     return false;
225   }
226   
227   bool ReadBlockEnd() {
228     if (BlockScope.empty()) return true;
229     
230     // Block tail:
231     //    [END_BLOCK, <align4bytes>]
232     SkipToWord();
233     CurCodeSize = BlockScope.back().PrevCodeSize;
234     
235     // Delete abbrevs from popped scope.
236     for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
237       delete CurAbbrevs[i];
238     
239     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
240     BlockScope.pop_back();
241     return false;
242   }
243   
244   //===--------------------------------------------------------------------===//
245   // Record Processing
246   //===--------------------------------------------------------------------===//
247   
248   unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
249     if (AbbrevID == bitc::UNABBREV_RECORD) {
250       unsigned Code = ReadVBR(6);
251       unsigned NumElts = ReadVBR(6);
252       for (unsigned i = 0; i != NumElts; ++i)
253         Vals.push_back(ReadVBR64(6));
254       return Code;
255     }
256     
257     unsigned AbbrevNo = AbbrevID-bitc::FIRST_ABBREV;
258     assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
259     BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
260
261     for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
262       const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
263       if (Op.isLiteral()) {
264         // If the abbrev specifies the literal value to use, use it.
265         Vals.push_back(Op.getLiteralValue());
266       } else {
267         // Decode the value as we are commanded.
268         switch (Op.getEncoding()) {
269         default: assert(0 && "Unknown encoding!");
270         case BitCodeAbbrevOp::FixedWidth:
271           Vals.push_back(Read(Op.getEncodingData()));
272           break;
273         case BitCodeAbbrevOp::VBR:
274           Vals.push_back(ReadVBR64(Op.getEncodingData()));
275           break;
276         }
277       }
278     }
279     
280     unsigned Code = Vals[0];
281     Vals.erase(Vals.begin());
282     return Code;
283   }
284   
285   //===--------------------------------------------------------------------===//
286   // Abbrev Processing
287   //===--------------------------------------------------------------------===//
288   
289   void ReadAbbrevRecord() {
290     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
291     unsigned NumOpInfo = ReadVBR(5);
292     for (unsigned i = 0; i != NumOpInfo; ++i) {
293       bool IsLiteral = Read(1);
294       if (IsLiteral) {
295         Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
296         continue;
297       }
298
299       BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
300       if (BitCodeAbbrevOp::hasEncodingData(E)) {
301         Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
302       } else {
303         assert(0 && "unimp");
304       }
305     }
306     CurAbbrevs.push_back(Abbv);
307   }
308 };
309
310 } // End llvm namespace
311
312 #endif