1*139f7f9bSDimitry Andric //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2*139f7f9bSDimitry Andric //
3*139f7f9bSDimitry Andric //                     The LLVM Compiler Infrastructure
4*139f7f9bSDimitry Andric //
5*139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6*139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7*139f7f9bSDimitry Andric //
8*139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9*139f7f9bSDimitry Andric 
10*139f7f9bSDimitry Andric #include "llvm/Bitcode/BitstreamReader.h"
11*139f7f9bSDimitry Andric 
12*139f7f9bSDimitry Andric using namespace llvm;
13*139f7f9bSDimitry Andric 
14*139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
15*139f7f9bSDimitry Andric //  BitstreamCursor implementation
16*139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
17*139f7f9bSDimitry Andric 
18*139f7f9bSDimitry Andric void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
19*139f7f9bSDimitry Andric   freeState();
20*139f7f9bSDimitry Andric 
21*139f7f9bSDimitry Andric   BitStream = RHS.BitStream;
22*139f7f9bSDimitry Andric   NextChar = RHS.NextChar;
23*139f7f9bSDimitry Andric   CurWord = RHS.CurWord;
24*139f7f9bSDimitry Andric   BitsInCurWord = RHS.BitsInCurWord;
25*139f7f9bSDimitry Andric   CurCodeSize = RHS.CurCodeSize;
26*139f7f9bSDimitry Andric 
27*139f7f9bSDimitry Andric   // Copy abbreviations, and bump ref counts.
28*139f7f9bSDimitry Andric   CurAbbrevs = RHS.CurAbbrevs;
29*139f7f9bSDimitry Andric   for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
30*139f7f9bSDimitry Andric     CurAbbrevs[i]->addRef();
31*139f7f9bSDimitry Andric 
32*139f7f9bSDimitry Andric   // Copy block scope and bump ref counts.
33*139f7f9bSDimitry Andric   BlockScope = RHS.BlockScope;
34*139f7f9bSDimitry Andric   for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
35*139f7f9bSDimitry Andric     std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
36*139f7f9bSDimitry Andric     for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
37*139f7f9bSDimitry Andric       Abbrevs[i]->addRef();
38*139f7f9bSDimitry Andric   }
39*139f7f9bSDimitry Andric }
40*139f7f9bSDimitry Andric 
41*139f7f9bSDimitry Andric void BitstreamCursor::freeState() {
42*139f7f9bSDimitry Andric   // Free all the Abbrevs.
43*139f7f9bSDimitry Andric   for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
44*139f7f9bSDimitry Andric     CurAbbrevs[i]->dropRef();
45*139f7f9bSDimitry Andric   CurAbbrevs.clear();
46*139f7f9bSDimitry Andric 
47*139f7f9bSDimitry Andric   // Free all the Abbrevs in the block scope.
48*139f7f9bSDimitry Andric   for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
49*139f7f9bSDimitry Andric     std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
50*139f7f9bSDimitry Andric     for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
51*139f7f9bSDimitry Andric       Abbrevs[i]->dropRef();
52*139f7f9bSDimitry Andric   }
53*139f7f9bSDimitry Andric   BlockScope.clear();
54*139f7f9bSDimitry Andric }
55*139f7f9bSDimitry Andric 
56*139f7f9bSDimitry Andric /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
57*139f7f9bSDimitry Andric /// the block, and return true if the block has an error.
58*139f7f9bSDimitry Andric bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
59*139f7f9bSDimitry Andric   // Save the current block's state on BlockScope.
60*139f7f9bSDimitry Andric   BlockScope.push_back(Block(CurCodeSize));
61*139f7f9bSDimitry Andric   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
62*139f7f9bSDimitry Andric 
63*139f7f9bSDimitry Andric   // Add the abbrevs specific to this block to the CurAbbrevs list.
64*139f7f9bSDimitry Andric   if (const BitstreamReader::BlockInfo *Info =
65*139f7f9bSDimitry Andric       BitStream->getBlockInfo(BlockID)) {
66*139f7f9bSDimitry Andric     for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
67*139f7f9bSDimitry Andric       CurAbbrevs.push_back(Info->Abbrevs[i]);
68*139f7f9bSDimitry Andric       CurAbbrevs.back()->addRef();
69*139f7f9bSDimitry Andric     }
70*139f7f9bSDimitry Andric   }
71*139f7f9bSDimitry Andric 
72*139f7f9bSDimitry Andric   // Get the codesize of this block.
73*139f7f9bSDimitry Andric   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
74*139f7f9bSDimitry Andric   SkipToFourByteBoundary();
75*139f7f9bSDimitry Andric   unsigned NumWords = Read(bitc::BlockSizeWidth);
76*139f7f9bSDimitry Andric   if (NumWordsP) *NumWordsP = NumWords;
77*139f7f9bSDimitry Andric 
78*139f7f9bSDimitry Andric   // Validate that this block is sane.
79*139f7f9bSDimitry Andric   if (CurCodeSize == 0 || AtEndOfStream())
80*139f7f9bSDimitry Andric     return true;
81*139f7f9bSDimitry Andric 
82*139f7f9bSDimitry Andric   return false;
83*139f7f9bSDimitry Andric }
84*139f7f9bSDimitry Andric 
85*139f7f9bSDimitry Andric void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
86*139f7f9bSDimitry Andric                                              SmallVectorImpl<uint64_t> &Vals) {
87*139f7f9bSDimitry Andric   assert(Op.isLiteral() && "Not a literal");
88*139f7f9bSDimitry Andric   // If the abbrev specifies the literal value to use, use it.
89*139f7f9bSDimitry Andric   Vals.push_back(Op.getLiteralValue());
90*139f7f9bSDimitry Andric }
91*139f7f9bSDimitry Andric 
92*139f7f9bSDimitry Andric void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
93*139f7f9bSDimitry Andric                                            SmallVectorImpl<uint64_t> &Vals) {
94*139f7f9bSDimitry Andric   assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
95*139f7f9bSDimitry Andric 
96*139f7f9bSDimitry Andric   // Decode the value as we are commanded.
97*139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
98*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
99*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
100*139f7f9bSDimitry Andric     assert(0 && "Should not reach here");
101*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
102*139f7f9bSDimitry Andric     Vals.push_back(Read((unsigned)Op.getEncodingData()));
103*139f7f9bSDimitry Andric     break;
104*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
105*139f7f9bSDimitry Andric     Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
106*139f7f9bSDimitry Andric     break;
107*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
108*139f7f9bSDimitry Andric     Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
109*139f7f9bSDimitry Andric     break;
110*139f7f9bSDimitry Andric   }
111*139f7f9bSDimitry Andric }
112*139f7f9bSDimitry Andric 
113*139f7f9bSDimitry Andric void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
114*139f7f9bSDimitry Andric   assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
115*139f7f9bSDimitry Andric 
116*139f7f9bSDimitry Andric   // Decode the value as we are commanded.
117*139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
118*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
119*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
120*139f7f9bSDimitry Andric     assert(0 && "Should not reach here");
121*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
122*139f7f9bSDimitry Andric     (void)Read((unsigned)Op.getEncodingData());
123*139f7f9bSDimitry Andric     break;
124*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
125*139f7f9bSDimitry Andric     (void)ReadVBR64((unsigned)Op.getEncodingData());
126*139f7f9bSDimitry Andric     break;
127*139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
128*139f7f9bSDimitry Andric     (void)Read(6);
129*139f7f9bSDimitry Andric     break;
130*139f7f9bSDimitry Andric   }
131*139f7f9bSDimitry Andric }
132*139f7f9bSDimitry Andric 
133*139f7f9bSDimitry Andric 
134*139f7f9bSDimitry Andric 
135*139f7f9bSDimitry Andric /// skipRecord - Read the current record and discard it.
136*139f7f9bSDimitry Andric void BitstreamCursor::skipRecord(unsigned AbbrevID) {
137*139f7f9bSDimitry Andric   // Skip unabbreviated records by reading past their entries.
138*139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
139*139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
140*139f7f9bSDimitry Andric     (void)Code;
141*139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
142*139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
143*139f7f9bSDimitry Andric       (void)ReadVBR64(6);
144*139f7f9bSDimitry Andric     return;
145*139f7f9bSDimitry Andric   }
146*139f7f9bSDimitry Andric 
147*139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
148*139f7f9bSDimitry Andric 
149*139f7f9bSDimitry Andric   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
150*139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
151*139f7f9bSDimitry Andric     if (Op.isLiteral())
152*139f7f9bSDimitry Andric       continue;
153*139f7f9bSDimitry Andric 
154*139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
155*139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
156*139f7f9bSDimitry Andric       skipAbbreviatedField(Op);
157*139f7f9bSDimitry Andric       continue;
158*139f7f9bSDimitry Andric     }
159*139f7f9bSDimitry Andric 
160*139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
161*139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
162*139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
163*139f7f9bSDimitry Andric 
164*139f7f9bSDimitry Andric       // Get the element encoding.
165*139f7f9bSDimitry Andric       assert(i+2 == e && "array op not second to last?");
166*139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
167*139f7f9bSDimitry Andric 
168*139f7f9bSDimitry Andric       // Read all the elements.
169*139f7f9bSDimitry Andric       for (; NumElts; --NumElts)
170*139f7f9bSDimitry Andric         skipAbbreviatedField(EltEnc);
171*139f7f9bSDimitry Andric       continue;
172*139f7f9bSDimitry Andric     }
173*139f7f9bSDimitry Andric 
174*139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
175*139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
176*139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
177*139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
178*139f7f9bSDimitry Andric 
179*139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
180*139f7f9bSDimitry Andric     size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
181*139f7f9bSDimitry Andric 
182*139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
183*139f7f9bSDimitry Andric     // record to empty and return.
184*139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
185*139f7f9bSDimitry Andric       NextChar = BitStream->getBitcodeBytes().getExtent();
186*139f7f9bSDimitry Andric       break;
187*139f7f9bSDimitry Andric     }
188*139f7f9bSDimitry Andric 
189*139f7f9bSDimitry Andric     // Skip over the blob.
190*139f7f9bSDimitry Andric     JumpToBit(NewEnd);
191*139f7f9bSDimitry Andric   }
192*139f7f9bSDimitry Andric }
193*139f7f9bSDimitry Andric 
194*139f7f9bSDimitry Andric unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
195*139f7f9bSDimitry Andric                                      SmallVectorImpl<uint64_t> &Vals,
196*139f7f9bSDimitry Andric                                      StringRef *Blob) {
197*139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
198*139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
199*139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
200*139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
201*139f7f9bSDimitry Andric       Vals.push_back(ReadVBR64(6));
202*139f7f9bSDimitry Andric     return Code;
203*139f7f9bSDimitry Andric   }
204*139f7f9bSDimitry Andric 
205*139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
206*139f7f9bSDimitry Andric 
207*139f7f9bSDimitry Andric   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
208*139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
209*139f7f9bSDimitry Andric     if (Op.isLiteral()) {
210*139f7f9bSDimitry Andric       readAbbreviatedLiteral(Op, Vals);
211*139f7f9bSDimitry Andric       continue;
212*139f7f9bSDimitry Andric     }
213*139f7f9bSDimitry Andric 
214*139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
215*139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
216*139f7f9bSDimitry Andric       readAbbreviatedField(Op, Vals);
217*139f7f9bSDimitry Andric       continue;
218*139f7f9bSDimitry Andric     }
219*139f7f9bSDimitry Andric 
220*139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
221*139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
222*139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
223*139f7f9bSDimitry Andric 
224*139f7f9bSDimitry Andric       // Get the element encoding.
225*139f7f9bSDimitry Andric       assert(i+2 == e && "array op not second to last?");
226*139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
227*139f7f9bSDimitry Andric 
228*139f7f9bSDimitry Andric       // Read all the elements.
229*139f7f9bSDimitry Andric       for (; NumElts; --NumElts)
230*139f7f9bSDimitry Andric         readAbbreviatedField(EltEnc, Vals);
231*139f7f9bSDimitry Andric       continue;
232*139f7f9bSDimitry Andric     }
233*139f7f9bSDimitry Andric 
234*139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
235*139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
236*139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
237*139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
238*139f7f9bSDimitry Andric 
239*139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
240*139f7f9bSDimitry Andric     size_t CurBitPos = GetCurrentBitNo();
241*139f7f9bSDimitry Andric     size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
242*139f7f9bSDimitry Andric 
243*139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
244*139f7f9bSDimitry Andric     // record to empty and return.
245*139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
246*139f7f9bSDimitry Andric       Vals.append(NumElts, 0);
247*139f7f9bSDimitry Andric       NextChar = BitStream->getBitcodeBytes().getExtent();
248*139f7f9bSDimitry Andric       break;
249*139f7f9bSDimitry Andric     }
250*139f7f9bSDimitry Andric 
251*139f7f9bSDimitry Andric     // Otherwise, inform the streamer that we need these bytes in memory.
252*139f7f9bSDimitry Andric     const char *Ptr = (const char*)
253*139f7f9bSDimitry Andric       BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
254*139f7f9bSDimitry Andric 
255*139f7f9bSDimitry Andric     // If we can return a reference to the data, do so to avoid copying it.
256*139f7f9bSDimitry Andric     if (Blob) {
257*139f7f9bSDimitry Andric       *Blob = StringRef(Ptr, NumElts);
258*139f7f9bSDimitry Andric     } else {
259*139f7f9bSDimitry Andric       // Otherwise, unpack into Vals with zero extension.
260*139f7f9bSDimitry Andric       for (; NumElts; --NumElts)
261*139f7f9bSDimitry Andric         Vals.push_back((unsigned char)*Ptr++);
262*139f7f9bSDimitry Andric     }
263*139f7f9bSDimitry Andric     // Skip over tail padding.
264*139f7f9bSDimitry Andric     JumpToBit(NewEnd);
265*139f7f9bSDimitry Andric   }
266*139f7f9bSDimitry Andric 
267*139f7f9bSDimitry Andric   unsigned Code = (unsigned)Vals[0];
268*139f7f9bSDimitry Andric   Vals.erase(Vals.begin());
269*139f7f9bSDimitry Andric   return Code;
270*139f7f9bSDimitry Andric }
271*139f7f9bSDimitry Andric 
272*139f7f9bSDimitry Andric 
273*139f7f9bSDimitry Andric void BitstreamCursor::ReadAbbrevRecord() {
274*139f7f9bSDimitry Andric   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
275*139f7f9bSDimitry Andric   unsigned NumOpInfo = ReadVBR(5);
276*139f7f9bSDimitry Andric   for (unsigned i = 0; i != NumOpInfo; ++i) {
277*139f7f9bSDimitry Andric     bool IsLiteral = Read(1) ? true : false;
278*139f7f9bSDimitry Andric     if (IsLiteral) {
279*139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
280*139f7f9bSDimitry Andric       continue;
281*139f7f9bSDimitry Andric     }
282*139f7f9bSDimitry Andric 
283*139f7f9bSDimitry Andric     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
284*139f7f9bSDimitry Andric     if (BitCodeAbbrevOp::hasEncodingData(E)) {
285*139f7f9bSDimitry Andric       unsigned Data = ReadVBR64(5);
286*139f7f9bSDimitry Andric 
287*139f7f9bSDimitry Andric       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
288*139f7f9bSDimitry Andric       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
289*139f7f9bSDimitry Andric       // a slow path in Read() to have to handle reading zero bits.
290*139f7f9bSDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
291*139f7f9bSDimitry Andric           Data == 0) {
292*139f7f9bSDimitry Andric         Abbv->Add(BitCodeAbbrevOp(0));
293*139f7f9bSDimitry Andric         continue;
294*139f7f9bSDimitry Andric       }
295*139f7f9bSDimitry Andric 
296*139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E, Data));
297*139f7f9bSDimitry Andric     } else
298*139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E));
299*139f7f9bSDimitry Andric   }
300*139f7f9bSDimitry Andric   CurAbbrevs.push_back(Abbv);
301*139f7f9bSDimitry Andric }
302*139f7f9bSDimitry Andric 
303*139f7f9bSDimitry Andric bool BitstreamCursor::ReadBlockInfoBlock() {
304*139f7f9bSDimitry Andric   // If this is the second stream to get to the block info block, skip it.
305*139f7f9bSDimitry Andric   if (BitStream->hasBlockInfoRecords())
306*139f7f9bSDimitry Andric     return SkipBlock();
307*139f7f9bSDimitry Andric 
308*139f7f9bSDimitry Andric   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
309*139f7f9bSDimitry Andric 
310*139f7f9bSDimitry Andric   SmallVector<uint64_t, 64> Record;
311*139f7f9bSDimitry Andric   BitstreamReader::BlockInfo *CurBlockInfo = 0;
312*139f7f9bSDimitry Andric 
313*139f7f9bSDimitry Andric   // Read all the records for this module.
314*139f7f9bSDimitry Andric   while (1) {
315*139f7f9bSDimitry Andric     BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
316*139f7f9bSDimitry Andric 
317*139f7f9bSDimitry Andric     switch (Entry.Kind) {
318*139f7f9bSDimitry Andric     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
319*139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Error:
320*139f7f9bSDimitry Andric       return true;
321*139f7f9bSDimitry Andric     case llvm::BitstreamEntry::EndBlock:
322*139f7f9bSDimitry Andric       return false;
323*139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Record:
324*139f7f9bSDimitry Andric       // The interesting case.
325*139f7f9bSDimitry Andric       break;
326*139f7f9bSDimitry Andric     }
327*139f7f9bSDimitry Andric 
328*139f7f9bSDimitry Andric     // Read abbrev records, associate them with CurBID.
329*139f7f9bSDimitry Andric     if (Entry.ID == bitc::DEFINE_ABBREV) {
330*139f7f9bSDimitry Andric       if (!CurBlockInfo) return true;
331*139f7f9bSDimitry Andric       ReadAbbrevRecord();
332*139f7f9bSDimitry Andric 
333*139f7f9bSDimitry Andric       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
334*139f7f9bSDimitry Andric       // appropriate BlockInfo.
335*139f7f9bSDimitry Andric       BitCodeAbbrev *Abbv = CurAbbrevs.back();
336*139f7f9bSDimitry Andric       CurAbbrevs.pop_back();
337*139f7f9bSDimitry Andric       CurBlockInfo->Abbrevs.push_back(Abbv);
338*139f7f9bSDimitry Andric       continue;
339*139f7f9bSDimitry Andric     }
340*139f7f9bSDimitry Andric 
341*139f7f9bSDimitry Andric     // Read a record.
342*139f7f9bSDimitry Andric     Record.clear();
343*139f7f9bSDimitry Andric     switch (readRecord(Entry.ID, Record)) {
344*139f7f9bSDimitry Andric       default: break;  // Default behavior, ignore unknown content.
345*139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETBID:
346*139f7f9bSDimitry Andric         if (Record.size() < 1) return true;
347*139f7f9bSDimitry Andric         CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
348*139f7f9bSDimitry Andric         break;
349*139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
350*139f7f9bSDimitry Andric         if (!CurBlockInfo) return true;
351*139f7f9bSDimitry Andric         if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
352*139f7f9bSDimitry Andric         std::string Name;
353*139f7f9bSDimitry Andric         for (unsigned i = 0, e = Record.size(); i != e; ++i)
354*139f7f9bSDimitry Andric           Name += (char)Record[i];
355*139f7f9bSDimitry Andric         CurBlockInfo->Name = Name;
356*139f7f9bSDimitry Andric         break;
357*139f7f9bSDimitry Andric       }
358*139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
359*139f7f9bSDimitry Andric         if (!CurBlockInfo) return true;
360*139f7f9bSDimitry Andric         if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
361*139f7f9bSDimitry Andric         std::string Name;
362*139f7f9bSDimitry Andric         for (unsigned i = 1, e = Record.size(); i != e; ++i)
363*139f7f9bSDimitry Andric           Name += (char)Record[i];
364*139f7f9bSDimitry Andric         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
365*139f7f9bSDimitry Andric                                                            Name));
366*139f7f9bSDimitry Andric         break;
367*139f7f9bSDimitry Andric       }
368*139f7f9bSDimitry Andric     }
369*139f7f9bSDimitry Andric   }
370*139f7f9bSDimitry Andric }
371*139f7f9bSDimitry Andric 
372