• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 <climits>
20 #include <string>
21 #include <vector>
22 
23 namespace llvm {
24 
25   class Deserializer;
26 
27 class BitstreamReader {
28 public:
29   /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
30   /// These describe abbreviations that all blocks of the specified ID inherit.
31   struct BlockInfo {
32     unsigned BlockID;
33     std::vector<BitCodeAbbrev*> Abbrevs;
34     std::string Name;
35 
36     std::vector<std::pair<unsigned, std::string> > RecordNames;
37   };
38 private:
39   /// FirstChar/LastChar - This remembers the first and last bytes of the
40   /// stream.
41   const unsigned char *FirstChar, *LastChar;
42 
43   std::vector<BlockInfo> BlockInfoRecords;
44 
45   /// IgnoreBlockInfoNames - This is set to true if we don't care about the
46   /// block/record name information in the BlockInfo block. Only llvm-bcanalyzer
47   /// uses this.
48   bool IgnoreBlockInfoNames;
49 
50   BitstreamReader(const BitstreamReader&);  // NOT IMPLEMENTED
51   void operator=(const BitstreamReader&);  // NOT IMPLEMENTED
52 public:
BitstreamReader()53   BitstreamReader() : FirstChar(0), LastChar(0), IgnoreBlockInfoNames(true) {
54   }
55 
BitstreamReader(const unsigned char * Start,const unsigned char * End)56   BitstreamReader(const unsigned char *Start, const unsigned char *End) {
57     IgnoreBlockInfoNames = true;
58     init(Start, End);
59   }
60 
init(const unsigned char * Start,const unsigned char * End)61   void init(const unsigned char *Start, const unsigned char *End) {
62     FirstChar = Start;
63     LastChar = End;
64     assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
65   }
66 
~BitstreamReader()67   ~BitstreamReader() {
68     // Free the BlockInfoRecords.
69     while (!BlockInfoRecords.empty()) {
70       BlockInfo &Info = BlockInfoRecords.back();
71       // Free blockinfo abbrev info.
72       for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
73            i != e; ++i)
74         Info.Abbrevs[i]->dropRef();
75       BlockInfoRecords.pop_back();
76     }
77   }
78 
getFirstChar()79   const unsigned char *getFirstChar() const { return FirstChar; }
getLastChar()80   const unsigned char *getLastChar() const { return LastChar; }
81 
82   /// CollectBlockInfoNames - This is called by clients that want block/record
83   /// name information.
CollectBlockInfoNames()84   void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
isIgnoringBlockInfoNames()85   bool isIgnoringBlockInfoNames() { return IgnoreBlockInfoNames; }
86 
87   //===--------------------------------------------------------------------===//
88   // Block Manipulation
89   //===--------------------------------------------------------------------===//
90 
91   /// hasBlockInfoRecords - Return true if we've already read and processed the
92   /// block info block for this Bitstream.  We only process it for the first
93   /// cursor that walks over it.
hasBlockInfoRecords()94   bool hasBlockInfoRecords() const { return !BlockInfoRecords.empty(); }
95 
96   /// getBlockInfo - If there is block info for the specified ID, return it,
97   /// otherwise return null.
getBlockInfo(unsigned BlockID)98   const BlockInfo *getBlockInfo(unsigned BlockID) const {
99     // Common case, the most recent entry matches BlockID.
100     if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
101       return &BlockInfoRecords.back();
102 
103     for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
104          i != e; ++i)
105       if (BlockInfoRecords[i].BlockID == BlockID)
106         return &BlockInfoRecords[i];
107     return 0;
108   }
109 
getOrCreateBlockInfo(unsigned BlockID)110   BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
111     if (const BlockInfo *BI = getBlockInfo(BlockID))
112       return *const_cast<BlockInfo*>(BI);
113 
114     // Otherwise, add a new record.
115     BlockInfoRecords.push_back(BlockInfo());
116     BlockInfoRecords.back().BlockID = BlockID;
117     return BlockInfoRecords.back();
118   }
119 
120 };
121 
122 class BitstreamCursor {
123   friend class Deserializer;
124   BitstreamReader *BitStream;
125   const unsigned char *NextChar;
126 
127   /// CurWord - This is the current data we have pulled from the stream but have
128   /// not returned to the client.
129   uint32_t CurWord;
130 
131   /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
132   /// is always from [0...31] inclusive.
133   unsigned BitsInCurWord;
134 
135   // CurCodeSize - This is the declared size of code values used for the current
136   // block, in bits.
137   unsigned CurCodeSize;
138 
139   /// CurAbbrevs - Abbrevs installed at in this block.
140   std::vector<BitCodeAbbrev*> CurAbbrevs;
141 
142   struct Block {
143     unsigned PrevCodeSize;
144     std::vector<BitCodeAbbrev*> PrevAbbrevs;
BlockBlock145     explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
146   };
147 
148   /// BlockScope - This tracks the codesize of parent blocks.
149   SmallVector<Block, 8> BlockScope;
150 
151 public:
BitstreamCursor()152   BitstreamCursor() : BitStream(0), NextChar(0) {
153   }
BitstreamCursor(const BitstreamCursor & RHS)154   BitstreamCursor(const BitstreamCursor &RHS) : BitStream(0), NextChar(0) {
155     operator=(RHS);
156   }
157 
BitstreamCursor(BitstreamReader & R)158   explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
159     NextChar = R.getFirstChar();
160     assert(NextChar && "Bitstream not initialized yet");
161     CurWord = 0;
162     BitsInCurWord = 0;
163     CurCodeSize = 2;
164   }
165 
init(BitstreamReader & R)166   void init(BitstreamReader &R) {
167     freeState();
168 
169     BitStream = &R;
170     NextChar = R.getFirstChar();
171     assert(NextChar && "Bitstream not initialized yet");
172     CurWord = 0;
173     BitsInCurWord = 0;
174     CurCodeSize = 2;
175   }
176 
~BitstreamCursor()177   ~BitstreamCursor() {
178     freeState();
179   }
180 
181   void operator=(const BitstreamCursor &RHS) {
182     freeState();
183 
184     BitStream = RHS.BitStream;
185     NextChar = RHS.NextChar;
186     CurWord = RHS.CurWord;
187     BitsInCurWord = RHS.BitsInCurWord;
188     CurCodeSize = RHS.CurCodeSize;
189 
190     // Copy abbreviations, and bump ref counts.
191     CurAbbrevs = RHS.CurAbbrevs;
192     for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
193          i != e; ++i)
194       CurAbbrevs[i]->addRef();
195 
196     // Copy block scope and bump ref counts.
197     BlockScope = RHS.BlockScope;
198     for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
199          S != e; ++S) {
200       std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
201       for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
202            i != e; ++i)
203         Abbrevs[i]->addRef();
204     }
205   }
206 
freeState()207   void freeState() {
208     // Free all the Abbrevs.
209     for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
210          i != e; ++i)
211       CurAbbrevs[i]->dropRef();
212     CurAbbrevs.clear();
213 
214     // Free all the Abbrevs in the block scope.
215     for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
216          S != e; ++S) {
217       std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
218       for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
219            i != e; ++i)
220         Abbrevs[i]->dropRef();
221     }
222     BlockScope.clear();
223   }
224 
225   /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
GetAbbrevIDWidth()226   unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
227 
AtEndOfStream()228   bool AtEndOfStream() const {
229     return NextChar == BitStream->getLastChar() && BitsInCurWord == 0;
230   }
231 
232   /// GetCurrentBitNo - Return the bit # of the bit we are reading.
GetCurrentBitNo()233   uint64_t GetCurrentBitNo() const {
234     return (NextChar-BitStream->getFirstChar())*CHAR_BIT - BitsInCurWord;
235   }
236 
getBitStreamReader()237   BitstreamReader *getBitStreamReader() {
238     return BitStream;
239   }
getBitStreamReader()240   const BitstreamReader *getBitStreamReader() const {
241     return BitStream;
242   }
243 
244 
245   /// JumpToBit - Reset the stream to the specified bit number.
JumpToBit(uint64_t BitNo)246   void JumpToBit(uint64_t BitNo) {
247     uintptr_t ByteNo = uintptr_t(BitNo/8) & ~3;
248     uintptr_t WordBitNo = uintptr_t(BitNo) & 31;
249     assert(ByteNo <= (uintptr_t)(BitStream->getLastChar()-
250                                  BitStream->getFirstChar()) &&
251            "Invalid location");
252 
253     // Move the cursor to the right word.
254     NextChar = BitStream->getFirstChar()+ByteNo;
255     BitsInCurWord = 0;
256     CurWord = 0;
257 
258     // Skip over any bits that are already consumed.
259     if (WordBitNo)
260       Read(static_cast<unsigned>(WordBitNo));
261   }
262 
263 
Read(unsigned NumBits)264   uint32_t Read(unsigned NumBits) {
265     assert(NumBits <= 32 && "Cannot return more than 32 bits!");
266     // If the field is fully contained by CurWord, return it quickly.
267     if (BitsInCurWord >= NumBits) {
268       uint32_t R = CurWord & ((1U << NumBits)-1);
269       CurWord >>= NumBits;
270       BitsInCurWord -= NumBits;
271       return R;
272     }
273 
274     // If we run out of data, stop at the end of the stream.
275     if (NextChar == BitStream->getLastChar()) {
276       CurWord = 0;
277       BitsInCurWord = 0;
278       return 0;
279     }
280 
281     unsigned R = CurWord;
282 
283     // Read the next word from the stream.
284     CurWord = (NextChar[0] <<  0) | (NextChar[1] << 8) |
285               (NextChar[2] << 16) | (NextChar[3] << 24);
286     NextChar += 4;
287 
288     // Extract NumBits-BitsInCurWord from what we just read.
289     unsigned BitsLeft = NumBits-BitsInCurWord;
290 
291     // Be careful here, BitsLeft is in the range [1..32] inclusive.
292     R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
293 
294     // BitsLeft bits have just been used up from CurWord.
295     if (BitsLeft != 32)
296       CurWord >>= BitsLeft;
297     else
298       CurWord = 0;
299     BitsInCurWord = 32-BitsLeft;
300     return R;
301   }
302 
Read64(unsigned NumBits)303   uint64_t Read64(unsigned NumBits) {
304     if (NumBits <= 32) return Read(NumBits);
305 
306     uint64_t V = Read(32);
307     return V | (uint64_t)Read(NumBits-32) << 32;
308   }
309 
ReadVBR(unsigned NumBits)310   uint32_t ReadVBR(unsigned NumBits) {
311     uint32_t Piece = Read(NumBits);
312     if ((Piece & (1U << (NumBits-1))) == 0)
313       return Piece;
314 
315     uint32_t Result = 0;
316     unsigned NextBit = 0;
317     while (1) {
318       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
319 
320       if ((Piece & (1U << (NumBits-1))) == 0)
321         return Result;
322 
323       NextBit += NumBits-1;
324       Piece = Read(NumBits);
325     }
326   }
327 
328   // ReadVBR64 - Read a VBR that may have a value up to 64-bits in size.  The
329   // chunk size of the VBR must still be <= 32 bits though.
ReadVBR64(unsigned NumBits)330   uint64_t ReadVBR64(unsigned NumBits) {
331     uint32_t Piece = Read(NumBits);
332     if ((Piece & (1U << (NumBits-1))) == 0)
333       return uint64_t(Piece);
334 
335     uint64_t Result = 0;
336     unsigned NextBit = 0;
337     while (1) {
338       Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
339 
340       if ((Piece & (1U << (NumBits-1))) == 0)
341         return Result;
342 
343       NextBit += NumBits-1;
344       Piece = Read(NumBits);
345     }
346   }
347 
SkipToWord()348   void SkipToWord() {
349     BitsInCurWord = 0;
350     CurWord = 0;
351   }
352 
ReadCode()353   unsigned ReadCode() {
354     return Read(CurCodeSize);
355   }
356 
357 
358   // Block header:
359   //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
360 
361   /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
362   /// the block.
ReadSubBlockID()363   unsigned ReadSubBlockID() {
364     return ReadVBR(bitc::BlockIDWidth);
365   }
366 
367   /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
368   /// over the body of this block.  If the block record is malformed, return
369   /// true.
SkipBlock()370   bool SkipBlock() {
371     // Read and ignore the codelen value.  Since we are skipping this block, we
372     // don't care what code widths are used inside of it.
373     ReadVBR(bitc::CodeLenWidth);
374     SkipToWord();
375     unsigned NumWords = Read(bitc::BlockSizeWidth);
376 
377     // Check that the block wasn't partially defined, and that the offset isn't
378     // bogus.
379     const unsigned char *const SkipTo = NextChar + NumWords*4;
380     if (AtEndOfStream() || SkipTo > BitStream->getLastChar() ||
381                            SkipTo < BitStream->getFirstChar())
382       return true;
383 
384     NextChar = SkipTo;
385     return false;
386   }
387 
388   /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
389   /// the block, and return true if the block is valid.
390   bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = 0) {
391     // Save the current block's state on BlockScope.
392     BlockScope.push_back(Block(CurCodeSize));
393     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
394 
395     // Add the abbrevs specific to this block to the CurAbbrevs list.
396     if (const BitstreamReader::BlockInfo *Info =
397           BitStream->getBlockInfo(BlockID)) {
398       for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
399            i != e; ++i) {
400         CurAbbrevs.push_back(Info->Abbrevs[i]);
401         CurAbbrevs.back()->addRef();
402       }
403     }
404 
405     // Get the codesize of this block.
406     CurCodeSize = ReadVBR(bitc::CodeLenWidth);
407     SkipToWord();
408     unsigned NumWords = Read(bitc::BlockSizeWidth);
409     if (NumWordsP) *NumWordsP = NumWords;
410 
411     // Validate that this block is sane.
412     if (CurCodeSize == 0 || AtEndOfStream() ||
413         NextChar+NumWords*4 > BitStream->getLastChar())
414       return true;
415 
416     return false;
417   }
418 
ReadBlockEnd()419   bool ReadBlockEnd() {
420     if (BlockScope.empty()) return true;
421 
422     // Block tail:
423     //    [END_BLOCK, <align4bytes>]
424     SkipToWord();
425 
426     PopBlockScope();
427     return false;
428   }
429 
430 private:
PopBlockScope()431   void PopBlockScope() {
432     CurCodeSize = BlockScope.back().PrevCodeSize;
433 
434     // Delete abbrevs from popped scope.
435     for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
436          i != e; ++i)
437       CurAbbrevs[i]->dropRef();
438 
439     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
440     BlockScope.pop_back();
441   }
442 
443  //===--------------------------------------------------------------------===//
444   // Record Processing
445   //===--------------------------------------------------------------------===//
446 
447 private:
ReadAbbreviatedLiteral(const BitCodeAbbrevOp & Op,SmallVectorImpl<uint64_t> & Vals)448   void ReadAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
449                               SmallVectorImpl<uint64_t> &Vals) {
450     assert(Op.isLiteral() && "Not a literal");
451     // If the abbrev specifies the literal value to use, use it.
452     Vals.push_back(Op.getLiteralValue());
453   }
454 
ReadAbbreviatedField(const BitCodeAbbrevOp & Op,SmallVectorImpl<uint64_t> & Vals)455   void ReadAbbreviatedField(const BitCodeAbbrevOp &Op,
456                             SmallVectorImpl<uint64_t> &Vals) {
457     assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
458 
459     // Decode the value as we are commanded.
460     switch (Op.getEncoding()) {
461     default: assert(0 && "Unknown encoding!");
462     case BitCodeAbbrevOp::Fixed:
463       Vals.push_back(Read((unsigned)Op.getEncodingData()));
464       break;
465     case BitCodeAbbrevOp::VBR:
466       Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
467       break;
468     case BitCodeAbbrevOp::Char6:
469       Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
470       break;
471     }
472   }
473 public:
474 
475   /// getAbbrev - Return the abbreviation for the specified AbbrevId.
getAbbrev(unsigned AbbrevID)476   const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
477     unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
478     assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
479     return CurAbbrevs[AbbrevNo];
480   }
481 
482   unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
483                       const char **BlobStart = 0, unsigned *BlobLen = 0) {
484     if (AbbrevID == bitc::UNABBREV_RECORD) {
485       unsigned Code = ReadVBR(6);
486       unsigned NumElts = ReadVBR(6);
487       for (unsigned i = 0; i != NumElts; ++i)
488         Vals.push_back(ReadVBR64(6));
489       return Code;
490     }
491 
492     const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
493 
494     for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
495       const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
496       if (Op.isLiteral()) {
497         ReadAbbreviatedLiteral(Op, Vals);
498       } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
499         // Array case.  Read the number of elements as a vbr6.
500         unsigned NumElts = ReadVBR(6);
501 
502         // Get the element encoding.
503         assert(i+2 == e && "array op not second to last?");
504         const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
505 
506         // Read all the elements.
507         for (; NumElts; --NumElts)
508           ReadAbbreviatedField(EltEnc, Vals);
509       } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
510         // Blob case.  Read the number of bytes as a vbr6.
511         unsigned NumElts = ReadVBR(6);
512         SkipToWord();  // 32-bit alignment
513 
514         // Figure out where the end of this blob will be including tail padding.
515         const unsigned char *NewEnd = NextChar+((NumElts+3)&~3);
516 
517         // If this would read off the end of the bitcode file, just set the
518         // record to empty and return.
519         if (NewEnd > BitStream->getLastChar()) {
520           Vals.append(NumElts, 0);
521           NextChar = BitStream->getLastChar();
522           break;
523         }
524 
525         // Otherwise, read the number of bytes.  If we can return a reference to
526         // the data, do so to avoid copying it.
527         if (BlobStart) {
528           *BlobStart = (const char*)NextChar;
529           *BlobLen = NumElts;
530         } else {
531           for (; NumElts; ++NextChar, --NumElts)
532             Vals.push_back(*NextChar);
533         }
534         // Skip over tail padding.
535         NextChar = NewEnd;
536       } else {
537         ReadAbbreviatedField(Op, Vals);
538       }
539     }
540 
541     unsigned Code = (unsigned)Vals[0];
542     Vals.erase(Vals.begin());
543     return Code;
544   }
545 
ReadRecord(unsigned AbbrevID,SmallVectorImpl<uint64_t> & Vals,const char * & BlobStart,unsigned & BlobLen)546   unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
547                       const char *&BlobStart, unsigned &BlobLen) {
548     return ReadRecord(AbbrevID, Vals, &BlobStart, &BlobLen);
549   }
550 
551 
552   //===--------------------------------------------------------------------===//
553   // Abbrev Processing
554   //===--------------------------------------------------------------------===//
555 
ReadAbbrevRecord()556   void ReadAbbrevRecord() {
557     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
558     unsigned NumOpInfo = ReadVBR(5);
559     for (unsigned i = 0; i != NumOpInfo; ++i) {
560       bool IsLiteral = Read(1) ? true : false;
561       if (IsLiteral) {
562         Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
563         continue;
564       }
565 
566       BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
567       if (BitCodeAbbrevOp::hasEncodingData(E))
568         Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
569       else
570         Abbv->Add(BitCodeAbbrevOp(E));
571     }
572     CurAbbrevs.push_back(Abbv);
573   }
574 
575 public:
576 
ReadBlockInfoBlock()577   bool ReadBlockInfoBlock() {
578     // If this is the second stream to get to the block info block, skip it.
579     if (BitStream->hasBlockInfoRecords())
580       return SkipBlock();
581 
582     if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
583 
584     SmallVector<uint64_t, 64> Record;
585     BitstreamReader::BlockInfo *CurBlockInfo = 0;
586 
587     // Read all the records for this module.
588     while (1) {
589       unsigned Code = ReadCode();
590       if (Code == bitc::END_BLOCK)
591         return ReadBlockEnd();
592       if (Code == bitc::ENTER_SUBBLOCK) {
593         ReadSubBlockID();
594         if (SkipBlock()) return true;
595         continue;
596       }
597 
598       // Read abbrev records, associate them with CurBID.
599       if (Code == bitc::DEFINE_ABBREV) {
600         if (!CurBlockInfo) return true;
601         ReadAbbrevRecord();
602 
603         // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
604         // appropriate BlockInfo.
605         BitCodeAbbrev *Abbv = CurAbbrevs.back();
606         CurAbbrevs.pop_back();
607         CurBlockInfo->Abbrevs.push_back(Abbv);
608         continue;
609       }
610 
611       // Read a record.
612       Record.clear();
613       switch (ReadRecord(Code, Record)) {
614       default: break;  // Default behavior, ignore unknown content.
615       case bitc::BLOCKINFO_CODE_SETBID:
616         if (Record.size() < 1) return true;
617         CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
618         break;
619       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
620         if (!CurBlockInfo) return true;
621         if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
622         std::string Name;
623         for (unsigned i = 0, e = Record.size(); i != e; ++i)
624           Name += (char)Record[i];
625         CurBlockInfo->Name = Name;
626         break;
627       }
628       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
629         if (!CurBlockInfo) return true;
630         if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
631         std::string Name;
632         for (unsigned i = 1, e = Record.size(); i != e; ++i)
633           Name += (char)Record[i];
634         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
635                                                            Name));
636         break;
637       }
638       }
639     }
640   }
641 };
642 
643 } // End llvm namespace
644 
645 #endif
646