1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Bitcode/BitstreamReader.h"
14 //===----------------------------------------------------------------------===//
15 // BitstreamCursor implementation
16 //===----------------------------------------------------------------------===//
18 void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
21 BitStream = RHS.BitStream;
22 NextChar = RHS.NextChar;
23 CurWord = RHS.CurWord;
24 BitsInCurWord = RHS.BitsInCurWord;
25 CurCodeSize = RHS.CurCodeSize;
27 // Copy abbreviations, and bump ref counts.
28 CurAbbrevs = RHS.CurAbbrevs;
29 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
31 CurAbbrevs[i]->addRef();
33 // Copy block scope and bump ref counts.
34 BlockScope = RHS.BlockScope;
35 for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
37 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
38 for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
44 void BitstreamCursor::freeState() {
45 // Free all the Abbrevs.
46 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
48 CurAbbrevs[i]->dropRef();
51 // Free all the Abbrevs in the block scope.
52 for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
54 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
55 for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
57 Abbrevs[i]->dropRef();
62 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
63 /// the block, and return true if the block has an error.
64 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
65 // Save the current block's state on BlockScope.
66 BlockScope.push_back(Block(CurCodeSize));
67 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
69 // Add the abbrevs specific to this block to the CurAbbrevs list.
70 if (const BitstreamReader::BlockInfo *Info =
71 BitStream->getBlockInfo(BlockID)) {
72 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
74 CurAbbrevs.push_back(Info->Abbrevs[i]);
75 CurAbbrevs.back()->addRef();
79 // Get the codesize of this block.
80 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
82 unsigned NumWords = Read(bitc::BlockSizeWidth);
83 if (NumWordsP) *NumWordsP = NumWords;
85 // Validate that this block is sane.
86 if (CurCodeSize == 0 || AtEndOfStream())
92 void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
93 SmallVectorImpl<uint64_t> &Vals) {
94 assert(Op.isLiteral() && "Not a literal");
95 // If the abbrev specifies the literal value to use, use it.
96 Vals.push_back(Op.getLiteralValue());
99 void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
100 SmallVectorImpl<uint64_t> &Vals) {
101 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
103 // Decode the value as we are commanded.
104 switch (Op.getEncoding()) {
105 case BitCodeAbbrevOp::Array:
106 case BitCodeAbbrevOp::Blob:
107 assert(0 && "Should not reach here");
108 case BitCodeAbbrevOp::Fixed:
109 Vals.push_back(Read((unsigned)Op.getEncodingData()));
111 case BitCodeAbbrevOp::VBR:
112 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
114 case BitCodeAbbrevOp::Char6:
115 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
120 void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
121 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
123 // Decode the value as we are commanded.
124 switch (Op.getEncoding()) {
125 case BitCodeAbbrevOp::Array:
126 case BitCodeAbbrevOp::Blob:
127 assert(0 && "Should not reach here");
128 case BitCodeAbbrevOp::Fixed:
129 (void)Read((unsigned)Op.getEncodingData());
131 case BitCodeAbbrevOp::VBR:
132 (void)ReadVBR64((unsigned)Op.getEncodingData());
134 case BitCodeAbbrevOp::Char6:
142 /// skipRecord - Read the current record and discard it.
143 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
144 // Skip unabbreviated records by reading past their entries.
145 if (AbbrevID == bitc::UNABBREV_RECORD) {
146 unsigned Code = ReadVBR(6);
148 unsigned NumElts = ReadVBR(6);
149 for (unsigned i = 0; i != NumElts; ++i)
154 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
156 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
157 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
161 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
162 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
163 skipAbbreviatedField(Op);
167 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
168 // Array case. Read the number of elements as a vbr6.
169 unsigned NumElts = ReadVBR(6);
171 // Get the element encoding.
172 assert(i+2 == e && "array op not second to last?");
173 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
175 // Read all the elements.
176 for (; NumElts; --NumElts)
177 skipAbbreviatedField(EltEnc);
181 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
182 // Blob case. Read the number of bytes as a vbr6.
183 unsigned NumElts = ReadVBR(6);
184 SkipToWord(); // 32-bit alignment
186 // Figure out where the end of this blob will be including tail padding.
187 size_t NewEnd = NextChar+((NumElts+3)&~3);
189 // If this would read off the end of the bitcode file, just set the
190 // record to empty and return.
191 if (!canSkipToPos(NewEnd)) {
192 NextChar = BitStream->getBitcodeBytes().getExtent();
196 // Skip over the blob.
201 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
202 SmallVectorImpl<uint64_t> &Vals,
204 if (AbbrevID == bitc::UNABBREV_RECORD) {
205 unsigned Code = ReadVBR(6);
206 unsigned NumElts = ReadVBR(6);
207 for (unsigned i = 0; i != NumElts; ++i)
208 Vals.push_back(ReadVBR64(6));
212 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
214 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
215 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
216 if (Op.isLiteral()) {
217 readAbbreviatedLiteral(Op, Vals);
221 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
222 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
223 readAbbreviatedField(Op, Vals);
227 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
228 // Array case. Read the number of elements as a vbr6.
229 unsigned NumElts = ReadVBR(6);
231 // Get the element encoding.
232 assert(i+2 == e && "array op not second to last?");
233 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
235 // Read all the elements.
236 for (; NumElts; --NumElts)
237 readAbbreviatedField(EltEnc, Vals);
241 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
242 // Blob case. Read the number of bytes as a vbr6.
243 unsigned NumElts = ReadVBR(6);
244 SkipToWord(); // 32-bit alignment
246 // Figure out where the end of this blob will be including tail padding.
247 size_t NewEnd = NextChar+((NumElts+3)&~3);
249 // If this would read off the end of the bitcode file, just set the
250 // record to empty and return.
251 if (!canSkipToPos(NewEnd)) {
252 Vals.append(NumElts, 0);
253 NextChar = BitStream->getBitcodeBytes().getExtent();
257 // Otherwise, read the number of bytes. If we can return a reference to
258 // the data, do so to avoid copying it.
261 StringRef((const char*)BitStream->getBitcodeBytes().getPointer(
265 for (; NumElts; ++NextChar, --NumElts)
266 Vals.push_back(getByte(NextChar));
268 // Skip over tail padding.
272 unsigned Code = (unsigned)Vals[0];
273 Vals.erase(Vals.begin());
278 void BitstreamCursor::ReadAbbrevRecord() {
279 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
280 unsigned NumOpInfo = ReadVBR(5);
281 for (unsigned i = 0; i != NumOpInfo; ++i) {
282 bool IsLiteral = Read(1) ? true : false;
284 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
288 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
289 if (BitCodeAbbrevOp::hasEncodingData(E))
290 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
292 Abbv->Add(BitCodeAbbrevOp(E));
294 CurAbbrevs.push_back(Abbv);
297 bool BitstreamCursor::ReadBlockInfoBlock() {
298 // If this is the second stream to get to the block info block, skip it.
299 if (BitStream->hasBlockInfoRecords())
302 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
304 SmallVector<uint64_t, 64> Record;
305 BitstreamReader::BlockInfo *CurBlockInfo = 0;
307 // Read all the records for this module.
309 unsigned Code = ReadCode();
310 if (Code == bitc::END_BLOCK)
311 return ReadBlockEnd();
312 if (Code == bitc::ENTER_SUBBLOCK) {
314 if (SkipBlock()) return true;
318 // Read abbrev records, associate them with CurBID.
319 if (Code == bitc::DEFINE_ABBREV) {
320 if (!CurBlockInfo) return true;
323 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
324 // appropriate BlockInfo.
325 BitCodeAbbrev *Abbv = CurAbbrevs.back();
326 CurAbbrevs.pop_back();
327 CurBlockInfo->Abbrevs.push_back(Abbv);
333 switch (ReadRecord(Code, Record)) {
334 default: break; // Default behavior, ignore unknown content.
335 case bitc::BLOCKINFO_CODE_SETBID:
336 if (Record.size() < 1) return true;
337 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
339 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
340 if (!CurBlockInfo) return true;
341 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
343 for (unsigned i = 0, e = Record.size(); i != e; ++i)
344 Name += (char)Record[i];
345 CurBlockInfo->Name = Name;
348 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
349 if (!CurBlockInfo) return true;
350 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
352 for (unsigned i = 1, e = Record.size(); i != e; ++i)
353 Name += (char)Record[i];
354 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],