blob: 5e3232e531351ef66f0e6d0f90f587b94c95dc27 [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
Chris Lattner312c7d92013-01-19 18:19:39 +000018void BitstreamCursor::freeState() {
19 // Free all the Abbrevs.
Chris Lattner312c7d92013-01-19 18:19:39 +000020 CurAbbrevs.clear();
Joe Abbeyacb61942013-02-06 22:14:06 +000021
Chris Lattner312c7d92013-01-19 18:19:39 +000022 // Free all the Abbrevs in the block scope.
Chris Lattner312c7d92013-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 Abbeyacb61942013-02-06 22:14:06 +000032
Chris Lattner312c7d92013-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)) {
Stephen Hines37ed9c12014-12-01 14:51:49 -080036 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
37 Info->Abbrevs.end());
Chris Lattner312c7d92013-01-19 18:19:39 +000038 }
Joe Abbeyacb61942013-02-06 22:14:06 +000039
Chris Lattner312c7d92013-01-19 18:19:39 +000040 // Get the codesize of this block.
41 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
Chris Lattnerfd0543d2013-01-21 18:04:19 +000042 SkipToFourByteBoundary();
Chris Lattner312c7d92013-01-19 18:19:39 +000043 unsigned NumWords = Read(bitc::BlockSizeWidth);
44 if (NumWordsP) *NumWordsP = NumWords;
Joe Abbeyacb61942013-02-06 22:14:06 +000045
Chris Lattner312c7d92013-01-19 18:19:39 +000046 // Validate that this block is sane.
47 if (CurCodeSize == 0 || AtEndOfStream())
48 return true;
Joe Abbeyacb61942013-02-06 22:14:06 +000049
Chris Lattner312c7d92013-01-19 18:19:39 +000050 return false;
51}
52
Stephen Hines37ed9c12014-12-01 14:51:49 -080053static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
54 const BitCodeAbbrevOp &Op) {
55 assert(!Op.isLiteral() && "Not to be used with literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +000056
Chris Lattnerf9147c42013-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:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070061 llvm_unreachable("Should not reach here");
Chris Lattnerf9147c42013-01-20 00:00:00 +000062 case BitCodeAbbrevOp::Fixed:
Stephen Hines37ed9c12014-12-01 14:51:49 -080063 return Cursor.Read((unsigned)Op.getEncodingData());
Chris Lattnerf9147c42013-01-20 00:00:00 +000064 case BitCodeAbbrevOp::VBR:
Stephen Hines37ed9c12014-12-01 14:51:49 -080065 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattnerf9147c42013-01-20 00:00:00 +000066 case BitCodeAbbrevOp::Char6:
Stephen Hines37ed9c12014-12-01 14:51:49 -080067 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
Chris Lattnerf9147c42013-01-20 00:00:00 +000068 }
Stephen Hines37ed9c12014-12-01 14:51:49 -080069 llvm_unreachable("invalid abbreviation encoding");
Chris Lattnerf9147c42013-01-20 00:00:00 +000070}
71
Stephen Hines37ed9c12014-12-01 14:51:49 -080072static void skipAbbreviatedField(BitstreamCursor &Cursor,
73 const BitCodeAbbrevOp &Op) {
74 assert(!Op.isLiteral() && "Not to be used with literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +000075
Chris Lattnerf9147c42013-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:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070080 llvm_unreachable("Should not reach here");
Chris Lattnerf9147c42013-01-20 00:00:00 +000081 case BitCodeAbbrevOp::Fixed:
Stephen Hines37ed9c12014-12-01 14:51:49 -080082 Cursor.Read((unsigned)Op.getEncodingData());
Chris Lattnerf9147c42013-01-20 00:00:00 +000083 break;
84 case BitCodeAbbrevOp::VBR:
Stephen Hines37ed9c12014-12-01 14:51:49 -080085 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
Chris Lattnerf9147c42013-01-20 00:00:00 +000086 break;
87 case BitCodeAbbrevOp::Char6:
Stephen Hines37ed9c12014-12-01 14:51:49 -080088 Cursor.Read(6);
Chris Lattnerf9147c42013-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 Abbeyacb61942013-02-06 22:14:06 +0000108
Chris Lattnerf9147c42013-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 Abbeyacb61942013-02-06 22:14:06 +0000113
Chris Lattnerf9147c42013-01-20 00:00:00 +0000114 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
115 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800116 skipAbbreviatedField(*this, Op);
Chris Lattnerf9147c42013-01-20 00:00:00 +0000117 continue;
118 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000119
Chris Lattnerf9147c42013-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 Abbeyacb61942013-02-06 22:14:06 +0000123
Chris Lattnerf9147c42013-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 Abbeyacb61942013-02-06 22:14:06 +0000127
Chris Lattnerf9147c42013-01-20 00:00:00 +0000128 // Read all the elements.
129 for (; NumElts; --NumElts)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800130 skipAbbreviatedField(*this, EltEnc);
Chris Lattnerf9147c42013-01-20 00:00:00 +0000131 continue;
132 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000133
Chris Lattnerf9147c42013-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 Lattnerfd0543d2013-01-21 18:04:19 +0000137 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000138
Chris Lattnerf9147c42013-01-20 00:00:00 +0000139 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000140 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000141
Chris Lattnerf9147c42013-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 Lattner47543a82013-01-21 18:18:25 +0000144 if (!canSkipToPos(NewEnd/8)) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000145 NextChar = BitStream->getBitcodeBytes().getExtent();
146 break;
147 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000148
Chris Lattnerf9147c42013-01-20 00:00:00 +0000149 // Skip over the blob.
Chris Lattner47543a82013-01-21 18:18:25 +0000150 JumpToBit(NewEnd);
Chris Lattnerf9147c42013-01-20 00:00:00 +0000151 }
152}
Chris Lattner312c7d92013-01-19 18:19:39 +0000153
Chris Lattner194ef242013-01-20 01:06:48 +0000154unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner312c7d92013-01-19 18:19:39 +0000155 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner194ef242013-01-20 01:06:48 +0000156 StringRef *Blob) {
Chris Lattner312c7d92013-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 Abbeyacb61942013-02-06 22:14:06 +0000164
Chris Lattner312c7d92013-01-19 18:19:39 +0000165 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbeyacb61942013-02-06 22:14:06 +0000166
Jordan Rose1197e382013-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);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800170 unsigned Code;
Jordan Rose1197e382013-05-10 22:17:10 +0000171 if (CodeOp.isLiteral())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800172 Code = CodeOp.getLiteralValue();
Jordan Rose1197e382013-05-10 22:17:10 +0000173 else
Stephen Hines37ed9c12014-12-01 14:51:49 -0800174 Code = readAbbreviatedField(*this, CodeOp);
Jordan Rose1197e382013-05-10 22:17:10 +0000175
176 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000177 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
178 if (Op.isLiteral()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800179 Vals.push_back(Op.getLiteralValue());
Chris Lattner312c7d92013-01-19 18:19:39 +0000180 continue;
181 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000182
Chris Lattner312c7d92013-01-19 18:19:39 +0000183 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
184 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800185 Vals.push_back(readAbbreviatedField(*this, Op));
Chris Lattner312c7d92013-01-19 18:19:39 +0000186 continue;
187 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000188
Chris Lattner312c7d92013-01-19 18:19:39 +0000189 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
190 // Array case. Read the number of elements as a vbr6.
191 unsigned NumElts = ReadVBR(6);
Joe Abbeyacb61942013-02-06 22:14:06 +0000192
Chris Lattner312c7d92013-01-19 18:19:39 +0000193 // Get the element encoding.
194 assert(i+2 == e && "array op not second to last?");
195 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbeyacb61942013-02-06 22:14:06 +0000196
Chris Lattner312c7d92013-01-19 18:19:39 +0000197 // Read all the elements.
198 for (; NumElts; --NumElts)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800199 Vals.push_back(readAbbreviatedField(*this, EltEnc));
Chris Lattner312c7d92013-01-19 18:19:39 +0000200 continue;
201 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000202
Chris Lattner312c7d92013-01-19 18:19:39 +0000203 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
204 // Blob case. Read the number of bytes as a vbr6.
205 unsigned NumElts = ReadVBR(6);
Chris Lattnerfd0543d2013-01-21 18:04:19 +0000206 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000207
Chris Lattner312c7d92013-01-19 18:19:39 +0000208 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000209 size_t CurBitPos = GetCurrentBitNo();
210 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000211
Chris Lattner312c7d92013-01-19 18:19:39 +0000212 // If this would read off the end of the bitcode file, just set the
213 // record to empty and return.
Chris Lattner47543a82013-01-21 18:18:25 +0000214 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000215 Vals.append(NumElts, 0);
216 NextChar = BitStream->getBitcodeBytes().getExtent();
217 break;
218 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000219
Chris Lattner69582cf2013-01-21 18:24:49 +0000220 // Otherwise, inform the streamer that we need these bytes in memory.
221 const char *Ptr = (const char*)
222 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbeyacb61942013-02-06 22:14:06 +0000223
Chris Lattner69582cf2013-01-21 18:24:49 +0000224 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner194ef242013-01-20 01:06:48 +0000225 if (Blob) {
Chris Lattner69582cf2013-01-21 18:24:49 +0000226 *Blob = StringRef(Ptr, NumElts);
Chris Lattner312c7d92013-01-19 18:19:39 +0000227 } else {
Chris Lattner69582cf2013-01-21 18:24:49 +0000228 // Otherwise, unpack into Vals with zero extension.
Chris Lattner47543a82013-01-21 18:18:25 +0000229 for (; NumElts; --NumElts)
Chris Lattner69582cf2013-01-21 18:24:49 +0000230 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner312c7d92013-01-19 18:19:39 +0000231 }
232 // Skip over tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000233 JumpToBit(NewEnd);
Chris Lattner312c7d92013-01-19 18:19:39 +0000234 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000235
Chris Lattner312c7d92013-01-19 18:19:39 +0000236 return Code;
237}
238
239
240void BitstreamCursor::ReadAbbrevRecord() {
241 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
242 unsigned NumOpInfo = ReadVBR(5);
243 for (unsigned i = 0; i != NumOpInfo; ++i) {
244 bool IsLiteral = Read(1) ? true : false;
245 if (IsLiteral) {
246 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
247 continue;
248 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000249
Chris Lattner312c7d92013-01-19 18:19:39 +0000250 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattnerb24f5b72013-02-09 07:07:29 +0000251 if (BitCodeAbbrevOp::hasEncodingData(E)) {
252 unsigned Data = ReadVBR64(5);
253
254 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
255 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
256 // a slow path in Read() to have to handle reading zero bits.
257 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
258 Data == 0) {
259 Abbv->Add(BitCodeAbbrevOp(0));
260 continue;
261 }
Joe Abbeyef7964c2013-04-01 02:28:07 +0000262
Chris Lattnerb24f5b72013-02-09 07:07:29 +0000263 Abbv->Add(BitCodeAbbrevOp(E, Data));
264 } else
Chris Lattner312c7d92013-01-19 18:19:39 +0000265 Abbv->Add(BitCodeAbbrevOp(E));
266 }
267 CurAbbrevs.push_back(Abbv);
268}
269
270bool BitstreamCursor::ReadBlockInfoBlock() {
271 // If this is the second stream to get to the block info block, skip it.
272 if (BitStream->hasBlockInfoRecords())
273 return SkipBlock();
Joe Abbeyacb61942013-02-06 22:14:06 +0000274
Chris Lattner312c7d92013-01-19 18:19:39 +0000275 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbeyacb61942013-02-06 22:14:06 +0000276
Chris Lattner312c7d92013-01-19 18:19:39 +0000277 SmallVector<uint64_t, 64> Record;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700278 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
Joe Abbeyacb61942013-02-06 22:14:06 +0000279
Chris Lattner312c7d92013-01-19 18:19:39 +0000280 // Read all the records for this module.
281 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000282 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +0000283
Chris Lattner5a4251c2013-01-20 02:13:19 +0000284 switch (Entry.Kind) {
285 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
286 case llvm::BitstreamEntry::Error:
287 return true;
288 case llvm::BitstreamEntry::EndBlock:
289 return false;
290 case llvm::BitstreamEntry::Record:
291 // The interesting case.
292 break;
293 }
294
Chris Lattner312c7d92013-01-19 18:19:39 +0000295 // Read abbrev records, associate them with CurBID.
Chris Lattner5a4251c2013-01-20 02:13:19 +0000296 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000297 if (!CurBlockInfo) return true;
298 ReadAbbrevRecord();
Joe Abbeyacb61942013-02-06 22:14:06 +0000299
Chris Lattner312c7d92013-01-19 18:19:39 +0000300 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
301 // appropriate BlockInfo.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800302 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
Chris Lattner312c7d92013-01-19 18:19:39 +0000303 CurAbbrevs.pop_back();
Chris Lattner312c7d92013-01-19 18:19:39 +0000304 continue;
305 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000306
Chris Lattner312c7d92013-01-19 18:19:39 +0000307 // Read a record.
308 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000309 switch (readRecord(Entry.ID, Record)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000310 default: break; // Default behavior, ignore unknown content.
311 case bitc::BLOCKINFO_CODE_SETBID:
312 if (Record.size() < 1) return true;
313 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
314 break;
315 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
316 if (!CurBlockInfo) return true;
317 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
318 std::string Name;
319 for (unsigned i = 0, e = Record.size(); i != e; ++i)
320 Name += (char)Record[i];
321 CurBlockInfo->Name = Name;
322 break;
323 }
324 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
325 if (!CurBlockInfo) return true;
326 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
327 std::string Name;
328 for (unsigned i = 1, e = Record.size(); i != e; ++i)
329 Name += (char)Record[i];
330 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
331 Name));
332 break;
333 }
334 }
335 }
336}
337