1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Bitcode/BitstreamReader.h"
14 //===----------------------------------------------------------------------===//
15 // BitstreamCursor implementation
16 //===----------------------------------------------------------------------===//
18 void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
21 BitStream = RHS.BitStream;
22 NextChar = RHS.NextChar;
23 CurWord = RHS.CurWord;
24 BitsInCurWord = RHS.BitsInCurWord;
25 CurCodeSize = RHS.CurCodeSize;
27 // Copy abbreviations, and bump ref counts.
28 CurAbbrevs = RHS.CurAbbrevs;
29 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
30 CurAbbrevs[i]->addRef();
32 // Copy block scope and bump ref counts.
33 BlockScope = RHS.BlockScope;
34 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
35 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
36 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
41 void BitstreamCursor::freeState() {
42 // Free all the Abbrevs.
43 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
44 CurAbbrevs[i]->dropRef();
47 // Free all the Abbrevs in the block scope.
48 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
49 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
50 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
51 Abbrevs[i]->dropRef();
56 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
57 /// the block, and return true if the block has an error.
58 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
59 // Save the current block's state on BlockScope.
60 BlockScope.push_back(Block(CurCodeSize));
61 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
63 // Add the abbrevs specific to this block to the CurAbbrevs list.
64 if (const BitstreamReader::BlockInfo *Info =
65 BitStream->getBlockInfo(BlockID)) {
66 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
67 CurAbbrevs.push_back(Info->Abbrevs[i]);
68 CurAbbrevs.back()->addRef();
72 // Get the codesize of this block.
73 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
74 SkipToFourByteBoundary();
75 unsigned NumWords = Read(bitc::BlockSizeWidth);
76 if (NumWordsP) *NumWordsP = NumWords;
78 // Validate that this block is sane.
79 if (CurCodeSize == 0 || AtEndOfStream())
85 void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
86 SmallVectorImpl<uint64_t> &Vals) {
87 assert(Op.isLiteral() && "Not a literal");
88 // If the abbrev specifies the literal value to use, use it.
89 Vals.push_back(Op.getLiteralValue());
92 void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
93 SmallVectorImpl<uint64_t> &Vals) {
94 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
96 // Decode the value as we are commanded.
97 switch (Op.getEncoding()) {
98 case BitCodeAbbrevOp::Array:
99 case BitCodeAbbrevOp::Blob:
100 assert(0 && "Should not reach here");
101 case BitCodeAbbrevOp::Fixed:
102 Vals.push_back(Read((unsigned)Op.getEncodingData()));
104 case BitCodeAbbrevOp::VBR:
105 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
107 case BitCodeAbbrevOp::Char6:
108 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
113 void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
114 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
116 // Decode the value as we are commanded.
117 switch (Op.getEncoding()) {
118 case BitCodeAbbrevOp::Array:
119 case BitCodeAbbrevOp::Blob:
120 assert(0 && "Should not reach here");
121 case BitCodeAbbrevOp::Fixed:
122 (void)Read((unsigned)Op.getEncodingData());
124 case BitCodeAbbrevOp::VBR:
125 (void)ReadVBR64((unsigned)Op.getEncodingData());
127 case BitCodeAbbrevOp::Char6:
135 /// skipRecord - Read the current record and discard it.
136 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
137 // Skip unabbreviated records by reading past their entries.
138 if (AbbrevID == bitc::UNABBREV_RECORD) {
139 unsigned Code = ReadVBR(6);
141 unsigned NumElts = ReadVBR(6);
142 for (unsigned i = 0; i != NumElts; ++i)
147 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
149 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
150 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
154 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
155 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
156 skipAbbreviatedField(Op);
160 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
161 // Array case. Read the number of elements as a vbr6.
162 unsigned NumElts = ReadVBR(6);
164 // Get the element encoding.
165 assert(i+2 == e && "array op not second to last?");
166 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
168 // Read all the elements.
169 for (; NumElts; --NumElts)
170 skipAbbreviatedField(EltEnc);
174 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
175 // Blob case. Read the number of bytes as a vbr6.
176 unsigned NumElts = ReadVBR(6);
177 SkipToFourByteBoundary(); // 32-bit alignment
179 // Figure out where the end of this blob will be including tail padding.
180 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
182 // If this would read off the end of the bitcode file, just set the
183 // record to empty and return.
184 if (!canSkipToPos(NewEnd/8)) {
185 NextChar = BitStream->getBitcodeBytes().getExtent();
189 // Skip over the blob.
194 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
195 SmallVectorImpl<uint64_t> &Vals,
197 if (AbbrevID == bitc::UNABBREV_RECORD) {
198 unsigned Code = ReadVBR(6);
199 unsigned NumElts = ReadVBR(6);
200 for (unsigned i = 0; i != NumElts; ++i)
201 Vals.push_back(ReadVBR64(6));
205 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
207 // Read the record code first.
208 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
209 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
210 if (CodeOp.isLiteral())
211 readAbbreviatedLiteral(CodeOp, Vals);
213 readAbbreviatedField(CodeOp, Vals);
214 unsigned Code = (unsigned)Vals.pop_back_val();
216 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
217 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
218 if (Op.isLiteral()) {
219 readAbbreviatedLiteral(Op, Vals);
223 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
224 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
225 readAbbreviatedField(Op, Vals);
229 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
230 // Array case. Read the number of elements as a vbr6.
231 unsigned NumElts = ReadVBR(6);
233 // Get the element encoding.
234 assert(i+2 == e && "array op not second to last?");
235 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
237 // Read all the elements.
238 for (; NumElts; --NumElts)
239 readAbbreviatedField(EltEnc, Vals);
243 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
244 // Blob case. Read the number of bytes as a vbr6.
245 unsigned NumElts = ReadVBR(6);
246 SkipToFourByteBoundary(); // 32-bit alignment
248 // Figure out where the end of this blob will be including tail padding.
249 size_t CurBitPos = GetCurrentBitNo();
250 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
252 // If this would read off the end of the bitcode file, just set the
253 // record to empty and return.
254 if (!canSkipToPos(NewEnd/8)) {
255 Vals.append(NumElts, 0);
256 NextChar = BitStream->getBitcodeBytes().getExtent();
260 // Otherwise, inform the streamer that we need these bytes in memory.
261 const char *Ptr = (const char*)
262 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
264 // If we can return a reference to the data, do so to avoid copying it.
266 *Blob = StringRef(Ptr, NumElts);
268 // Otherwise, unpack into Vals with zero extension.
269 for (; NumElts; --NumElts)
270 Vals.push_back((unsigned char)*Ptr++);
272 // Skip over tail padding.
280 void BitstreamCursor::ReadAbbrevRecord() {
281 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
282 unsigned NumOpInfo = ReadVBR(5);
283 for (unsigned i = 0; i != NumOpInfo; ++i) {
284 bool IsLiteral = Read(1) ? true : false;
286 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
290 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
291 if (BitCodeAbbrevOp::hasEncodingData(E)) {
292 unsigned Data = ReadVBR64(5);
294 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
295 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
296 // a slow path in Read() to have to handle reading zero bits.
297 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
299 Abbv->Add(BitCodeAbbrevOp(0));
303 Abbv->Add(BitCodeAbbrevOp(E, Data));
305 Abbv->Add(BitCodeAbbrevOp(E));
307 CurAbbrevs.push_back(Abbv);
310 bool BitstreamCursor::ReadBlockInfoBlock() {
311 // If this is the second stream to get to the block info block, skip it.
312 if (BitStream->hasBlockInfoRecords())
315 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
317 SmallVector<uint64_t, 64> Record;
318 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
320 // Read all the records for this module.
322 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
324 switch (Entry.Kind) {
325 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
326 case llvm::BitstreamEntry::Error:
328 case llvm::BitstreamEntry::EndBlock:
330 case llvm::BitstreamEntry::Record:
331 // The interesting case.
335 // Read abbrev records, associate them with CurBID.
336 if (Entry.ID == bitc::DEFINE_ABBREV) {
337 if (!CurBlockInfo) return true;
340 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
341 // appropriate BlockInfo.
342 BitCodeAbbrev *Abbv = CurAbbrevs.back();
343 CurAbbrevs.pop_back();
344 CurBlockInfo->Abbrevs.push_back(Abbv);
350 switch (readRecord(Entry.ID, Record)) {
351 default: break; // Default behavior, ignore unknown content.
352 case bitc::BLOCKINFO_CODE_SETBID:
353 if (Record.size() < 1) return true;
354 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
356 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
357 if (!CurBlockInfo) return true;
358 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
360 for (unsigned i = 0, e = Record.size(); i != e; ++i)
361 Name += (char)Record[i];
362 CurBlockInfo->Name = Name;
365 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
366 if (!CurBlockInfo) return true;
367 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
369 for (unsigned i = 1, e = Record.size(); i != e; ++i)
370 Name += (char)Record[i];
371 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],