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 return Cursor.Read((unsigned)Op.getEncodingData());
64 case BitCodeAbbrevOp::VBR:
65 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
66 case BitCodeAbbrevOp::Char6:
67 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
69 llvm_unreachable("invalid abbreviation encoding");
72 static void skipAbbreviatedField(BitstreamCursor &Cursor,
73 const BitCodeAbbrevOp &Op) {
74 assert(!Op.isLiteral() && "Not to be used with literals!");
76 // Decode the value as we are commanded.
77 switch (Op.getEncoding()) {
78 case BitCodeAbbrevOp::Array:
79 case BitCodeAbbrevOp::Blob:
80 llvm_unreachable("Should not reach here");
81 case BitCodeAbbrevOp::Fixed:
82 Cursor.Read((unsigned)Op.getEncodingData());
84 case BitCodeAbbrevOp::VBR:
85 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
87 case BitCodeAbbrevOp::Char6:
95 /// skipRecord - Read the current record and discard it.
96 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
97 // Skip unabbreviated records by reading past their entries.
98 if (AbbrevID == bitc::UNABBREV_RECORD) {
99 unsigned Code = ReadVBR(6);
101 unsigned NumElts = ReadVBR(6);
102 for (unsigned i = 0; i != NumElts; ++i)
107 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
109 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
110 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
114 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
115 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
116 skipAbbreviatedField(*this, Op);
120 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
121 // Array case. Read the number of elements as a vbr6.
122 unsigned NumElts = ReadVBR(6);
124 // Get the element encoding.
125 assert(i+2 == e && "array op not second to last?");
126 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
128 // Read all the elements.
129 for (; NumElts; --NumElts)
130 skipAbbreviatedField(*this, EltEnc);
134 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
135 // Blob case. Read the number of bytes as a vbr6.
136 unsigned NumElts = ReadVBR(6);
137 SkipToFourByteBoundary(); // 32-bit alignment
139 // Figure out where the end of this blob will be including tail padding.
140 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
142 // If this would read off the end of the bitcode file, just set the
143 // record to empty and return.
144 if (!canSkipToPos(NewEnd/8)) {
145 NextChar = BitStream->getBitcodeBytes().getExtent();
149 // Skip over the blob.
154 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
155 SmallVectorImpl<uint64_t> &Vals,
157 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));
165 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
167 // Read the record code first.
168 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
169 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
171 if (CodeOp.isLiteral())
172 Code = CodeOp.getLiteralValue();
174 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
175 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
176 report_fatal_error("Abbreviation starts with an Array or a Blob");
177 Code = readAbbreviatedField(*this, CodeOp);
180 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
181 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
182 if (Op.isLiteral()) {
183 Vals.push_back(Op.getLiteralValue());
187 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
188 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
189 Vals.push_back(readAbbreviatedField(*this, Op));
193 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
194 // Array case. Read the number of elements as a vbr6.
195 unsigned NumElts = ReadVBR(6);
197 // Get the element encoding.
198 assert(i+2 == e && "array op not second to last?");
199 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
201 // Read all the elements.
202 for (; NumElts; --NumElts)
203 Vals.push_back(readAbbreviatedField(*this, EltEnc));
207 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
208 // Blob case. Read the number of bytes as a vbr6.
209 unsigned NumElts = ReadVBR(6);
210 SkipToFourByteBoundary(); // 32-bit alignment
212 // Figure out where the end of this blob will be including tail padding.
213 size_t CurBitPos = GetCurrentBitNo();
214 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
216 // If this would read off the end of the bitcode file, just set the
217 // record to empty and return.
218 if (!canSkipToPos(NewEnd/8)) {
219 Vals.append(NumElts, 0);
220 NextChar = BitStream->getBitcodeBytes().getExtent();
224 // Otherwise, inform the streamer that we need these bytes in memory.
225 const char *Ptr = (const char*)
226 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
228 // If we can return a reference to the data, do so to avoid copying it.
230 *Blob = StringRef(Ptr, NumElts);
232 // Otherwise, unpack into Vals with zero extension.
233 for (; NumElts; --NumElts)
234 Vals.push_back((unsigned char)*Ptr++);
236 // Skip over tail padding.
244 void BitstreamCursor::ReadAbbrevRecord() {
245 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
246 unsigned NumOpInfo = ReadVBR(5);
247 for (unsigned i = 0; i != NumOpInfo; ++i) {
248 bool IsLiteral = Read(1);
250 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
254 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
255 if (BitCodeAbbrevOp::hasEncodingData(E)) {
256 uint64_t Data = ReadVBR64(5);
258 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
259 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
260 // a slow path in Read() to have to handle reading zero bits.
261 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
263 Abbv->Add(BitCodeAbbrevOp(0));
267 Abbv->Add(BitCodeAbbrevOp(E, Data));
269 Abbv->Add(BitCodeAbbrevOp(E));
271 CurAbbrevs.push_back(Abbv);
274 bool BitstreamCursor::ReadBlockInfoBlock() {
275 // If this is the second stream to get to the block info block, skip it.
276 if (BitStream->hasBlockInfoRecords())
279 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
281 SmallVector<uint64_t, 64> Record;
282 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
284 // Read all the records for this module.
286 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
288 switch (Entry.Kind) {
289 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
290 case llvm::BitstreamEntry::Error:
292 case llvm::BitstreamEntry::EndBlock:
294 case llvm::BitstreamEntry::Record:
295 // The interesting case.
299 // Read abbrev records, associate them with CurBID.
300 if (Entry.ID == bitc::DEFINE_ABBREV) {
301 if (!CurBlockInfo) return true;
304 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
305 // appropriate BlockInfo.
306 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
307 CurAbbrevs.pop_back();
313 switch (readRecord(Entry.ID, Record)) {
314 default: break; // Default behavior, ignore unknown content.
315 case bitc::BLOCKINFO_CODE_SETBID:
316 if (Record.size() < 1) return true;
317 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
319 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
320 if (!CurBlockInfo) return true;
321 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
323 for (unsigned i = 0, e = Record.size(); i != e; ++i)
324 Name += (char)Record[i];
325 CurBlockInfo->Name = Name;
328 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
329 if (!CurBlockInfo) return true;
330 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
332 for (unsigned i = 1, e = Record.size(); i != e; ++i)
333 Name += (char)Record[i];
334 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],