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::freeState() {
19 // Free all the Abbrevs.
22 // Free all the Abbrevs in the block scope.
26 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27 /// the block, and return true if the block has an error.
28 bool 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);
33 // Add the abbrevs specific to this block to the CurAbbrevs list.
34 if (const BitstreamReader::BlockInfo *Info =
35 BitStream->getBlockInfo(BlockID)) {
36 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
40 // Get the codesize of this block.
41 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
42 SkipToFourByteBoundary();
43 unsigned NumWords = Read(bitc::BlockSizeWidth);
44 if (NumWordsP) *NumWordsP = NumWords;
46 // Validate that this block is sane.
47 if (CurCodeSize == 0 || AtEndOfStream())
53 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
54 const BitCodeAbbrevOp &Op) {
55 assert(!Op.isLiteral() && "Not to be used with literals!");
57 // Decode the value as we are commanded.
58 switch (Op.getEncoding()) {
59 case BitCodeAbbrevOp::Array:
60 case BitCodeAbbrevOp::Blob:
61 llvm_unreachable("Should not reach here");
62 case BitCodeAbbrevOp::Fixed:
63 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
64 return Cursor.Read((unsigned)Op.getEncodingData());
65 case BitCodeAbbrevOp::VBR:
66 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
67 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
68 case BitCodeAbbrevOp::Char6:
69 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
71 llvm_unreachable("invalid abbreviation encoding");
74 static void skipAbbreviatedField(BitstreamCursor &Cursor,
75 const BitCodeAbbrevOp &Op) {
76 assert(!Op.isLiteral() && "Not to be used with literals!");
78 // Decode the value as we are commanded.
79 switch (Op.getEncoding()) {
80 case BitCodeAbbrevOp::Array:
81 case BitCodeAbbrevOp::Blob:
82 llvm_unreachable("Should not reach here");
83 case BitCodeAbbrevOp::Fixed:
84 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
85 Cursor.Read((unsigned)Op.getEncodingData());
87 case BitCodeAbbrevOp::VBR:
88 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
89 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
91 case BitCodeAbbrevOp::Char6:
99 /// skipRecord - Read the current record and discard it.
100 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
101 // Skip unabbreviated records by reading past their entries.
102 if (AbbrevID == bitc::UNABBREV_RECORD) {
103 unsigned Code = ReadVBR(6);
105 unsigned NumElts = ReadVBR(6);
106 for (unsigned i = 0; i != NumElts; ++i)
111 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
113 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
114 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
118 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
119 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
120 skipAbbreviatedField(*this, Op);
124 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
125 // Array case. Read the number of elements as a vbr6.
126 unsigned NumElts = ReadVBR(6);
128 // Get the element encoding.
129 assert(i+2 == e && "array op not second to last?");
130 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
132 // Read all the elements.
133 for (; NumElts; --NumElts)
134 skipAbbreviatedField(*this, EltEnc);
138 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
139 // Blob case. Read the number of bytes as a vbr6.
140 unsigned NumElts = ReadVBR(6);
141 SkipToFourByteBoundary(); // 32-bit alignment
143 // Figure out where the end of this blob will be including tail padding.
144 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
146 // If this would read off the end of the bitcode file, just set the
147 // record to empty and return.
148 if (!canSkipToPos(NewEnd/8)) {
149 NextChar = BitStream->getBitcodeBytes().getExtent();
153 // Skip over the blob.
158 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
159 SmallVectorImpl<uint64_t> &Vals,
161 if (AbbrevID == bitc::UNABBREV_RECORD) {
162 unsigned Code = ReadVBR(6);
163 unsigned NumElts = ReadVBR(6);
164 for (unsigned i = 0; i != NumElts; ++i)
165 Vals.push_back(ReadVBR64(6));
169 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
171 // Read the record code first.
172 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
173 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
175 if (CodeOp.isLiteral())
176 Code = CodeOp.getLiteralValue();
178 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
179 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
180 report_fatal_error("Abbreviation starts with an Array or a Blob");
181 Code = readAbbreviatedField(*this, CodeOp);
184 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
185 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
186 if (Op.isLiteral()) {
187 Vals.push_back(Op.getLiteralValue());
191 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
192 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
193 Vals.push_back(readAbbreviatedField(*this, Op));
197 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
198 // Array case. Read the number of elements as a vbr6.
199 unsigned NumElts = ReadVBR(6);
201 // Get the element encoding.
202 assert(i+2 == e && "array op not second to last?");
203 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
205 // Read all the elements.
206 for (; NumElts; --NumElts)
207 Vals.push_back(readAbbreviatedField(*this, EltEnc));
211 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
212 // Blob case. Read the number of bytes as a vbr6.
213 unsigned NumElts = ReadVBR(6);
214 SkipToFourByteBoundary(); // 32-bit alignment
216 // Figure out where the end of this blob will be including tail padding.
217 size_t CurBitPos = GetCurrentBitNo();
218 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
220 // If this would read off the end of the bitcode file, just set the
221 // record to empty and return.
222 if (!canSkipToPos(NewEnd/8)) {
223 Vals.append(NumElts, 0);
224 NextChar = BitStream->getBitcodeBytes().getExtent();
228 // Otherwise, inform the streamer that we need these bytes in memory.
229 const char *Ptr = (const char*)
230 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
232 // If we can return a reference to the data, do so to avoid copying it.
234 *Blob = StringRef(Ptr, NumElts);
236 // Otherwise, unpack into Vals with zero extension.
237 for (; NumElts; --NumElts)
238 Vals.push_back((unsigned char)*Ptr++);
240 // Skip over tail padding.
248 void BitstreamCursor::ReadAbbrevRecord() {
249 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
250 unsigned NumOpInfo = ReadVBR(5);
251 for (unsigned i = 0; i != NumOpInfo; ++i) {
252 bool IsLiteral = Read(1);
254 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
258 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
259 if (BitCodeAbbrevOp::hasEncodingData(E)) {
260 uint64_t Data = ReadVBR64(5);
262 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
263 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
264 // a slow path in Read() to have to handle reading zero bits.
265 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
267 Abbv->Add(BitCodeAbbrevOp(0));
271 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
274 "Fixed or VBR abbrev record with size > MaxChunkData");
276 Abbv->Add(BitCodeAbbrevOp(E, Data));
278 Abbv->Add(BitCodeAbbrevOp(E));
280 CurAbbrevs.push_back(Abbv);
283 bool BitstreamCursor::ReadBlockInfoBlock() {
284 // If this is the second stream to get to the block info block, skip it.
285 if (BitStream->hasBlockInfoRecords())
288 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
290 SmallVector<uint64_t, 64> Record;
291 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
293 // Read all the records for this module.
295 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
297 switch (Entry.Kind) {
298 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
299 case llvm::BitstreamEntry::Error:
301 case llvm::BitstreamEntry::EndBlock:
303 case llvm::BitstreamEntry::Record:
304 // The interesting case.
308 // Read abbrev records, associate them with CurBID.
309 if (Entry.ID == bitc::DEFINE_ABBREV) {
310 if (!CurBlockInfo) return true;
313 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
314 // appropriate BlockInfo.
315 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
316 CurAbbrevs.pop_back();
322 switch (readRecord(Entry.ID, Record)) {
323 default: break; // Default behavior, ignore unknown content.
324 case bitc::BLOCKINFO_CODE_SETBID:
325 if (Record.size() < 1) return true;
326 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
328 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
329 if (!CurBlockInfo) return true;
330 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
332 for (unsigned i = 0, e = Record.size(); i != e; ++i)
333 Name += (char)Record[i];
334 CurBlockInfo->Name = Name;
337 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
338 if (!CurBlockInfo) return true;
339 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
341 for (unsigned i = 1, e = Record.size(); i != e; ++i)
342 Name += (char)Record[i];
343 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],