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