Pass a std::uinque_ptr to ParseAssembly to make the ownership explicit. NFC.
[oota-llvm.git] / lib / Bitcode / Reader / BitstreamReader.cpp
index be70f5213bc844af0acc8db9a84963053aefa4b5..72451ec9500c6be3ae2170e86d668550c5136e29 100644 (file)
@@ -17,43 +17,37 @@ using namespace llvm;
 
 void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
   freeState();
-  
+
   BitStream = RHS.BitStream;
   NextChar = RHS.NextChar;
   CurWord = RHS.CurWord;
   BitsInCurWord = RHS.BitsInCurWord;
   CurCodeSize = RHS.CurCodeSize;
-  
+
   // Copy abbreviations, and bump ref counts.
   CurAbbrevs = RHS.CurAbbrevs;
-  for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
-       i != e; ++i)
+  for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
     CurAbbrevs[i]->addRef();
-  
+
   // Copy block scope and bump ref counts.
   BlockScope = RHS.BlockScope;
-  for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
-       S != e; ++S) {
+  for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
     std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
-    for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
-         i != e; ++i)
+    for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
       Abbrevs[i]->addRef();
   }
 }
 
 void BitstreamCursor::freeState() {
   // Free all the Abbrevs.
-  for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
-       i != e; ++i)
+  for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
     CurAbbrevs[i]->dropRef();
   CurAbbrevs.clear();
-  
+
   // Free all the Abbrevs in the block scope.
-  for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
-       S != e; ++S) {
+  for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
     std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
-    for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
-         i != e; ++i)
+    for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
       Abbrevs[i]->dropRef();
   }
   BlockScope.clear();
@@ -65,27 +59,26 @@ bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
   // Save the current block's state on BlockScope.
   BlockScope.push_back(Block(CurCodeSize));
   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
-  
+
   // Add the abbrevs specific to this block to the CurAbbrevs list.
   if (const BitstreamReader::BlockInfo *Info =
       BitStream->getBlockInfo(BlockID)) {
-    for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
-         i != e; ++i) {
+    for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
       CurAbbrevs.push_back(Info->Abbrevs[i]);
       CurAbbrevs.back()->addRef();
     }
   }
-  
+
   // Get the codesize of this block.
   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
-  SkipToWord();
+  SkipToFourByteBoundary();
   unsigned NumWords = Read(bitc::BlockSizeWidth);
   if (NumWordsP) *NumWordsP = NumWords;
-  
+
   // Validate that this block is sane.
   if (CurCodeSize == 0 || AtEndOfStream())
     return true;
-  
+
   return false;
 }
 
@@ -99,12 +92,12 @@ void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
 void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
                                            SmallVectorImpl<uint64_t> &Vals) {
   assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
-  
+
   // Decode the value as we are commanded.
   switch (Op.getEncoding()) {
   case BitCodeAbbrevOp::Array:
   case BitCodeAbbrevOp::Blob:
-    assert(0 && "Should not reach here");
+    llvm_unreachable("Should not reach here");
   case BitCodeAbbrevOp::Fixed:
     Vals.push_back(Read((unsigned)Op.getEncodingData()));
     break;
@@ -119,12 +112,12 @@ void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
 
 void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
   assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
-  
+
   // Decode the value as we are commanded.
   switch (Op.getEncoding()) {
   case BitCodeAbbrevOp::Array:
   case BitCodeAbbrevOp::Blob:
-    assert(0 && "Should not reach here");
+    llvm_unreachable("Should not reach here");
   case BitCodeAbbrevOp::Fixed:
     (void)Read((unsigned)Op.getEncodingData());
     break;
@@ -152,55 +145,55 @@ void BitstreamCursor::skipRecord(unsigned AbbrevID) {
   }
 
   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
-  
+
   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
     if (Op.isLiteral())
       continue;
-    
+
     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
       skipAbbreviatedField(Op);
       continue;
     }
-    
+
     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
       // Array case.  Read the number of elements as a vbr6.
       unsigned NumElts = ReadVBR(6);
-      
+
       // Get the element encoding.
       assert(i+2 == e && "array op not second to last?");
       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
-      
+
       // Read all the elements.
       for (; NumElts; --NumElts)
         skipAbbreviatedField(EltEnc);
       continue;
     }
-    
+
     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
     // Blob case.  Read the number of bytes as a vbr6.
     unsigned NumElts = ReadVBR(6);
-    SkipToWord();  // 32-bit alignment
-    
+    SkipToFourByteBoundary();  // 32-bit alignment
+
     // Figure out where the end of this blob will be including tail padding.
-    size_t NewEnd = NextChar+((NumElts+3)&~3);
-    
+    size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
+
     // If this would read off the end of the bitcode file, just set the
     // record to empty and return.
-    if (!canSkipToPos(NewEnd)) {
+    if (!canSkipToPos(NewEnd/8)) {
       NextChar = BitStream->getBitcodeBytes().getExtent();
       break;
     }
-    
+
     // Skip over the blob.
-    NextChar = NewEnd;
+    JumpToBit(NewEnd);
   }
 }
 
