BitstreamReader.cpp [plain text]
#include "llvm/Bitcode/BitstreamReader.h"
using namespace llvm;
void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
freeState();
BitStream = RHS.BitStream;
NextChar = RHS.NextChar;
CurWord = RHS.CurWord;
BitsInCurWord = RHS.BitsInCurWord;
CurCodeSize = RHS.CurCodeSize;
CurAbbrevs = RHS.CurAbbrevs;
for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
CurAbbrevs[i]->addRef();
BlockScope = RHS.BlockScope;
for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
Abbrevs[i]->addRef();
}
}
void BitstreamCursor::freeState() {
for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
CurAbbrevs[i]->dropRef();
CurAbbrevs.clear();
for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
Abbrevs[i]->dropRef();
}
BlockScope.clear();
}
bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
BlockScope.push_back(Block(CurCodeSize));
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
if (const BitstreamReader::BlockInfo *Info =
BitStream->getBlockInfo(BlockID)) {
for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
CurAbbrevs.push_back(Info->Abbrevs[i]);
CurAbbrevs.back()->addRef();
}
}
CurCodeSize = ReadVBR(bitc::CodeLenWidth);
SkipToFourByteBoundary();
unsigned NumWords = Read(bitc::BlockSizeWidth);
if (NumWordsP) *NumWordsP = NumWords;
if (CurCodeSize == 0 || AtEndOfStream())
return true;
return false;
}
void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(Op.isLiteral() && "Not a literal");
Vals.push_back(Op.getLiteralValue());
}
void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
switch (Op.getEncoding()) {
case BitCodeAbbrevOp::Array:
case BitCodeAbbrevOp::Blob:
assert(0 && "Should not reach here");
case BitCodeAbbrevOp::Fixed:
Vals.push_back(Read((unsigned)Op.getEncodingData()));
break;
case BitCodeAbbrevOp::VBR:
Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
break;
case BitCodeAbbrevOp::Char6:
Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
break;
}
}
void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
switch (Op.getEncoding()) {
case BitCodeAbbrevOp::Array:
case BitCodeAbbrevOp::Blob:
assert(0 && "Should not reach here");
case BitCodeAbbrevOp::Fixed:
(void)Read((unsigned)Op.getEncodingData());
break;
case BitCodeAbbrevOp::VBR:
(void)ReadVBR64((unsigned)Op.getEncodingData());
break;
case BitCodeAbbrevOp::Char6:
(void)Read(6);
break;
}
}
void BitstreamCursor::skipRecord(unsigned AbbrevID) {
if (AbbrevID == bitc::UNABBREV_RECORD) {
unsigned Code = ReadVBR(6);
(void)Code;
unsigned NumElts = ReadVBR(6);
for (unsigned i = 0; i != NumElts; ++i)
(void)ReadVBR64(6);
return;
}
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
if (Op.isLiteral())
continue;
if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
Op.getEncoding() != BitCodeAbbrevOp::Blob) {
skipAbbreviatedField(Op);
continue;
}
if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
unsigned NumElts = ReadVBR(6);
assert(i+2 == e && "array op not second to last?");
const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
for (; NumElts; --NumElts)
skipAbbreviatedField(EltEnc);
continue;
}
assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
unsigned NumElts = ReadVBR(6);
SkipToFourByteBoundary();
size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
if (!canSkipToPos(NewEnd/8)) {
NextChar = BitStream->getBitcodeBytes().getExtent();
break;
}
JumpToBit(NewEnd);
}
}
unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
SmallVectorImpl<uint64_t> &Vals,
StringRef *Blob) {
if (AbbrevID == bitc::UNABBREV_RECORD) {
unsigned Code = ReadVBR(6);
unsigned NumElts = ReadVBR(6);
for (unsigned i = 0; i != NumElts; ++i)
Vals.push_back(ReadVBR64(6));
return Code;
}
const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
if (CodeOp.isLiteral())
readAbbreviatedLiteral(CodeOp, Vals);
else
readAbbreviatedField(CodeOp, Vals);
unsigned Code = (unsigned)Vals.pop_back_val();
for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
if (Op.isLiteral()) {
readAbbreviatedLiteral(Op, Vals);
continue;
}
if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
Op.getEncoding() != BitCodeAbbrevOp::Blob) {
readAbbreviatedField(Op, Vals);
continue;
}
if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
unsigned NumElts = ReadVBR(6);
assert(i+2 == e && "array op not second to last?");
const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
for (; NumElts; --NumElts)
readAbbreviatedField(EltEnc, Vals);
continue;
}
assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
unsigned NumElts = ReadVBR(6);
SkipToFourByteBoundary();
size_t CurBitPos = GetCurrentBitNo();
size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
if (!canSkipToPos(NewEnd/8)) {
Vals.append(NumElts, 0);
NextChar = BitStream->getBitcodeBytes().getExtent();
break;
}
const char *Ptr = (const char*)
BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
if (Blob) {
*Blob = StringRef(Ptr, NumElts);
} else {
for (; NumElts; --NumElts)
Vals.push_back((unsigned char)*Ptr++);
}
JumpToBit(NewEnd);
}
return Code;
}
void BitstreamCursor::ReadAbbrevRecord() {
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
unsigned NumOpInfo = ReadVBR(5);
for (unsigned i = 0; i != NumOpInfo; ++i) {
bool IsLiteral = Read(1) ? true : false;
if (IsLiteral) {
Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
continue;
}
BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
if (BitCodeAbbrevOp::hasEncodingData(E)) {
unsigned Data = ReadVBR64(5);
if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
Data == 0) {
Abbv->Add(BitCodeAbbrevOp(0));
continue;
}
Abbv->Add(BitCodeAbbrevOp(E, Data));
} else
Abbv->Add(BitCodeAbbrevOp(E));
}
CurAbbrevs.push_back(Abbv);
}
bool BitstreamCursor::ReadBlockInfoBlock() {
if (BitStream->hasBlockInfoRecords())
return SkipBlock();
if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
SmallVector<uint64_t, 64> Record;
BitstreamReader::BlockInfo *CurBlockInfo = 0;
while (1) {
BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
switch (Entry.Kind) {
case llvm::BitstreamEntry::SubBlock: case llvm::BitstreamEntry::Error:
return true;
case llvm::BitstreamEntry::EndBlock:
return false;
case llvm::BitstreamEntry::Record:
break;
}
if (Entry.ID == bitc::DEFINE_ABBREV) {
if (!CurBlockInfo) return true;
ReadAbbrevRecord();
BitCodeAbbrev *Abbv = CurAbbrevs.back();
CurAbbrevs.pop_back();
CurBlockInfo->Abbrevs.push_back(Abbv);
continue;
}
Record.clear();
switch (readRecord(Entry.ID, Record)) {
default: break; case bitc::BLOCKINFO_CODE_SETBID:
if (Record.size() < 1) return true;
CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
break;
case bitc::BLOCKINFO_CODE_BLOCKNAME: {
if (!CurBlockInfo) return true;
if (BitStream->isIgnoringBlockInfoNames()) break; std::string Name;
for (unsigned i = 0, e = Record.size(); i != e; ++i)
Name += (char)Record[i];
CurBlockInfo->Name = Name;
break;
}
case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
if (!CurBlockInfo) return true;
if (BitStream->isIgnoringBlockInfoNames()) break; 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;
}
}
}
}