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"
11 #include "llvm/ADT/StringRef.h"
17 //===----------------------------------------------------------------------===//
18 // BitstreamCursor implementation
19 //===----------------------------------------------------------------------===//
21 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
22 /// the block, and return true if the block has an error.
23 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
24 // Save the current block's state on BlockScope.
25 BlockScope.push_back(Block(CurCodeSize));
26 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
28 // Add the abbrevs specific to this block to the CurAbbrevs list.
30 if (const BitstreamBlockInfo::BlockInfo *Info =
31 BlockInfo->getBlockInfo(BlockID)) {
32 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
37 // Get the codesize of this block.
38 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
39 // We can't read more than MaxChunkSize at a time
40 if (CurCodeSize > MaxChunkSize)
43 SkipToFourByteBoundary();
44 unsigned NumWords = Read(bitc::BlockSizeWidth);
45 if (NumWordsP) *NumWordsP = NumWords;
47 // Validate that this block is sane.
48 return CurCodeSize == 0 || AtEndOfStream();
51 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
52 const BitCodeAbbrevOp &Op) {
53 assert(!Op.isLiteral() && "Not to be used with literals!");
55 // Decode the value as we are commanded.
56 switch (Op.getEncoding()) {
57 case BitCodeAbbrevOp::Array:
58 case BitCodeAbbrevOp::Blob:
59 llvm_unreachable("Should not reach here");
60 case BitCodeAbbrevOp::Fixed:
61 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
62 return Cursor.Read((unsigned)Op.getEncodingData());
63 case BitCodeAbbrevOp::VBR:
64 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
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 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
83 Cursor.Read((unsigned)Op.getEncodingData());
85 case BitCodeAbbrevOp::VBR:
86 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
87 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
89 case BitCodeAbbrevOp::Char6:
95 /// skipRecord - Read the current record and discard it.
96 unsigned 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 unsigned NumElts = ReadVBR(6);
101 for (unsigned i = 0; i != NumElts; ++i)
106 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
107 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
109 if (CodeOp.isLiteral())
110 Code = CodeOp.getLiteralValue();
112 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
113 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
114 report_fatal_error("Abbreviation starts with an Array or a Blob");
115 Code = readAbbreviatedField(*this, CodeOp);
118 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
119 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
123 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
124 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
125 skipAbbreviatedField(*this, Op);
129 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
130 // Array case. Read the number of elements as a vbr6.
131 unsigned NumElts = ReadVBR(6);
133 // Get the element encoding.
134 assert(i+2 == e && "array op not second to last?");
135 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
137 // Read all the elements.
138 // Decode the value as we are commanded.
139 switch (EltEnc.getEncoding()) {
141 report_fatal_error("Array element type can't be an Array or a Blob");
142 case BitCodeAbbrevOp::Fixed:
143 assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
144 JumpToBit(GetCurrentBitNo() + NumElts * EltEnc.getEncodingData());
146 case BitCodeAbbrevOp::VBR:
147 assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
148 for (; NumElts; --NumElts)
149 ReadVBR64((unsigned)EltEnc.getEncodingData());
151 case BitCodeAbbrevOp::Char6:
152 JumpToBit(GetCurrentBitNo() + NumElts * 6);
158 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
159 // Blob case. Read the number of bytes as a vbr6.
160 unsigned NumElts = ReadVBR(6);
161 SkipToFourByteBoundary(); // 32-bit alignment
163 // Figure out where the end of this blob will be including tail padding.
164 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
166 // If this would read off the end of the bitcode file, just set the
167 // record to empty and return.
168 if (!canSkipToPos(NewEnd/8)) {
173 // Skip over the blob.
179 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
180 SmallVectorImpl<uint64_t> &Vals,
182 if (AbbrevID == bitc::UNABBREV_RECORD) {
183 unsigned Code = ReadVBR(6);
184 unsigned NumElts = ReadVBR(6);
185 for (unsigned i = 0; i != NumElts; ++i)
186 Vals.push_back(ReadVBR64(6));
190 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
192 // Read the record code first.
193 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
194 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
196 if (CodeOp.isLiteral())
197 Code = CodeOp.getLiteralValue();
199 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
200 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
201 report_fatal_error("Abbreviation starts with an Array or a Blob");
202 Code = readAbbreviatedField(*this, CodeOp);
205 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
206 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
207 if (Op.isLiteral()) {
208 Vals.push_back(Op.getLiteralValue());
212 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
213 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
214 Vals.push_back(readAbbreviatedField(*this, Op));
218 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
219 // Array case. Read the number of elements as a vbr6.
220 unsigned NumElts = ReadVBR(6);
222 // Get the element encoding.
224 report_fatal_error("Array op not second to last");
225 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
226 if (!EltEnc.isEncoding())
228 "Array element type has to be an encoding of a type");
230 // Read all the elements.
231 switch (EltEnc.getEncoding()) {
233 report_fatal_error("Array element type can't be an Array or a Blob");
234 case BitCodeAbbrevOp::Fixed:
235 for (; NumElts; --NumElts)
236 Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
238 case BitCodeAbbrevOp::VBR:
239 for (; NumElts; --NumElts)
240 Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
242 case BitCodeAbbrevOp::Char6:
243 for (; NumElts; --NumElts)
244 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
249 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
250 // Blob case. Read the number of bytes as a vbr6.
251 unsigned NumElts = ReadVBR(6);
252 SkipToFourByteBoundary(); // 32-bit alignment
254 // Figure out where the end of this blob will be including tail padding.
255 size_t CurBitPos = GetCurrentBitNo();
256 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
258 // If this would read off the end of the bitcode file, just set the
259 // record to empty and return.
260 if (!canSkipToPos(NewEnd/8)) {
261 Vals.append(NumElts, 0);
266 // Otherwise, inform the streamer that we need these bytes in memory. Skip
267 // over tail padding first, in case jumping to NewEnd invalidates the Blob
270 const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
272 // If we can return a reference to the data, do so to avoid copying it.
274 *Blob = StringRef(Ptr, NumElts);
276 // Otherwise, unpack into Vals with zero extension.
277 for (; NumElts; --NumElts)
278 Vals.push_back((unsigned char)*Ptr++);
285 void BitstreamCursor::ReadAbbrevRecord() {
286 auto Abbv = std::make_shared<BitCodeAbbrev>();
287 unsigned NumOpInfo = ReadVBR(5);
288 for (unsigned i = 0; i != NumOpInfo; ++i) {
289 bool IsLiteral = Read(1);
291 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
295 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
296 if (BitCodeAbbrevOp::hasEncodingData(E)) {
297 uint64_t Data = ReadVBR64(5);
299 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
300 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
301 // a slow path in Read() to have to handle reading zero bits.
302 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
304 Abbv->Add(BitCodeAbbrevOp(0));
308 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
311 "Fixed or VBR abbrev record with size > MaxChunkData");
313 Abbv->Add(BitCodeAbbrevOp(E, Data));
315 Abbv->Add(BitCodeAbbrevOp(E));
318 if (Abbv->getNumOperandInfos() == 0)
319 report_fatal_error("Abbrev record with no operands");
320 CurAbbrevs.push_back(std::move(Abbv));
323 Optional<BitstreamBlockInfo>
324 BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
325 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return None;
327 BitstreamBlockInfo NewBlockInfo;
329 SmallVector<uint64_t, 64> Record;
330 BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
332 // Read all the records for this module.
334 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
336 switch (Entry.Kind) {
337 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
338 case llvm::BitstreamEntry::Error:
340 case llvm::BitstreamEntry::EndBlock:
341 return std::move(NewBlockInfo);
342 case llvm::BitstreamEntry::Record:
343 // The interesting case.
347 // Read abbrev records, associate them with CurBID.
348 if (Entry.ID == bitc::DEFINE_ABBREV) {
349 if (!CurBlockInfo) return None;
352 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
353 // appropriate BlockInfo.
354 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
355 CurAbbrevs.pop_back();
361 switch (readRecord(Entry.ID, Record)) {
362 default: break; // Default behavior, ignore unknown content.
363 case bitc::BLOCKINFO_CODE_SETBID:
364 if (Record.size() < 1) return None;
365 CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
367 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
368 if (!CurBlockInfo) return None;
369 if (!ReadBlockInfoNames)
370 break; // Ignore name.
372 for (unsigned i = 0, e = Record.size(); i != e; ++i)
373 Name += (char)Record[i];
374 CurBlockInfo->Name = Name;
377 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
378 if (!CurBlockInfo) return None;
379 if (!ReadBlockInfoNames)
380 break; // Ignore name.
382 for (unsigned i = 1, e = Record.size(); i != e; ++i)
383 Name += (char)Record[i];
384 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],