-unsigned BitstreamCursor::ReadRecord(unsigned AbbrevID,
+unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
                                      SmallVectorImpl<uint64_t> &Vals,
-                                     const char **BlobStart, unsigned *BlobLen){
+                                     StringRef *Blob) {
   if (AbbrevID == bitc::UNABBREV_RECORD) {
     unsigned Code = ReadVBR(6);
     unsigned NumElts = ReadVBR(6);
@@ -208,68 +201,78 @@ unsigned BitstreamCursor::ReadRecord(unsigned AbbrevID,
       Vals.push_back(ReadVBR64(6));
     return Code;
   }
-  
+
   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
-  
-  for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
+
+  // Read the record code first.
+  assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
+  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
+  if (CodeOp.isLiteral())
+    readAbbreviatedLiteral(CodeOp, Vals);
+  else
+    readAbbreviatedField(CodeOp, Vals);
+  unsigned Code = (unsigned)Vals.pop_back_val();
+
+  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
     if (Op.isLiteral()) {
       readAbbreviatedLiteral(Op, Vals);
       continue;
     }
-    
+
     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
       readAbbreviatedField(Op, Vals);
       continue;
     }
-    
+
     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
       // Array case.  Read the number of elements as a vbr6.
       unsigned NumElts = ReadVBR(6);
-      
+
       // Get the element encoding.
       assert(i+2 == e && "array op not second to last?");
       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
-      
+
       // Read all the elements.
       for (; NumElts; --NumElts)
         readAbbreviatedField(EltEnc, Vals);
       continue;
     }
-    
+
     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
     // Blob case.  Read the number of bytes as a vbr6.
     unsigned NumElts = ReadVBR(6);
-    SkipToWord();  // 32-bit alignment
-    
+    SkipToFourByteBoundary();  // 32-bit alignment
+
     // Figure out where the end of this blob will be including tail padding.
-    size_t NewEnd = NextChar+((NumElts+3)&~3);
-    
+    size_t CurBitPos = GetCurrentBitNo();
+    size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
+
     // If this would read off the end of the bitcode file, just set the
     // record to empty and return.
-    if (!canSkipToPos(NewEnd)) {
+    if (!canSkipToPos(NewEnd/8)) {
       Vals.append(NumElts, 0);
       NextChar = BitStream->getBitcodeBytes().getExtent();
       break;
     }
-    
-    // Otherwise, read the number of bytes.  If we can return a reference to
-    // the data, do so to avoid copying it.
-    if (BlobStart) {
-      *BlobStart = (const char*)BitStream->getBitcodeBytes().getPointer(
-                                                            NextChar, NumElts);
-      *BlobLen = NumElts;
+
+    // Otherwise, inform the streamer that we need these bytes in memory.
+    const char *Ptr = (const char*)
+      BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
+
+    // If we can return a reference to the data, do so to avoid copying it.
+    if (Blob) {
+      *Blob = StringRef(Ptr, NumElts);
     } else {
-      for (; NumElts; ++NextChar, --NumElts)
-        Vals.push_back(getByte(NextChar));
+      // Otherwise, unpack into Vals with zero extension.
+      for (; NumElts; --NumElts)
+        Vals.push_back((unsigned char)*Ptr++);
     }
     // Skip over tail padding.
-    NextChar = NewEnd;
+    JumpToBit(NewEnd);
   }
-  
-  unsigned Code = (unsigned)Vals[0];
-  Vals.erase(Vals.begin());
+
   return Code;
 }
 
@@ -283,11 +286,22 @@ void BitstreamCursor::ReadAbbrevRecord() {
       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
       continue;
     }
-    
+
     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
-    if (BitCodeAbbrevOp::hasEncodingData(E))
-      Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
-    else
+    if (BitCodeAbbrevOp::hasEncodingData(E)) {
+      unsigned Data = ReadVBR64(5);
+
+      // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
+      // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
+      // a slow path in Read() to have to handle reading zero bits.
+      if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
+          Data == 0) {
+        Abbv->Add(BitCodeAbbrevOp(0));
+        continue;
+      }
+
+      Abbv->Add(BitCodeAbbrevOp(E, Data));
+    } else
       Abbv->Add(BitCodeAbbrevOp(E));
   }
   CurAbbrevs.push_back(Abbv);
@@ -297,28 +311,32 @@ bool BitstreamCursor::ReadBlockInfoBlock() {
   // If this is the second stream to get to the block info block, skip it.
   if (BitStream->hasBlockInfoRecords())
     return SkipBlock();
-  
+
   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
-  
+
   SmallVector<uint64_t, 64> Record;
-  BitstreamReader::BlockInfo *CurBlockInfo = 0;
-  
+  BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
+
   // Read all the records for this module.
   while (1) {
-    unsigned Code = ReadCode();
-    if (Code == bitc::END_BLOCK)
-      return ReadBlockEnd();
-    if (Code == bitc::ENTER_SUBBLOCK) {
-      ReadSubBlockID();
-      if (SkipBlock()) return true;
-      continue;
+    BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
+
+    switch (Entry.Kind) {
+    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
+    case llvm::BitstreamEntry::Error:
+      return true;
+    case llvm::BitstreamEntry::EndBlock:
+      return false;
+    case llvm::BitstreamEntry::Record:
+      // The interesting case.
+      break;
     }
-    
+
     // Read abbrev records, associate them with CurBID.
-    if (Code == bitc::DEFINE_ABBREV) {
+    if (Entry.ID == bitc::DEFINE_ABBREV) {
       if (!CurBlockInfo) return true;
       ReadAbbrevRecord();
-      
+
       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
       // appropriate BlockInfo.
       BitCodeAbbrev *Abbv = CurAbbrevs.back();
@@ -326,10 +344,10 @@ bool BitstreamCursor::ReadBlockInfoBlock() {
       CurBlockInfo->Abbrevs.push_back(Abbv);
       continue;
     }
-    
+
     // Read a record.
     Record.clear();
-    switch (ReadRecord(Code, Record)) {
+    switch (readRecord(Entry.ID, Record)) {
       default: break;  // Default behavior, ignore unknown content.
       case bitc::BLOCKINFO_CODE_SETBID:
         if (Record.size() < 1) return true;
@@ -358,4 +376,3 @@ bool BitstreamCursor::ReadBlockInfoBlock() {
   }
 }
 
-