blob: 942346b44e32e8d85582929de55222b17d52c59f [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;
Jakub Staszakc61e83e2013-02-19 09:48:30 +000029 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
Chris Lattner312c7d92013-01-19 18:19:39 +000030 CurAbbrevs[i]->addRef();
Joe Abbeyacb61942013-02-06 22:14:06 +000031
Chris Lattner312c7d92013-01-19 18:19:39 +000032 // Copy block scope and bump ref counts.
33 BlockScope = RHS.BlockScope;
Jakub Staszakc61e83e2013-02-19 09:48:30 +000034 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
Chris Lattner312c7d92013-01-19 18:19:39 +000035 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
Jakub Staszakc61e83e2013-02-19 09:48:30 +000036 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
Chris Lattner312c7d92013-01-19 18:19:39 +000037 Abbrevs[i]->addRef();
38 }
39}
40
41void BitstreamCursor::freeState() {
42 // Free all the Abbrevs.
Jakub Staszakc61e83e2013-02-19 09:48:30 +000043 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
Chris Lattner312c7d92013-01-19 18:19:39 +000044 CurAbbrevs[i]->dropRef();
45 CurAbbrevs.clear();
Joe Abbeyacb61942013-02-06 22:14:06 +000046
Chris Lattner312c7d92013-01-19 18:19:39 +000047 // Free all the Abbrevs in the block scope.
Jakub Staszakc61e83e2013-02-19 09:48:30 +000048 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
Chris Lattner312c7d92013-01-19 18:19:39 +000049 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
Jakub Staszakc61e83e2013-02-19 09:48:30 +000050 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
Chris Lattner312c7d92013-01-19 18:19:39 +000051 Abbrevs[i]->dropRef();
52 }
53 BlockScope.clear();
54}
55
56/// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
57/// the block, and return true if the block has an error.
58bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
59 // Save the current block's state on BlockScope.
60 BlockScope.push_back(Block(CurCodeSize));
61 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +000062
Chris Lattner312c7d92013-01-19 18:19:39 +000063 // Add the abbrevs specific to this block to the CurAbbrevs list.
64 if (const BitstreamReader::BlockInfo *Info =
65 BitStream->getBlockInfo(BlockID)) {
Jakub Staszakc61e83e2013-02-19 09:48:30 +000066 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
Chris Lattner312c7d92013-01-19 18:19:39 +000067 CurAbbrevs.push_back(Info->Abbrevs[i]);
68 CurAbbrevs.back()->addRef();
69 }
70 }
Joe Abbeyacb61942013-02-06 22:14:06 +000071
Chris Lattner312c7d92013-01-19 18:19:39 +000072 // Get the codesize of this block.
73 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
Chris Lattnerfd0543d2013-01-21 18:04:19 +000074 SkipToFourByteBoundary();
Chris Lattner312c7d92013-01-19 18:19:39 +000075 unsigned NumWords = Read(bitc::BlockSizeWidth);
76 if (NumWordsP) *NumWordsP = NumWords;
Joe Abbeyacb61942013-02-06 22:14:06 +000077
Chris Lattner312c7d92013-01-19 18:19:39 +000078 // Validate that this block is sane.
79 if (CurCodeSize == 0 || AtEndOfStream())
80 return true;
Joe Abbeyacb61942013-02-06 22:14:06 +000081
Chris Lattner312c7d92013-01-19 18:19:39 +000082 return false;
83}
84
Chris Lattnerf9147c42013-01-20 00:00:00 +000085void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
86 SmallVectorImpl<uint64_t> &Vals) {
87 assert(Op.isLiteral() && "Not a literal");
88 // If the abbrev specifies the literal value to use, use it.
89 Vals.push_back(Op.getLiteralValue());
90}
91
92void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
93 SmallVectorImpl<uint64_t> &Vals) {
94 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +000095
Chris Lattnerf9147c42013-01-20 00:00:00 +000096 // Decode the value as we are commanded.
97 switch (Op.getEncoding()) {
98 case BitCodeAbbrevOp::Array:
99 case BitCodeAbbrevOp::Blob:
100 assert(0 && "Should not reach here");
101 case BitCodeAbbrevOp::Fixed:
102 Vals.push_back(Read((unsigned)Op.getEncodingData()));
103 break;
104 case BitCodeAbbrevOp::VBR:
105 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
106 break;
107 case BitCodeAbbrevOp::Char6:
108 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
109 break;
110 }
111}
112
113void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
114 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
Joe Abbeyacb61942013-02-06 22:14:06 +0000115
Chris Lattnerf9147c42013-01-20 00:00:00 +0000116 // Decode the value as we are commanded.
117 switch (Op.getEncoding()) {
118 case BitCodeAbbrevOp::Array:
119 case BitCodeAbbrevOp::Blob:
120 assert(0 && "Should not reach here");
121 case BitCodeAbbrevOp::Fixed:
122 (void)Read((unsigned)Op.getEncodingData());
123 break;
124 case BitCodeAbbrevOp::VBR:
125 (void)ReadVBR64((unsigned)Op.getEncodingData());
126 break;
127 case BitCodeAbbrevOp::Char6:
128 (void)Read(6);
129 break;
130 }
131}
132
133
134
135/// skipRecord - Read the current record and discard it.
136void BitstreamCursor::skipRecord(unsigned AbbrevID) {
137 // Skip unabbreviated records by reading past their entries.
138 if (AbbrevID == bitc::UNABBREV_RECORD) {
139 unsigned Code = ReadVBR(6);
140 (void)Code;
141 unsigned NumElts = ReadVBR(6);
142 for (unsigned i = 0; i != NumElts; ++i)
143 (void)ReadVBR64(6);
144 return;
145 }
146
147 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbeyacb61942013-02-06 22:14:06 +0000148
Chris Lattnerf9147c42013-01-20 00:00:00 +0000149 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
150 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
151 if (Op.isLiteral())
152 continue;
Joe Abbeyacb61942013-02-06 22:14:06 +0000153
Chris Lattnerf9147c42013-01-20 00:00:00 +0000154 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
155 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
156 skipAbbreviatedField(Op);
157 continue;
158 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000159
Chris Lattnerf9147c42013-01-20 00:00:00 +0000160 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
161 // Array case. Read the number of elements as a vbr6.
162 unsigned NumElts = ReadVBR(6);
Joe Abbeyacb61942013-02-06 22:14:06 +0000163
Chris Lattnerf9147c42013-01-20 00:00:00 +0000164 // Get the element encoding.
165 assert(i+2 == e && "array op not second to last?");
166 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbeyacb61942013-02-06 22:14:06 +0000167
Chris Lattnerf9147c42013-01-20 00:00:00 +0000168 // Read all the elements.
169 for (; NumElts; --NumElts)
170 skipAbbreviatedField(EltEnc);
171 continue;
172 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000173
Chris Lattnerf9147c42013-01-20 00:00:00 +0000174 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
175 // Blob case. Read the number of bytes as a vbr6.
176 unsigned NumElts = ReadVBR(6);
Chris Lattnerfd0543d2013-01-21 18:04:19 +0000177 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000178
Chris Lattnerf9147c42013-01-20 00:00:00 +0000179 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000180 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000181
Chris Lattnerf9147c42013-01-20 00:00:00 +0000182 // If this would read off the end of the bitcode file, just set the
183 // record to empty and return.
Chris Lattner47543a82013-01-21 18:18:25 +0000184 if (!canSkipToPos(NewEnd/8)) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000185 NextChar = BitStream->getBitcodeBytes().getExtent();
186 break;
187 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000188
Chris Lattnerf9147c42013-01-20 00:00:00 +0000189 // Skip over the blob.
Chris Lattner47543a82013-01-21 18:18:25 +0000190 JumpToBit(NewEnd);
Chris Lattnerf9147c42013-01-20 00:00:00 +0000191 }
192}
Chris Lattner312c7d92013-01-19 18:19:39 +0000193
Chris Lattner194ef242013-01-20 01:06:48 +0000194unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
Chris Lattner312c7d92013-01-19 18:19:39 +0000195 SmallVectorImpl<uint64_t> &Vals,
Chris Lattner194ef242013-01-20 01:06:48 +0000196 StringRef *Blob) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000197 if (AbbrevID == bitc::UNABBREV_RECORD) {
198 unsigned Code = ReadVBR(6);
199 unsigned NumElts = ReadVBR(6);
200 for (unsigned i = 0; i != NumElts; ++i)
201 Vals.push_back(ReadVBR64(6));
202 return Code;
203 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000204
Chris Lattner312c7d92013-01-19 18:19:39 +0000205 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
Joe Abbeyacb61942013-02-06 22:14:06 +0000206
Chris Lattner312c7d92013-01-19 18:19:39 +0000207 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
208 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
209 if (Op.isLiteral()) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000210 readAbbreviatedLiteral(Op, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000211 continue;
212 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000213
Chris Lattner312c7d92013-01-19 18:19:39 +0000214 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
215 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
Chris Lattnerf9147c42013-01-20 00:00:00 +0000216 readAbbreviatedField(Op, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000217 continue;
218 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000219
Chris Lattner312c7d92013-01-19 18:19:39 +0000220 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
221 // Array case. Read the number of elements as a vbr6.
222 unsigned NumElts = ReadVBR(6);
Joe Abbeyacb61942013-02-06 22:14:06 +0000223
Chris Lattner312c7d92013-01-19 18:19:39 +0000224 // Get the element encoding.
225 assert(i+2 == e && "array op not second to last?");
226 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
Joe Abbeyacb61942013-02-06 22:14:06 +0000227
Chris Lattner312c7d92013-01-19 18:19:39 +0000228 // Read all the elements.
229 for (; NumElts; --NumElts)
Chris Lattnerf9147c42013-01-20 00:00:00 +0000230 readAbbreviatedField(EltEnc, Vals);
Chris Lattner312c7d92013-01-19 18:19:39 +0000231 continue;
232 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000233
Chris Lattner312c7d92013-01-19 18:19:39 +0000234 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
235 // Blob case. Read the number of bytes as a vbr6.
236 unsigned NumElts = ReadVBR(6);
Chris Lattnerfd0543d2013-01-21 18:04:19 +0000237 SkipToFourByteBoundary(); // 32-bit alignment
Joe Abbeyacb61942013-02-06 22:14:06 +0000238
Chris Lattner312c7d92013-01-19 18:19:39 +0000239 // Figure out where the end of this blob will be including tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000240 size_t CurBitPos = GetCurrentBitNo();
241 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
Joe Abbeyacb61942013-02-06 22:14:06 +0000242
Chris Lattner312c7d92013-01-19 18:19:39 +0000243 // If this would read off the end of the bitcode file, just set the
244 // record to empty and return.
Chris Lattner47543a82013-01-21 18:18:25 +0000245 if (!canSkipToPos(NewEnd/8)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000246 Vals.append(NumElts, 0);
247 NextChar = BitStream->getBitcodeBytes().getExtent();
248 break;
249 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000250
Chris Lattner69582cf2013-01-21 18:24:49 +0000251 // Otherwise, inform the streamer that we need these bytes in memory.
252 const char *Ptr = (const char*)
253 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
Joe Abbeyacb61942013-02-06 22:14:06 +0000254
Chris Lattner69582cf2013-01-21 18:24:49 +0000255 // If we can return a reference to the data, do so to avoid copying it.
Chris Lattner194ef242013-01-20 01:06:48 +0000256 if (Blob) {
Chris Lattner69582cf2013-01-21 18:24:49 +0000257 *Blob = StringRef(Ptr, NumElts);
Chris Lattner312c7d92013-01-19 18:19:39 +0000258 } else {
Chris Lattner69582cf2013-01-21 18:24:49 +0000259 // Otherwise, unpack into Vals with zero extension.
Chris Lattner47543a82013-01-21 18:18:25 +0000260 for (; NumElts; --NumElts)
Chris Lattner69582cf2013-01-21 18:24:49 +0000261 Vals.push_back((unsigned char)*Ptr++);
Chris Lattner312c7d92013-01-19 18:19:39 +0000262 }
263 // Skip over tail padding.
Chris Lattner47543a82013-01-21 18:18:25 +0000264 JumpToBit(NewEnd);
Chris Lattner312c7d92013-01-19 18:19:39 +0000265 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000266
Chris Lattner312c7d92013-01-19 18:19:39 +0000267 unsigned Code = (unsigned)Vals[0];
268 Vals.erase(Vals.begin());
269 return Code;
270}
271
272
273void BitstreamCursor::ReadAbbrevRecord() {
274 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
275 unsigned NumOpInfo = ReadVBR(5);
276 for (unsigned i = 0; i != NumOpInfo; ++i) {
277 bool IsLiteral = Read(1) ? true : false;
278 if (IsLiteral) {
279 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
280 continue;
281 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000282
Chris Lattner312c7d92013-01-19 18:19:39 +0000283 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
Chris Lattnerb24f5b72013-02-09 07:07:29 +0000284 if (BitCodeAbbrevOp::hasEncodingData(E)) {
285 unsigned Data = ReadVBR64(5);
286
287 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
288 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
289 // a slow path in Read() to have to handle reading zero bits.
290 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
291 Data == 0) {
292 Abbv->Add(BitCodeAbbrevOp(0));
293 continue;
294 }
295
296 Abbv->Add(BitCodeAbbrevOp(E, Data));
297 } else
Chris Lattner312c7d92013-01-19 18:19:39 +0000298 Abbv->Add(BitCodeAbbrevOp(E));
299 }
300 CurAbbrevs.push_back(Abbv);
301}
302
303bool BitstreamCursor::ReadBlockInfoBlock() {
304 // If this is the second stream to get to the block info block, skip it.
305 if (BitStream->hasBlockInfoRecords())
306 return SkipBlock();
Joe Abbeyacb61942013-02-06 22:14:06 +0000307
Chris Lattner312c7d92013-01-19 18:19:39 +0000308 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
Joe Abbeyacb61942013-02-06 22:14:06 +0000309
Chris Lattner312c7d92013-01-19 18:19:39 +0000310 SmallVector<uint64_t, 64> Record;
311 BitstreamReader::BlockInfo *CurBlockInfo = 0;
Joe Abbeyacb61942013-02-06 22:14:06 +0000312
Chris Lattner312c7d92013-01-19 18:19:39 +0000313 // Read all the records for this module.
314 while (1) {
Chris Lattner5a4251c2013-01-20 02:13:19 +0000315 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
Joe Abbeyacb61942013-02-06 22:14:06 +0000316
Chris Lattner5a4251c2013-01-20 02:13:19 +0000317 switch (Entry.Kind) {
318 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
319 case llvm::BitstreamEntry::Error:
320 return true;
321 case llvm::BitstreamEntry::EndBlock:
322 return false;
323 case llvm::BitstreamEntry::Record:
324 // The interesting case.
325 break;
326 }
327
Chris Lattner312c7d92013-01-19 18:19:39 +0000328 // Read abbrev records, associate them with CurBID.
Chris Lattner5a4251c2013-01-20 02:13:19 +0000329 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000330 if (!CurBlockInfo) return true;
331 ReadAbbrevRecord();
Joe Abbeyacb61942013-02-06 22:14:06 +0000332
Chris Lattner312c7d92013-01-19 18:19:39 +0000333 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
334 // appropriate BlockInfo.
335 BitCodeAbbrev *Abbv = CurAbbrevs.back();
336 CurAbbrevs.pop_back();
337 CurBlockInfo->Abbrevs.push_back(Abbv);
338 continue;
339 }
Joe Abbeyacb61942013-02-06 22:14:06 +0000340
Chris Lattner312c7d92013-01-19 18:19:39 +0000341 // Read a record.
342 Record.clear();
Chris Lattner5a4251c2013-01-20 02:13:19 +0000343 switch (readRecord(Entry.ID, Record)) {
Chris Lattner312c7d92013-01-19 18:19:39 +0000344 default: break; // Default behavior, ignore unknown content.
345 case bitc::BLOCKINFO_CODE_SETBID:
346 if (Record.size() < 1) return true;
347 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
348 break;
349 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
350 if (!CurBlockInfo) return true;
351 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
352 std::string Name;
353 for (unsigned i = 0, e = Record.size(); i != e; ++i)
354 Name += (char)Record[i];
355 CurBlockInfo->Name = Name;
356 break;
357 }
358 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
359 if (!CurBlockInfo) return true;
360 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
361 std::string Name;
362 for (unsigned i = 1, e = Record.size(); i != e; ++i)
363 Name += (char)Record[i];
364 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
365 Name));
366 break;
367 }
368 }
369 }
370}
371