blob: 9d5fab9147a7605e32e4209068f30a12dcb65c7c [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 }
Reid Kleckner9651f01a82014-11-13 23:07:22 +000069 llvm_unreachable("invalid abbreviation encoding");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000070}
71
Rafael Espindola76d41f82014-11-13 21:54:59 +000072static void skipAbbreviatedField(BitstreamCursor &Cursor,
73 const BitCodeAbbrevOp &Op) {
Rafael Espindolaf8950452014-11-13 22:29:02 +000074 assert(!Op.isLiteral() && "Not to be used with literals!");
Joe Abbey97b7a172013-02-06 22:14:06 +000075
Chris Lattner5ba7bca2013-01-20 00:00:00 +000076 // Decode the value as we are commanded.
77 switch (Op.getEncoding()) {
78 case BitCodeAbbrevOp::Array:
79 case BitCodeAbbrevOp::Blob:
Craig Topper2a30d782014-06-18 05:05:13 +000080 llvm_unreachable("Should not reach here");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000081 case BitCodeAbbrevOp::Fixed:
Rafael Espindola76d41f82014-11-13 21:54:59 +000082 Cursor.Read((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000083 break;
84 case BitCodeAbbrevOp::VBR:
Rafael Espindola76d41f82014-11-13 21:54:59 +000085 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000086 break;
87 case BitCodeAbbrevOp::Char6:
Rafael Espindola76d41f82014-11-13 21:54:59 +000088 Cursor.Read(6);
Chris Lattner5ba7bca2013-01-20 00:00:00 +000089 break;
90 }
91}
92
93
94
95/// skipRecord - Read the current record and discard it.
96void BitstreamCursor::skipRecord(unsigned AbbrevID) {
97 // Skip unabbreviated records by reading past their entries.
98 if (AbbrevID == bitc::UNABBREV_RECORD) {
99 unsigned Code = ReadVBR(6);
100 (void)Code;
101 unsigned NumElts = ReadVBR(6);
102 for (unsigned i = 0; i != NumElts; ++i)
103 (void)ReadVBR64(6);
104 return;
105 }
106
107 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000108
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000109 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
110 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
111 if (Op.isLiteral())
112 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +0000113
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000114 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
115 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Rafael Espindola76d41f82014-11-13 21:54:59 +0000116 skipAbbreviatedField(*this, Op);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000117 continue;
118 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000119
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000120 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
121 // Array case. Read the number of elements as a vbr6.
122 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000123
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000124 // Get the element encoding.
125 assert(i+2 == e && "array op not second to last?");
126 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000127
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000128 // Read all the elements.
129 for (; NumElts; --NumElts)
Rafael Espindola76d41f82014-11-13 21:54:59 +0000130 skipAbbreviatedField(*this, EltEnc);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000131 continue;
132 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000133
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000134 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
135 // Blob case. Read the number of bytes as a vbr6.
136 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000137 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000138
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000139 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000140 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000141
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000142 // If this would read off the end of the bitcode file, just set the
143 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000144 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000145 NextChar = BitStream->getBitcodeBytes().getExtent();
146 break;
147 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000148
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000149 // Skip over the blob.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000150 JumpToBit(NewEnd);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000151 }
152}
Chris Lattner59c3abc2013-01-19 18:19:39 +0000153
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000154unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner59c3abc2013-01-19 18:19:39 +0000155 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000156 StringRef *Blob) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000157 if (AbbrevID == bitc::UNABBREV_RECORD) {
158 unsigned Code = ReadVBR(6);
159 unsigned NumElts = ReadVBR(6);
160 for (unsigned i = 0; i != NumElts; ++i)
161 Vals.push_back(ReadVBR64(6));
162 return Code;
163 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000164
Chris Lattner59c3abc2013-01-19 18:19:39 +0000165 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000166
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000167 // Read the record code first.
168 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
169 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
Rafael Espindolaf8950452014-11-13 22:29:02 +0000170 unsigned Code;
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000171 if (CodeOp.isLiteral())
Rafael Espindolaf8950452014-11-13 22:29:02 +0000172 Code = CodeOp.getLiteralValue();
Filipe Cabecinhasde968ec2015-01-24 04:15:05 +0000173 else {
174 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
175 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
176 report_fatal_error("Abbreviation starts with an Array or a Blob");
Rafael Espindolaf8950452014-11-13 22:29:02 +0000177 Code = readAbbreviatedField(*this, CodeOp);
Filipe Cabecinhasde968ec2015-01-24 04:15:05 +0000178 }
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000179
180 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000181 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
182 if (Op.isLiteral()) {
Rafael Espindolaf8950452014-11-13 22:29:02 +0000183 Vals.push_back(Op.getLiteralValue());
Chris Lattner59c3abc2013-01-19 18:19:39 +0000184 continue;
185 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000186
Chris Lattner59c3abc2013-01-19 18:19:39 +0000187 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
188 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Rafael Espindolaf8950452014-11-13 22:29:02 +0000189 Vals.push_back(readAbbreviatedField(*this, Op));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000190 continue;
191 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000192
Chris Lattner59c3abc2013-01-19 18:19:39 +0000193 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
194 // Array case. Read the number of elements as a vbr6.
195 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000196
Chris Lattner59c3abc2013-01-19 18:19:39 +0000197 // Get the element encoding.
198 assert(i+2 == e && "array op not second to last?");
199 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000200
Chris Lattner59c3abc2013-01-19 18:19:39 +0000201 // Read all the elements.
202 for (; NumElts; --NumElts)
Rafael Espindolaf8950452014-11-13 22:29:02 +0000203 Vals.push_back(readAbbreviatedField(*this, EltEnc));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000204 continue;
205 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000206
Chris Lattner59c3abc2013-01-19 18:19:39 +0000207 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
208 // Blob case. Read the number of bytes as a vbr6.
209 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000210 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000211
Chris Lattner59c3abc2013-01-19 18:19:39 +0000212 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000213 size_t CurBitPos = GetCurrentBitNo();
214 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000215
Chris Lattner59c3abc2013-01-19 18:19:39 +0000216 // If this would read off the end of the bitcode file, just set the
217 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000218 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000219 Vals.append(NumElts, 0);
220 NextChar = BitStream->getBitcodeBytes().getExtent();
221 break;
222 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000223
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000224 // Otherwise, inform the streamer that we need these bytes in memory.
225 const char *Ptr = (const char*)
226 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbey97b7a172013-02-06 22:14:06 +0000227
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000228 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000229 if (Blob) {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000230 *Blob = StringRef(Ptr, NumElts);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000231 } else {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000232 // Otherwise, unpack into Vals with zero extension.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000233 for (; NumElts; --NumElts)
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000234 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000235 }
236 // Skip over tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000237 JumpToBit(NewEnd);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000238 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000239
Chris Lattner59c3abc2013-01-19 18:19:39 +0000240 return Code;
241}
242
243
244void BitstreamCursor::ReadAbbrevRecord() {
245 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
246 unsigned NumOpInfo = ReadVBR(5);
247 for (unsigned i = 0; i != NumOpInfo; ++i) {
248 bool IsLiteral = Read(1) ? true : false;
249 if (IsLiteral) {
250 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
251 continue;
252 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000253
Chris Lattner59c3abc2013-01-19 18:19:39 +0000254 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattner6c29cb32013-02-09 07:07:29 +0000255 if (BitCodeAbbrevOp::hasEncodingData(E)) {
256 unsigned Data = ReadVBR64(5);
257
258 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
259 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
260 // a slow path in Read() to have to handle reading zero bits.
261 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
262 Data == 0) {
263 Abbv->Add(BitCodeAbbrevOp(0));
264 continue;
265 }
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000266
Chris Lattner6c29cb32013-02-09 07:07:29 +0000267 Abbv->Add(BitCodeAbbrevOp(E, Data));
268 } else
Chris Lattner59c3abc2013-01-19 18:19:39 +0000269 Abbv->Add(BitCodeAbbrevOp(E));
270 }
271 CurAbbrevs.push_back(Abbv);
272}
273
274bool BitstreamCursor::ReadBlockInfoBlock() {
275 // If this is the second stream to get to the block info block, skip it.
276 if (BitStream->hasBlockInfoRecords())
277 return SkipBlock();
Joe Abbey97b7a172013-02-06 22:14:06 +0000278
Chris Lattner59c3abc2013-01-19 18:19:39 +0000279 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbey97b7a172013-02-06 22:14:06 +0000280
Chris Lattner59c3abc2013-01-19 18:19:39 +0000281 SmallVector<uint64_t, 64> Record;
Craig Topper2617dcc2014-04-15 06:32:26 +0000282 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
Joe Abbey97b7a172013-02-06 22:14:06 +0000283
Chris Lattner59c3abc2013-01-19 18:19:39 +0000284 // Read all the records for this module.
285 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000286 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +0000287
Chris Lattner27d38752013-01-20 02:13:19 +0000288 switch (Entry.Kind) {
289 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
290 case llvm::BitstreamEntry::Error:
291 return true;
292 case llvm::BitstreamEntry::EndBlock:
293 return false;
294 case llvm::BitstreamEntry::Record:
295 // The interesting case.
296 break;
297 }
298
Chris Lattner59c3abc2013-01-19 18:19:39 +0000299 // Read abbrev records, associate them with CurBID.
Chris Lattner27d38752013-01-20 02:13:19 +0000300 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000301 if (!CurBlockInfo) return true;
302 ReadAbbrevRecord();
Joe Abbey97b7a172013-02-06 22:14:06 +0000303
Chris Lattner59c3abc2013-01-19 18:19:39 +0000304 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
305 // appropriate BlockInfo.
Benjamin Kramer6891ba02014-09-15 15:44:14 +0000306 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000307 CurAbbrevs.pop_back();
Chris Lattner59c3abc2013-01-19 18:19:39 +0000308 continue;
309 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000310
Chris Lattner59c3abc2013-01-19 18:19:39 +0000311 // Read a record.
312 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000313 switch (readRecord(Entry.ID, Record)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000314 default: break; // Default behavior, ignore unknown content.
315 case bitc::BLOCKINFO_CODE_SETBID:
316 if (Record.size() < 1) return true;
317 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
318 break;
319 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
320 if (!CurBlockInfo) return true;
321 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
322 std::string Name;
323 for (unsigned i = 0, e = Record.size(); i != e; ++i)
324 Name += (char)Record[i];
325 CurBlockInfo->Name = Name;
326 break;
327 }
328 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
329 if (!CurBlockInfo) return true;
330 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
331 std::string Name;
332 for (unsigned i = 1, e = Record.size(); i != e; ++i)
333 Name += (char)Record[i];
334 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
335 Name));
336 break;
337 }
338 }
339 }
340}
341