blob: b1335029a7587e8baaee518ce6dfd182f905a56f [file] [log] [blame]
Chris Lattner312c7d92013-01-19 18:19:39 +00001//===- 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
12using namespace llvm;
13
14//===----------------------------------------------------------------------===//
15// BitstreamCursor implementation
16//===----------------------------------------------------------------------===//
17
18void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
19 freeState();
Joe Abbeyacb61942013-02-06 22:14:06 +000020
Chris Lattner312c7d92013-01-19 18:19:39 +000021 BitStream = RHS.BitStream;
22 NextChar = RHS.NextChar;
23 CurWord = RHS.CurWord;
24 BitsInCurWord = RHS.BitsInCurWord;
25 CurCodeSize = RHS.CurCodeSize;
Joe Abbeyacb61942013-02-06 22:14:06 +000026
Chris Lattner312c7d92013-01-19 18:19:39 +000027 // Copy abbreviations, and bump ref counts.
28 CurAbbrevs = RHS.CurAbbrevs;
29 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
30 i != e; ++i)
31 CurAbbrevs[i]->addRef();
Joe Abbeyacb61942013-02-06 22:14:06 +000032
Chris Lattner312c7d92013-01-19 18:19:39 +000033 // Copy block scope and bump ref counts.
34 BlockScope = RHS.BlockScope;
35 for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
36 S != e; ++S) {
37 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
38 for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
39 i != e; ++i)
40 Abbrevs[i]->addRef();
41 }
42}
43
44void BitstreamCursor::freeState() {
45 // Free all the Abbrevs.
46 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
47 i != e; ++i)
48 CurAbbrevs[i]->dropRef();
49 CurAbbrevs.clear();
Joe Abbeyacb61942013-02-06 22:14:06 +000050
Chris Lattner312c7d92013-01-19 18:19:39 +000051 // Free all the Abbrevs in the block scope.
52 for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
53 S != e; ++S) {
54 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
55 for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
56 i != e; ++i)
57 Abbrevs[i]->dropRef();
58 }
59 BlockScope.clear();
60}
61
62/// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
63/// the block, and return true if the block has an error.
64bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
65 // Save the current block's state on BlockScope.
66 BlockScope.push_back(Block(CurCodeSize));
67 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +000068
Chris Lattner312c7d92013-01-19 18:19:39 +000069 // Add the abbrevs specific to this block to the CurAbbrevs list.
70 if (const BitstreamReader::BlockInfo *Info =
71 BitStream->getBlockInfo(BlockID)) {
72 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
73 i != e; ++i) {
74 CurAbbrevs.push_back(Info->Abbrevs[i]);
75 CurAbbrevs.back()->addRef();
76 }
77 }
Joe Abbeyacb61942013-02-06 22:14:06 +000078
Chris Lattner312c7d92013-01-19 18:19:39 +000079 // Get the codesize of this block.
80 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
Chris Lattnerfd0543d2013-01-21 18:04:19 +000081 SkipToFourByteBoundary();
Chris Lattner312c7d92013-01-19 18:19:39 +000082 unsigned NumWords = Read(bitc::BlockSizeWidth);
83 if (NumWordsP) *NumWordsP = NumWords;
Joe Abbeyacb61942013-02-06 22:14:06 +000084
Chris Lattner312c7d92013-01-19 18:19:39 +000085 // Validate that this block is sane.
86 if (CurCodeSize == 0 || AtEndOfStream())
87 return true;
Joe Abbeyacb61942013-02-06 22:14:06 +000088
Chris Lattner312c7d92013-01-19 18:19:39 +000089 return false;
90}
91
Chris Lattnerf9147c42013-01-20 00:00:00 +000092void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
93 SmallVectorImpl<uint64_t> &Vals) {
94 assert(Op.isLiteral() && "Not a literal");
95 // If the abbrev specifies the literal value to use, use it.
96 Vals.push_back(Op.getLiteralValue());
97}
98
99void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
100 SmallVectorImpl<uint64_t> &Vals) {
101 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +0000102
Chris Lattnerf9147c42013-01-20 00:00:00 +0000103 // Decode the value as we are commanded.
104 switch (Op.getEncoding()) {
105 case BitCodeAbbrevOp::Array:
106 case BitCodeAbbrevOp::Blob:
107 assert(0 && "Should not reach here");
108 case BitCodeAbbrevOp::Fixed:
109 Vals.push_back(Read((unsigned)Op.getEncodingData()));
110 break;
111 case BitCodeAbbrevOp::VBR:
112 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
113 break;
114 case BitCodeAbbrevOp::Char6:
115 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
116 break;
117 }
118}
119
120void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
121 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +0000122
Chris Lattnerf9147c42013-01-20 00:00:00 +0000123 // Decode the value as we are commanded.
124 switch (Op.getEncoding()) {
125 case BitCodeAbbrevOp::Array:
126 case BitCodeAbbrevOp::Blob:
127 assert(0 && "Should not reach here");
128 case BitCodeAbbrevOp::Fixed:
129 (void)Read((unsigned)Op.getEncodingData());
130 break;
131 case BitCodeAbbrevOp::VBR:
132 (void)ReadVBR64((unsigned)Op.getEncodingData());
133 break;
134 case BitCodeAbbrevOp::Char6:
135 (void)Read(6);
136 break;
137 }
138}
139
140
141
142/// skipRecord - Read the current record and discard it.
143void BitstreamCursor::skipRecord(unsigned AbbrevID) {
144 // Skip unabbreviated records by reading past their entries.
145 if (AbbrevID == bitc::UNABBREV_RECORD) {
146 unsigned Code = ReadVBR(6);
147 (void)Code;
148 unsigned NumElts = ReadVBR(6);
149 for (unsigned i = 0; i != NumElts; ++i)
150 (void)ReadVBR64(6);
151 return;
152 }
153
154 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbeyacb61942013-02-06 22:14:06 +0000155
Chris Lattnerf9147c42013-01-20 00:00:00 +0000156 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
157 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
158 if (Op.isLiteral())
159 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +0000160
Chris Lattnerf9147c42013-01-20 00:00:00 +0000161 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
162 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
163 skipAbbreviatedField(Op);
164 continue;
165 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000166
Chris Lattnerf9147c42013-01-20 00:00:00 +0000167 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
168 // Array case. Read the number of elements as a vbr6.
169 unsigned NumElts = ReadVBR(6);
Joe Abbeyacb61942013-02-06 22:14:06 +0000170
Chris Lattnerf9147c42013-01-20 00:00:00 +0000171 // Get the element encoding.
172 assert(i+2 == e && "array op not second to last?");
173 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbeyacb61942013-02-06 22:14:06 +0000174
Chris Lattnerf9147c42013-01-20 00:00:00 +0000175 // Read all the elements.
176 for (; NumElts; --NumElts)
177 skipAbbreviatedField(EltEnc);
178 continue;
179 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000180
Chris Lattnerf9147c42013-01-20 00:00:00 +0000181 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
182 // Blob case. Read the number of bytes as a vbr6.
183 unsigned NumElts = ReadVBR(6);
Chris Lattnerfd0543d2013-01-21 18:04:19 +0000184 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000185
Chris Lattnerf9147c42013-01-20 00:00:00 +0000186 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000187 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000188
Chris Lattnerf9147c42013-01-20 00:00:00 +0000189 // If this would read off the end of the bitcode file, just set the
190 // record to empty and return.
Chris Lattner47543a82013-01-21 18:18:25 +0000191 if (!canSkipToPos(NewEnd/8)) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000192 NextChar = BitStream->getBitcodeBytes().getExtent();
193 break;
194 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000195
Chris Lattnerf9147c42013-01-20 00:00:00 +0000196 // Skip over the blob.
Chris Lattner47543a82013-01-21 18:18:25 +0000197 JumpToBit(NewEnd);
Chris Lattnerf9147c42013-01-20 00:00:00 +0000198 }
199}
Chris Lattner312c7d92013-01-19 18:19:39 +0000200
Chris Lattner194ef242013-01-20 01:06:48 +0000201unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner312c7d92013-01-19 18:19:39 +0000202 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner194ef242013-01-20 01:06:48 +0000203 StringRef *Blob) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000204 if (AbbrevID == bitc::UNABBREV_RECORD) {
205 unsigned Code = ReadVBR(6);
206 unsigned NumElts = ReadVBR(6);
207 for (unsigned i = 0; i != NumElts; ++i)
208 Vals.push_back(ReadVBR64(6));
209 return Code;
210 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000211
Chris Lattner312c7d92013-01-19 18:19:39 +0000212 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbeyacb61942013-02-06 22:14:06 +0000213
Chris Lattner312c7d92013-01-19 18:19:39 +0000214 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
215 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
216 if (Op.isLiteral()) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000217 readAbbreviatedLiteral(Op, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000218 continue;
219 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000220
Chris Lattner312c7d92013-01-19 18:19:39 +0000221 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
222 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000223 readAbbreviatedField(Op, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000224 continue;
225 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000226
Chris Lattner312c7d92013-01-19 18:19:39 +0000227 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
228 // Array case. Read the number of elements as a vbr6.
229 unsigned NumElts = ReadVBR(6);
Joe Abbeyacb61942013-02-06 22:14:06 +0000230
Chris Lattner312c7d92013-01-19 18:19:39 +0000231 // Get the element encoding.
232 assert(i+2 == e && "array op not second to last?");
233 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbeyacb61942013-02-06 22:14:06 +0000234
Chris Lattner312c7d92013-01-19 18:19:39 +0000235 // Read all the elements.
236 for (; NumElts; --NumElts)
Chris Lattnerf9147c42013-01-20 00:00:00 +0000237 readAbbreviatedField(EltEnc, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000238 continue;
239 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000240
Chris Lattner312c7d92013-01-19 18:19:39 +0000241 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
242 // Blob case. Read the number of bytes as a vbr6.
243 unsigned NumElts = ReadVBR(6);
Chris Lattnerfd0543d2013-01-21 18:04:19 +0000244 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000245
Chris Lattner312c7d92013-01-19 18:19:39 +0000246 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000247 size_t CurBitPos = GetCurrentBitNo();
248 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000249
Chris Lattner312c7d92013-01-19 18:19:39 +0000250 // If this would read off the end of the bitcode file, just set the
251 // record to empty and return.
Chris Lattner47543a82013-01-21 18:18:25 +0000252 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000253 Vals.append(NumElts, 0);
254 NextChar = BitStream->getBitcodeBytes().getExtent();
255 break;
256 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000257
Chris Lattner69582cf2013-01-21 18:24:49 +0000258 // Otherwise, inform the streamer that we need these bytes in memory.
259 const char *Ptr = (const char*)
260 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbeyacb61942013-02-06 22:14:06 +0000261
Chris Lattner69582cf2013-01-21 18:24:49 +0000262 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner194ef242013-01-20 01:06:48 +0000263 if (Blob) {
Chris Lattner69582cf2013-01-21 18:24:49 +0000264 *Blob = StringRef(Ptr, NumElts);
Chris Lattner312c7d92013-01-19 18:19:39 +0000265 } else {
Chris Lattner69582cf2013-01-21 18:24:49 +0000266 // Otherwise, unpack into Vals with zero extension.
Chris Lattner47543a82013-01-21 18:18:25 +0000267 for (; NumElts; --NumElts)
Chris Lattner69582cf2013-01-21 18:24:49 +0000268 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner312c7d92013-01-19 18:19:39 +0000269 }
270 // Skip over tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000271 JumpToBit(NewEnd);
Chris Lattner312c7d92013-01-19 18:19:39 +0000272 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000273
Chris Lattner312c7d92013-01-19 18:19:39 +0000274 unsigned Code = (unsigned)Vals[0];
275 Vals.erase(Vals.begin());
276 return Code;
277}
278
279
280void 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;
285 if (IsLiteral) {
286 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
287 continue;
288 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000289
Chris Lattner312c7d92013-01-19 18:19:39 +0000290 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattnerb24f5b72013-02-09 07:07:29 +0000291 if (BitCodeAbbrevOp::hasEncodingData(E)) {
292 unsigned Data = ReadVBR64(5);
293
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) &&
298 Data == 0) {
299 Abbv->Add(BitCodeAbbrevOp(0));
300 continue;
301 }
302
303 Abbv->Add(BitCodeAbbrevOp(E, Data));
304 } else
Chris Lattner312c7d92013-01-19 18:19:39 +0000305 Abbv->Add(BitCodeAbbrevOp(E));
306 }
307 CurAbbrevs.push_back(Abbv);
308}
309
310bool BitstreamCursor::ReadBlockInfoBlock() {
311 // If this is the second stream to get to the block info block, skip it.
312 if (BitStream->hasBlockInfoRecords())
313 return SkipBlock();
Joe Abbeyacb61942013-02-06 22:14:06 +0000314
Chris Lattner312c7d92013-01-19 18:19:39 +0000315 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbeyacb61942013-02-06 22:14:06 +0000316
Chris Lattner312c7d92013-01-19 18:19:39 +0000317 SmallVector<uint64_t, 64> Record;
318 BitstreamReader::BlockInfo *CurBlockInfo = 0;
Joe Abbeyacb61942013-02-06 22:14:06 +0000319
Chris Lattner312c7d92013-01-19 18:19:39 +0000320 // Read all the records for this module.
321 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000322 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +0000323
Chris Lattner5a4251c2013-01-20 02:13:19 +0000324 switch (Entry.Kind) {
325 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
326 case llvm::BitstreamEntry::Error:
327 return true;
328 case llvm::BitstreamEntry::EndBlock:
329 return false;
330 case llvm::BitstreamEntry::Record:
331 // The interesting case.
332 break;
333 }
334
Chris Lattner312c7d92013-01-19 18:19:39 +0000335 // Read abbrev records, associate them with CurBID.
Chris Lattner5a4251c2013-01-20 02:13:19 +0000336 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000337 if (!CurBlockInfo) return true;
338 ReadAbbrevRecord();
Joe Abbeyacb61942013-02-06 22:14:06 +0000339
Chris Lattner312c7d92013-01-19 18:19:39 +0000340 // 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);
345 continue;
346 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000347
Chris Lattner312c7d92013-01-19 18:19:39 +0000348 // Read a record.
349 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000350 switch (readRecord(Entry.ID, Record)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000351 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]);
355 break;
356 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
357 if (!CurBlockInfo) return true;
358 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
359 std::string Name;
360 for (unsigned i = 0, e = Record.size(); i != e; ++i)
361 Name += (char)Record[i];
362 CurBlockInfo->Name = Name;
363 break;
364 }
365 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
366 if (!CurBlockInfo) return true;
367 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
368 std::string 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],
372 Name));
373 break;
374 }
375 }
376 }
377}
378