blob: 27bb62f97d41cc253394b31d22f1a5f1ea8f674a [file] [log] [blame]
Chris Lattner59c3abc2013-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
Chris Lattner59c3abc2013-01-19 18:19:39 +000018void BitstreamCursor::freeState() {
19 // Free all the Abbrevs.
Chris Lattner59c3abc2013-01-19 18:19:39 +000020 CurAbbrevs.clear();
Joe Abbey97b7a172013-02-06 22:14:06 +000021
Chris Lattner59c3abc2013-01-19 18:19:39 +000022 // Free all the Abbrevs in the block scope.
Chris Lattner59c3abc2013-01-19 18:19:39 +000023 BlockScope.clear();
24}
25
26/// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27/// the block, and return true if the block has an error.
28bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
29 // Save the current block's state on BlockScope.
30 BlockScope.push_back(Block(CurCodeSize));
31 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +000032
Chris Lattner59c3abc2013-01-19 18:19:39 +000033 // Add the abbrevs specific to this block to the CurAbbrevs list.
34 if (const BitstreamReader::BlockInfo *Info =
35 BitStream->getBlockInfo(BlockID)) {
Benjamin Kramer6891ba02014-09-15 15:44:14 +000036 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
37 Info->Abbrevs.end());
Chris Lattner59c3abc2013-01-19 18:19:39 +000038 }
Joe Abbey97b7a172013-02-06 22:14:06 +000039
Chris Lattner59c3abc2013-01-19 18:19:39 +000040 // Get the codesize of this block.
41 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
Chris Lattnere1817aa2013-01-21 18:04:19 +000042 SkipToFourByteBoundary();
Chris Lattner59c3abc2013-01-19 18:19:39 +000043 unsigned NumWords = Read(bitc::BlockSizeWidth);
44 if (NumWordsP) *NumWordsP = NumWords;
Joe Abbey97b7a172013-02-06 22:14:06 +000045
Chris Lattner59c3abc2013-01-19 18:19:39 +000046 // Validate that this block is sane.
47 if (CurCodeSize == 0 || AtEndOfStream())
48 return true;
Joe Abbey97b7a172013-02-06 22:14:06 +000049
Chris Lattner59c3abc2013-01-19 18:19:39 +000050 return false;
51}
52
Rafael Espindolaf8950452014-11-13 22:29:02 +000053static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
54 const BitCodeAbbrevOp &Op) {
55 assert(!Op.isLiteral() && "Not to be used with literals!");
Joe Abbey97b7a172013-02-06 22:14:06 +000056
Chris Lattner5ba7bca2013-01-20 00:00:00 +000057 // Decode the value as we are commanded.
58 switch (Op.getEncoding()) {
59 case BitCodeAbbrevOp::Array:
60 case BitCodeAbbrevOp::Blob:
Craig Topper2a30d782014-06-18 05:05:13 +000061 llvm_unreachable("Should not reach here");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000062 case BitCodeAbbrevOp::Fixed:
Rafael Espindolaf8950452014-11-13 22:29:02 +000063 return Cursor.Read((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000064 case BitCodeAbbrevOp::VBR:
Rafael Espindolaf8950452014-11-13 22:29:02 +000065 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000066 case BitCodeAbbrevOp::Char6:
Rafael Espindolaf8950452014-11-13 22:29:02 +000067 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
Chris Lattner5ba7bca2013-01-20 00:00:00 +000068 }
69}
70
Rafael Espindola76d41f82014-11-13 21:54:59 +000071static void skipAbbreviatedField(BitstreamCursor &Cursor,
72 const BitCodeAbbrevOp &Op) {
Rafael Espindolaf8950452014-11-13 22:29:02 +000073 assert(!Op.isLiteral() && "Not to be used with literals!");
Joe Abbey97b7a172013-02-06 22:14:06 +000074
Chris Lattner5ba7bca2013-01-20 00:00:00 +000075 // Decode the value as we are commanded.
76 switch (Op.getEncoding()) {
77 case BitCodeAbbrevOp::Array:
78 case BitCodeAbbrevOp::Blob:
Craig Topper2a30d782014-06-18 05:05:13 +000079 llvm_unreachable("Should not reach here");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000080 case BitCodeAbbrevOp::Fixed:
Rafael Espindola76d41f82014-11-13 21:54:59 +000081 Cursor.Read((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000082 break;
83 case BitCodeAbbrevOp::VBR:
Rafael Espindola76d41f82014-11-13 21:54:59 +000084 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000085 break;
86 case BitCodeAbbrevOp::Char6:
Rafael Espindola76d41f82014-11-13 21:54:59 +000087 Cursor.Read(6);
Chris Lattner5ba7bca2013-01-20 00:00:00 +000088 break;
89 }
90}
91
92
93
94/// skipRecord - Read the current record and discard it.
95void BitstreamCursor::skipRecord(unsigned AbbrevID) {
96 // Skip unabbreviated records by reading past their entries.
97 if (AbbrevID == bitc::UNABBREV_RECORD) {
98 unsigned Code = ReadVBR(6);
99 (void)Code;
100 unsigned NumElts = ReadVBR(6);
101 for (unsigned i = 0; i != NumElts; ++i)
102 (void)ReadVBR64(6);
103 return;
104 }
105
106 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000107
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000108 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
109 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
110 if (Op.isLiteral())
111 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +0000112
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000113 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
114 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Rafael Espindola76d41f82014-11-13 21:54:59 +0000115 skipAbbreviatedField(*this, Op);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000116 continue;
117 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000118
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000119 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
120 // Array case. Read the number of elements as a vbr6.
121 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000122
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000123 // Get the element encoding.
124 assert(i+2 == e && "array op not second to last?");
125 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000126
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000127 // Read all the elements.
128 for (; NumElts; --NumElts)
Rafael Espindola76d41f82014-11-13 21:54:59 +0000129 skipAbbreviatedField(*this, EltEnc);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000130 continue;
131 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000132
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000133 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
134 // Blob case. Read the number of bytes as a vbr6.
135 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000136 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000137
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000138 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000139 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000140
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000141 // If this would read off the end of the bitcode file, just set the
142 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000143 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000144 NextChar = BitStream->getBitcodeBytes().getExtent();
145 break;
146 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000147
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000148 // Skip over the blob.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000149 JumpToBit(NewEnd);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000150 }
151}
Chris Lattner59c3abc2013-01-19 18:19:39 +0000152
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000153unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner59c3abc2013-01-19 18:19:39 +0000154 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000155 StringRef *Blob) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000156 if (AbbrevID == bitc::UNABBREV_RECORD) {
157 unsigned Code = ReadVBR(6);
158 unsigned NumElts = ReadVBR(6);
159 for (unsigned i = 0; i != NumElts; ++i)
160 Vals.push_back(ReadVBR64(6));
161 return Code;
162 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000163
Chris Lattner59c3abc2013-01-19 18:19:39 +0000164 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000165
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000166 // Read the record code first.
167 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
168 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
Rafael Espindolaf8950452014-11-13 22:29:02 +0000169 unsigned Code;
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000170 if (CodeOp.isLiteral())
Rafael Espindolaf8950452014-11-13 22:29:02 +0000171 Code = CodeOp.getLiteralValue();
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000172 else
Rafael Espindolaf8950452014-11-13 22:29:02 +0000173 Code = readAbbreviatedField(*this, CodeOp);
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000174
175 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000176 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
177 if (Op.isLiteral()) {
Rafael Espindolaf8950452014-11-13 22:29:02 +0000178 Vals.push_back(Op.getLiteralValue());
Chris Lattner59c3abc2013-01-19 18:19:39 +0000179 continue;
180 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000181
Chris Lattner59c3abc2013-01-19 18:19:39 +0000182 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
183 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Rafael Espindolaf8950452014-11-13 22:29:02 +0000184 Vals.push_back(readAbbreviatedField(*this, Op));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000185 continue;
186 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000187
Chris Lattner59c3abc2013-01-19 18:19:39 +0000188 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
189 // Array case. Read the number of elements as a vbr6.
190 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000191
Chris Lattner59c3abc2013-01-19 18:19:39 +0000192 // Get the element encoding.
193 assert(i+2 == e && "array op not second to last?");
194 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000195
Chris Lattner59c3abc2013-01-19 18:19:39 +0000196 // Read all the elements.
197 for (; NumElts; --NumElts)
Rafael Espindolaf8950452014-11-13 22:29:02 +0000198 Vals.push_back(readAbbreviatedField(*this, EltEnc));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000199 continue;
200 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000201
Chris Lattner59c3abc2013-01-19 18:19:39 +0000202 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
203 // Blob case. Read the number of bytes as a vbr6.
204 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000205 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000206
Chris Lattner59c3abc2013-01-19 18:19:39 +0000207 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000208 size_t CurBitPos = GetCurrentBitNo();
209 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000210
Chris Lattner59c3abc2013-01-19 18:19:39 +0000211 // If this would read off the end of the bitcode file, just set the
212 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000213 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000214 Vals.append(NumElts, 0);
215 NextChar = BitStream->getBitcodeBytes().getExtent();
216 break;
217 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000218
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000219 // Otherwise, inform the streamer that we need these bytes in memory.
220 const char *Ptr = (const char*)
221 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbey97b7a172013-02-06 22:14:06 +0000222
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000223 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000224 if (Blob) {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000225 *Blob = StringRef(Ptr, NumElts);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000226 } else {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000227 // Otherwise, unpack into Vals with zero extension.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000228 for (; NumElts; --NumElts)
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000229 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000230 }
231 // Skip over tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000232 JumpToBit(NewEnd);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000233 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000234
Chris Lattner59c3abc2013-01-19 18:19:39 +0000235 return Code;
236}
237
238
239void BitstreamCursor::ReadAbbrevRecord() {
240 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
241 unsigned NumOpInfo = ReadVBR(5);
242 for (unsigned i = 0; i != NumOpInfo; ++i) {
243 bool IsLiteral = Read(1) ? true : false;
244 if (IsLiteral) {
245 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
246 continue;
247 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000248
Chris Lattner59c3abc2013-01-19 18:19:39 +0000249 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattner6c29cb32013-02-09 07:07:29 +0000250 if (BitCodeAbbrevOp::hasEncodingData(E)) {
251 unsigned Data = ReadVBR64(5);
252
253 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
254 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
255 // a slow path in Read() to have to handle reading zero bits.
256 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
257 Data == 0) {
258 Abbv->Add(BitCodeAbbrevOp(0));
259 continue;
260 }
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000261
Chris Lattner6c29cb32013-02-09 07:07:29 +0000262 Abbv->Add(BitCodeAbbrevOp(E, Data));
263 } else
Chris Lattner59c3abc2013-01-19 18:19:39 +0000264 Abbv->Add(BitCodeAbbrevOp(E));
265 }
266 CurAbbrevs.push_back(Abbv);
267}
268
269bool BitstreamCursor::ReadBlockInfoBlock() {
270 // If this is the second stream to get to the block info block, skip it.
271 if (BitStream->hasBlockInfoRecords())
272 return SkipBlock();
Joe Abbey97b7a172013-02-06 22:14:06 +0000273
Chris Lattner59c3abc2013-01-19 18:19:39 +0000274 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbey97b7a172013-02-06 22:14:06 +0000275
Chris Lattner59c3abc2013-01-19 18:19:39 +0000276 SmallVector<uint64_t, 64> Record;
Craig Topper2617dcc2014-04-15 06:32:26 +0000277 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
Joe Abbey97b7a172013-02-06 22:14:06 +0000278
Chris Lattner59c3abc2013-01-19 18:19:39 +0000279 // Read all the records for this module.
280 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000281 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +0000282
Chris Lattner27d38752013-01-20 02:13:19 +0000283 switch (Entry.Kind) {
284 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
285 case llvm::BitstreamEntry::Error:
286 return true;
287 case llvm::BitstreamEntry::EndBlock:
288 return false;
289 case llvm::BitstreamEntry::Record:
290 // The interesting case.
291 break;
292 }
293
Chris Lattner59c3abc2013-01-19 18:19:39 +0000294 // Read abbrev records, associate them with CurBID.
Chris Lattner27d38752013-01-20 02:13:19 +0000295 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000296 if (!CurBlockInfo) return true;
297 ReadAbbrevRecord();
Joe Abbey97b7a172013-02-06 22:14:06 +0000298
Chris Lattner59c3abc2013-01-19 18:19:39 +0000299 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
300 // appropriate BlockInfo.
Benjamin Kramer6891ba02014-09-15 15:44:14 +0000301 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000302 CurAbbrevs.pop_back();
Chris Lattner59c3abc2013-01-19 18:19:39 +0000303 continue;
304 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000305
Chris Lattner59c3abc2013-01-19 18:19:39 +0000306 // Read a record.
307 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000308 switch (readRecord(Entry.ID, Record)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000309 default: break; // Default behavior, ignore unknown content.
310 case bitc::BLOCKINFO_CODE_SETBID:
311 if (Record.size() < 1) return true;
312 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
313 break;
314 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
315 if (!CurBlockInfo) return true;
316 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
317 std::string Name;
318 for (unsigned i = 0, e = Record.size(); i != e; ++i)
319 Name += (char)Record[i];
320 CurBlockInfo->Name = Name;
321 break;
322 }
323 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
324 if (!CurBlockInfo) return true;
325 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
326 std::string Name;
327 for (unsigned i = 1, e = Record.size(); i != e; ++i)
328 Name += (char)Record[i];
329 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
330 Name));
331 break;
332 }
333 }
334 }
335}
336