2013-01-20 02:19:39 +08:00
|
|
|
//===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-20 02:19:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-04 06:40:07 +08:00
|
|
|
#include "llvm/Bitstream/BitstreamReader.h"
|
2016-08-19 01:56:27 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
2013-01-20 02:19:39 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BitstreamCursor implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
/// Having read the ENTER_SUBBLOCK abbrevid, enter the block.
|
|
|
|
Error BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
|
2013-01-20 02:19:39 +08:00
|
|
|
// Save the current block's state on BlockScope.
|
|
|
|
BlockScope.push_back(Block(CurCodeSize));
|
|
|
|
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Add the abbrevs specific to this block to the CurAbbrevs list.
|
2016-11-08 12:17:11 +08:00
|
|
|
if (BlockInfo) {
|
|
|
|
if (const BitstreamBlockInfo::BlockInfo *Info =
|
|
|
|
BlockInfo->getBlockInfo(BlockID)) {
|
|
|
|
CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
|
|
|
|
Info->Abbrevs.end());
|
|
|
|
}
|
2013-01-20 02:19:39 +08:00
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Get the codesize of this block.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeVBR = ReadVBR(bitc::CodeLenWidth);
|
|
|
|
if (!MaybeVBR)
|
|
|
|
return MaybeVBR.takeError();
|
|
|
|
CurCodeSize = MaybeVBR.get();
|
|
|
|
|
2015-05-19 08:34:17 +08:00
|
|
|
if (CurCodeSize > MaxChunkSize)
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(
|
|
|
|
std::errc::illegal_byte_sequence,
|
|
|
|
"can't read more than %zu at a time, trying to read %u", +MaxChunkSize,
|
|
|
|
CurCodeSize);
|
2015-05-19 08:34:17 +08:00
|
|
|
|
2013-01-22 02:04:19 +08:00
|
|
|
SkipToFourByteBoundary();
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<word_t> MaybeNum = Read(bitc::BlockSizeWidth);
|
|
|
|
if (!MaybeNum)
|
|
|
|
return MaybeNum.takeError();
|
|
|
|
word_t NumWords = MaybeNum.get();
|
|
|
|
if (NumWordsP)
|
|
|
|
*NumWordsP = NumWords;
|
|
|
|
|
|
|
|
if (CurCodeSize == 0)
|
|
|
|
return llvm::createStringError(
|
|
|
|
std::errc::illegal_byte_sequence,
|
|
|
|
"can't enter sub-block: current code size is 0");
|
|
|
|
if (AtEndOfStream())
|
|
|
|
return llvm::createStringError(
|
|
|
|
std::errc::illegal_byte_sequence,
|
|
|
|
"can't enter sub block: already at end of stream");
|
|
|
|
|
|
|
|
return Error::success();
|
2013-01-20 02:19:39 +08:00
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
static Expected<uint64_t> readAbbreviatedField(BitstreamCursor &Cursor,
|
|
|
|
const BitCodeAbbrevOp &Op) {
|
2014-11-14 06:29:02 +08:00
|
|
|
assert(!Op.isLiteral() && "Not to be used with literals!");
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// Decode the value as we are commanded.
|
|
|
|
switch (Op.getEncoding()) {
|
|
|
|
case BitCodeAbbrevOp::Array:
|
|
|
|
case BitCodeAbbrevOp::Blob:
|
2014-06-18 13:05:13 +08:00
|
|
|
llvm_unreachable("Should not reach here");
|
2013-01-20 08:00:00 +08:00
|
|
|
case BitCodeAbbrevOp::Fixed:
|
2015-04-23 21:25:35 +08:00
|
|
|
assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
|
2014-11-14 06:29:02 +08:00
|
|
|
return Cursor.Read((unsigned)Op.getEncodingData());
|
2013-01-20 08:00:00 +08:00
|
|
|
case BitCodeAbbrevOp::VBR:
|
2015-04-23 21:25:35 +08:00
|
|
|
assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
|
2014-11-14 06:29:02 +08:00
|
|
|
return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
|
2013-01-20 08:00:00 +08:00
|
|
|
case BitCodeAbbrevOp::Char6:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<unsigned> Res = Cursor.Read(6))
|
|
|
|
return BitCodeAbbrevOp::DecodeChar6(Res.get());
|
|
|
|
else
|
|
|
|
return Res.takeError();
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
2014-11-14 07:07:22 +08:00
|
|
|
llvm_unreachable("invalid abbreviation encoding");
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// skipRecord - Read the current record and discard it.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) {
|
2013-01-20 08:00:00 +08:00
|
|
|
// Skip unabbreviated records by reading past their entries.
|
|
|
|
if (AbbrevID == bitc::UNABBREV_RECORD) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeCode = ReadVBR(6);
|
|
|
|
if (!MaybeCode)
|
|
|
|
return MaybeCode.takeError();
|
|
|
|
unsigned Code = MaybeCode.get();
|
|
|
|
Expected<uint32_t> MaybeVBR = ReadVBR(6);
|
|
|
|
if (!MaybeVBR)
|
|
|
|
return MaybeVBR.get();
|
|
|
|
unsigned NumElts = MaybeVBR.get();
|
2013-01-20 08:00:00 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> Res = ReadVBR64(6))
|
|
|
|
; // Skip!
|
|
|
|
else
|
|
|
|
return Res.takeError();
|
2017-01-05 06:54:14 +08:00
|
|
|
return Code;
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
|
2017-01-05 06:54:14 +08:00
|
|
|
const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
|
|
|
|
unsigned Code;
|
|
|
|
if (CodeOp.isLiteral())
|
|
|
|
Code = CodeOp.getLiteralValue();
|
|
|
|
else {
|
|
|
|
if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
|
|
|
|
CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(
|
|
|
|
std::errc::illegal_byte_sequence,
|
|
|
|
"Abbreviation starts with an Array or a Blob");
|
|
|
|
Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp);
|
|
|
|
if (!MaybeCode)
|
|
|
|
return MaybeCode.takeError();
|
|
|
|
Code = MaybeCode.get();
|
2017-01-05 06:54:14 +08:00
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2017-01-05 06:54:14 +08:00
|
|
|
for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) {
|
2013-01-20 08:00:00 +08:00
|
|
|
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
|
|
|
|
if (Op.isLiteral())
|
|
|
|
continue;
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
|
|
|
|
Op.getEncoding() != BitCodeAbbrevOp::Blob) {
|
2019-12-26 10:40:21 +08:00
|
|
|
if (Expected<uint64_t> MaybeField = readAbbreviatedField(*this, Op))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return MaybeField.takeError();
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
|
|
|
|
// Array case. Read the number of elements as a vbr6.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeNum = ReadVBR(6);
|
|
|
|
if (!MaybeNum)
|
|
|
|
return MaybeNum.takeError();
|
|
|
|
unsigned NumElts = MaybeNum.get();
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// Get the element encoding.
|
|
|
|
assert(i+2 == e && "array op not second to last?");
|
|
|
|
const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// Read all the elements.
|
2016-03-07 08:38:09 +08:00
|
|
|
// Decode the value as we are commanded.
|
|
|
|
switch (EltEnc.getEncoding()) {
|
|
|
|
default:
|
|
|
|
report_fatal_error("Array element type can't be an Array or a Blob");
|
|
|
|
case BitCodeAbbrevOp::Fixed:
|
2016-12-01 13:47:58 +08:00
|
|
|
assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Error Err = JumpToBit(GetCurrentBitNo() +
|
|
|
|
NumElts * EltEnc.getEncodingData()))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2016-03-07 08:38:09 +08:00
|
|
|
break;
|
|
|
|
case BitCodeAbbrevOp::VBR:
|
2016-12-01 13:47:58 +08:00
|
|
|
assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
|
2016-03-07 08:38:09 +08:00
|
|
|
for (; NumElts; --NumElts)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> Res =
|
|
|
|
ReadVBR64((unsigned)EltEnc.getEncodingData()))
|
|
|
|
; // Skip!
|
|
|
|
else
|
|
|
|
return Res.takeError();
|
2016-03-07 08:38:09 +08:00
|
|
|
break;
|
|
|
|
case BitCodeAbbrevOp::Char6:
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Error Err = JumpToBit(GetCurrentBitNo() + NumElts * 6))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2016-03-07 08:38:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-01-20 08:00:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
|
|
|
|
// Blob case. Read the number of bytes as a vbr6.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeNum = ReadVBR(6);
|
|
|
|
if (!MaybeNum)
|
|
|
|
return MaybeNum.takeError();
|
|
|
|
unsigned NumElts = MaybeNum.get();
|
2013-01-22 02:04:19 +08:00
|
|
|
SkipToFourByteBoundary(); // 32-bit alignment
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// Figure out where the end of this blob will be including tail padding.
|
2013-01-22 02:18:25 +08:00
|
|
|
size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// If this would read off the end of the bitcode file, just set the
|
|
|
|
// record to empty and return.
|
2013-01-22 02:18:25 +08:00
|
|
|
if (!canSkipToPos(NewEnd/8)) {
|
2016-03-28 06:40:55 +08:00
|
|
|
skipToEnd();
|
2013-01-20 08:00:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 08:00:00 +08:00
|
|
|
// Skip over the blob.
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Error Err = JumpToBit(NewEnd))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
2017-01-05 06:54:14 +08:00
|
|
|
return Code;
|
2013-01-20 08:00:00 +08:00
|
|
|
}
|
2013-01-20 02:19:39 +08:00
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,
|
|
|
|
SmallVectorImpl<uint64_t> &Vals,
|
|
|
|
StringRef *Blob) {
|
2013-01-20 02:19:39 +08:00
|
|
|
if (AbbrevID == bitc::UNABBREV_RECORD) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeCode = ReadVBR(6);
|
|
|
|
if (!MaybeCode)
|
|
|
|
return MaybeCode.takeError();
|
|
|
|
uint32_t Code = MaybeCode.get();
|
|
|
|
Expected<uint32_t> MaybeNumElts = ReadVBR(6);
|
|
|
|
if (!MaybeNumElts)
|
|
|
|
return MaybeNumElts.takeError();
|
|
|
|
uint32_t NumElts = MaybeNumElts.get();
|
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> MaybeVal = ReadVBR64(6))
|
|
|
|
Vals.push_back(MaybeVal.get());
|
|
|
|
else
|
|
|
|
return MaybeVal.takeError();
|
2013-01-20 02:19:39 +08:00
|
|
|
return Code;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-05-11 06:17:10 +08:00
|
|
|
// Read the record code first.
|
|
|
|
assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
|
|
|
|
const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
|
2014-11-14 06:29:02 +08:00
|
|
|
unsigned Code;
|
2013-05-11 06:17:10 +08:00
|
|
|
if (CodeOp.isLiteral())
|
2014-11-14 06:29:02 +08:00
|
|
|
Code = CodeOp.getLiteralValue();
|
2015-01-24 12:15:05 +08:00
|
|
|
else {
|
|
|
|
if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
|
|
|
|
CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
|
|
|
|
report_fatal_error("Abbreviation starts with an Array or a Blob");
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp))
|
|
|
|
Code = MaybeCode.get();
|
|
|
|
else
|
|
|
|
return MaybeCode.takeError();
|
2015-01-24 12:15:05 +08:00
|
|
|
}
|
2013-05-11 06:17:10 +08:00
|
|
|
|
|
|
|
for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
|
2013-01-20 02:19:39 +08:00
|
|
|
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
|
|
|
|
if (Op.isLiteral()) {
|
2014-11-14 06:29:02 +08:00
|
|
|
Vals.push_back(Op.getLiteralValue());
|
2013-01-20 02:19:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
|
|
|
|
Op.getEncoding() != BitCodeAbbrevOp::Blob) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> MaybeVal = readAbbreviatedField(*this, Op))
|
|
|
|
Vals.push_back(MaybeVal.get());
|
|
|
|
else
|
|
|
|
return MaybeVal.takeError();
|
2013-01-20 02:19:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
|
|
|
|
// Array case. Read the number of elements as a vbr6.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeNumElts = ReadVBR(6);
|
|
|
|
if (!MaybeNumElts)
|
|
|
|
return MaybeNumElts.takeError();
|
|
|
|
uint32_t NumElts = MaybeNumElts.get();
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Get the element encoding.
|
2015-05-27 08:48:37 +08:00
|
|
|
if (i + 2 != e)
|
|
|
|
report_fatal_error("Array op not second to last");
|
2013-01-20 02:19:39 +08:00
|
|
|
const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
|
2015-05-27 08:48:43 +08:00
|
|
|
if (!EltEnc.isEncoding())
|
|
|
|
report_fatal_error(
|
|
|
|
"Array element type has to be an encoding of a type");
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Read all the elements.
|
2016-03-07 08:38:09 +08:00
|
|
|
switch (EltEnc.getEncoding()) {
|
|
|
|
default:
|
|
|
|
report_fatal_error("Array element type can't be an Array or a Blob");
|
|
|
|
case BitCodeAbbrevOp::Fixed:
|
|
|
|
for (; NumElts; --NumElts)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<SimpleBitstreamCursor::word_t> MaybeVal =
|
|
|
|
Read((unsigned)EltEnc.getEncodingData()))
|
|
|
|
Vals.push_back(MaybeVal.get());
|
|
|
|
else
|
|
|
|
return MaybeVal.takeError();
|
2016-03-07 08:38:09 +08:00
|
|
|
break;
|
|
|
|
case BitCodeAbbrevOp::VBR:
|
|
|
|
for (; NumElts; --NumElts)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<uint64_t> MaybeVal =
|
|
|
|
ReadVBR64((unsigned)EltEnc.getEncodingData()))
|
|
|
|
Vals.push_back(MaybeVal.get());
|
|
|
|
else
|
|
|
|
return MaybeVal.takeError();
|
2016-03-07 08:38:09 +08:00
|
|
|
break;
|
|
|
|
case BitCodeAbbrevOp::Char6:
|
|
|
|
for (; NumElts; --NumElts)
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Expected<SimpleBitstreamCursor::word_t> MaybeVal = Read(6))
|
|
|
|
Vals.push_back(BitCodeAbbrevOp::DecodeChar6(MaybeVal.get()));
|
|
|
|
else
|
|
|
|
return MaybeVal.takeError();
|
2016-03-07 08:38:09 +08:00
|
|
|
}
|
2013-01-20 02:19:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
|
|
|
|
// Blob case. Read the number of bytes as a vbr6.
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeNumElts = ReadVBR(6);
|
|
|
|
if (!MaybeNumElts)
|
|
|
|
return MaybeNumElts.takeError();
|
|
|
|
uint32_t NumElts = MaybeNumElts.get();
|
2013-01-22 02:04:19 +08:00
|
|
|
SkipToFourByteBoundary(); // 32-bit alignment
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Figure out where the end of this blob will be including tail padding.
|
2013-01-22 02:18:25 +08:00
|
|
|
size_t CurBitPos = GetCurrentBitNo();
|
|
|
|
size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// If this would read off the end of the bitcode file, just set the
|
|
|
|
// record to empty and return.
|
2013-01-22 02:18:25 +08:00
|
|
|
if (!canSkipToPos(NewEnd/8)) {
|
2013-01-20 02:19:39 +08:00
|
|
|
Vals.append(NumElts, 0);
|
2016-03-28 06:40:55 +08:00
|
|
|
skipToEnd();
|
2013-01-20 02:19:39 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2016-03-28 07:00:59 +08:00
|
|
|
// Otherwise, inform the streamer that we need these bytes in memory. Skip
|
|
|
|
// over tail padding first, in case jumping to NewEnd invalidates the Blob
|
|
|
|
// pointer.
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Error Err = JumpToBit(NewEnd))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2016-03-28 06:45:25 +08:00
|
|
|
const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-22 02:24:49 +08:00
|
|
|
// If we can return a reference to the data, do so to avoid copying it.
|
2013-01-20 09:06:48 +08:00
|
|
|
if (Blob) {
|
2013-01-22 02:24:49 +08:00
|
|
|
*Blob = StringRef(Ptr, NumElts);
|
2013-01-20 02:19:39 +08:00
|
|
|
} else {
|
2013-01-22 02:24:49 +08:00
|
|
|
// Otherwise, unpack into Vals with zero extension.
|
2013-01-22 02:18:25 +08:00
|
|
|
for (; NumElts; --NumElts)
|
2013-01-22 02:24:49 +08:00
|
|
|
Vals.push_back((unsigned char)*Ptr++);
|
2013-01-20 02:19:39 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
return Code;
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
Error BitstreamCursor::ReadAbbrevRecord() {
|
2017-01-05 06:36:33 +08:00
|
|
|
auto Abbv = std::make_shared<BitCodeAbbrev>();
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint32_t> MaybeNumOpInfo = ReadVBR(5);
|
|
|
|
if (!MaybeNumOpInfo)
|
|
|
|
return MaybeNumOpInfo.takeError();
|
|
|
|
unsigned NumOpInfo = MaybeNumOpInfo.get();
|
2013-01-20 02:19:39 +08:00
|
|
|
for (unsigned i = 0; i != NumOpInfo; ++i) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<word_t> MaybeIsLiteral = Read(1);
|
|
|
|
if (!MaybeIsLiteral)
|
|
|
|
return MaybeIsLiteral.takeError();
|
|
|
|
bool IsLiteral = MaybeIsLiteral.get();
|
2013-01-20 02:19:39 +08:00
|
|
|
if (IsLiteral) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint64_t> MaybeOp = ReadVBR64(8);
|
|
|
|
if (!MaybeOp)
|
|
|
|
return MaybeOp.takeError();
|
|
|
|
Abbv->Add(BitCodeAbbrevOp(MaybeOp.get()));
|
2013-01-20 02:19:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<word_t> MaybeEncoding = Read(3);
|
|
|
|
if (!MaybeEncoding)
|
|
|
|
return MaybeEncoding.takeError();
|
|
|
|
BitCodeAbbrevOp::Encoding E =
|
|
|
|
(BitCodeAbbrevOp::Encoding)MaybeEncoding.get();
|
2013-02-09 15:07:29 +08:00
|
|
|
if (BitCodeAbbrevOp::hasEncodingData(E)) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<uint64_t> MaybeData = ReadVBR64(5);
|
|
|
|
if (!MaybeData)
|
|
|
|
return MaybeData.takeError();
|
|
|
|
uint64_t Data = MaybeData.get();
|
2013-02-09 15:07:29 +08:00
|
|
|
|
|
|
|
// As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
|
|
|
|
// and vbr(0) as a literal zero. This is decoded the same way, and avoids
|
|
|
|
// a slow path in Read() to have to handle reading zero bits.
|
|
|
|
if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
|
|
|
|
Data == 0) {
|
|
|
|
Abbv->Add(BitCodeAbbrevOp(0));
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-01 10:28:07 +08:00
|
|
|
|
2015-04-23 21:25:35 +08:00
|
|
|
if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
|
|
|
|
Data > MaxChunkSize)
|
|
|
|
report_fatal_error(
|
|
|
|
"Fixed or VBR abbrev record with size > MaxChunkData");
|
|
|
|
|
2013-02-09 15:07:29 +08:00
|
|
|
Abbv->Add(BitCodeAbbrevOp(E, Data));
|
|
|
|
} else
|
2013-01-20 02:19:39 +08:00
|
|
|
Abbv->Add(BitCodeAbbrevOp(E));
|
|
|
|
}
|
2015-05-27 07:52:21 +08:00
|
|
|
|
|
|
|
if (Abbv->getNumOperandInfos() == 0)
|
|
|
|
report_fatal_error("Abbrev record with no operands");
|
2017-01-05 06:36:33 +08:00
|
|
|
CurAbbrevs.push_back(std::move(Abbv));
|
2019-06-27 03:50:12 +08:00
|
|
|
|
|
|
|
return Error::success();
|
2013-01-20 02:19:39 +08:00
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<Optional<BitstreamBlockInfo>>
|
2016-11-08 12:17:11 +08:00
|
|
|
BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2016-11-08 12:17:11 +08:00
|
|
|
BitstreamBlockInfo NewBlockInfo;
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
SmallVector<uint64_t, 64> Record;
|
2016-11-08 12:17:11 +08:00
|
|
|
BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Read all the records for this module.
|
2016-08-19 01:56:27 +08:00
|
|
|
while (true) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<BitstreamEntry> MaybeEntry =
|
|
|
|
advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
|
|
|
|
if (!MaybeEntry)
|
|
|
|
return MaybeEntry.takeError();
|
|
|
|
BitstreamEntry Entry = MaybeEntry.get();
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 10:13:19 +08:00
|
|
|
switch (Entry.Kind) {
|
|
|
|
case llvm::BitstreamEntry::SubBlock: // Handled for us already.
|
|
|
|
case llvm::BitstreamEntry::Error:
|
2016-11-08 12:17:11 +08:00
|
|
|
return None;
|
2013-01-20 10:13:19 +08:00
|
|
|
case llvm::BitstreamEntry::EndBlock:
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(NewBlockInfo);
|
2013-01-20 10:13:19 +08:00
|
|
|
case llvm::BitstreamEntry::Record:
|
|
|
|
// The interesting case.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Read abbrev records, associate them with CurBID.
|
2013-01-20 10:13:19 +08:00
|
|
|
if (Entry.ID == bitc::DEFINE_ABBREV) {
|
2016-11-08 12:17:11 +08:00
|
|
|
if (!CurBlockInfo) return None;
|
2019-06-27 03:50:12 +08:00
|
|
|
if (Error Err = ReadAbbrevRecord())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
|
|
|
|
// appropriate BlockInfo.
|
2014-09-15 23:44:14 +08:00
|
|
|
CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
|
2013-01-20 02:19:39 +08:00
|
|
|
CurAbbrevs.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 06:14:06 +08:00
|
|
|
|
2013-01-20 02:19:39 +08:00
|
|
|
// Read a record.
|
|
|
|
Record.clear();
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeBlockInfo = readRecord(Entry.ID, Record);
|
|
|
|
if (!MaybeBlockInfo)
|
|
|
|
return MaybeBlockInfo.takeError();
|
|
|
|
switch (MaybeBlockInfo.get()) {
|
|
|
|
default:
|
|
|
|
break; // Default behavior, ignore unknown content.
|
|
|
|
case bitc::BLOCKINFO_CODE_SETBID:
|
|
|
|
if (Record.size() < 1)
|
|
|
|
return None;
|
|
|
|
CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
|
|
|
|
break;
|
|
|
|
case bitc::BLOCKINFO_CODE_BLOCKNAME: {
|
|
|
|
if (!CurBlockInfo)
|
|
|
|
return None;
|
|
|
|
if (!ReadBlockInfoNames)
|
|
|
|
break; // Ignore name.
|
|
|
|
std::string Name;
|
|
|
|
for (unsigned i = 0, e = Record.size(); i != e; ++i)
|
|
|
|
Name += (char)Record[i];
|
|
|
|
CurBlockInfo->Name = Name;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-20 02:19:39 +08:00
|
|
|
case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
|
2016-11-08 12:17:11 +08:00
|
|
|
if (!CurBlockInfo) return None;
|
|
|
|
if (!ReadBlockInfoNames)
|
2016-03-28 06:40:55 +08:00
|
|
|
break; // Ignore name.
|
2013-01-20 02:19:39 +08:00
|
|
|
std::string Name;
|
|
|
|
for (unsigned i = 1, e = Record.size(); i != e; ++i)
|
|
|
|
Name += (char)Record[i];
|
|
|
|
CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
|
|
|
|
Name));
|
|
|
|
break;
|
|
|
|
}
|
2019-06-27 03:50:12 +08:00
|
|
|
}
|
2013-01-20 02:19:39 +08:00
|
|
|
}
|
|
|
|
}
|