blob: 581a4402f94c9763b3695f2ac886096d87ba0ab0 [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
Chris Lattner5ba7bca2013-01-20 00:00:00 +000053void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
54 SmallVectorImpl<uint64_t> &Vals) {
55 assert(Op.isLiteral() && "Not a literal");
56 // If the abbrev specifies the literal value to use, use it.
57 Vals.push_back(Op.getLiteralValue());
58}
59
60void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
61 SmallVectorImpl<uint64_t> &Vals) {
62 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbey97b7a172013-02-06 22:14:06 +000063
Chris Lattner5ba7bca2013-01-20 00:00:00 +000064 // Decode the value as we are commanded.
Rafael Espindolae2541bd2014-11-06 22:39:16 +000065 uint64_t Val;
Chris Lattner5ba7bca2013-01-20 00:00:00 +000066 switch (Op.getEncoding()) {
67 case BitCodeAbbrevOp::Array:
68 case BitCodeAbbrevOp::Blob:
Craig Topper2a30d782014-06-18 05:05:13 +000069 llvm_unreachable("Should not reach here");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000070 case BitCodeAbbrevOp::Fixed:
Rafael Espindolae2541bd2014-11-06 22:39:16 +000071 Val = Read((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000072 break;
73 case BitCodeAbbrevOp::VBR:
Rafael Espindolae2541bd2014-11-06 22:39:16 +000074 Val = ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattner5ba7bca2013-01-20 00:00:00 +000075 break;
76 case BitCodeAbbrevOp::Char6:
Rafael Espindolae2541bd2014-11-06 22:39:16 +000077 Val = BitCodeAbbrevOp::DecodeChar6(Read(6));
Chris Lattner5ba7bca2013-01-20 00:00:00 +000078 break;
79 }
Rafael Espindolae2541bd2014-11-06 22:39:16 +000080 Vals.push_back(Val);
Chris Lattner5ba7bca2013-01-20 00:00:00 +000081}
82
83void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
84 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbey97b7a172013-02-06 22:14:06 +000085
Chris Lattner5ba7bca2013-01-20 00:00:00 +000086 // Decode the value as we are commanded.
87 switch (Op.getEncoding()) {
88 case BitCodeAbbrevOp::Array:
89 case BitCodeAbbrevOp::Blob:
Craig Topper2a30d782014-06-18 05:05:13 +000090 llvm_unreachable("Should not reach here");
Chris Lattner5ba7bca2013-01-20 00:00:00 +000091 case BitCodeAbbrevOp::Fixed:
92 (void)Read((unsigned)Op.getEncodingData());
93 break;
94 case BitCodeAbbrevOp::VBR:
95 (void)ReadVBR64((unsigned)Op.getEncodingData());
96 break;
97 case BitCodeAbbrevOp::Char6:
98 (void)Read(6);
99 break;
100 }
101}
102
103
104
105/// skipRecord - Read the current record and discard it.
106void BitstreamCursor::skipRecord(unsigned AbbrevID) {
107 // Skip unabbreviated records by reading past their entries.
108 if (AbbrevID == bitc::UNABBREV_RECORD) {
109 unsigned Code = ReadVBR(6);
110 (void)Code;
111 unsigned NumElts = ReadVBR(6);
112 for (unsigned i = 0; i != NumElts; ++i)
113 (void)ReadVBR64(6);
114 return;
115 }
116
117 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000118
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000119 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
120 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
121 if (Op.isLiteral())
122 continue;
Joe Abbey97b7a172013-02-06 22:14:06 +0000123
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000124 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
125 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
126 skipAbbreviatedField(Op);
127 continue;
128 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000129
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000130 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
131 // Array case. Read the number of elements as a vbr6.
132 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000133
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000134 // Get the element encoding.
135 assert(i+2 == e && "array op not second to last?");
136 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000137
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000138 // Read all the elements.
139 for (; NumElts; --NumElts)
140 skipAbbreviatedField(EltEnc);
141 continue;
142 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000143
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000144 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
145 // Blob case. Read the number of bytes as a vbr6.
146 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000147 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000148
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000149 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000150 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000151
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000152 // If this would read off the end of the bitcode file, just set the
153 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000154 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000155 NextChar = BitStream->getBitcodeBytes().getExtent();
156 break;
157 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000158
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000159 // Skip over the blob.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000160 JumpToBit(NewEnd);
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000161 }
162}
Chris Lattner59c3abc2013-01-19 18:19:39 +0000163
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000164unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner59c3abc2013-01-19 18:19:39 +0000165 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000166 StringRef *Blob) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000167 if (AbbrevID == bitc::UNABBREV_RECORD) {
168 unsigned Code = ReadVBR(6);
169 unsigned NumElts = ReadVBR(6);
170 for (unsigned i = 0; i != NumElts; ++i)
171 Vals.push_back(ReadVBR64(6));
172 return Code;
173 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000174
Chris Lattner59c3abc2013-01-19 18:19:39 +0000175 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbey97b7a172013-02-06 22:14:06 +0000176
Jordan Rose6ac4ba22013-05-10 22:17:10 +0000177 // Read the record code first.
178 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
179 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
180 if (CodeOp.isLiteral())
181 readAbbreviatedLiteral(CodeOp, Vals);
182 else
183 readAbbreviatedField(CodeOp, Vals);
184 unsigned Code = (unsigned)Vals.pop_back_val();
185
186 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000187 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
188 if (Op.isLiteral()) {
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000189 readAbbreviatedLiteral(Op, Vals);
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 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000195 readAbbreviatedField(Op, Vals);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000196 continue;
197 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000198
Chris Lattner59c3abc2013-01-19 18:19:39 +0000199 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
200 // Array case. Read the number of elements as a vbr6.
201 unsigned NumElts = ReadVBR(6);
Joe Abbey97b7a172013-02-06 22:14:06 +0000202
Chris Lattner59c3abc2013-01-19 18:19:39 +0000203 // Get the element encoding.
204 assert(i+2 == e && "array op not second to last?");
205 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbey97b7a172013-02-06 22:14:06 +0000206
Chris Lattner59c3abc2013-01-19 18:19:39 +0000207 // Read all the elements.
208 for (; NumElts; --NumElts)
Chris Lattner5ba7bca2013-01-20 00:00:00 +0000209 readAbbreviatedField(EltEnc, Vals);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000210 continue;
211 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000212
Chris Lattner59c3abc2013-01-19 18:19:39 +0000213 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
214 // Blob case. Read the number of bytes as a vbr6.
215 unsigned NumElts = ReadVBR(6);
Chris Lattnere1817aa2013-01-21 18:04:19 +0000216 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbey97b7a172013-02-06 22:14:06 +0000217
Chris Lattner59c3abc2013-01-19 18:19:39 +0000218 // Figure out where the end of this blob will be including tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000219 size_t CurBitPos = GetCurrentBitNo();
220 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbey97b7a172013-02-06 22:14:06 +0000221
Chris Lattner59c3abc2013-01-19 18:19:39 +0000222 // If this would read off the end of the bitcode file, just set the
223 // record to empty and return.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000224 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000225 Vals.append(NumElts, 0);
226 NextChar = BitStream->getBitcodeBytes().getExtent();
227 break;
228 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000229
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000230 // Otherwise, inform the streamer that we need these bytes in memory.
231 const char *Ptr = (const char*)
232 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbey97b7a172013-02-06 22:14:06 +0000233
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000234 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner3cf49cf2013-01-20 01:06:48 +0000235 if (Blob) {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000236 *Blob = StringRef(Ptr, NumElts);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000237 } else {
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000238 // Otherwise, unpack into Vals with zero extension.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000239 for (; NumElts; --NumElts)
Chris Lattner1aeca1e2013-01-21 18:24:49 +0000240 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000241 }
242 // Skip over tail padding.
Chris Lattner9221b8f2013-01-21 18:18:25 +0000243 JumpToBit(NewEnd);
Chris Lattner59c3abc2013-01-19 18:19:39 +0000244 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000245
Chris Lattner59c3abc2013-01-19 18:19:39 +0000246 return Code;
247}
248
249
250void BitstreamCursor::ReadAbbrevRecord() {
251 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
252 unsigned NumOpInfo = ReadVBR(5);
253 for (unsigned i = 0; i != NumOpInfo; ++i) {
254 bool IsLiteral = Read(1) ? true : false;
255 if (IsLiteral) {
256 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
257 continue;
258 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000259
Chris Lattner59c3abc2013-01-19 18:19:39 +0000260 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattner6c29cb32013-02-09 07:07:29 +0000261 if (BitCodeAbbrevOp::hasEncodingData(E)) {
262 unsigned Data = ReadVBR64(5);
263
264 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
265 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
266 // a slow path in Read() to have to handle reading zero bits.
267 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
268 Data == 0) {
269 Abbv->Add(BitCodeAbbrevOp(0));
270 continue;
271 }
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000272
Chris Lattner6c29cb32013-02-09 07:07:29 +0000273 Abbv->Add(BitCodeAbbrevOp(E, Data));
274 } else
Chris Lattner59c3abc2013-01-19 18:19:39 +0000275 Abbv->Add(BitCodeAbbrevOp(E));
276 }
277 CurAbbrevs.push_back(Abbv);
278}
279
280bool BitstreamCursor::ReadBlockInfoBlock() {
281 // If this is the second stream to get to the block info block, skip it.
282 if (BitStream->hasBlockInfoRecords())
283 return SkipBlock();
Joe Abbey97b7a172013-02-06 22:14:06 +0000284
Chris Lattner59c3abc2013-01-19 18:19:39 +0000285 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbey97b7a172013-02-06 22:14:06 +0000286
Chris Lattner59c3abc2013-01-19 18:19:39 +0000287 SmallVector<uint64_t, 64> Record;
Craig Topper2617dcc2014-04-15 06:32:26 +0000288 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
Joe Abbey97b7a172013-02-06 22:14:06 +0000289
Chris Lattner59c3abc2013-01-19 18:19:39 +0000290 // Read all the records for this module.
291 while (1) {
Chris Lattner27d38752013-01-20 02:13:19 +0000292 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbey97b7a172013-02-06 22:14:06 +0000293
Chris Lattner27d38752013-01-20 02:13:19 +0000294 switch (Entry.Kind) {
295 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
296 case llvm::BitstreamEntry::Error:
297 return true;
298 case llvm::BitstreamEntry::EndBlock:
299 return false;
300 case llvm::BitstreamEntry::Record:
301 // The interesting case.
302 break;
303 }
304
Chris Lattner59c3abc2013-01-19 18:19:39 +0000305 // Read abbrev records, associate them with CurBID.
Chris Lattner27d38752013-01-20 02:13:19 +0000306 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000307 if (!CurBlockInfo) return true;
308 ReadAbbrevRecord();
Joe Abbey97b7a172013-02-06 22:14:06 +0000309
Chris Lattner59c3abc2013-01-19 18:19:39 +0000310 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
311 // appropriate BlockInfo.
Benjamin Kramer6891ba02014-09-15 15:44:14 +0000312 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
Chris Lattner59c3abc2013-01-19 18:19:39 +0000313 CurAbbrevs.pop_back();
Chris Lattner59c3abc2013-01-19 18:19:39 +0000314 continue;
315 }
Joe Abbey97b7a172013-02-06 22:14:06 +0000316
Chris Lattner59c3abc2013-01-19 18:19:39 +0000317 // Read a record.
318 Record.clear();
Chris Lattner27d38752013-01-20 02:13:19 +0000319 switch (readRecord(Entry.ID, Record)) {
Chris Lattner59c3abc2013-01-19 18:19:39 +0000320 default: break; // Default behavior, ignore unknown content.
321 case bitc::BLOCKINFO_CODE_SETBID:
322 if (Record.size() < 1) return true;
323 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
324 break;
325 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
326 if (!CurBlockInfo) return true;
327 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
328 std::string Name;
329 for (unsigned i = 0, e = Record.size(); i != e; ++i)
330 Name += (char)Record[i];
331 CurBlockInfo->Name = Name;
332 break;
333 }
334 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
335 if (!CurBlockInfo) return true;
336 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
337 std::string Name;
338 for (unsigned i = 1, e = Record.size(); i != e; ++i)
339 Name += (char)Record[i];
340 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
341 Name));
342 break;
343 }
344 }
345 }
346}
347