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 = 35139f7f9bSDimitry Andric BitStream->getBlockInfo(BlockID)) { 36*39d628a0SDimitry Andric CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(), 37*39d628a0SDimitry Andric Info->Abbrevs.end()); 38139f7f9bSDimitry Andric } 39139f7f9bSDimitry Andric 40139f7f9bSDimitry Andric // Get the codesize of this block. 41139f7f9bSDimitry Andric CurCodeSize = ReadVBR(bitc::CodeLenWidth); 42139f7f9bSDimitry Andric SkipToFourByteBoundary(); 43139f7f9bSDimitry Andric unsigned NumWords = Read(bitc::BlockSizeWidth); 44139f7f9bSDimitry Andric if (NumWordsP) *NumWordsP = NumWords; 45139f7f9bSDimitry Andric 46139f7f9bSDimitry Andric // Validate that this block is sane. 47139f7f9bSDimitry Andric if (CurCodeSize == 0 || AtEndOfStream()) 48139f7f9bSDimitry Andric return true; 49139f7f9bSDimitry Andric 50139f7f9bSDimitry Andric return false; 51139f7f9bSDimitry Andric } 52139f7f9bSDimitry Andric 53*39d628a0SDimitry Andric static uint64_t readAbbreviatedField(BitstreamCursor &Cursor, 54*39d628a0SDimitry Andric const BitCodeAbbrevOp &Op) { 55*39d628a0SDimitry Andric assert(!Op.isLiteral() && "Not to be used with literals!"); 56139f7f9bSDimitry Andric 57139f7f9bSDimitry Andric // Decode the value as we are commanded. 58139f7f9bSDimitry Andric switch (Op.getEncoding()) { 59139f7f9bSDimitry Andric case BitCodeAbbrevOp::Array: 60139f7f9bSDimitry Andric case BitCodeAbbrevOp::Blob: 6191bc56edSDimitry Andric llvm_unreachable("Should not reach here"); 62139f7f9bSDimitry Andric case BitCodeAbbrevOp::Fixed: 63*39d628a0SDimitry Andric return Cursor.Read((unsigned)Op.getEncodingData()); 64139f7f9bSDimitry Andric case BitCodeAbbrevOp::VBR: 65*39d628a0SDimitry Andric return Cursor.ReadVBR64((unsigned)Op.getEncodingData()); 66139f7f9bSDimitry Andric case BitCodeAbbrevOp::Char6: 67*39d628a0SDimitry Andric return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6)); 68139f7f9bSDimitry Andric } 69*39d628a0SDimitry Andric llvm_unreachable("invalid abbreviation encoding"); 70139f7f9bSDimitry Andric } 71139f7f9bSDimitry Andric 72*39d628a0SDimitry Andric static void skipAbbreviatedField(BitstreamCursor &Cursor, 73*39d628a0SDimitry Andric const BitCodeAbbrevOp &Op) { 74*39d628a0SDimitry Andric assert(!Op.isLiteral() && "Not to be used with literals!"); 75139f7f9bSDimitry Andric 76139f7f9bSDimitry Andric // Decode the value as we are commanded. 77139f7f9bSDimitry Andric switch (Op.getEncoding()) { 78139f7f9bSDimitry Andric case BitCodeAbbrevOp::Array: 79139f7f9bSDimitry Andric case BitCodeAbbrevOp::Blob: 8091bc56edSDimitry Andric llvm_unreachable("Should not reach here"); 81139f7f9bSDimitry Andric case BitCodeAbbrevOp::Fixed: 82*39d628a0SDimitry Andric Cursor.Read((unsigned)Op.getEncodingData()); 83139f7f9bSDimitry Andric break; 84139f7f9bSDimitry Andric case BitCodeAbbrevOp::VBR: 85*39d628a0SDimitry Andric Cursor.ReadVBR64((unsigned)Op.getEncodingData()); 86139f7f9bSDimitry Andric break; 87139f7f9bSDimitry Andric case BitCodeAbbrevOp::Char6: 88*39d628a0SDimitry Andric Cursor.Read(6); 89139f7f9bSDimitry Andric break; 90139f7f9bSDimitry Andric } 91139f7f9bSDimitry Andric } 92139f7f9bSDimitry Andric 93139f7f9bSDimitry Andric 94139f7f9bSDimitry Andric 95139f7f9bSDimitry Andric /// skipRecord - Read the current record and discard it. 96139f7f9bSDimitry Andric void BitstreamCursor::skipRecord(unsigned AbbrevID) { 97139f7f9bSDimitry Andric // Skip unabbreviated records by reading past their entries. 98139f7f9bSDimitry Andric if (AbbrevID == bitc::UNABBREV_RECORD) { 99139f7f9bSDimitry Andric unsigned Code = ReadVBR(6); 100139f7f9bSDimitry Andric (void)Code; 101139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 102139f7f9bSDimitry Andric for (unsigned i = 0; i != NumElts; ++i) 103139f7f9bSDimitry Andric (void)ReadVBR64(6); 104139f7f9bSDimitry Andric return; 105139f7f9bSDimitry Andric } 106139f7f9bSDimitry Andric 107139f7f9bSDimitry Andric const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 108139f7f9bSDimitry Andric 109139f7f9bSDimitry Andric for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) { 110139f7f9bSDimitry Andric const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); 111139f7f9bSDimitry Andric if (Op.isLiteral()) 112139f7f9bSDimitry Andric continue; 113139f7f9bSDimitry Andric 114139f7f9bSDimitry Andric if (Op.getEncoding() != BitCodeAbbrevOp::Array && 115139f7f9bSDimitry Andric Op.getEncoding() != BitCodeAbbrevOp::Blob) { 116*39d628a0SDimitry Andric skipAbbreviatedField(*this, Op); 117139f7f9bSDimitry Andric continue; 118139f7f9bSDimitry Andric } 119139f7f9bSDimitry Andric 120139f7f9bSDimitry Andric if (Op.getEncoding() == BitCodeAbbrevOp::Array) { 121139f7f9bSDimitry Andric // Array case. Read the number of elements as a vbr6. 122139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 123139f7f9bSDimitry Andric 124139f7f9bSDimitry Andric // Get the element encoding. 125139f7f9bSDimitry Andric assert(i+2 == e && "array op not second to last?"); 126139f7f9bSDimitry Andric const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); 127139f7f9bSDimitry Andric 128139f7f9bSDimitry Andric // Read all the elements. 129139f7f9bSDimitry Andric for (; NumElts; --NumElts) 130*39d628a0SDimitry Andric skipAbbreviatedField(*this, EltEnc); 131139f7f9bSDimitry Andric continue; 132139f7f9bSDimitry Andric } 133139f7f9bSDimitry Andric 134139f7f9bSDimitry Andric assert(Op.getEncoding() == BitCodeAbbrevOp::Blob); 135139f7f9bSDimitry Andric // Blob case. Read the number of bytes as a vbr6. 136139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 137139f7f9bSDimitry Andric SkipToFourByteBoundary(); // 32-bit alignment 138139f7f9bSDimitry Andric 139139f7f9bSDimitry Andric // Figure out where the end of this blob will be including tail padding. 140139f7f9bSDimitry Andric size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8; 141139f7f9bSDimitry Andric 142139f7f9bSDimitry Andric // If this would read off the end of the bitcode file, just set the 143139f7f9bSDimitry Andric // record to empty and return. 144139f7f9bSDimitry Andric if (!canSkipToPos(NewEnd/8)) { 145139f7f9bSDimitry Andric NextChar = BitStream->getBitcodeBytes().getExtent(); 146139f7f9bSDimitry Andric break; 147139f7f9bSDimitry Andric } 148139f7f9bSDimitry Andric 149139f7f9bSDimitry Andric // Skip over the blob. 150139f7f9bSDimitry Andric JumpToBit(NewEnd); 151139f7f9bSDimitry Andric } 152139f7f9bSDimitry Andric } 153139f7f9bSDimitry Andric 154139f7f9bSDimitry Andric unsigned BitstreamCursor::readRecord(unsigned AbbrevID, 155139f7f9bSDimitry Andric SmallVectorImpl<uint64_t> &Vals, 156139f7f9bSDimitry Andric StringRef *Blob) { 157139f7f9bSDimitry Andric if (AbbrevID == bitc::UNABBREV_RECORD) { 158139f7f9bSDimitry Andric unsigned Code = ReadVBR(6); 159139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 160139f7f9bSDimitry Andric for (unsigned i = 0; i != NumElts; ++i) 161139f7f9bSDimitry Andric Vals.push_back(ReadVBR64(6)); 162139f7f9bSDimitry Andric return Code; 163139f7f9bSDimitry Andric } 164139f7f9bSDimitry Andric 165139f7f9bSDimitry Andric const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 166139f7f9bSDimitry Andric 167f785676fSDimitry Andric // Read the record code first. 168f785676fSDimitry Andric assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?"); 169f785676fSDimitry Andric const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0); 170*39d628a0SDimitry Andric unsigned Code; 171f785676fSDimitry Andric if (CodeOp.isLiteral()) 172*39d628a0SDimitry Andric Code = CodeOp.getLiteralValue(); 173f785676fSDimitry Andric else 174*39d628a0SDimitry Andric Code = readAbbreviatedField(*this, CodeOp); 175f785676fSDimitry Andric 176f785676fSDimitry Andric for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { 177139f7f9bSDimitry Andric const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); 178139f7f9bSDimitry Andric if (Op.isLiteral()) { 179*39d628a0SDimitry Andric Vals.push_back(Op.getLiteralValue()); 180139f7f9bSDimitry Andric continue; 181139f7f9bSDimitry Andric } 182139f7f9bSDimitry Andric 183139f7f9bSDimitry Andric if (Op.getEncoding() != BitCodeAbbrevOp::Array && 184139f7f9bSDimitry Andric Op.getEncoding() != BitCodeAbbrevOp::Blob) { 185*39d628a0SDimitry Andric Vals.push_back(readAbbreviatedField(*this, Op)); 186139f7f9bSDimitry Andric continue; 187139f7f9bSDimitry Andric } 188139f7f9bSDimitry Andric 189139f7f9bSDimitry Andric if (Op.getEncoding() == BitCodeAbbrevOp::Array) { 190139f7f9bSDimitry Andric // Array case. Read the number of elements as a vbr6. 191139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 192139f7f9bSDimitry Andric 193139f7f9bSDimitry Andric // Get the element encoding. 194139f7f9bSDimitry Andric assert(i+2 == e && "array op not second to last?"); 195139f7f9bSDimitry Andric const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); 196139f7f9bSDimitry Andric 197139f7f9bSDimitry Andric // Read all the elements. 198139f7f9bSDimitry Andric for (; NumElts; --NumElts) 199*39d628a0SDimitry Andric Vals.push_back(readAbbreviatedField(*this, EltEnc)); 200139f7f9bSDimitry Andric continue; 201139f7f9bSDimitry Andric } 202139f7f9bSDimitry Andric 203139f7f9bSDimitry Andric assert(Op.getEncoding() == BitCodeAbbrevOp::Blob); 204139f7f9bSDimitry Andric // Blob case. Read the number of bytes as a vbr6. 205139f7f9bSDimitry Andric unsigned NumElts = ReadVBR(6); 206139f7f9bSDimitry Andric SkipToFourByteBoundary(); // 32-bit alignment 207139f7f9bSDimitry Andric 208139f7f9bSDimitry Andric // Figure out where the end of this blob will be including tail padding. 209139f7f9bSDimitry Andric size_t CurBitPos = GetCurrentBitNo(); 210139f7f9bSDimitry Andric size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8; 211139f7f9bSDimitry Andric 212139f7f9bSDimitry Andric // If this would read off the end of the bitcode file, just set the 213139f7f9bSDimitry Andric // record to empty and return. 214139f7f9bSDimitry Andric if (!canSkipToPos(NewEnd/8)) { 215139f7f9bSDimitry Andric Vals.append(NumElts, 0); 216139f7f9bSDimitry Andric NextChar = BitStream->getBitcodeBytes().getExtent(); 217139f7f9bSDimitry Andric break; 218139f7f9bSDimitry Andric } 219139f7f9bSDimitry Andric 220139f7f9bSDimitry Andric // Otherwise, inform the streamer that we need these bytes in memory. 221139f7f9bSDimitry Andric const char *Ptr = (const char*) 222139f7f9bSDimitry Andric BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts); 223139f7f9bSDimitry Andric 224139f7f9bSDimitry Andric // If we can return a reference to the data, do so to avoid copying it. 225139f7f9bSDimitry Andric if (Blob) { 226139f7f9bSDimitry Andric *Blob = StringRef(Ptr, NumElts); 227139f7f9bSDimitry Andric } else { 228139f7f9bSDimitry Andric // Otherwise, unpack into Vals with zero extension. 229139f7f9bSDimitry Andric for (; NumElts; --NumElts) 230139f7f9bSDimitry Andric Vals.push_back((unsigned char)*Ptr++); 231139f7f9bSDimitry Andric } 232139f7f9bSDimitry Andric // Skip over tail padding. 233139f7f9bSDimitry Andric JumpToBit(NewEnd); 234139f7f9bSDimitry Andric } 235139f7f9bSDimitry Andric 236139f7f9bSDimitry Andric return Code; 237139f7f9bSDimitry Andric } 238139f7f9bSDimitry Andric 239139f7f9bSDimitry Andric 240139f7f9bSDimitry Andric void BitstreamCursor::ReadAbbrevRecord() { 241139f7f9bSDimitry Andric BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 242139f7f9bSDimitry Andric unsigned NumOpInfo = ReadVBR(5); 243139f7f9bSDimitry Andric for (unsigned i = 0; i != NumOpInfo; ++i) { 244139f7f9bSDimitry Andric bool IsLiteral = Read(1) ? true : false; 245139f7f9bSDimitry Andric if (IsLiteral) { 246139f7f9bSDimitry Andric Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8))); 247139f7f9bSDimitry Andric continue; 248139f7f9bSDimitry Andric } 249139f7f9bSDimitry Andric 250139f7f9bSDimitry Andric BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3); 251139f7f9bSDimitry Andric if (BitCodeAbbrevOp::hasEncodingData(E)) { 252139f7f9bSDimitry Andric unsigned Data = ReadVBR64(5); 253139f7f9bSDimitry Andric 254139f7f9bSDimitry Andric // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) 255139f7f9bSDimitry Andric // and vbr(0) as a literal zero. This is decoded the same way, and avoids 256139f7f9bSDimitry Andric // a slow path in Read() to have to handle reading zero bits. 257139f7f9bSDimitry Andric if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) && 258139f7f9bSDimitry Andric Data == 0) { 259139f7f9bSDimitry Andric Abbv->Add(BitCodeAbbrevOp(0)); 260139f7f9bSDimitry Andric continue; 261139f7f9bSDimitry Andric } 262139f7f9bSDimitry Andric 263139f7f9bSDimitry Andric Abbv->Add(BitCodeAbbrevOp(E, Data)); 264139f7f9bSDimitry Andric } else 265139f7f9bSDimitry Andric Abbv->Add(BitCodeAbbrevOp(E)); 266139f7f9bSDimitry Andric } 267139f7f9bSDimitry Andric CurAbbrevs.push_back(Abbv); 268139f7f9bSDimitry Andric } 269139f7f9bSDimitry Andric 270139f7f9bSDimitry Andric bool BitstreamCursor::ReadBlockInfoBlock() { 271139f7f9bSDimitry Andric // If this is the second stream to get to the block info block, skip it. 272139f7f9bSDimitry Andric if (BitStream->hasBlockInfoRecords()) 273139f7f9bSDimitry Andric return SkipBlock(); 274139f7f9bSDimitry Andric 275139f7f9bSDimitry Andric if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true; 276139f7f9bSDimitry Andric 277139f7f9bSDimitry Andric SmallVector<uint64_t, 64> Record; 27891bc56edSDimitry Andric BitstreamReader::BlockInfo *CurBlockInfo = nullptr; 279139f7f9bSDimitry Andric 280139f7f9bSDimitry Andric // Read all the records for this module. 281139f7f9bSDimitry Andric while (1) { 282139f7f9bSDimitry Andric BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs); 283139f7f9bSDimitry Andric 284139f7f9bSDimitry Andric switch (Entry.Kind) { 285139f7f9bSDimitry Andric case llvm::BitstreamEntry::SubBlock: // Handled for us already. 286139f7f9bSDimitry Andric case llvm::BitstreamEntry::Error: 287139f7f9bSDimitry Andric return true; 288139f7f9bSDimitry Andric case llvm::BitstreamEntry::EndBlock: 289139f7f9bSDimitry Andric return false; 290139f7f9bSDimitry Andric case llvm::BitstreamEntry::Record: 291139f7f9bSDimitry Andric // The interesting case. 292139f7f9bSDimitry Andric break; 293139f7f9bSDimitry Andric } 294139f7f9bSDimitry Andric 295139f7f9bSDimitry Andric // Read abbrev records, associate them with CurBID. 296139f7f9bSDimitry Andric if (Entry.ID == bitc::DEFINE_ABBREV) { 297139f7f9bSDimitry Andric if (!CurBlockInfo) return true; 298139f7f9bSDimitry Andric ReadAbbrevRecord(); 299139f7f9bSDimitry Andric 300139f7f9bSDimitry Andric // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the 301139f7f9bSDimitry Andric // appropriate BlockInfo. 302*39d628a0SDimitry Andric CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back())); 303139f7f9bSDimitry Andric CurAbbrevs.pop_back(); 304139f7f9bSDimitry Andric continue; 305139f7f9bSDimitry Andric } 306139f7f9bSDimitry Andric 307139f7f9bSDimitry Andric // Read a record. 308139f7f9bSDimitry Andric Record.clear(); 309139f7f9bSDimitry Andric switch (readRecord(Entry.ID, Record)) { 310139f7f9bSDimitry Andric default: break; // Default behavior, ignore unknown content. 311139f7f9bSDimitry Andric case bitc::BLOCKINFO_CODE_SETBID: 312139f7f9bSDimitry Andric if (Record.size() < 1) return true; 313139f7f9bSDimitry Andric CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); 314139f7f9bSDimitry Andric break; 315139f7f9bSDimitry Andric case bitc::BLOCKINFO_CODE_BLOCKNAME: { 316139f7f9bSDimitry Andric if (!CurBlockInfo) return true; 317139f7f9bSDimitry Andric if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name. 318139f7f9bSDimitry Andric std::string Name; 319139f7f9bSDimitry Andric for (unsigned i = 0, e = Record.size(); i != e; ++i) 320139f7f9bSDimitry Andric Name += (char)Record[i]; 321139f7f9bSDimitry Andric CurBlockInfo->Name = Name; 322139f7f9bSDimitry Andric break; 323139f7f9bSDimitry Andric } 324139f7f9bSDimitry Andric case bitc::BLOCKINFO_CODE_SETRECORDNAME: { 325139f7f9bSDimitry Andric if (!CurBlockInfo) return true; 326139f7f9bSDimitry Andric if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name. 327139f7f9bSDimitry Andric std::string Name; 328139f7f9bSDimitry Andric for (unsigned i = 1, e = Record.size(); i != e; ++i) 329139f7f9bSDimitry Andric Name += (char)Record[i]; 330139f7f9bSDimitry Andric CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0], 331139f7f9bSDimitry Andric Name)); 332139f7f9bSDimitry Andric break; 333139f7f9bSDimitry Andric } 334139f7f9bSDimitry Andric } 335139f7f9bSDimitry Andric } 336139f7f9bSDimitry Andric } 337139f7f9bSDimitry Andric 338