10b57cec5SDimitry Andric //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h"
100b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
110b57cec5SDimitry Andric #include <cassert>
120b57cec5SDimitry Andric #include <string>
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric using namespace llvm;
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric // BitstreamCursor implementation
180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric /// Having read the ENTER_SUBBLOCK abbrevid, enter the block.
EnterSubBlock(unsigned BlockID,unsigned * NumWordsP)210b57cec5SDimitry Andric Error BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
220b57cec5SDimitry Andric // Save the current block's state on BlockScope.
230b57cec5SDimitry Andric BlockScope.push_back(Block(CurCodeSize));
240b57cec5SDimitry Andric BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric // Add the abbrevs specific to this block to the CurAbbrevs list.
270b57cec5SDimitry Andric if (BlockInfo) {
280b57cec5SDimitry Andric if (const BitstreamBlockInfo::BlockInfo *Info =
290b57cec5SDimitry Andric BlockInfo->getBlockInfo(BlockID)) {
30*af732203SDimitry Andric llvm::append_range(CurAbbrevs, Info->Abbrevs);
310b57cec5SDimitry Andric }
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric // Get the codesize of this block.
350b57cec5SDimitry Andric Expected<uint32_t> MaybeVBR = ReadVBR(bitc::CodeLenWidth);
360b57cec5SDimitry Andric if (!MaybeVBR)
370b57cec5SDimitry Andric return MaybeVBR.takeError();
380b57cec5SDimitry Andric CurCodeSize = MaybeVBR.get();
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric if (CurCodeSize > MaxChunkSize)
410b57cec5SDimitry Andric return llvm::createStringError(
420b57cec5SDimitry Andric std::errc::illegal_byte_sequence,
430b57cec5SDimitry Andric "can't read more than %zu at a time, trying to read %u", +MaxChunkSize,
440b57cec5SDimitry Andric CurCodeSize);
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric SkipToFourByteBoundary();
470b57cec5SDimitry Andric Expected<word_t> MaybeNum = Read(bitc::BlockSizeWidth);
480b57cec5SDimitry Andric if (!MaybeNum)
490b57cec5SDimitry Andric return MaybeNum.takeError();
500b57cec5SDimitry Andric word_t NumWords = MaybeNum.get();
510b57cec5SDimitry Andric if (NumWordsP)
520b57cec5SDimitry Andric *NumWordsP = NumWords;
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric if (CurCodeSize == 0)
550b57cec5SDimitry Andric return llvm::createStringError(
560b57cec5SDimitry Andric std::errc::illegal_byte_sequence,
570b57cec5SDimitry Andric "can't enter sub-block: current code size is 0");
580b57cec5SDimitry Andric if (AtEndOfStream())
590b57cec5SDimitry Andric return llvm::createStringError(
600b57cec5SDimitry Andric std::errc::illegal_byte_sequence,
610b57cec5SDimitry Andric "can't enter sub block: already at end of stream");
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric return Error::success();
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric
readAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)660b57cec5SDimitry Andric static Expected<uint64_t> readAbbreviatedField(BitstreamCursor &Cursor,
670b57cec5SDimitry Andric const BitCodeAbbrevOp &Op) {
680b57cec5SDimitry Andric assert(!Op.isLiteral() && "Not to be used with literals!");
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric // Decode the value as we are commanded.
710b57cec5SDimitry Andric switch (Op.getEncoding()) {
720b57cec5SDimitry Andric case BitCodeAbbrevOp::Array:
730b57cec5SDimitry Andric case BitCodeAbbrevOp::Blob:
740b57cec5SDimitry Andric llvm_unreachable("Should not reach here");
750b57cec5SDimitry Andric case BitCodeAbbrevOp::Fixed:
760b57cec5SDimitry Andric assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
770b57cec5SDimitry Andric return Cursor.Read((unsigned)Op.getEncodingData());
780b57cec5SDimitry Andric case BitCodeAbbrevOp::VBR:
790b57cec5SDimitry Andric assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
800b57cec5SDimitry Andric return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
810b57cec5SDimitry Andric case BitCodeAbbrevOp::Char6:
820b57cec5SDimitry Andric if (Expected<unsigned> Res = Cursor.Read(6))
830b57cec5SDimitry Andric return BitCodeAbbrevOp::DecodeChar6(Res.get());
840b57cec5SDimitry Andric else
850b57cec5SDimitry Andric return Res.takeError();
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric llvm_unreachable("invalid abbreviation encoding");
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric /// skipRecord - Read the current record and discard it.
skipRecord(unsigned AbbrevID)910b57cec5SDimitry Andric Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
920b57cec5SDimitry Andric // Skip unabbreviated records by reading past their entries.
930b57cec5SDimitry Andric if (AbbrevID == bitc::UNABBREV_RECORD) {
940b57cec5SDimitry Andric Expected<uint32_t> MaybeCode = ReadVBR(6);
950b57cec5SDimitry Andric if (!MaybeCode)
960b57cec5SDimitry Andric return MaybeCode.takeError();
970b57cec5SDimitry Andric unsigned Code = MaybeCode.get();
980b57cec5SDimitry Andric Expected<uint32_t> MaybeVBR = ReadVBR(6);
990b57cec5SDimitry Andric if (!MaybeVBR)
1000b57cec5SDimitry Andric return MaybeVBR.get();
1010b57cec5SDimitry Andric unsigned NumElts = MaybeVBR.get();
1020b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i)
1030b57cec5SDimitry Andric if (Expected<uint64_t> Res = ReadVBR64(6))
1040b57cec5SDimitry Andric ; // Skip!
1050b57cec5SDimitry Andric else
1060b57cec5SDimitry Andric return Res.takeError();
1070b57cec5SDimitry Andric return Code;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
1110b57cec5SDimitry Andric const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
1120b57cec5SDimitry Andric unsigned Code;
1130b57cec5SDimitry Andric if (CodeOp.isLiteral())
1140b57cec5SDimitry Andric Code = CodeOp.getLiteralValue();
1150b57cec5SDimitry Andric else {
1160b57cec5SDimitry Andric if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
1170b57cec5SDimitry Andric CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
1180b57cec5SDimitry Andric return llvm::createStringError(
1190b57cec5SDimitry Andric std::errc::illegal_byte_sequence,
1200b57cec5SDimitry Andric "Abbreviation starts with an Array or a Blob");
1210b57cec5SDimitry Andric Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp);
1220b57cec5SDimitry Andric if (!MaybeCode)
1230b57cec5SDimitry Andric return MaybeCode.takeError();
1240b57cec5SDimitry Andric Code = MaybeCode.get();
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
1280b57cec5SDimitry Andric const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
1290b57cec5SDimitry Andric if (Op.isLiteral())
1300b57cec5SDimitry Andric continue;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
1330b57cec5SDimitry Andric Op.getEncoding() != BitCodeAbbrevOp::Blob) {
134480093f4SDimitry Andric if (Expected<uint64_t> MaybeField = readAbbreviatedField(*this, Op))
1350b57cec5SDimitry Andric continue;
136480093f4SDimitry Andric else
137480093f4SDimitry Andric return MaybeField.takeError();
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
1410b57cec5SDimitry Andric // Array case. Read the number of elements as a vbr6.
1420b57cec5SDimitry Andric Expected<uint32_t> MaybeNum = ReadVBR(6);
1430b57cec5SDimitry Andric if (!MaybeNum)
1440b57cec5SDimitry Andric return MaybeNum.takeError();
1450b57cec5SDimitry Andric unsigned NumElts = MaybeNum.get();
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric // Get the element encoding.
1480b57cec5SDimitry Andric assert(i+2 == e && "array op not second to last?");
1490b57cec5SDimitry Andric const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric // Read all the elements.
1520b57cec5SDimitry Andric // Decode the value as we are commanded.
1530b57cec5SDimitry Andric switch (EltEnc.getEncoding()) {
1540b57cec5SDimitry Andric default:
1550b57cec5SDimitry Andric report_fatal_error("Array element type can't be an Array or a Blob");
1560b57cec5SDimitry Andric case BitCodeAbbrevOp::Fixed:
1570b57cec5SDimitry Andric assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
158*af732203SDimitry Andric if (Error Err =
159*af732203SDimitry Andric JumpToBit(GetCurrentBitNo() + static_cast<uint64_t>(NumElts) *
160*af732203SDimitry Andric EltEnc.getEncodingData()))
1610b57cec5SDimitry Andric return std::move(Err);
1620b57cec5SDimitry Andric break;
1630b57cec5SDimitry Andric case BitCodeAbbrevOp::VBR:
1640b57cec5SDimitry Andric assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
1650b57cec5SDimitry Andric for (; NumElts; --NumElts)
1660b57cec5SDimitry Andric if (Expected<uint64_t> Res =
1670b57cec5SDimitry Andric ReadVBR64((unsigned)EltEnc.getEncodingData()))
1680b57cec5SDimitry Andric ; // Skip!
1690b57cec5SDimitry Andric else
1700b57cec5SDimitry Andric return Res.takeError();
1710b57cec5SDimitry Andric break;
1720b57cec5SDimitry Andric case BitCodeAbbrevOp::Char6:
1730b57cec5SDimitry Andric if (Error Err = JumpToBit(GetCurrentBitNo() + NumElts * 6))
1740b57cec5SDimitry Andric return std::move(Err);
1750b57cec5SDimitry Andric break;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric continue;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
1810b57cec5SDimitry Andric // Blob case. Read the number of bytes as a vbr6.
1820b57cec5SDimitry Andric Expected<uint32_t> MaybeNum = ReadVBR(6);
1830b57cec5SDimitry Andric if (!MaybeNum)
1840b57cec5SDimitry Andric return MaybeNum.takeError();
1850b57cec5SDimitry Andric unsigned NumElts = MaybeNum.get();
1860b57cec5SDimitry Andric SkipToFourByteBoundary(); // 32-bit alignment
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric // Figure out where the end of this blob will be including tail padding.
189*af732203SDimitry Andric const size_t NewEnd = GetCurrentBitNo() + alignTo(NumElts, 4) * 8;
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric // If this would read off the end of the bitcode file, just set the
1920b57cec5SDimitry Andric // record to empty and return.
1930b57cec5SDimitry Andric if (!canSkipToPos(NewEnd/8)) {
1940b57cec5SDimitry Andric skipToEnd();
1950b57cec5SDimitry Andric break;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric // Skip over the blob.
1990b57cec5SDimitry Andric if (Error Err = JumpToBit(NewEnd))
2000b57cec5SDimitry Andric return std::move(Err);
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric return Code;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric
readRecord(unsigned AbbrevID,SmallVectorImpl<uint64_t> & Vals,StringRef * Blob)2050b57cec5SDimitry Andric Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
2060b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Vals,
2070b57cec5SDimitry Andric StringRef *Blob) {
2080b57cec5SDimitry Andric if (AbbrevID == bitc::UNABBREV_RECORD) {
2090b57cec5SDimitry Andric Expected<uint32_t> MaybeCode = ReadVBR(6);
2100b57cec5SDimitry Andric if (!MaybeCode)
2110b57cec5SDimitry Andric return MaybeCode.takeError();
2120b57cec5SDimitry Andric uint32_t Code = MaybeCode.get();
2130b57cec5SDimitry Andric Expected<uint32_t> MaybeNumElts = ReadVBR(6);
2140b57cec5SDimitry Andric if (!MaybeNumElts)
2150b57cec5SDimitry Andric return MaybeNumElts.takeError();
2160b57cec5SDimitry Andric uint32_t NumElts = MaybeNumElts.get();
2175ffd83dbSDimitry Andric Vals.reserve(Vals.size() + NumElts);
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i)
2200b57cec5SDimitry Andric if (Expected<uint64_t> MaybeVal = ReadVBR64(6))
2210b57cec5SDimitry Andric Vals.push_back(MaybeVal.get());
2220b57cec5SDimitry Andric else
2230b57cec5SDimitry Andric return MaybeVal.takeError();
2240b57cec5SDimitry Andric return Code;
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric // Read the record code first.
2300b57cec5SDimitry Andric assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
2310b57cec5SDimitry Andric const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
2320b57cec5SDimitry Andric unsigned Code;
2330b57cec5SDimitry Andric if (CodeOp.isLiteral())
2340b57cec5SDimitry Andric Code = CodeOp.getLiteralValue();
2350b57cec5SDimitry Andric else {
2360b57cec5SDimitry Andric if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
2370b57cec5SDimitry Andric CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
2380b57cec5SDimitry Andric report_fatal_error("Abbreviation starts with an Array or a Blob");
2390b57cec5SDimitry Andric if (Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp))
2400b57cec5SDimitry Andric Code = MaybeCode.get();
2410b57cec5SDimitry Andric else
2420b57cec5SDimitry Andric return MaybeCode.takeError();
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
2460b57cec5SDimitry Andric const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
2470b57cec5SDimitry Andric if (Op.isLiteral()) {
2480b57cec5SDimitry Andric Vals.push_back(Op.getLiteralValue());
2490b57cec5SDimitry Andric continue;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
2530b57cec5SDimitry Andric Op.getEncoding() != BitCodeAbbrevOp::Blob) {
2540b57cec5SDimitry Andric if (Expected<uint64_t> MaybeVal = readAbbreviatedField(*this, Op))
2550b57cec5SDimitry Andric Vals.push_back(MaybeVal.get());
2560b57cec5SDimitry Andric else
2570b57cec5SDimitry Andric return MaybeVal.takeError();
2580b57cec5SDimitry Andric continue;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
2620b57cec5SDimitry Andric // Array case. Read the number of elements as a vbr6.
2630b57cec5SDimitry Andric Expected<uint32_t> MaybeNumElts = ReadVBR(6);
2640b57cec5SDimitry Andric if (!MaybeNumElts)
2650b57cec5SDimitry Andric return MaybeNumElts.takeError();
2660b57cec5SDimitry Andric uint32_t NumElts = MaybeNumElts.get();
2675ffd83dbSDimitry Andric Vals.reserve(Vals.size() + NumElts);
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric // Get the element encoding.
2700b57cec5SDimitry Andric if (i + 2 != e)
2710b57cec5SDimitry Andric report_fatal_error("Array op not second to last");
2720b57cec5SDimitry Andric const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
2730b57cec5SDimitry Andric if (!EltEnc.isEncoding())
2740b57cec5SDimitry Andric report_fatal_error(
2750b57cec5SDimitry Andric "Array element type has to be an encoding of a type");
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric // Read all the elements.
2780b57cec5SDimitry Andric switch (EltEnc.getEncoding()) {
2790b57cec5SDimitry Andric default:
2800b57cec5SDimitry Andric report_fatal_error("Array element type can't be an Array or a Blob");
2810b57cec5SDimitry Andric case BitCodeAbbrevOp::Fixed:
2820b57cec5SDimitry Andric for (; NumElts; --NumElts)
2830b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> MaybeVal =
2840b57cec5SDimitry Andric Read((unsigned)EltEnc.getEncodingData()))
2850b57cec5SDimitry Andric Vals.push_back(MaybeVal.get());
2860b57cec5SDimitry Andric else
2870b57cec5SDimitry Andric return MaybeVal.takeError();
2880b57cec5SDimitry Andric break;
2890b57cec5SDimitry Andric case BitCodeAbbrevOp::VBR:
2900b57cec5SDimitry Andric for (; NumElts; --NumElts)
2910b57cec5SDimitry Andric if (Expected<uint64_t> MaybeVal =
2920b57cec5SDimitry Andric ReadVBR64((unsigned)EltEnc.getEncodingData()))
2930b57cec5SDimitry Andric Vals.push_back(MaybeVal.get());
2940b57cec5SDimitry Andric else
2950b57cec5SDimitry Andric return MaybeVal.takeError();
2960b57cec5SDimitry Andric break;
2970b57cec5SDimitry Andric case BitCodeAbbrevOp::Char6:
2980b57cec5SDimitry Andric for (; NumElts; --NumElts)
2990b57cec5SDimitry Andric if (Expected<SimpleBitstreamCursor::word_t> MaybeVal = Read(6))
3000b57cec5SDimitry Andric Vals.push_back(BitCodeAbbrevOp::DecodeChar6(MaybeVal.get()));
3010b57cec5SDimitry Andric else
3020b57cec5SDimitry Andric return MaybeVal.takeError();
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric continue;
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andric assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
3080b57cec5SDimitry Andric // Blob case. Read the number of bytes as a vbr6.
3090b57cec5SDimitry Andric Expected<uint32_t> MaybeNumElts = ReadVBR(6);
3100b57cec5SDimitry Andric if (!MaybeNumElts)
3110b57cec5SDimitry Andric return MaybeNumElts.takeError();
3120b57cec5SDimitry Andric uint32_t NumElts = MaybeNumElts.get();
3130b57cec5SDimitry Andric SkipToFourByteBoundary(); // 32-bit alignment
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andric // Figure out where the end of this blob will be including tail padding.
3160b57cec5SDimitry Andric size_t CurBitPos = GetCurrentBitNo();
317*af732203SDimitry Andric const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8;
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andric // If this would read off the end of the bitcode file, just set the
3200b57cec5SDimitry Andric // record to empty and return.
3210b57cec5SDimitry Andric if (!canSkipToPos(NewEnd/8)) {
3220b57cec5SDimitry Andric Vals.append(NumElts, 0);
3230b57cec5SDimitry Andric skipToEnd();
3240b57cec5SDimitry Andric break;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric // Otherwise, inform the streamer that we need these bytes in memory. Skip
3280b57cec5SDimitry Andric // over tail padding first, in case jumping to NewEnd invalidates the Blob
3290b57cec5SDimitry Andric // pointer.
3300b57cec5SDimitry Andric if (Error Err = JumpToBit(NewEnd))
3310b57cec5SDimitry Andric return std::move(Err);
3320b57cec5SDimitry Andric const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric // If we can return a reference to the data, do so to avoid copying it.
3350b57cec5SDimitry Andric if (Blob) {
3360b57cec5SDimitry Andric *Blob = StringRef(Ptr, NumElts);
3370b57cec5SDimitry Andric } else {
3380b57cec5SDimitry Andric // Otherwise, unpack into Vals with zero extension.
3395ffd83dbSDimitry Andric auto *UPtr = reinterpret_cast<const unsigned char *>(Ptr);
3405ffd83dbSDimitry Andric Vals.append(UPtr, UPtr + NumElts);
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric
3440b57cec5SDimitry Andric return Code;
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
ReadAbbrevRecord()3470b57cec5SDimitry Andric Error BitstreamCursor::ReadAbbrevRecord() {
3480b57cec5SDimitry Andric auto Abbv = std::make_shared<BitCodeAbbrev>();
3490b57cec5SDimitry Andric Expected<uint32_t> MaybeNumOpInfo = ReadVBR(5);
3500b57cec5SDimitry Andric if (!MaybeNumOpInfo)
3510b57cec5SDimitry Andric return MaybeNumOpInfo.takeError();
3520b57cec5SDimitry Andric unsigned NumOpInfo = MaybeNumOpInfo.get();
3530b57cec5SDimitry Andric for (unsigned i = 0; i != NumOpInfo; ++i) {
3540b57cec5SDimitry Andric Expected<word_t> MaybeIsLiteral = Read(1);
3550b57cec5SDimitry Andric if (!MaybeIsLiteral)
3560b57cec5SDimitry Andric return MaybeIsLiteral.takeError();
3570b57cec5SDimitry Andric bool IsLiteral = MaybeIsLiteral.get();
3580b57cec5SDimitry Andric if (IsLiteral) {
3590b57cec5SDimitry Andric Expected<uint64_t> MaybeOp = ReadVBR64(8);
3600b57cec5SDimitry Andric if (!MaybeOp)
3610b57cec5SDimitry Andric return MaybeOp.takeError();
3620b57cec5SDimitry Andric Abbv->Add(BitCodeAbbrevOp(MaybeOp.get()));
3630b57cec5SDimitry Andric continue;
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric Expected<word_t> MaybeEncoding = Read(3);
3670b57cec5SDimitry Andric if (!MaybeEncoding)
3680b57cec5SDimitry Andric return MaybeEncoding.takeError();
3690b57cec5SDimitry Andric BitCodeAbbrevOp::Encoding E =
3700b57cec5SDimitry Andric (BitCodeAbbrevOp::Encoding)MaybeEncoding.get();
3710b57cec5SDimitry Andric if (BitCodeAbbrevOp::hasEncodingData(E)) {
3720b57cec5SDimitry Andric Expected<uint64_t> MaybeData = ReadVBR64(5);
3730b57cec5SDimitry Andric if (!MaybeData)
3740b57cec5SDimitry Andric return MaybeData.takeError();
3750b57cec5SDimitry Andric uint64_t Data = MaybeData.get();
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andric // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
3780b57cec5SDimitry Andric // and vbr(0) as a literal zero. This is decoded the same way, and avoids
3790b57cec5SDimitry Andric // a slow path in Read() to have to handle reading zero bits.
3800b57cec5SDimitry Andric if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
3810b57cec5SDimitry Andric Data == 0) {
3820b57cec5SDimitry Andric Abbv->Add(BitCodeAbbrevOp(0));
3830b57cec5SDimitry Andric continue;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
3870b57cec5SDimitry Andric Data > MaxChunkSize)
3880b57cec5SDimitry Andric report_fatal_error(
3890b57cec5SDimitry Andric "Fixed or VBR abbrev record with size > MaxChunkData");
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric Abbv->Add(BitCodeAbbrevOp(E, Data));
3920b57cec5SDimitry Andric } else
3930b57cec5SDimitry Andric Abbv->Add(BitCodeAbbrevOp(E));
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric if (Abbv->getNumOperandInfos() == 0)
3970b57cec5SDimitry Andric report_fatal_error("Abbrev record with no operands");
3980b57cec5SDimitry Andric CurAbbrevs.push_back(std::move(Abbv));
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric return Error::success();
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric Expected<Optional<BitstreamBlockInfo>>
ReadBlockInfoBlock(bool ReadBlockInfoNames)4040b57cec5SDimitry Andric BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
4050b57cec5SDimitry Andric if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID))
4060b57cec5SDimitry Andric return std::move(Err);
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andric BitstreamBlockInfo NewBlockInfo;
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record;
4110b57cec5SDimitry Andric BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric // Read all the records for this module.
4140b57cec5SDimitry Andric while (true) {
4150b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry =
4160b57cec5SDimitry Andric advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
4170b57cec5SDimitry Andric if (!MaybeEntry)
4180b57cec5SDimitry Andric return MaybeEntry.takeError();
4190b57cec5SDimitry Andric BitstreamEntry Entry = MaybeEntry.get();
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric switch (Entry.Kind) {
4220b57cec5SDimitry Andric case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4230b57cec5SDimitry Andric case llvm::BitstreamEntry::Error:
4240b57cec5SDimitry Andric return None;
4250b57cec5SDimitry Andric case llvm::BitstreamEntry::EndBlock:
4260b57cec5SDimitry Andric return std::move(NewBlockInfo);
4270b57cec5SDimitry Andric case llvm::BitstreamEntry::Record:
4280b57cec5SDimitry Andric // The interesting case.
4290b57cec5SDimitry Andric break;
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric
4320b57cec5SDimitry Andric // Read abbrev records, associate them with CurBID.
4330b57cec5SDimitry Andric if (Entry.ID == bitc::DEFINE_ABBREV) {
4340b57cec5SDimitry Andric if (!CurBlockInfo) return None;
4350b57cec5SDimitry Andric if (Error Err = ReadAbbrevRecord())
4360b57cec5SDimitry Andric return std::move(Err);
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
4390b57cec5SDimitry Andric // appropriate BlockInfo.
4400b57cec5SDimitry Andric CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
4410b57cec5SDimitry Andric CurAbbrevs.pop_back();
4420b57cec5SDimitry Andric continue;
4430b57cec5SDimitry Andric }
4440b57cec5SDimitry Andric
4450b57cec5SDimitry Andric // Read a record.
4460b57cec5SDimitry Andric Record.clear();
4470b57cec5SDimitry Andric Expected<unsigned> MaybeBlockInfo = readRecord(Entry.ID, Record);
4480b57cec5SDimitry Andric if (!MaybeBlockInfo)
4490b57cec5SDimitry Andric return MaybeBlockInfo.takeError();
4500b57cec5SDimitry Andric switch (MaybeBlockInfo.get()) {
4510b57cec5SDimitry Andric default:
4520b57cec5SDimitry Andric break; // Default behavior, ignore unknown content.
4530b57cec5SDimitry Andric case bitc::BLOCKINFO_CODE_SETBID:
4540b57cec5SDimitry Andric if (Record.size() < 1)
4550b57cec5SDimitry Andric return None;
4560b57cec5SDimitry Andric CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
4570b57cec5SDimitry Andric break;
4580b57cec5SDimitry Andric case bitc::BLOCKINFO_CODE_BLOCKNAME: {
4590b57cec5SDimitry Andric if (!CurBlockInfo)
4600b57cec5SDimitry Andric return None;
4610b57cec5SDimitry Andric if (!ReadBlockInfoNames)
4620b57cec5SDimitry Andric break; // Ignore name.
4635ffd83dbSDimitry Andric CurBlockInfo->Name = std::string(Record.begin(), Record.end());
4640b57cec5SDimitry Andric break;
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
4670b57cec5SDimitry Andric if (!CurBlockInfo) return None;
4680b57cec5SDimitry Andric if (!ReadBlockInfoNames)
4690b57cec5SDimitry Andric break; // Ignore name.
4705ffd83dbSDimitry Andric CurBlockInfo->RecordNames.emplace_back(
4715ffd83dbSDimitry Andric (unsigned)Record[0], std::string(Record.begin() + 1, Record.end()));
4720b57cec5SDimitry Andric break;
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric }
477