1139f7f9bSDimitry Andric //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric //                     The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric 
10139f7f9bSDimitry Andric #include "llvm/Bitcode/BitstreamReader.h"
11139f7f9bSDimitry Andric 
12139f7f9bSDimitry Andric using namespace llvm;
13139f7f9bSDimitry Andric 
14139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
15139f7f9bSDimitry Andric //  BitstreamCursor implementation
16139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
17139f7f9bSDimitry Andric 
18139f7f9bSDimitry Andric void BitstreamCursor::freeState() {
19139f7f9bSDimitry Andric   // Free all the Abbrevs.
20139f7f9bSDimitry Andric   CurAbbrevs.clear();
21139f7f9bSDimitry Andric 
22139f7f9bSDimitry Andric   // Free all the Abbrevs in the block scope.
23139f7f9bSDimitry Andric   BlockScope.clear();
24139f7f9bSDimitry Andric }
25139f7f9bSDimitry Andric 
26139f7f9bSDimitry Andric /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27139f7f9bSDimitry Andric /// the block, and return true if the block has an error.
28139f7f9bSDimitry Andric bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
29139f7f9bSDimitry Andric   // Save the current block's state on BlockScope.
30139f7f9bSDimitry Andric   BlockScope.push_back(Block(CurCodeSize));
31139f7f9bSDimitry Andric   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
32139f7f9bSDimitry Andric 
33139f7f9bSDimitry Andric   // Add the abbrevs specific to this block to the CurAbbrevs list.
34139f7f9bSDimitry Andric   if (const BitstreamReader::BlockInfo *Info =
35*3ca95b02SDimitry Andric           getBitStreamReader()->getBlockInfo(BlockID)) {
3639d628a0SDimitry Andric     CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
3739d628a0SDimitry Andric                       Info->Abbrevs.end());
38139f7f9bSDimitry Andric   }
39139f7f9bSDimitry Andric 
40139f7f9bSDimitry Andric   // Get the codesize of this block.
41139f7f9bSDimitry Andric   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
42ff0cc061SDimitry Andric   // We can't read more than MaxChunkSize at a time
43ff0cc061SDimitry Andric   if (CurCodeSize > MaxChunkSize)
44ff0cc061SDimitry Andric     return true;
45ff0cc061SDimitry Andric 
46139f7f9bSDimitry Andric   SkipToFourByteBoundary();
47139f7f9bSDimitry Andric   unsigned NumWords = Read(bitc::BlockSizeWidth);
48139f7f9bSDimitry Andric   if (NumWordsP) *NumWordsP = NumWords;
49139f7f9bSDimitry Andric 
50139f7f9bSDimitry Andric   // Validate that this block is sane.
51ff0cc061SDimitry Andric   return CurCodeSize == 0 || AtEndOfStream();
52139f7f9bSDimitry Andric }
53139f7f9bSDimitry Andric 
5439d628a0SDimitry Andric static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
5539d628a0SDimitry Andric                                      const BitCodeAbbrevOp &Op) {
5639d628a0SDimitry Andric   assert(!Op.isLiteral() && "Not to be used with literals!");
57139f7f9bSDimitry Andric 
58139f7f9bSDimitry Andric   // Decode the value as we are commanded.
59139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
60139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
61139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
6291bc56edSDimitry Andric     llvm_unreachable("Should not reach here");
63139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
64ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
6539d628a0SDimitry Andric     return Cursor.Read((unsigned)Op.getEncodingData());
66139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
67ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
6839d628a0SDimitry Andric     return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
69139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
7039d628a0SDimitry Andric     return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
71139f7f9bSDimitry Andric   }
7239d628a0SDimitry Andric   llvm_unreachable("invalid abbreviation encoding");
73139f7f9bSDimitry Andric }
74139f7f9bSDimitry Andric 
7539d628a0SDimitry Andric static void skipAbbreviatedField(BitstreamCursor &Cursor,
7639d628a0SDimitry Andric                                  const BitCodeAbbrevOp &Op) {
7739d628a0SDimitry Andric   assert(!Op.isLiteral() && "Not to be used with literals!");
78139f7f9bSDimitry Andric 
79139f7f9bSDimitry Andric   // Decode the value as we are commanded.
80139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
81139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
82139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
8391bc56edSDimitry Andric     llvm_unreachable("Should not reach here");
84139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
85ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
8639d628a0SDimitry Andric     Cursor.Read((unsigned)Op.getEncodingData());
87139f7f9bSDimitry Andric     break;
88139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
89ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
9039d628a0SDimitry Andric     Cursor.ReadVBR64((unsigned)Op.getEncodingData());
91139f7f9bSDimitry Andric     break;
92139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
9339d628a0SDimitry Andric     Cursor.Read(6);
94139f7f9bSDimitry Andric     break;
95139f7f9bSDimitry Andric   }
96139f7f9bSDimitry Andric }
97139f7f9bSDimitry Andric 
98139f7f9bSDimitry Andric 
99139f7f9bSDimitry Andric 
100139f7f9bSDimitry Andric /// skipRecord - Read the current record and discard it.
101139f7f9bSDimitry Andric void BitstreamCursor::skipRecord(unsigned AbbrevID) {
102139f7f9bSDimitry Andric   // Skip unabbreviated records by reading past their entries.
103139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
104139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
105139f7f9bSDimitry Andric     (void)Code;
106139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
107139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
108139f7f9bSDimitry Andric       (void)ReadVBR64(6);
109139f7f9bSDimitry Andric     return;
110139f7f9bSDimitry Andric   }
111139f7f9bSDimitry Andric 
112139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
113139f7f9bSDimitry Andric 
114139f7f9bSDimitry Andric   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
115139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
116139f7f9bSDimitry Andric     if (Op.isLiteral())
117139f7f9bSDimitry Andric       continue;
118139f7f9bSDimitry Andric 
119139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
120139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
12139d628a0SDimitry Andric       skipAbbreviatedField(*this, Op);
122139f7f9bSDimitry Andric       continue;
123139f7f9bSDimitry Andric     }
124139f7f9bSDimitry Andric 
125139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
126139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
127139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
128139f7f9bSDimitry Andric 
129139f7f9bSDimitry Andric       // Get the element encoding.
130139f7f9bSDimitry Andric       assert(i+2 == e && "array op not second to last?");
131139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
132139f7f9bSDimitry Andric 
133139f7f9bSDimitry Andric       // Read all the elements.
134*3ca95b02SDimitry Andric       // Decode the value as we are commanded.
135*3ca95b02SDimitry Andric       switch (EltEnc.getEncoding()) {
136*3ca95b02SDimitry Andric       default:
137*3ca95b02SDimitry Andric         report_fatal_error("Array element type can't be an Array or a Blob");
138*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::Fixed:
139*3ca95b02SDimitry Andric         assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
140139f7f9bSDimitry Andric         for (; NumElts; --NumElts)
141*3ca95b02SDimitry Andric           Read((unsigned)EltEnc.getEncodingData());
142*3ca95b02SDimitry Andric         break;
143*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::VBR:
144*3ca95b02SDimitry Andric         assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
145*3ca95b02SDimitry Andric         for (; NumElts; --NumElts)
146*3ca95b02SDimitry Andric           ReadVBR64((unsigned)EltEnc.getEncodingData());
147*3ca95b02SDimitry Andric         break;
148*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::Char6:
149*3ca95b02SDimitry Andric         for (; NumElts; --NumElts)
150*3ca95b02SDimitry Andric           Read(6);
151*3ca95b02SDimitry Andric         break;
152*3ca95b02SDimitry Andric       }
153139f7f9bSDimitry Andric       continue;
154139f7f9bSDimitry Andric     }
155139f7f9bSDimitry Andric 
156139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
157139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
158139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
159139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
160139f7f9bSDimitry Andric 
161139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
162139f7f9bSDimitry Andric     size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
163139f7f9bSDimitry Andric 
164139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
165139f7f9bSDimitry Andric     // record to empty and return.
166139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
167*3ca95b02SDimitry Andric       skipToEnd();
168139f7f9bSDimitry Andric       break;
169139f7f9bSDimitry Andric     }
170139f7f9bSDimitry Andric 
171139f7f9bSDimitry Andric     // Skip over the blob.
172139f7f9bSDimitry Andric     JumpToBit(NewEnd);
173139f7f9bSDimitry Andric   }
174139f7f9bSDimitry Andric }
175139f7f9bSDimitry Andric 
176139f7f9bSDimitry Andric unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
177139f7f9bSDimitry Andric                                      SmallVectorImpl<uint64_t> &Vals,
178139f7f9bSDimitry Andric                                      StringRef *Blob) {
179139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
180139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
181139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
182139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
183139f7f9bSDimitry Andric       Vals.push_back(ReadVBR64(6));
184139f7f9bSDimitry Andric     return Code;
185139f7f9bSDimitry Andric   }
186139f7f9bSDimitry Andric 
187139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
188139f7f9bSDimitry Andric 
189f785676fSDimitry Andric   // Read the record code first.
190f785676fSDimitry Andric   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
191f785676fSDimitry Andric   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
19239d628a0SDimitry Andric   unsigned Code;
193f785676fSDimitry Andric   if (CodeOp.isLiteral())
19439d628a0SDimitry Andric     Code = CodeOp.getLiteralValue();
195ff0cc061SDimitry Andric   else {
196ff0cc061SDimitry Andric     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
197ff0cc061SDimitry Andric         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
198ff0cc061SDimitry Andric       report_fatal_error("Abbreviation starts with an Array or a Blob");
19939d628a0SDimitry Andric     Code = readAbbreviatedField(*this, CodeOp);
200ff0cc061SDimitry Andric   }
201f785676fSDimitry Andric 
202f785676fSDimitry Andric   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
203139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
204139f7f9bSDimitry Andric     if (Op.isLiteral()) {
20539d628a0SDimitry Andric       Vals.push_back(Op.getLiteralValue());
206139f7f9bSDimitry Andric       continue;
207139f7f9bSDimitry Andric     }
208139f7f9bSDimitry Andric 
209139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
210139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
21139d628a0SDimitry Andric       Vals.push_back(readAbbreviatedField(*this, Op));
212139f7f9bSDimitry Andric       continue;
213139f7f9bSDimitry Andric     }
214139f7f9bSDimitry Andric 
215139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
216139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
217139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
218139f7f9bSDimitry Andric 
219139f7f9bSDimitry Andric       // Get the element encoding.
220ff0cc061SDimitry Andric       if (i + 2 != e)
221ff0cc061SDimitry Andric         report_fatal_error("Array op not second to last");
222139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
223ff0cc061SDimitry Andric       if (!EltEnc.isEncoding())
224ff0cc061SDimitry Andric         report_fatal_error(
225ff0cc061SDimitry Andric             "Array element type has to be an encoding of a type");
226139f7f9bSDimitry Andric 
227139f7f9bSDimitry Andric       // Read all the elements.
228*3ca95b02SDimitry Andric       switch (EltEnc.getEncoding()) {
229*3ca95b02SDimitry Andric       default:
230*3ca95b02SDimitry Andric         report_fatal_error("Array element type can't be an Array or a Blob");
231*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::Fixed:
232139f7f9bSDimitry Andric         for (; NumElts; --NumElts)
233*3ca95b02SDimitry Andric           Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
234*3ca95b02SDimitry Andric         break;
235*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::VBR:
236*3ca95b02SDimitry Andric         for (; NumElts; --NumElts)
237*3ca95b02SDimitry Andric           Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
238*3ca95b02SDimitry Andric         break;
239*3ca95b02SDimitry Andric       case BitCodeAbbrevOp::Char6:
240*3ca95b02SDimitry Andric         for (; NumElts; --NumElts)
241*3ca95b02SDimitry Andric           Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
242*3ca95b02SDimitry Andric       }
243139f7f9bSDimitry Andric       continue;
244139f7f9bSDimitry Andric     }
245139f7f9bSDimitry Andric 
246139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
247139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
248139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
249139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
250139f7f9bSDimitry Andric 
251139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
252139f7f9bSDimitry Andric     size_t CurBitPos = GetCurrentBitNo();
253139f7f9bSDimitry Andric     size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
254139f7f9bSDimitry Andric 
255139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
256139f7f9bSDimitry Andric     // record to empty and return.
257139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
258139f7f9bSDimitry Andric       Vals.append(NumElts, 0);
259*3ca95b02SDimitry Andric       skipToEnd();
260139f7f9bSDimitry Andric       break;
261139f7f9bSDimitry Andric     }
262139f7f9bSDimitry Andric 
263*3ca95b02SDimitry Andric     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
264*3ca95b02SDimitry Andric     // over tail padding first, in case jumping to NewEnd invalidates the Blob
265*3ca95b02SDimitry Andric     // pointer.
266*3ca95b02SDimitry Andric     JumpToBit(NewEnd);
267*3ca95b02SDimitry Andric     const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
268139f7f9bSDimitry Andric 
269139f7f9bSDimitry Andric     // If we can return a reference to the data, do so to avoid copying it.
270139f7f9bSDimitry Andric     if (Blob) {
271139f7f9bSDimitry Andric       *Blob = StringRef(Ptr, NumElts);
272139f7f9bSDimitry Andric     } else {
273139f7f9bSDimitry Andric       // Otherwise, unpack into Vals with zero extension.
274139f7f9bSDimitry Andric       for (; NumElts; --NumElts)
275139f7f9bSDimitry Andric         Vals.push_back((unsigned char)*Ptr++);
276139f7f9bSDimitry Andric     }
277139f7f9bSDimitry Andric   }
278139f7f9bSDimitry Andric 
279139f7f9bSDimitry Andric   return Code;
280139f7f9bSDimitry Andric }
281139f7f9bSDimitry Andric 
282139f7f9bSDimitry Andric 
283139f7f9bSDimitry Andric void BitstreamCursor::ReadAbbrevRecord() {
284139f7f9bSDimitry Andric   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
285139f7f9bSDimitry Andric   unsigned NumOpInfo = ReadVBR(5);
286139f7f9bSDimitry Andric   for (unsigned i = 0; i != NumOpInfo; ++i) {
287ff0cc061SDimitry Andric     bool IsLiteral = Read(1);
288139f7f9bSDimitry Andric     if (IsLiteral) {
289139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
290139f7f9bSDimitry Andric       continue;
291139f7f9bSDimitry Andric     }
292139f7f9bSDimitry Andric 
293139f7f9bSDimitry Andric     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
294139f7f9bSDimitry Andric     if (BitCodeAbbrevOp::hasEncodingData(E)) {
295ff0cc061SDimitry Andric       uint64_t Data = ReadVBR64(5);
296139f7f9bSDimitry Andric 
297139f7f9bSDimitry Andric       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
298139f7f9bSDimitry Andric       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
299139f7f9bSDimitry Andric       // a slow path in Read() to have to handle reading zero bits.
300139f7f9bSDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
301139f7f9bSDimitry Andric           Data == 0) {
302139f7f9bSDimitry Andric         Abbv->Add(BitCodeAbbrevOp(0));
303139f7f9bSDimitry Andric         continue;
304139f7f9bSDimitry Andric       }
305139f7f9bSDimitry Andric 
306ff0cc061SDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
307ff0cc061SDimitry Andric           Data > MaxChunkSize)
308ff0cc061SDimitry Andric         report_fatal_error(
309ff0cc061SDimitry Andric             "Fixed or VBR abbrev record with size > MaxChunkData");
310ff0cc061SDimitry Andric 
311139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E, Data));
312139f7f9bSDimitry Andric     } else
313139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E));
314139f7f9bSDimitry Andric   }
315ff0cc061SDimitry Andric 
316ff0cc061SDimitry Andric   if (Abbv->getNumOperandInfos() == 0)
317ff0cc061SDimitry Andric     report_fatal_error("Abbrev record with no operands");
318139f7f9bSDimitry Andric   CurAbbrevs.push_back(Abbv);
319139f7f9bSDimitry Andric }
320139f7f9bSDimitry Andric 
321139f7f9bSDimitry Andric bool BitstreamCursor::ReadBlockInfoBlock() {
322139f7f9bSDimitry Andric   // If this is the second stream to get to the block info block, skip it.
323*3ca95b02SDimitry Andric   if (getBitStreamReader()->hasBlockInfoRecords())
324139f7f9bSDimitry Andric     return SkipBlock();
325139f7f9bSDimitry Andric 
326139f7f9bSDimitry Andric   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
327139f7f9bSDimitry Andric 
328139f7f9bSDimitry Andric   SmallVector<uint64_t, 64> Record;
32991bc56edSDimitry Andric   BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
330139f7f9bSDimitry Andric 
331139f7f9bSDimitry Andric   // Read all the records for this module.
332139f7f9bSDimitry Andric   while (1) {
333139f7f9bSDimitry Andric     BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
334139f7f9bSDimitry Andric 
335139f7f9bSDimitry Andric     switch (Entry.Kind) {
336139f7f9bSDimitry Andric     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
337139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Error:
338139f7f9bSDimitry Andric       return true;
339139f7f9bSDimitry Andric     case llvm::BitstreamEntry::EndBlock:
340139f7f9bSDimitry Andric       return false;
341139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Record:
342139f7f9bSDimitry Andric       // The interesting case.
343139f7f9bSDimitry Andric       break;
344139f7f9bSDimitry Andric     }
345139f7f9bSDimitry Andric 
346139f7f9bSDimitry Andric     // Read abbrev records, associate them with CurBID.
347139f7f9bSDimitry Andric     if (Entry.ID == bitc::DEFINE_ABBREV) {
348139f7f9bSDimitry Andric       if (!CurBlockInfo) return true;
349139f7f9bSDimitry Andric       ReadAbbrevRecord();
350139f7f9bSDimitry Andric 
351139f7f9bSDimitry Andric       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
352139f7f9bSDimitry Andric       // appropriate BlockInfo.
35339d628a0SDimitry Andric       CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
354139f7f9bSDimitry Andric       CurAbbrevs.pop_back();
355139f7f9bSDimitry Andric       continue;
356139f7f9bSDimitry Andric     }
357139f7f9bSDimitry Andric 
358139f7f9bSDimitry Andric     // Read a record.
359139f7f9bSDimitry Andric     Record.clear();
360139f7f9bSDimitry Andric     switch (readRecord(Entry.ID, Record)) {
361139f7f9bSDimitry Andric       default: break;  // Default behavior, ignore unknown content.
362139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETBID:
363139f7f9bSDimitry Andric         if (Record.size() < 1) return true;
364*3ca95b02SDimitry Andric         CurBlockInfo =
365*3ca95b02SDimitry Andric             &getBitStreamReader()->getOrCreateBlockInfo((unsigned)Record[0]);
366139f7f9bSDimitry Andric         break;
367139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
368139f7f9bSDimitry Andric         if (!CurBlockInfo) return true;
369*3ca95b02SDimitry Andric         if (getBitStreamReader()->isIgnoringBlockInfoNames())
370*3ca95b02SDimitry Andric           break; // Ignore name.
371139f7f9bSDimitry Andric         std::string Name;
372139f7f9bSDimitry Andric         for (unsigned i = 0, e = Record.size(); i != e; ++i)
373139f7f9bSDimitry Andric           Name += (char)Record[i];
374139f7f9bSDimitry Andric         CurBlockInfo->Name = Name;
375139f7f9bSDimitry Andric         break;
376139f7f9bSDimitry Andric       }
377139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
378139f7f9bSDimitry Andric         if (!CurBlockInfo) return true;
379*3ca95b02SDimitry Andric         if (getBitStreamReader()->isIgnoringBlockInfoNames())
380*3ca95b02SDimitry Andric           break; // Ignore name.
381139f7f9bSDimitry Andric         std::string Name;
382139f7f9bSDimitry Andric         for (unsigned i = 1, e = Record.size(); i != e; ++i)
383139f7f9bSDimitry Andric           Name += (char)Record[i];
384139f7f9bSDimitry Andric         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
385139f7f9bSDimitry Andric                                                            Name));
386139f7f9bSDimitry Andric         break;
387139f7f9bSDimitry Andric       }
388139f7f9bSDimitry Andric     }
389139f7f9bSDimitry Andric   }
390139f7f9bSDimitry Andric }
391139f7f9bSDimitry Andric 
392