LLVM API Documentation
00001 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/Bitcode/BitstreamReader.h" 00011 00012 using namespace llvm; 00013 00014 //===----------------------------------------------------------------------===// 00015 // BitstreamCursor implementation 00016 //===----------------------------------------------------------------------===// 00017 00018 void BitstreamCursor::freeState() { 00019 // Free all the Abbrevs. 00020 CurAbbrevs.clear(); 00021 00022 // Free all the Abbrevs in the block scope. 00023 BlockScope.clear(); 00024 } 00025 00026 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter 00027 /// the block, and return true if the block has an error. 00028 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { 00029 // Save the current block's state on BlockScope. 00030 BlockScope.push_back(Block(CurCodeSize)); 00031 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); 00032 00033 // Add the abbrevs specific to this block to the CurAbbrevs list. 00034 if (const BitstreamReader::BlockInfo *Info = 00035 BitStream->getBlockInfo(BlockID)) { 00036 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(), 00037 Info->Abbrevs.end()); 00038 } 00039 00040 // Get the codesize of this block. 00041 CurCodeSize = ReadVBR(bitc::CodeLenWidth); 00042 SkipToFourByteBoundary(); 00043 unsigned NumWords = Read(bitc::BlockSizeWidth); 00044 if (NumWordsP) *NumWordsP = NumWords; 00045 00046 // Validate that this block is sane. 00047 if (CurCodeSize == 0 || AtEndOfStream()) 00048 return true; 00049 00050 return false; 00051 } 00052 00053 void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op, 00054 SmallVectorImpl<uint64_t> &Vals) { 00055 assert(Op.isLiteral() && "Not a literal"); 00056 // If the abbrev specifies the literal value to use, use it. 00057 Vals.push_back(Op.getLiteralValue()); 00058 } 00059 00060 void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op, 00061 SmallVectorImpl<uint64_t> &Vals) { 00062 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!"); 00063 00064 // Decode the value as we are commanded. 00065 switch (Op.getEncoding()) { 00066 case BitCodeAbbrevOp::Array: 00067 case BitCodeAbbrevOp::Blob: 00068 llvm_unreachable("Should not reach here"); 00069 case BitCodeAbbrevOp::Fixed: 00070 Vals.push_back(Read((unsigned)Op.getEncodingData())); 00071 break; 00072 case BitCodeAbbrevOp::VBR: 00073 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData())); 00074 break; 00075 case BitCodeAbbrevOp::Char6: 00076 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6))); 00077 break; 00078 } 00079 } 00080 00081 void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) { 00082 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!"); 00083 00084 // Decode the value as we are commanded. 00085 switch (Op.getEncoding()) { 00086 case BitCodeAbbrevOp::Array: 00087 case BitCodeAbbrevOp::Blob: 00088 llvm_unreachable("Should not reach here"); 00089 case BitCodeAbbrevOp::Fixed: 00090 (void)Read((unsigned)Op.getEncodingData()); 00091 break; 00092 case BitCodeAbbrevOp::VBR: 00093 (void)ReadVBR64((unsigned)Op.getEncodingData()); 00094 break; 00095 case BitCodeAbbrevOp::Char6: 00096 (void)Read(6); 00097 break; 00098 } 00099 } 00100 00101 00102 00103 /// skipRecord - Read the current record and discard it. 00104 void BitstreamCursor::skipRecord(unsigned AbbrevID) { 00105 // Skip unabbreviated records by reading past their entries. 00106 if (AbbrevID == bitc::UNABBREV_RECORD) { 00107 unsigned Code = ReadVBR(6); 00108 (void)Code; 00109 unsigned NumElts = ReadVBR(6); 00110 for (unsigned i = 0; i != NumElts; ++i) 00111 (void)ReadVBR64(6); 00112 return; 00113 } 00114 00115 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 00116 00117 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) { 00118 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); 00119 if (Op.isLiteral()) 00120 continue; 00121 00122 if (Op.getEncoding() != BitCodeAbbrevOp::Array && 00123 Op.getEncoding() != BitCodeAbbrevOp::Blob) { 00124 skipAbbreviatedField(Op); 00125 continue; 00126 } 00127 00128 if (Op.getEncoding() == BitCodeAbbrevOp::Array) { 00129 // Array case. Read the number of elements as a vbr6. 00130 unsigned NumElts = ReadVBR(6); 00131 00132 // Get the element encoding. 00133 assert(i+2 == e && "array op not second to last?"); 00134 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); 00135 00136 // Read all the elements. 00137 for (; NumElts; --NumElts) 00138 skipAbbreviatedField(EltEnc); 00139 continue; 00140 } 00141 00142 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob); 00143 // Blob case. Read the number of bytes as a vbr6. 00144 unsigned NumElts = ReadVBR(6); 00145 SkipToFourByteBoundary(); // 32-bit alignment 00146 00147 // Figure out where the end of this blob will be including tail padding. 00148 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8; 00149 00150 // If this would read off the end of the bitcode file, just set the 00151 // record to empty and return. 00152 if (!canSkipToPos(NewEnd/8)) { 00153 NextChar = BitStream->getBitcodeBytes().getExtent(); 00154 break; 00155 } 00156 00157 // Skip over the blob. 00158 JumpToBit(NewEnd); 00159 } 00160 } 00161 00162 unsigned BitstreamCursor::readRecord(unsigned AbbrevID, 00163 SmallVectorImpl<uint64_t> &Vals, 00164 StringRef *Blob) { 00165 if (AbbrevID == bitc::UNABBREV_RECORD) { 00166 unsigned Code = ReadVBR(6); 00167 unsigned NumElts = ReadVBR(6); 00168 for (unsigned i = 0; i != NumElts; ++i) 00169 Vals.push_back(ReadVBR64(6)); 00170 return Code; 00171 } 00172 00173 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 00174 00175 // Read the record code first. 00176 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?"); 00177 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0); 00178 if (CodeOp.isLiteral()) 00179 readAbbreviatedLiteral(CodeOp, Vals); 00180 else 00181 readAbbreviatedField(CodeOp, Vals); 00182 unsigned Code = (unsigned)Vals.pop_back_val(); 00183 00184 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { 00185 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); 00186 if (Op.isLiteral()) { 00187 readAbbreviatedLiteral(Op, Vals); 00188 continue; 00189 } 00190 00191 if (Op.getEncoding() != BitCodeAbbrevOp::Array && 00192 Op.getEncoding() != BitCodeAbbrevOp::Blob) { 00193 readAbbreviatedField(Op, Vals); 00194 continue; 00195 } 00196 00197 if (Op.getEncoding() == BitCodeAbbrevOp::Array) { 00198 // Array case. Read the number of elements as a vbr6. 00199 unsigned NumElts = ReadVBR(6); 00200 00201 // Get the element encoding. 00202 assert(i+2 == e && "array op not second to last?"); 00203 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); 00204 00205 // Read all the elements. 00206 for (; NumElts; --NumElts) 00207 readAbbreviatedField(EltEnc, Vals); 00208 continue; 00209 } 00210 00211 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob); 00212 // Blob case. Read the number of bytes as a vbr6. 00213 unsigned NumElts = ReadVBR(6); 00214 SkipToFourByteBoundary(); // 32-bit alignment 00215 00216 // Figure out where the end of this blob will be including tail padding. 00217 size_t CurBitPos = GetCurrentBitNo(); 00218 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8; 00219 00220 // If this would read off the end of the bitcode file, just set the 00221 // record to empty and return. 00222 if (!canSkipToPos(NewEnd/8)) { 00223 Vals.append(NumElts, 0); 00224 NextChar = BitStream->getBitcodeBytes().getExtent(); 00225 break; 00226 } 00227 00228 // Otherwise, inform the streamer that we need these bytes in memory. 00229 const char *Ptr = (const char*) 00230 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts); 00231 00232 // If we can return a reference to the data, do so to avoid copying it. 00233 if (Blob) { 00234 *Blob = StringRef(Ptr, NumElts); 00235 } else { 00236 // Otherwise, unpack into Vals with zero extension. 00237 for (; NumElts; --NumElts) 00238 Vals.push_back((unsigned char)*Ptr++); 00239 } 00240 // Skip over tail padding. 00241 JumpToBit(NewEnd); 00242 } 00243 00244 return Code; 00245 } 00246 00247 00248 void BitstreamCursor::ReadAbbrevRecord() { 00249 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 00250 unsigned NumOpInfo = ReadVBR(5); 00251 for (unsigned i = 0; i != NumOpInfo; ++i) { 00252 bool IsLiteral = Read(1) ? true : false; 00253 if (IsLiteral) { 00254 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8))); 00255 continue; 00256 } 00257 00258 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3); 00259 if (BitCodeAbbrevOp::hasEncodingData(E)) { 00260 unsigned Data = ReadVBR64(5); 00261 00262 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) 00263 // and vbr(0) as a literal zero. This is decoded the same way, and avoids 00264 // a slow path in Read() to have to handle reading zero bits. 00265 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) && 00266 Data == 0) { 00267 Abbv->Add(BitCodeAbbrevOp(0)); 00268 continue; 00269 } 00270 00271 Abbv->Add(BitCodeAbbrevOp(E, Data)); 00272 } else 00273 Abbv->Add(BitCodeAbbrevOp(E)); 00274 } 00275 CurAbbrevs.push_back(Abbv); 00276 } 00277 00278 bool BitstreamCursor::ReadBlockInfoBlock() { 00279 // If this is the second stream to get to the block info block, skip it. 00280 if (BitStream->hasBlockInfoRecords()) 00281 return SkipBlock(); 00282 00283 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true; 00284 00285 SmallVector<uint64_t, 64> Record; 00286 BitstreamReader::BlockInfo *CurBlockInfo = nullptr; 00287 00288 // Read all the records for this module. 00289 while (1) { 00290 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs); 00291 00292 switch (Entry.Kind) { 00293 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 00294 case llvm::BitstreamEntry::Error: 00295 return true; 00296 case llvm::BitstreamEntry::EndBlock: 00297 return false; 00298 case llvm::BitstreamEntry::Record: 00299 // The interesting case. 00300 break; 00301 } 00302 00303 // Read abbrev records, associate them with CurBID. 00304 if (Entry.ID == bitc::DEFINE_ABBREV) { 00305 if (!CurBlockInfo) return true; 00306 ReadAbbrevRecord(); 00307 00308 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the 00309 // appropriate BlockInfo. 00310 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back())); 00311 CurAbbrevs.pop_back(); 00312 continue; 00313 } 00314 00315 // Read a record. 00316 Record.clear(); 00317 switch (readRecord(Entry.ID, Record)) { 00318 default: break; // Default behavior, ignore unknown content. 00319 case bitc::BLOCKINFO_CODE_SETBID: 00320 if (Record.size() < 1) return true; 00321 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); 00322 break; 00323 case bitc::BLOCKINFO_CODE_BLOCKNAME: { 00324 if (!CurBlockInfo) return true; 00325 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name. 00326 std::string Name; 00327 for (unsigned i = 0, e = Record.size(); i != e; ++i) 00328 Name += (char)Record[i]; 00329 CurBlockInfo->Name = Name; 00330 break; 00331 } 00332 case bitc::BLOCKINFO_CODE_SETRECORDNAME: { 00333 if (!CurBlockInfo) return true; 00334 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name. 00335 std::string Name; 00336 for (unsigned i = 1, e = Record.size(); i != e; ++i) 00337 Name += (char)Record[i]; 00338 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0], 00339 Name)); 00340 break; 00341 } 00342 } 00343 } 00344 } 00345