1*0b57cec5SDimitry Andric //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
11*0b57cec5SDimitry Andric #include <cassert>
12*0b57cec5SDimitry Andric #include <optional>
13*0b57cec5SDimitry Andric #include <string>
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric using namespace llvm;
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18*0b57cec5SDimitry Andric //  BitstreamCursor implementation
19*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20*0b57cec5SDimitry Andric //
error(const char * Message)21*0b57cec5SDimitry Andric static Error error(const char *Message) {
22*0b57cec5SDimitry Andric   return createStringError(std::errc::illegal_byte_sequence, Message);
23*0b57cec5SDimitry Andric }
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric /// Having read the ENTER_SUBBLOCK abbrevid, enter the block.
EnterSubBlock(unsigned BlockID,unsigned * NumWordsP)26*0b57cec5SDimitry Andric Error BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
27*0b57cec5SDimitry Andric   // Save the current block's state on BlockScope.
28*0b57cec5SDimitry Andric   BlockScope.push_back(Block(CurCodeSize));
29*0b57cec5SDimitry Andric   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric   // Add the abbrevs specific to this block to the CurAbbrevs list.
32*0b57cec5SDimitry Andric   if (BlockInfo) {
33*0b57cec5SDimitry Andric     if (const BitstreamBlockInfo::BlockInfo *Info =
34*0b57cec5SDimitry Andric             BlockInfo->getBlockInfo(BlockID)) {
35*0b57cec5SDimitry Andric       llvm::append_range(CurAbbrevs, Info->Abbrevs);
36*0b57cec5SDimitry Andric     }
37*0b57cec5SDimitry Andric   }
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric   // Get the codesize of this block.
40*0b57cec5SDimitry Andric   Expected<uint32_t> MaybeVBR = ReadVBR(bitc::CodeLenWidth);
41*0b57cec5SDimitry Andric   if (!MaybeVBR)
42*0b57cec5SDimitry Andric     return MaybeVBR.takeError();
43*0b57cec5SDimitry Andric   CurCodeSize = MaybeVBR.get();
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric   if (CurCodeSize > MaxChunkSize)
46*0b57cec5SDimitry Andric     return llvm::createStringError(
47*0b57cec5SDimitry Andric         std::errc::illegal_byte_sequence,
48*0b57cec5SDimitry Andric         "can't read more than %zu at a time, trying to read %u", +MaxChunkSize,
49*0b57cec5SDimitry Andric         CurCodeSize);
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric   SkipToFourByteBoundary();
52*0b57cec5SDimitry Andric   Expected<word_t> MaybeNum = Read(bitc::BlockSizeWidth);
53*0b57cec5SDimitry Andric   if (!MaybeNum)
54*0b57cec5SDimitry Andric     return MaybeNum.takeError();
55*0b57cec5SDimitry Andric   word_t NumWords = MaybeNum.get();
56*0b57cec5SDimitry Andric   if (NumWordsP)
57*0b57cec5SDimitry Andric     *NumWordsP = NumWords;
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   if (CurCodeSize == 0)
60*0b57cec5SDimitry Andric     return llvm::createStringError(
61*0b57cec5SDimitry Andric         std::errc::illegal_byte_sequence,
62*0b57cec5SDimitry Andric         "can't enter sub-block: current code size is 0");
63*0b57cec5SDimitry Andric   if (AtEndOfStream())
64*0b57cec5SDimitry Andric     return llvm::createStringError(
65*0b57cec5SDimitry Andric         std::errc::illegal_byte_sequence,
66*0b57cec5SDimitry Andric         "can't enter sub block: already at end of stream");
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric   return Error::success();
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric 
readAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)71*0b57cec5SDimitry Andric static Expected<uint64_t> readAbbreviatedField(BitstreamCursor &Cursor,
72*0b57cec5SDimitry Andric                                                const BitCodeAbbrevOp &Op) {
73*0b57cec5SDimitry Andric   assert(!Op.isLiteral() && "Not to be used with literals!");
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric   // Decode the value as we are commanded.
76*0b57cec5SDimitry Andric   switch (Op.getEncoding()) {
77*0b57cec5SDimitry Andric   case BitCodeAbbrevOp::Array:
78*0b57cec5SDimitry Andric   case BitCodeAbbrevOp::Blob:
79*0b57cec5SDimitry Andric     llvm_unreachable("Should not reach here");
80*0b57cec5SDimitry Andric   case BitCodeAbbrevOp::Fixed:
81*0b57cec5SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
82*0b57cec5SDimitry Andric     return Cursor.Read((unsigned)Op.getEncodingData());
83*0b57cec5SDimitry Andric   case BitCodeAbbrevOp::VBR:
84*0b57cec5SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
85*0b57cec5SDimitry Andric     return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
86*0b57cec5SDimitry Andric   case BitCodeAbbrevOp::Char6:
87*0b57cec5SDimitry Andric     if (Expected<unsigned> Res = Cursor.Read(6))
88*0b57cec5SDimitry Andric       return BitCodeAbbrevOp::DecodeChar6(Res.get());
89*0b57cec5SDimitry Andric     else
90*0b57cec5SDimitry Andric       return Res.takeError();
91*0b57cec5SDimitry Andric   }
92*0b57cec5SDimitry Andric   llvm_unreachable("invalid abbreviation encoding");
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric /// skipRecord - Read the current record and discard it.
skipRecord(unsigned AbbrevID)96*0b57cec5SDimitry Andric Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
97*0b57cec5SDimitry Andric   // Skip unabbreviated records by reading past their entries.
98*0b57cec5SDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
99*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeCode = ReadVBR(6);
100*0b57cec5SDimitry Andric     if (!MaybeCode)
101*0b57cec5SDimitry Andric       return MaybeCode.takeError();
102*0b57cec5SDimitry Andric     unsigned Code = MaybeCode.get();
103*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeVBR = ReadVBR(6);
104*0b57cec5SDimitry Andric     if (!MaybeVBR)
105*0b57cec5SDimitry Andric       return MaybeVBR.takeError();
106*0b57cec5SDimitry Andric     unsigned NumElts = MaybeVBR.get();
107*0b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
108*0b57cec5SDimitry Andric       if (Expected<uint64_t> Res = ReadVBR64(6))
109*0b57cec5SDimitry Andric         ; // Skip!
110*0b57cec5SDimitry Andric       else
111*0b57cec5SDimitry Andric         return Res.takeError();
112*0b57cec5SDimitry Andric     return Code;
113*0b57cec5SDimitry Andric   }
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric   Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
116*0b57cec5SDimitry Andric   if (!MaybeAbbv)
117*0b57cec5SDimitry Andric     return MaybeAbbv.takeError();
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric   const BitCodeAbbrev *Abbv = MaybeAbbv.get();
120*0b57cec5SDimitry Andric   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
121*0b57cec5SDimitry Andric   unsigned Code;
122*0b57cec5SDimitry Andric   if (CodeOp.isLiteral())
123*0b57cec5SDimitry Andric     Code = CodeOp.getLiteralValue();
124*0b57cec5SDimitry Andric   else {
125*0b57cec5SDimitry Andric     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
126*0b57cec5SDimitry Andric         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
127*0b57cec5SDimitry Andric       return llvm::createStringError(
128*0b57cec5SDimitry Andric           std::errc::illegal_byte_sequence,
129*0b57cec5SDimitry Andric           "Abbreviation starts with an Array or a Blob");
130*0b57cec5SDimitry Andric     Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp);
131*0b57cec5SDimitry Andric     if (!MaybeCode)
132*0b57cec5SDimitry Andric       return MaybeCode.takeError();
133*0b57cec5SDimitry Andric     Code = MaybeCode.get();
134*0b57cec5SDimitry Andric   }
135*0b57cec5SDimitry Andric 
136*0b57cec5SDimitry Andric   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
137*0b57cec5SDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
138*0b57cec5SDimitry Andric     if (Op.isLiteral())
139*0b57cec5SDimitry Andric       continue;
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
142*0b57cec5SDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
143*0b57cec5SDimitry Andric       if (Expected<uint64_t> MaybeField = readAbbreviatedField(*this, Op))
144*0b57cec5SDimitry Andric         continue;
145*0b57cec5SDimitry Andric       else
146*0b57cec5SDimitry Andric         return MaybeField.takeError();
147*0b57cec5SDimitry Andric     }
148*0b57cec5SDimitry Andric 
149*0b57cec5SDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
150*0b57cec5SDimitry Andric       // Array case.  Read the number of elements as a vbr6.
151*0b57cec5SDimitry Andric       Expected<uint32_t> MaybeNum = ReadVBR(6);
152*0b57cec5SDimitry Andric       if (!MaybeNum)
153*0b57cec5SDimitry Andric         return MaybeNum.takeError();
154*0b57cec5SDimitry Andric       unsigned NumElts = MaybeNum.get();
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric       // Get the element encoding.
157*0b57cec5SDimitry Andric       assert(i+2 == e && "array op not second to last?");
158*0b57cec5SDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric       // Read all the elements.
161*0b57cec5SDimitry Andric       // Decode the value as we are commanded.
162*0b57cec5SDimitry Andric       switch (EltEnc.getEncoding()) {
163*0b57cec5SDimitry Andric       default:
164*0b57cec5SDimitry Andric         return error("Array element type can't be an Array or a Blob");
165*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::Fixed:
166*0b57cec5SDimitry Andric         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
167*0b57cec5SDimitry Andric         if (Error Err =
168*0b57cec5SDimitry Andric                 JumpToBit(GetCurrentBitNo() + static_cast<uint64_t>(NumElts) *
169*0b57cec5SDimitry Andric                                                   EltEnc.getEncodingData()))
170*0b57cec5SDimitry Andric           return std::move(Err);
171*0b57cec5SDimitry Andric         break;
172*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::VBR:
173*0b57cec5SDimitry Andric         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
174*0b57cec5SDimitry Andric         for (; NumElts; --NumElts)
175*0b57cec5SDimitry Andric           if (Expected<uint64_t> Res =
176*0b57cec5SDimitry Andric                   ReadVBR64((unsigned)EltEnc.getEncodingData()))
177*0b57cec5SDimitry Andric             ; // Skip!
178*0b57cec5SDimitry Andric           else
179*0b57cec5SDimitry Andric             return Res.takeError();
180*0b57cec5SDimitry Andric         break;
181*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::Char6:
182*0b57cec5SDimitry Andric         if (Error Err = JumpToBit(GetCurrentBitNo() + NumElts * 6))
183*0b57cec5SDimitry Andric           return std::move(Err);
184*0b57cec5SDimitry Andric         break;
185*0b57cec5SDimitry Andric       }
186*0b57cec5SDimitry Andric       continue;
187*0b57cec5SDimitry Andric     }
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
190*0b57cec5SDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
191*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeNum = ReadVBR(6);
192*0b57cec5SDimitry Andric     if (!MaybeNum)
193*0b57cec5SDimitry Andric       return MaybeNum.takeError();
194*0b57cec5SDimitry Andric     unsigned NumElts = MaybeNum.get();
195*0b57cec5SDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
196*0b57cec5SDimitry Andric 
197*0b57cec5SDimitry Andric     // Figure out where the end of this blob will be including tail padding.
198*0b57cec5SDimitry Andric     const size_t NewEnd = GetCurrentBitNo() + alignTo(NumElts, 4) * 8;
199*0b57cec5SDimitry Andric 
200*0b57cec5SDimitry Andric     // If this would read off the end of the bitcode file, just set the
201*0b57cec5SDimitry Andric     // record to empty and return.
202*0b57cec5SDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
203*0b57cec5SDimitry Andric       skipToEnd();
204*0b57cec5SDimitry Andric       break;
205*0b57cec5SDimitry Andric     }
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric     // Skip over the blob.
208*0b57cec5SDimitry Andric     if (Error Err = JumpToBit(NewEnd))
209*0b57cec5SDimitry Andric       return std::move(Err);
210*0b57cec5SDimitry Andric   }
211*0b57cec5SDimitry Andric   return Code;
212*0b57cec5SDimitry Andric }
213*0b57cec5SDimitry Andric 
readRecord(unsigned AbbrevID,SmallVectorImpl<uint64_t> & Vals,StringRef * Blob)214*0b57cec5SDimitry Andric Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
215*0b57cec5SDimitry Andric                                                SmallVectorImpl<uint64_t> &Vals,
216*0b57cec5SDimitry Andric                                                StringRef *Blob) {
217*0b57cec5SDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
218*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeCode = ReadVBR(6);
219*0b57cec5SDimitry Andric     if (!MaybeCode)
220*0b57cec5SDimitry Andric       return MaybeCode.takeError();
221*0b57cec5SDimitry Andric     uint32_t Code = MaybeCode.get();
222*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeNumElts = ReadVBR(6);
223*0b57cec5SDimitry Andric     if (!MaybeNumElts)
224*0b57cec5SDimitry Andric       return error(
225*0b57cec5SDimitry Andric           ("Failed to read size: " + toString(MaybeNumElts.takeError()))
226*0b57cec5SDimitry Andric               .c_str());
227*0b57cec5SDimitry Andric     uint32_t NumElts = MaybeNumElts.get();
228*0b57cec5SDimitry Andric     if (!isSizePlausible(NumElts))
229*0b57cec5SDimitry Andric       return error("Size is not plausible");
230*0b57cec5SDimitry Andric     Vals.reserve(Vals.size() + NumElts);
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
233*0b57cec5SDimitry Andric       if (Expected<uint64_t> MaybeVal = ReadVBR64(6))
234*0b57cec5SDimitry Andric         Vals.push_back(MaybeVal.get());
235*0b57cec5SDimitry Andric       else
236*0b57cec5SDimitry Andric         return MaybeVal.takeError();
237*0b57cec5SDimitry Andric     return Code;
238*0b57cec5SDimitry Andric   }
239*0b57cec5SDimitry Andric 
240*0b57cec5SDimitry Andric   Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID);
241*0b57cec5SDimitry Andric   if (!MaybeAbbv)
242*0b57cec5SDimitry Andric     return MaybeAbbv.takeError();
243*0b57cec5SDimitry Andric   const BitCodeAbbrev *Abbv = MaybeAbbv.get();
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric   // Read the record code first.
246*0b57cec5SDimitry Andric   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
247*0b57cec5SDimitry Andric   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
248*0b57cec5SDimitry Andric   unsigned Code;
249*0b57cec5SDimitry Andric   if (CodeOp.isLiteral())
250*0b57cec5SDimitry Andric     Code = CodeOp.getLiteralValue();
251*0b57cec5SDimitry Andric   else {
252*0b57cec5SDimitry Andric     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
253*0b57cec5SDimitry Andric         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
254*0b57cec5SDimitry Andric       return error("Abbreviation starts with an Array or a Blob");
255*0b57cec5SDimitry Andric     if (Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp))
256*0b57cec5SDimitry Andric       Code = MaybeCode.get();
257*0b57cec5SDimitry Andric     else
258*0b57cec5SDimitry Andric       return MaybeCode.takeError();
259*0b57cec5SDimitry Andric   }
260*0b57cec5SDimitry Andric 
261*0b57cec5SDimitry Andric   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
262*0b57cec5SDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
263*0b57cec5SDimitry Andric     if (Op.isLiteral()) {
264*0b57cec5SDimitry Andric       Vals.push_back(Op.getLiteralValue());
265*0b57cec5SDimitry Andric       continue;
266*0b57cec5SDimitry Andric     }
267*0b57cec5SDimitry Andric 
268*0b57cec5SDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
269*0b57cec5SDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
270*0b57cec5SDimitry Andric       if (Expected<uint64_t> MaybeVal = readAbbreviatedField(*this, Op))
271*0b57cec5SDimitry Andric         Vals.push_back(MaybeVal.get());
272*0b57cec5SDimitry Andric       else
273*0b57cec5SDimitry Andric         return MaybeVal.takeError();
274*0b57cec5SDimitry Andric       continue;
275*0b57cec5SDimitry Andric     }
276*0b57cec5SDimitry Andric 
277*0b57cec5SDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
278*0b57cec5SDimitry Andric       // Array case.  Read the number of elements as a vbr6.
279*0b57cec5SDimitry Andric       Expected<uint32_t> MaybeNumElts = ReadVBR(6);
280*0b57cec5SDimitry Andric       if (!MaybeNumElts)
281*0b57cec5SDimitry Andric         return error(
282*0b57cec5SDimitry Andric             ("Failed to read size: " + toString(MaybeNumElts.takeError()))
283*0b57cec5SDimitry Andric                 .c_str());
284*0b57cec5SDimitry Andric       uint32_t NumElts = MaybeNumElts.get();
285*0b57cec5SDimitry Andric       if (!isSizePlausible(NumElts))
286*0b57cec5SDimitry Andric         return error("Size is not plausible");
287*0b57cec5SDimitry Andric       Vals.reserve(Vals.size() + NumElts);
288*0b57cec5SDimitry Andric 
289*0b57cec5SDimitry Andric       // Get the element encoding.
290*0b57cec5SDimitry Andric       if (i + 2 != e)
291*0b57cec5SDimitry Andric         return error("Array op not second to last");
292*0b57cec5SDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
293*0b57cec5SDimitry Andric       if (!EltEnc.isEncoding())
294*0b57cec5SDimitry Andric         return error(
295*0b57cec5SDimitry Andric             "Array element type has to be an encoding of a type");
296*0b57cec5SDimitry Andric 
297*0b57cec5SDimitry Andric       // Read all the elements.
298*0b57cec5SDimitry Andric       switch (EltEnc.getEncoding()) {
299*0b57cec5SDimitry Andric       default:
300*0b57cec5SDimitry Andric         return error("Array element type can't be an Array or a Blob");
301*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::Fixed:
302*0b57cec5SDimitry Andric         for (; NumElts; --NumElts)
303*0b57cec5SDimitry Andric           if (Expected<SimpleBitstreamCursor::word_t> MaybeVal =
304*0b57cec5SDimitry Andric                   Read((unsigned)EltEnc.getEncodingData()))
305*0b57cec5SDimitry Andric             Vals.push_back(MaybeVal.get());
306*0b57cec5SDimitry Andric           else
307*0b57cec5SDimitry Andric             return MaybeVal.takeError();
308*0b57cec5SDimitry Andric         break;
309*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::VBR:
310*0b57cec5SDimitry Andric         for (; NumElts; --NumElts)
311*0b57cec5SDimitry Andric           if (Expected<uint64_t> MaybeVal =
312*0b57cec5SDimitry Andric                   ReadVBR64((unsigned)EltEnc.getEncodingData()))
313*0b57cec5SDimitry Andric             Vals.push_back(MaybeVal.get());
314*0b57cec5SDimitry Andric           else
315*0b57cec5SDimitry Andric             return MaybeVal.takeError();
316*0b57cec5SDimitry Andric         break;
317*0b57cec5SDimitry Andric       case BitCodeAbbrevOp::Char6:
318*0b57cec5SDimitry Andric         for (; NumElts; --NumElts)
319*0b57cec5SDimitry Andric           if (Expected<SimpleBitstreamCursor::word_t> MaybeVal = Read(6))
320*0b57cec5SDimitry Andric             Vals.push_back(BitCodeAbbrevOp::DecodeChar6(MaybeVal.get()));
321*0b57cec5SDimitry Andric           else
322*0b57cec5SDimitry Andric             return MaybeVal.takeError();
323*0b57cec5SDimitry Andric       }
324*0b57cec5SDimitry Andric       continue;
325*0b57cec5SDimitry Andric     }
326*0b57cec5SDimitry Andric 
327*0b57cec5SDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
328*0b57cec5SDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
329*0b57cec5SDimitry Andric     Expected<uint32_t> MaybeNumElts = ReadVBR(6);
330*0b57cec5SDimitry Andric     if (!MaybeNumElts)
331*0b57cec5SDimitry Andric       return MaybeNumElts.takeError();
332*0b57cec5SDimitry Andric     uint32_t NumElts = MaybeNumElts.get();
333*0b57cec5SDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric     // Figure out where the end of this blob will be including tail padding.
336*0b57cec5SDimitry Andric     size_t CurBitPos = GetCurrentBitNo();
337*0b57cec5SDimitry Andric     const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8;
338*0b57cec5SDimitry Andric 
339*0b57cec5SDimitry Andric     // Make sure the bitstream is large enough to contain the blob.
340*0b57cec5SDimitry Andric     if (!canSkipToPos(NewEnd/8))
341*0b57cec5SDimitry Andric       return error("Blob ends too soon");
342*0b57cec5SDimitry Andric 
343*0b57cec5SDimitry Andric     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
344*0b57cec5SDimitry Andric     // over tail padding first, in case jumping to NewEnd invalidates the Blob
345*0b57cec5SDimitry Andric     // pointer.
346*0b57cec5SDimitry Andric     if (Error Err = JumpToBit(NewEnd))
347*0b57cec5SDimitry Andric       return std::move(Err);
348*0b57cec5SDimitry Andric     const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
349*0b57cec5SDimitry Andric 
350*0b57cec5SDimitry Andric     // If we can return a reference to the data, do so to avoid copying it.
351*0b57cec5SDimitry Andric     if (Blob) {
352*0b57cec5SDimitry Andric       *Blob = StringRef(Ptr, NumElts);
353*0b57cec5SDimitry Andric     } else {
354*0b57cec5SDimitry Andric       // Otherwise, unpack into Vals with zero extension.
355*0b57cec5SDimitry Andric       auto *UPtr = reinterpret_cast<const unsigned char *>(Ptr);
356*0b57cec5SDimitry Andric       Vals.append(UPtr, UPtr + NumElts);
357*0b57cec5SDimitry Andric     }
358*0b57cec5SDimitry Andric   }
359*0b57cec5SDimitry Andric 
360*0b57cec5SDimitry Andric   return Code;
361*0b57cec5SDimitry Andric }
362*0b57cec5SDimitry Andric 
ReadAbbrevRecord()363*0b57cec5SDimitry Andric Error BitstreamCursor::ReadAbbrevRecord() {
364*0b57cec5SDimitry Andric   auto Abbv = std::make_shared<BitCodeAbbrev>();
365*0b57cec5SDimitry Andric   Expected<uint32_t> MaybeNumOpInfo = ReadVBR(5);
366*0b57cec5SDimitry Andric   if (!MaybeNumOpInfo)
367*0b57cec5SDimitry Andric     return MaybeNumOpInfo.takeError();
368*0b57cec5SDimitry Andric   unsigned NumOpInfo = MaybeNumOpInfo.get();
369*0b57cec5SDimitry Andric   for (unsigned i = 0; i != NumOpInfo; ++i) {
370*0b57cec5SDimitry Andric     Expected<word_t> MaybeIsLiteral = Read(1);
371*0b57cec5SDimitry Andric     if (!MaybeIsLiteral)
372*0b57cec5SDimitry Andric       return MaybeIsLiteral.takeError();
373*0b57cec5SDimitry Andric     bool IsLiteral = MaybeIsLiteral.get();
374*0b57cec5SDimitry Andric     if (IsLiteral) {
375*0b57cec5SDimitry Andric       Expected<uint64_t> MaybeOp = ReadVBR64(8);
376*0b57cec5SDimitry Andric       if (!MaybeOp)
377*0b57cec5SDimitry Andric         return MaybeOp.takeError();
378*0b57cec5SDimitry Andric       Abbv->Add(BitCodeAbbrevOp(MaybeOp.get()));
379*0b57cec5SDimitry Andric       continue;
380*0b57cec5SDimitry Andric     }
381*0b57cec5SDimitry Andric 
382*0b57cec5SDimitry Andric     Expected<word_t> MaybeEncoding = Read(3);
383*0b57cec5SDimitry Andric     if (!MaybeEncoding)
384*0b57cec5SDimitry Andric       return MaybeEncoding.takeError();
385*0b57cec5SDimitry Andric     if (!BitCodeAbbrevOp::isValidEncoding(MaybeEncoding.get()))
386*0b57cec5SDimitry Andric       return error("Invalid encoding");
387*0b57cec5SDimitry Andric 
388*0b57cec5SDimitry Andric     BitCodeAbbrevOp::Encoding E =
389*0b57cec5SDimitry Andric         (BitCodeAbbrevOp::Encoding)MaybeEncoding.get();
390*0b57cec5SDimitry Andric     if (BitCodeAbbrevOp::hasEncodingData(E)) {
391*0b57cec5SDimitry Andric       Expected<uint64_t> MaybeData = ReadVBR64(5);
392*0b57cec5SDimitry Andric       if (!MaybeData)
393*0b57cec5SDimitry Andric         return MaybeData.takeError();
394*0b57cec5SDimitry Andric       uint64_t Data = MaybeData.get();
395*0b57cec5SDimitry Andric 
396*0b57cec5SDimitry Andric       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
397*0b57cec5SDimitry Andric       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
398*0b57cec5SDimitry Andric       // a slow path in Read() to have to handle reading zero bits.
399*0b57cec5SDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
400*0b57cec5SDimitry Andric           Data == 0) {
401*0b57cec5SDimitry Andric         Abbv->Add(BitCodeAbbrevOp(0));
402*0b57cec5SDimitry Andric         continue;
403*0b57cec5SDimitry Andric       }
404*0b57cec5SDimitry Andric 
405*0b57cec5SDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
406*0b57cec5SDimitry Andric           Data > MaxChunkSize)
407*0b57cec5SDimitry Andric         return error("Fixed or VBR abbrev record with size > MaxChunkData");
408*0b57cec5SDimitry Andric 
409*0b57cec5SDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E, Data));
410*0b57cec5SDimitry Andric     } else
411*0b57cec5SDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E));
412*0b57cec5SDimitry Andric   }
413*0b57cec5SDimitry Andric 
414*0b57cec5SDimitry Andric   if (Abbv->getNumOperandInfos() == 0)
415*0b57cec5SDimitry Andric     return error("Abbrev record with no operands");
416*0b57cec5SDimitry Andric   CurAbbrevs.push_back(std::move(Abbv));
417*0b57cec5SDimitry Andric 
418*0b57cec5SDimitry Andric   return Error::success();
419*0b57cec5SDimitry Andric }
420*0b57cec5SDimitry Andric 
421*0b57cec5SDimitry Andric Expected<std::optional<BitstreamBlockInfo>>
ReadBlockInfoBlock(bool ReadBlockInfoNames)422*0b57cec5SDimitry Andric BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
423*0b57cec5SDimitry Andric   if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID))
424*0b57cec5SDimitry Andric     return std::move(Err);
425*0b57cec5SDimitry Andric 
426*0b57cec5SDimitry Andric   BitstreamBlockInfo NewBlockInfo;
427*0b57cec5SDimitry Andric 
428*0b57cec5SDimitry Andric   SmallVector<uint64_t, 64> Record;
429*0b57cec5SDimitry Andric   BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
430*0b57cec5SDimitry Andric 
431*0b57cec5SDimitry Andric   // Read all the records for this module.
432*0b57cec5SDimitry Andric   while (true) {
433*0b57cec5SDimitry Andric     Expected<BitstreamEntry> MaybeEntry =
434*0b57cec5SDimitry Andric         advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
435*0b57cec5SDimitry Andric     if (!MaybeEntry)
436*0b57cec5SDimitry Andric       return MaybeEntry.takeError();
437*0b57cec5SDimitry Andric     BitstreamEntry Entry = MaybeEntry.get();
438*0b57cec5SDimitry Andric 
439*0b57cec5SDimitry Andric     switch (Entry.Kind) {
440*0b57cec5SDimitry Andric     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
441*0b57cec5SDimitry Andric     case llvm::BitstreamEntry::Error:
442*0b57cec5SDimitry Andric       return std::nullopt;
443*0b57cec5SDimitry Andric     case llvm::BitstreamEntry::EndBlock:
444*0b57cec5SDimitry Andric       return std::move(NewBlockInfo);
445*0b57cec5SDimitry Andric     case llvm::BitstreamEntry::Record:
446*0b57cec5SDimitry Andric       // The interesting case.
447*0b57cec5SDimitry Andric       break;
448*0b57cec5SDimitry Andric     }
449*0b57cec5SDimitry Andric 
450*0b57cec5SDimitry Andric     // Read abbrev records, associate them with CurBID.
451*0b57cec5SDimitry Andric     if (Entry.ID == bitc::DEFINE_ABBREV) {
452*0b57cec5SDimitry Andric       if (!CurBlockInfo)
453*0b57cec5SDimitry Andric         return std::nullopt;
454*0b57cec5SDimitry Andric       if (Error Err = ReadAbbrevRecord())
455*0b57cec5SDimitry Andric         return std::move(Err);
456*0b57cec5SDimitry Andric 
457*0b57cec5SDimitry Andric       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
458*0b57cec5SDimitry Andric       // appropriate BlockInfo.
459*0b57cec5SDimitry Andric       CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
460*0b57cec5SDimitry Andric       CurAbbrevs.pop_back();
461*0b57cec5SDimitry Andric       continue;
462*0b57cec5SDimitry Andric     }
463*0b57cec5SDimitry Andric 
464*0b57cec5SDimitry Andric     // Read a record.
465*0b57cec5SDimitry Andric     Record.clear();
466*0b57cec5SDimitry Andric     Expected<unsigned> MaybeBlockInfo = readRecord(Entry.ID, Record);
467*0b57cec5SDimitry Andric     if (!MaybeBlockInfo)
468*0b57cec5SDimitry Andric       return MaybeBlockInfo.takeError();
469*0b57cec5SDimitry Andric     switch (MaybeBlockInfo.get()) {
470*0b57cec5SDimitry Andric     default:
471*0b57cec5SDimitry Andric       break; // Default behavior, ignore unknown content.
472*0b57cec5SDimitry Andric     case bitc::BLOCKINFO_CODE_SETBID:
473*0b57cec5SDimitry Andric       if (Record.size() < 1)
474*0b57cec5SDimitry Andric         return std::nullopt;
475*0b57cec5SDimitry Andric       CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
476*0b57cec5SDimitry Andric       break;
477*0b57cec5SDimitry Andric     case bitc::BLOCKINFO_CODE_BLOCKNAME: {
478*0b57cec5SDimitry Andric       if (!CurBlockInfo)
479*0b57cec5SDimitry Andric         return std::nullopt;
480*0b57cec5SDimitry Andric       if (!ReadBlockInfoNames)
481*0b57cec5SDimitry Andric         break; // Ignore name.
482*0b57cec5SDimitry Andric       CurBlockInfo->Name = std::string(Record.begin(), Record.end());
483*0b57cec5SDimitry Andric       break;
484*0b57cec5SDimitry Andric     }
485*0b57cec5SDimitry Andric       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
486*0b57cec5SDimitry Andric       if (!CurBlockInfo)
487*0b57cec5SDimitry Andric         return std::nullopt;
488*0b57cec5SDimitry Andric       if (!ReadBlockInfoNames)
489*0b57cec5SDimitry Andric         break; // Ignore name.
490*0b57cec5SDimitry Andric       CurBlockInfo->RecordNames.emplace_back(
491*0b57cec5SDimitry Andric           (unsigned)Record[0], std::string(Record.begin() + 1, Record.end()));
492*0b57cec5SDimitry Andric       break;
493*0b57cec5SDimitry Andric       }
494*0b57cec5SDimitry Andric       }
495*0b57cec5SDimitry Andric   }
496*0b57cec5SDimitry Andric }
497*0b57cec5SDimitry Andric