1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Bitstream/BitstreamReader.h"
10 #include "llvm/ADT/StringRef.h"
16 //===----------------------------------------------------------------------===//
17 // BitstreamCursor implementation
18 //===----------------------------------------------------------------------===//
20 /// Having read the ENTER_SUBBLOCK abbrevid, enter the block.
21 Error
BitstreamCursor::EnterSubBlock(unsigned BlockID
, unsigned *NumWordsP
) {
22 // Save the current block's state on BlockScope.
23 BlockScope
.push_back(Block(CurCodeSize
));
24 BlockScope
.back().PrevAbbrevs
.swap(CurAbbrevs
);
26 // Add the abbrevs specific to this block to the CurAbbrevs list.
28 if (const BitstreamBlockInfo::BlockInfo
*Info
=
29 BlockInfo
->getBlockInfo(BlockID
)) {
30 llvm::append_range(CurAbbrevs
, Info
->Abbrevs
);
34 // Get the codesize of this block.
35 Expected
<uint32_t> MaybeVBR
= ReadVBR(bitc::CodeLenWidth
);
37 return MaybeVBR
.takeError();
38 CurCodeSize
= MaybeVBR
.get();
40 if (CurCodeSize
> MaxChunkSize
)
41 return llvm::createStringError(
42 std::errc::illegal_byte_sequence
,
43 "can't read more than %zu at a time, trying to read %u", +MaxChunkSize
,
46 SkipToFourByteBoundary();
47 Expected
<word_t
> MaybeNum
= Read(bitc::BlockSizeWidth
);
49 return MaybeNum
.takeError();
50 word_t NumWords
= MaybeNum
.get();
52 *NumWordsP
= NumWords
;
55 return llvm::createStringError(
56 std::errc::illegal_byte_sequence
,
57 "can't enter sub-block: current code size is 0");
59 return llvm::createStringError(
60 std::errc::illegal_byte_sequence
,
61 "can't enter sub block: already at end of stream");
63 return Error::success();
66 static Expected
<uint64_t> readAbbreviatedField(BitstreamCursor
&Cursor
,
67 const BitCodeAbbrevOp
&Op
) {
68 assert(!Op
.isLiteral() && "Not to be used with literals!");
70 // Decode the value as we are commanded.
71 switch (Op
.getEncoding()) {
72 case BitCodeAbbrevOp::Array
:
73 case BitCodeAbbrevOp::Blob
:
74 llvm_unreachable("Should not reach here");
75 case BitCodeAbbrevOp::Fixed
:
76 assert((unsigned)Op
.getEncodingData() <= Cursor
.MaxChunkSize
);
77 return Cursor
.Read((unsigned)Op
.getEncodingData());
78 case BitCodeAbbrevOp::VBR
:
79 assert((unsigned)Op
.getEncodingData() <= Cursor
.MaxChunkSize
);
80 return Cursor
.ReadVBR64((unsigned)Op
.getEncodingData());
81 case BitCodeAbbrevOp::Char6
:
82 if (Expected
<unsigned> Res
= Cursor
.Read(6))
83 return BitCodeAbbrevOp::DecodeChar6(Res
.get());
85 return Res
.takeError();
87 llvm_unreachable("invalid abbreviation encoding");
90 /// skipRecord - Read the current record and discard it.
91 Expected
<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID
) {
92 // Skip unabbreviated records by reading past their entries.
93 if (AbbrevID
== bitc::UNABBREV_RECORD
) {
94 Expected
<uint32_t> MaybeCode
= ReadVBR(6);
96 return MaybeCode
.takeError();
97 unsigned Code
= MaybeCode
.get();
98 Expected
<uint32_t> MaybeVBR
= ReadVBR(6);
100 return MaybeVBR
.get();
101 unsigned NumElts
= MaybeVBR
.get();
102 for (unsigned i
= 0; i
!= NumElts
; ++i
)
103 if (Expected
<uint64_t> Res
= ReadVBR64(6))
106 return Res
.takeError();
110 const BitCodeAbbrev
*Abbv
= getAbbrev(AbbrevID
);
111 const BitCodeAbbrevOp
&CodeOp
= Abbv
->getOperandInfo(0);
113 if (CodeOp
.isLiteral())
114 Code
= CodeOp
.getLiteralValue();
116 if (CodeOp
.getEncoding() == BitCodeAbbrevOp::Array
||
117 CodeOp
.getEncoding() == BitCodeAbbrevOp::Blob
)
118 return llvm::createStringError(
119 std::errc::illegal_byte_sequence
,
120 "Abbreviation starts with an Array or a Blob");
121 Expected
<uint64_t> MaybeCode
= readAbbreviatedField(*this, CodeOp
);
123 return MaybeCode
.takeError();
124 Code
= MaybeCode
.get();
127 for (unsigned i
= 1, e
= Abbv
->getNumOperandInfos(); i
< e
; ++i
) {
128 const BitCodeAbbrevOp
&Op
= Abbv
->getOperandInfo(i
);
132 if (Op
.getEncoding() != BitCodeAbbrevOp::Array
&&
133 Op
.getEncoding() != BitCodeAbbrevOp::Blob
) {
134 if (Expected
<uint64_t> MaybeField
= readAbbreviatedField(*this, Op
))
137 return MaybeField
.takeError();
140 if (Op
.getEncoding() == BitCodeAbbrevOp::Array
) {
141 // Array case. Read the number of elements as a vbr6.
142 Expected
<uint32_t> MaybeNum
= ReadVBR(6);
144 return MaybeNum
.takeError();
145 unsigned NumElts
= MaybeNum
.get();
147 // Get the element encoding.
148 assert(i
+2 == e
&& "array op not second to last?");
149 const BitCodeAbbrevOp
&EltEnc
= Abbv
->getOperandInfo(++i
);
151 // Read all the elements.
152 // Decode the value as we are commanded.
153 switch (EltEnc
.getEncoding()) {
155 report_fatal_error("Array element type can't be an Array or a Blob");
156 case BitCodeAbbrevOp::Fixed
:
157 assert((unsigned)EltEnc
.getEncodingData() <= MaxChunkSize
);
159 JumpToBit(GetCurrentBitNo() + static_cast<uint64_t>(NumElts
) *
160 EltEnc
.getEncodingData()))
161 return std::move(Err
);
163 case BitCodeAbbrevOp::VBR
:
164 assert((unsigned)EltEnc
.getEncodingData() <= MaxChunkSize
);
165 for (; NumElts
; --NumElts
)
166 if (Expected
<uint64_t> Res
=
167 ReadVBR64((unsigned)EltEnc
.getEncodingData()))
170 return Res
.takeError();
172 case BitCodeAbbrevOp::Char6
:
173 if (Error Err
= JumpToBit(GetCurrentBitNo() + NumElts
* 6))
174 return std::move(Err
);
180 assert(Op
.getEncoding() == BitCodeAbbrevOp::Blob
);
181 // Blob case. Read the number of bytes as a vbr6.
182 Expected
<uint32_t> MaybeNum
= ReadVBR(6);
184 return MaybeNum
.takeError();
185 unsigned NumElts
= MaybeNum
.get();
186 SkipToFourByteBoundary(); // 32-bit alignment
188 // Figure out where the end of this blob will be including tail padding.
189 const size_t NewEnd
= GetCurrentBitNo() + alignTo(NumElts
, 4) * 8;
191 // If this would read off the end of the bitcode file, just set the
192 // record to empty and return.
193 if (!canSkipToPos(NewEnd
/8)) {
198 // Skip over the blob.
199 if (Error Err
= JumpToBit(NewEnd
))
200 return std::move(Err
);
205 Expected
<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID
,
206 SmallVectorImpl
<uint64_t> &Vals
,
208 if (AbbrevID
== bitc::UNABBREV_RECORD
) {
209 Expected
<uint32_t> MaybeCode
= ReadVBR(6);
211 return MaybeCode
.takeError();
212 uint32_t Code
= MaybeCode
.get();
213 Expected
<uint32_t> MaybeNumElts
= ReadVBR(6);
215 return MaybeNumElts
.takeError();
216 uint32_t NumElts
= MaybeNumElts
.get();
217 Vals
.reserve(Vals
.size() + NumElts
);
219 for (unsigned i
= 0; i
!= NumElts
; ++i
)
220 if (Expected
<uint64_t> MaybeVal
= ReadVBR64(6))
221 Vals
.push_back(MaybeVal
.get());
223 return MaybeVal
.takeError();
227 const BitCodeAbbrev
*Abbv
= getAbbrev(AbbrevID
);
229 // Read the record code first.
230 assert(Abbv
->getNumOperandInfos() != 0 && "no record code in abbreviation?");
231 const BitCodeAbbrevOp
&CodeOp
= Abbv
->getOperandInfo(0);
233 if (CodeOp
.isLiteral())
234 Code
= CodeOp
.getLiteralValue();
236 if (CodeOp
.getEncoding() == BitCodeAbbrevOp::Array
||
237 CodeOp
.getEncoding() == BitCodeAbbrevOp::Blob
)
238 report_fatal_error("Abbreviation starts with an Array or a Blob");
239 if (Expected
<uint64_t> MaybeCode
= readAbbreviatedField(*this, CodeOp
))
240 Code
= MaybeCode
.get();
242 return MaybeCode
.takeError();
245 for (unsigned i
= 1, e
= Abbv
->getNumOperandInfos(); i
!= e
; ++i
) {
246 const BitCodeAbbrevOp
&Op
= Abbv
->getOperandInfo(i
);
247 if (Op
.isLiteral()) {
248 Vals
.push_back(Op
.getLiteralValue());
252 if (Op
.getEncoding() != BitCodeAbbrevOp::Array
&&
253 Op
.getEncoding() != BitCodeAbbrevOp::Blob
) {
254 if (Expected
<uint64_t> MaybeVal
= readAbbreviatedField(*this, Op
))
255 Vals
.push_back(MaybeVal
.get());
257 return MaybeVal
.takeError();
261 if (Op
.getEncoding() == BitCodeAbbrevOp::Array
) {
262 // Array case. Read the number of elements as a vbr6.
263 Expected
<uint32_t> MaybeNumElts
= ReadVBR(6);
265 return MaybeNumElts
.takeError();
266 uint32_t NumElts
= MaybeNumElts
.get();
267 Vals
.reserve(Vals
.size() + NumElts
);
269 // Get the element encoding.
271 report_fatal_error("Array op not second to last");
272 const BitCodeAbbrevOp
&EltEnc
= Abbv
->getOperandInfo(++i
);
273 if (!EltEnc
.isEncoding())
275 "Array element type has to be an encoding of a type");
277 // Read all the elements.
278 switch (EltEnc
.getEncoding()) {
280 report_fatal_error("Array element type can't be an Array or a Blob");
281 case BitCodeAbbrevOp::Fixed
:
282 for (; NumElts
; --NumElts
)
283 if (Expected
<SimpleBitstreamCursor::word_t
> MaybeVal
=
284 Read((unsigned)EltEnc
.getEncodingData()))
285 Vals
.push_back(MaybeVal
.get());
287 return MaybeVal
.takeError();
289 case BitCodeAbbrevOp::VBR
:
290 for (; NumElts
; --NumElts
)
291 if (Expected
<uint64_t> MaybeVal
=
292 ReadVBR64((unsigned)EltEnc
.getEncodingData()))
293 Vals
.push_back(MaybeVal
.get());
295 return MaybeVal
.takeError();
297 case BitCodeAbbrevOp::Char6
:
298 for (; NumElts
; --NumElts
)
299 if (Expected
<SimpleBitstreamCursor::word_t
> MaybeVal
= Read(6))
300 Vals
.push_back(BitCodeAbbrevOp::DecodeChar6(MaybeVal
.get()));
302 return MaybeVal
.takeError();
307 assert(Op
.getEncoding() == BitCodeAbbrevOp::Blob
);
308 // Blob case. Read the number of bytes as a vbr6.
309 Expected
<uint32_t> MaybeNumElts
= ReadVBR(6);
311 return MaybeNumElts
.takeError();
312 uint32_t NumElts
= MaybeNumElts
.get();
313 SkipToFourByteBoundary(); // 32-bit alignment
315 // Figure out where the end of this blob will be including tail padding.
316 size_t CurBitPos
= GetCurrentBitNo();
317 const size_t NewEnd
= CurBitPos
+ alignTo(NumElts
, 4) * 8;
319 // If this would read off the end of the bitcode file, just set the
320 // record to empty and return.
321 if (!canSkipToPos(NewEnd
/8)) {
322 Vals
.append(NumElts
, 0);
327 // Otherwise, inform the streamer that we need these bytes in memory. Skip
328 // over tail padding first, in case jumping to NewEnd invalidates the Blob
330 if (Error Err
= JumpToBit(NewEnd
))
331 return std::move(Err
);
332 const char *Ptr
= (const char *)getPointerToBit(CurBitPos
, NumElts
);
334 // If we can return a reference to the data, do so to avoid copying it.
336 *Blob
= StringRef(Ptr
, NumElts
);
338 // Otherwise, unpack into Vals with zero extension.
339 auto *UPtr
= reinterpret_cast<const unsigned char *>(Ptr
);
340 Vals
.append(UPtr
, UPtr
+ NumElts
);
347 Error
BitstreamCursor::ReadAbbrevRecord() {
348 auto Abbv
= std::make_shared
<BitCodeAbbrev
>();
349 Expected
<uint32_t> MaybeNumOpInfo
= ReadVBR(5);
351 return MaybeNumOpInfo
.takeError();
352 unsigned NumOpInfo
= MaybeNumOpInfo
.get();
353 for (unsigned i
= 0; i
!= NumOpInfo
; ++i
) {
354 Expected
<word_t
> MaybeIsLiteral
= Read(1);
356 return MaybeIsLiteral
.takeError();
357 bool IsLiteral
= MaybeIsLiteral
.get();
359 Expected
<uint64_t> MaybeOp
= ReadVBR64(8);
361 return MaybeOp
.takeError();
362 Abbv
->Add(BitCodeAbbrevOp(MaybeOp
.get()));
366 Expected
<word_t
> MaybeEncoding
= Read(3);
368 return MaybeEncoding
.takeError();
369 BitCodeAbbrevOp::Encoding E
=
370 (BitCodeAbbrevOp::Encoding
)MaybeEncoding
.get();
371 if (BitCodeAbbrevOp::hasEncodingData(E
)) {
372 Expected
<uint64_t> MaybeData
= ReadVBR64(5);
374 return MaybeData
.takeError();
375 uint64_t Data
= MaybeData
.get();
377 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
378 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
379 // a slow path in Read() to have to handle reading zero bits.
380 if ((E
== BitCodeAbbrevOp::Fixed
|| E
== BitCodeAbbrevOp::VBR
) &&
382 Abbv
->Add(BitCodeAbbrevOp(0));
386 if ((E
== BitCodeAbbrevOp::Fixed
|| E
== BitCodeAbbrevOp::VBR
) &&
389 "Fixed or VBR abbrev record with size > MaxChunkData");
391 Abbv
->Add(BitCodeAbbrevOp(E
, Data
));
393 Abbv
->Add(BitCodeAbbrevOp(E
));
396 if (Abbv
->getNumOperandInfos() == 0)
397 report_fatal_error("Abbrev record with no operands");
398 CurAbbrevs
.push_back(std::move(Abbv
));
400 return Error::success();
403 Expected
<Optional
<BitstreamBlockInfo
>>
404 BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames
) {
405 if (llvm::Error Err
= EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID
))
406 return std::move(Err
);
408 BitstreamBlockInfo NewBlockInfo
;
410 SmallVector
<uint64_t, 64> Record
;
411 BitstreamBlockInfo::BlockInfo
*CurBlockInfo
= nullptr;
413 // Read all the records for this module.
415 Expected
<BitstreamEntry
> MaybeEntry
=
416 advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs
);
418 return MaybeEntry
.takeError();
419 BitstreamEntry Entry
= MaybeEntry
.get();
421 switch (Entry
.Kind
) {
422 case llvm::BitstreamEntry::SubBlock
: // Handled for us already.
423 case llvm::BitstreamEntry::Error
:
425 case llvm::BitstreamEntry::EndBlock
:
426 return std::move(NewBlockInfo
);
427 case llvm::BitstreamEntry::Record
:
428 // The interesting case.
432 // Read abbrev records, associate them with CurBID.
433 if (Entry
.ID
== bitc::DEFINE_ABBREV
) {
434 if (!CurBlockInfo
) return None
;
435 if (Error Err
= ReadAbbrevRecord())
436 return std::move(Err
);
438 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
439 // appropriate BlockInfo.
440 CurBlockInfo
->Abbrevs
.push_back(std::move(CurAbbrevs
.back()));
441 CurAbbrevs
.pop_back();
447 Expected
<unsigned> MaybeBlockInfo
= readRecord(Entry
.ID
, Record
);
449 return MaybeBlockInfo
.takeError();
450 switch (MaybeBlockInfo
.get()) {
452 break; // Default behavior, ignore unknown content.
453 case bitc::BLOCKINFO_CODE_SETBID
:
454 if (Record
.size() < 1)
456 CurBlockInfo
= &NewBlockInfo
.getOrCreateBlockInfo((unsigned)Record
[0]);
458 case bitc::BLOCKINFO_CODE_BLOCKNAME
: {
461 if (!ReadBlockInfoNames
)
462 break; // Ignore name.
463 CurBlockInfo
->Name
= std::string(Record
.begin(), Record
.end());
466 case bitc::BLOCKINFO_CODE_SETRECORDNAME
: {
467 if (!CurBlockInfo
) return None
;
468 if (!ReadBlockInfoNames
)
469 break; // Ignore name.
470 CurBlockInfo
->RecordNames
.emplace_back(
471 (unsigned)Record
[0], std::string(Record
.begin() + 1, Record
.end()));