Skip to content

Commit

Permalink
move some private methods out of line, add a skipRecord() method.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172931 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lattner committed Jan 20, 2013
1 parent 657a99c commit f9147c4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 29 deletions.
35 changes: 9 additions & 26 deletions include/llvm/Bitcode/BitstreamReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,31 +488,12 @@ class BitstreamCursor {
//===--------------------------------------------------------------------===//

private:
void ReadAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(Op.isLiteral() && "Not a literal");
// If the abbrev specifies the literal value to use, use it.
Vals.push_back(Op.getLiteralValue());
}

void ReadAbbreviatedField(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");

// Decode the value as we are commanded.
switch (Op.getEncoding()) {
default: llvm_unreachable("Unknown encoding!");
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 readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals);
void readAbbreviatedField(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals);
void skipAbbreviatedField(const BitCodeAbbrevOp &Op);

public:

/// getAbbrev - Return the abbreviation for the specified AbbrevId.
Expand All @@ -522,6 +503,9 @@ class BitstreamCursor {
return CurAbbrevs[AbbrevNo];
}

/// skipRecord - Read the current record and discard it.
void skipRecord(unsigned AbbrevID);

unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
const char **BlobStart = 0, unsigned *BlobLen = 0);

Expand All @@ -530,7 +514,6 @@ class BitstreamCursor {
return ReadRecord(AbbrevID, Vals, &BlobStart, &BlobLen);
}


//===--------------------------------------------------------------------===//
// Abbrev Processing
//===--------------------------------------------------------------------===//
Expand Down
114 changes: 111 additions & 3 deletions lib/Bitcode/Reader/BitstreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,114 @@ bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
return false;
}

void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(Op.isLiteral() && "Not a literal");
// If the abbrev specifies the literal value to use, use it.
Vals.push_back(Op.getLiteralValue());
}

void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
SmallVectorImpl<uint64_t> &Vals) {
assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");

// Decode the value as we are commanded.
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!");

// Decode the value as we are commanded.
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;
}
}



/// skipRecord - Read the current record and discard it.
void BitstreamCursor::skipRecord(unsigned AbbrevID) {
// Skip unabbreviated records by reading past their entries.
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) {
// Array case. Read the number of elements as a vbr6.
unsigned NumElts = ReadVBR(6);

// Get the element encoding.
assert(i+2 == e && "array op not second to last?");
const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);

// Read all the elements.
for (; NumElts; --NumElts)
skipAbbreviatedField(EltEnc);
continue;
}

assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
// Blob case. Read the number of bytes as a vbr6.
unsigned NumElts = ReadVBR(6);
SkipToWord(); // 32-bit alignment

// Figure out where the end of this blob will be including tail padding.
size_t NewEnd = NextChar+((NumElts+3)&~3);

// If this would read off the end of the bitcode file, just set the
// record to empty and return.
if (!canSkipToPos(NewEnd)) {
NextChar = BitStream->getBitcodeBytes().getExtent();
break;
}

// Skip over the blob.
NextChar = NewEnd;
}
}

unsigned BitstreamCursor::ReadRecord(unsigned AbbrevID,
SmallVectorImpl<uint64_t> &Vals,
Expand All @@ -106,13 +214,13 @@ unsigned BitstreamCursor::ReadRecord(unsigned AbbrevID,
for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
if (Op.isLiteral()) {
ReadAbbreviatedLiteral(Op, Vals);
readAbbreviatedLiteral(Op, Vals);
continue;
}

if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
Op.getEncoding() != BitCodeAbbrevOp::Blob) {
ReadAbbreviatedField(Op, Vals);
readAbbreviatedField(Op, Vals);
continue;
}

Expand All @@ -126,7 +234,7 @@ unsigned BitstreamCursor::ReadRecord(unsigned AbbrevID,

// Read all the elements.
for (; NumElts; --NumElts)
ReadAbbreviatedField(EltEnc, Vals);
readAbbreviatedField(EltEnc, Vals);
continue;
}

Expand Down

0 comments on commit f9147c4

Please sign in to comment.