LCOV - code coverage report
Current view: top level - lib/Bitcode/Reader - BitstreamReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 172 196 87.8 %
Date: 2017-09-14 15:23:50 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
       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             : #include "llvm/Bitcode/BitstreamReader.h"
      11             : #include "llvm/ADT/StringRef.h"
      12             : #include <cassert>
      13             : #include <string>
      14             : 
      15             : using namespace llvm;
      16             : 
      17             : //===----------------------------------------------------------------------===//
      18             : //  BitstreamCursor implementation
      19             : //===----------------------------------------------------------------------===//
      20             : 
      21             : /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
      22             : /// the block, and return true if the block has an error.
      23       97685 : bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
      24             :   // Save the current block's state on BlockScope.
      25      293056 :   BlockScope.push_back(Block(CurCodeSize));
      26      293058 :   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
      27             : 
      28             :   // Add the abbrevs specific to this block to the CurAbbrevs list.
      29       97686 :   if (BlockInfo) {
      30       41683 :     if (const BitstreamBlockInfo::BlockInfo *Info =
      31       83366 :             BlockInfo->getBlockInfo(BlockID)) {
      32       58656 :       CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
      33       78208 :                         Info->Abbrevs.end());
      34             :     }
      35             :   }
      36             : 
      37             :   // Get the codesize of this block.
      38       97686 :   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
      39             :   // We can't read more than MaxChunkSize at a time
      40       97688 :   if (CurCodeSize > MaxChunkSize)
      41             :     return true;
      42             : 
      43       97688 :   SkipToFourByteBoundary();
      44       97688 :   unsigned NumWords = Read(bitc::BlockSizeWidth);
      45       97688 :   if (NumWordsP) *NumWordsP = NumWords;
      46             : 
      47             :   // Validate that this block is sane.
      48       97688 :   return CurCodeSize == 0 || AtEndOfStream();
      49             : }
      50             : 
      51     1403069 : static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
      52             :                                      const BitCodeAbbrevOp &Op) {
      53             :   assert(!Op.isLiteral() && "Not to be used with literals!");
      54             : 
      55             :   // Decode the value as we are commanded.
      56     1403069 :   switch (Op.getEncoding()) {
      57           0 :   case BitCodeAbbrevOp::Array:
      58             :   case BitCodeAbbrevOp::Blob:
      59           0 :     llvm_unreachable("Should not reach here");
      60      911663 :   case BitCodeAbbrevOp::Fixed:
      61             :     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
      62      911663 :     return Cursor.Read((unsigned)Op.getEncodingData());
      63      491406 :   case BitCodeAbbrevOp::VBR:
      64             :     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
      65      491406 :     return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
      66           0 :   case BitCodeAbbrevOp::Char6:
      67           0 :     return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
      68             :   }
      69           0 :   llvm_unreachable("invalid abbreviation encoding");
      70             : }
      71             : 
      72        3678 : static void skipAbbreviatedField(BitstreamCursor &Cursor,
      73             :                                  const BitCodeAbbrevOp &Op) {
      74             :   assert(!Op.isLiteral() && "Not to be used with literals!");
      75             : 
      76             :   // Decode the value as we are commanded.
      77        3678 :   switch (Op.getEncoding()) {
      78           0 :   case BitCodeAbbrevOp::Array:
      79             :   case BitCodeAbbrevOp::Blob:
      80           0 :     llvm_unreachable("Should not reach here");
      81        1481 :   case BitCodeAbbrevOp::Fixed:
      82             :     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
      83        1481 :     Cursor.Read((unsigned)Op.getEncodingData());
      84        1481 :     break;
      85        2197 :   case BitCodeAbbrevOp::VBR:
      86             :     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
      87        2197 :     Cursor.ReadVBR64((unsigned)Op.getEncodingData());
      88        2197 :     break;
      89           0 :   case BitCodeAbbrevOp::Char6:
      90           0 :     Cursor.Read(6);
      91           0 :     break;
      92             :   }
      93        3678 : }
      94             : 
      95             : /// skipRecord - Read the current record and discard it.
      96       12469 : unsigned BitstreamCursor::skipRecord(unsigned AbbrevID) {
      97             :   // Skip unabbreviated records by reading past their entries.
      98       12469 :   if (AbbrevID == bitc::UNABBREV_RECORD) {
      99        9750 :     unsigned Code = ReadVBR(6);
     100        9750 :     unsigned NumElts = ReadVBR(6);
     101      127962 :     for (unsigned i = 0; i != NumElts; ++i)
     102      118212 :       (void)ReadVBR64(6);
     103             :     return Code;
     104             :   }
     105             : 
     106        2719 :   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
     107        2719 :   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
     108             :   unsigned Code;
     109        2719 :   if (CodeOp.isLiteral())
     110        2719 :     Code = CodeOp.getLiteralValue();
     111             :   else {
     112           0 :     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
     113           0 :         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
     114           0 :       report_fatal_error("Abbreviation starts with an Array or a Blob");
     115           0 :     Code = readAbbreviatedField(*this, CodeOp);
     116             :   }
     117             : 
     118       10954 :   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
     119        5516 :     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
     120        5516 :     if (Op.isLiteral())
     121         484 :       continue;
     122             : 
     123       12637 :     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
     124        3927 :         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
     125        3678 :       skipAbbreviatedField(*this, Op);
     126        3678 :       continue;
     127             :     }
     128             : 
     129        1354 :     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
     130             :       // Array case.  Read the number of elements as a vbr6.
     131        1105 :       unsigned NumElts = ReadVBR(6);
     132             : 
     133             :       // Get the element encoding.
     134             :       assert(i+2 == e && "array op not second to last?");
     135        2210 :       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
     136             : 
     137             :       // Read all the elements.
     138             :       // Decode the value as we are commanded.
     139        1105 :       switch (EltEnc.getEncoding()) {
     140           0 :       default:
     141           0 :         report_fatal_error("Array element type can't be an Array or a Blob");
     142         750 :       case BitCodeAbbrevOp::Fixed:
     143             :         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
     144        1500 :         JumpToBit(GetCurrentBitNo() + NumElts * EltEnc.getEncodingData());
     145             :         break;
     146         297 :       case BitCodeAbbrevOp::VBR:
     147             :         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
     148         444 :         for (; NumElts; --NumElts)
     149         147 :           ReadVBR64((unsigned)EltEnc.getEncodingData());
     150             :         break;
     151         205 :       case BitCodeAbbrevOp::Char6:
     152         410 :         JumpToBit(GetCurrentBitNo() + NumElts * 6);
     153             :         break;
     154             :       }
     155        1105 :       continue;
     156             :     }
     157             : 
     158             :     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
     159             :     // Blob case.  Read the number of bytes as a vbr6.
     160         249 :     unsigned NumElts = ReadVBR(6);
     161         249 :     SkipToFourByteBoundary();  // 32-bit alignment
     162             : 
     163             :     // Figure out where the end of this blob will be including tail padding.
     164         498 :     size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
     165             : 
     166             :     // If this would read off the end of the bitcode file, just set the
     167             :     // record to empty and return.
     168         498 :     if (!canSkipToPos(NewEnd/8)) {
     169           0 :       skipToEnd();
     170             :       break;
     171             :     }
     172             : 
     173             :     // Skip over the blob.
     174         249 :     JumpToBit(NewEnd);
     175             :   }
     176             :   return Code;
     177             : }
     178             : 
     179     1203445 : unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
     180             :                                      SmallVectorImpl<uint64_t> &Vals,
     181             :                                      StringRef *Blob) {
     182     1203445 :   if (AbbrevID == bitc::UNABBREV_RECORD) {
     183      828381 :     unsigned Code = ReadVBR(6);
     184      828380 :     unsigned NumElts = ReadVBR(6);
     185    11416258 :     for (unsigned i = 0; i != NumElts; ++i)
     186    10587877 :       Vals.push_back(ReadVBR64(6));
     187             :     return Code;
     188             :   }
     189             : 
     190      375064 :   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
     191             : 
     192             :   // Read the record code first.
     193             :   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
     194      375067 :   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
     195             :   unsigned Code;
     196      375067 :   if (CodeOp.isLiteral())
     197      375065 :     Code = CodeOp.getLiteralValue();
     198             :   else {
     199           4 :     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
     200           2 :         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
     201           0 :       report_fatal_error("Abbreviation starts with an Array or a Blob");
     202           2 :     Code = readAbbreviatedField(*this, CodeOp);
     203             :   }
     204             : 
     205     2627926 :   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
     206     1877794 :     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
     207     2133143 :     if (Op.isLiteral()) {
     208      255349 :       Vals.push_back(Op.getLiteralValue());
     209      255349 :       continue;
     210             :     }
     211             : 
     212     4579770 :     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
     213     1554258 :         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
     214     1403067 :       Vals.push_back(readAbbreviatedField(*this, Op));
     215     1403067 :       continue;
     216             :     }
     217             : 
     218      219378 :     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
     219             :       // Array case.  Read the number of elements as a vbr6.
     220       68187 :       unsigned NumElts = ReadVBR(6);
     221             : 
     222             :       // Get the element encoding.
     223       68188 :       if (i + 2 != e)
     224           1 :         report_fatal_error("Array op not second to last");
     225      136374 :       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
     226       68187 :       if (!EltEnc.isEncoding())
     227           0 :         report_fatal_error(
     228             :             "Array element type has to be an encoding of a type");
     229             : 
     230             :       // Read all the elements.
     231       68187 :       switch (EltEnc.getEncoding()) {
     232           1 :       default:
     233           1 :         report_fatal_error("Array element type can't be an Array or a Blob");
     234      301390 :       case BitCodeAbbrevOp::Fixed:
     235      588563 :         for (; NumElts; --NumElts)
     236      287173 :           Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
     237             :         break;
     238       93840 :       case BitCodeAbbrevOp::VBR:
     239      170538 :         for (; NumElts; --NumElts)
     240       76698 :           Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
     241             :         break;
     242      369409 :       case BitCodeAbbrevOp::Char6:
     243      701991 :         for (; NumElts; --NumElts)
     244      665164 :           Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
     245             :       }
     246       68185 :       continue;
     247             :     }
     248             : 
     249             :     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
     250             :     // Blob case.  Read the number of bytes as a vbr6.
     251      151191 :     unsigned NumElts = ReadVBR(6);
     252      151191 :     SkipToFourByteBoundary();  // 32-bit alignment
     253             : 
     254             :     // Figure out where the end of this blob will be including tail padding.
     255      302382 :     size_t CurBitPos = GetCurrentBitNo();
     256      151191 :     size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
     257             : 
     258             :     // If this would read off the end of the bitcode file, just set the
     259             :     // record to empty and return.
     260      302382 :     if (!canSkipToPos(NewEnd/8)) {
     261           0 :       Vals.append(NumElts, 0);
     262           0 :       skipToEnd();
     263             :       break;
     264             :     }
     265             : 
     266             :     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
     267             :     // over tail padding first, in case jumping to NewEnd invalidates the Blob
     268             :     // pointer.
     269      302382 :     JumpToBit(NewEnd);
     270      302382 :     const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
     271             : 
     272             :     // If we can return a reference to the data, do so to avoid copying it.
     273      151191 :     if (Blob) {
     274      151191 :       *Blob = StringRef(Ptr, NumElts);
     275             :     } else {
     276             :       // Otherwise, unpack into Vals with zero extension.
     277           0 :       for (; NumElts; --NumElts)
     278           0 :         Vals.push_back((unsigned char)*Ptr++);
     279             :     }
     280             :   }
     281             : 
     282             :   return Code;
     283             : }
     284             : 
     285      341087 : void BitstreamCursor::ReadAbbrevRecord() {
     286      341082 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
     287      341088 :   unsigned NumOpInfo = ReadVBR(5);
     288     2771371 :   for (unsigned i = 0; i != NumOpInfo; ++i) {
     289     2430287 :     bool IsLiteral = Read(1);
     290     3321618 :     if (IsLiteral) {
     291     2673993 :       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
     292      891330 :       continue;
     293             :     }
     294             : 
     295     1538957 :     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
     296     1538956 :     if (BitCodeAbbrevOp::hasEncodingData(E)) {
     297     1279576 :       uint64_t Data = ReadVBR64(5);
     298             : 
     299             :       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
     300             :       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
     301             :       // a slow path in Read() to have to handle reading zero bits.
     302     2559636 :       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
     303             :           Data == 0) {
     304        1455 :         Abbv->Add(BitCodeAbbrevOp(0));
     305         485 :         continue;
     306             :       }
     307             : 
     308     1279090 :       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
     309             :           Data > MaxChunkSize)
     310           2 :         report_fatal_error(
     311             :             "Fixed or VBR abbrev record with size > MaxChunkData");
     312             : 
     313     3837264 :       Abbv->Add(BitCodeAbbrevOp(E, Data));
     314             :     } else
     315      778140 :       Abbv->Add(BitCodeAbbrevOp(E));
     316             :   }
     317             : 
     318      682164 :   if (Abbv->getNumOperandInfos() == 0)
     319           1 :     report_fatal_error("Abbrev record with no operands");
     320      682163 :   CurAbbrevs.push_back(std::move(Abbv));
     321      341082 : }
     322             : 
     323             : Optional<BitstreamBlockInfo>
     324        3308 : BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
     325        3308 :   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return None;
     326             : 
     327        3309 :   BitstreamBlockInfo NewBlockInfo;
     328             : 
     329        6615 :   SmallVector<uint64_t, 64> Record;
     330        3309 :   BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
     331             : 
     332             :   // Read all the records for this module.
     333             :   while (true) {
     334       67407 :     BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
     335             : 
     336       67407 :     switch (Entry.Kind) {
     337           0 :     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
     338             :     case llvm::BitstreamEntry::Error:
     339        3306 :       return None;
     340        3306 :     case llvm::BitstreamEntry::EndBlock:
     341        3306 :       return std::move(NewBlockInfo);
     342             :     case llvm::BitstreamEntry::Record:
     343             :       // The interesting case.
     344             :       break;
     345             :     }
     346             : 
     347             :     // Read abbrev records, associate them with CurBID.
     348      116688 :     if (Entry.ID == bitc::DEFINE_ABBREV) {
     349       52590 :       if (!CurBlockInfo) return None;
     350       52590 :       ReadAbbrevRecord();
     351             : 
     352             :       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
     353             :       // appropriate BlockInfo.
     354      157761 :       CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
     355       52587 :       CurAbbrevs.pop_back();
     356       52587 :       continue;
     357             :     }
     358             : 
     359             :     // Read a record.
     360       11511 :     Record.clear();
     361       11511 :     switch (readRecord(Entry.ID, Record)) {
     362             :       default: break;  // Default behavior, ignore unknown content.
     363        9972 :       case bitc::BLOCKINFO_CODE_SETBID:
     364        9972 :         if (Record.size() < 1) return None;
     365        9972 :         CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
     366        9972 :         break;
     367          82 :       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
     368          82 :         if (!CurBlockInfo) return None;
     369          82 :         if (!ReadBlockInfoNames)
     370             :           break; // Ignore name.
     371          52 :         std::string Name;
     372         904 :         for (unsigned i = 0, e = Record.size(); i != e; ++i)
     373        2400 :           Name += (char)Record[i];
     374         104 :         CurBlockInfo->Name = Name;
     375             :         break;
     376             :       }
     377        1457 :       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
     378        1457 :         if (!CurBlockInfo) return None;
     379        1457 :         if (!ReadBlockInfoNames)
     380             :           break; // Ignore name.
     381        1352 :         std::string Name;
     382       28412 :         for (unsigned i = 1, e = Record.size(); i != e; ++i)
     383       77124 :           Name += (char)Record[i];
     384        6760 :         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
     385             :                                                            Name));
     386             :         break;
     387             :       }
     388             :     }
     389             :   }
     390             : }

Generated by: LCOV version 1.13