From d08fb26a14516282e7a1a8f82219c3962667ead3 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Wed, 9 Sep 2015 20:53:31 +0000 Subject: [PATCH] Change EmitRecordWithAbbrevImpl to take Optional record code. NFC. This change enables EmitRecord to pass the supplied record Code to EmitRecordWithAbbrevImpl, rather than insert it into the Vals array. It is an enabler for changing EmitRecord to take an ArrayRef instead of a SmallVectorImpl& Patch suggested by Duncan P. N. Exon Smith, modified by myself a bit to get correct assertion checking. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247186 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/BitstreamWriter.h | 45 +++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/include/llvm/Bitcode/BitstreamWriter.h b/include/llvm/Bitcode/BitstreamWriter.h index 9f23023a141..648a06e202e 100644 --- a/include/llvm/Bitcode/BitstreamWriter.h +++ b/include/llvm/Bitcode/BitstreamWriter.h @@ -15,6 +15,7 @@ #ifndef LLVM_BITCODE_BITSTREAMWRITER_H #define LLVM_BITCODE_BITSTREAMWRITER_H +#include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Bitcode/BitCodes.h" @@ -285,10 +286,12 @@ private: /// EmitRecordWithAbbrevImpl - This is the core implementation of the record /// emission code. If BlobData is non-null, then it specifies an array of /// data that should be emitted as part of the Blob or Array operand that is - /// known to exist at the end of the record. + /// known to exist at the end of the record. If Code is specified, then + /// it is the record code to emit before the Vals, which must not contain + /// the code. template void EmitRecordWithAbbrevImpl(unsigned Abbrev, SmallVectorImpl &Vals, - StringRef Blob) { + StringRef Blob, Optional Code) { const char *BlobData = Blob.data(); unsigned BlobLen = (unsigned) Blob.size(); unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV; @@ -297,9 +300,23 @@ private: EmitCode(Abbrev); + unsigned i = 0, e = static_cast(Abbv->getNumOperandInfos()); + if (Code) { + assert(e && "Expected non-empty abbreviation"); + const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i++); + + if (Op.isLiteral()) + EmitAbbreviatedLiteral(Op, Code.getValue()); + else { + assert(Op.getEncoding() != BitCodeAbbrevOp::Array && + Op.getEncoding() != BitCodeAbbrevOp::Blob && + "Expected literal or scalar"); + EmitAbbreviatedField(Op, Code.getValue()); + } + } + unsigned RecordIdx = 0; - for (unsigned i = 0, e = static_cast(Abbv->getNumOperandInfos()); - i != e; ++i) { + for (; i != e; ++i) { const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); if (Op.isLiteral()) { assert(RecordIdx < Vals.size() && "Invalid abbrev/record"); @@ -307,7 +324,7 @@ private: ++RecordIdx; } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) { // Array case. - assert(i+2 == e && "array op not second to last?"); + assert(i + 2 == e && "array op not second to last?"); const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); // If this record has blob data, emit it, otherwise we must have record @@ -395,10 +412,7 @@ public: return; } - // Insert the code into Vals to treat it uniformly. - Vals.insert(Vals.begin(), Code); - - EmitRecordWithAbbrev(Abbrev, Vals); + EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(), Code); } /// EmitRecordWithAbbrev - Emit a record with the specified abbreviation. @@ -406,7 +420,7 @@ public: /// the first entry. template void EmitRecordWithAbbrev(unsigned Abbrev, SmallVectorImpl &Vals) { - EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef()); + EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(), None); } /// EmitRecordWithBlob - Emit the specified record to the stream, using an @@ -417,12 +431,13 @@ public: template void EmitRecordWithBlob(unsigned Abbrev, SmallVectorImpl &Vals, StringRef Blob) { - EmitRecordWithAbbrevImpl(Abbrev, Vals, Blob); + EmitRecordWithAbbrevImpl(Abbrev, Vals, Blob, None); } template void EmitRecordWithBlob(unsigned Abbrev, SmallVectorImpl &Vals, const char *BlobData, unsigned BlobLen) { - return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(BlobData, BlobLen)); + return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(BlobData, BlobLen), + None); } /// EmitRecordWithArray - Just like EmitRecordWithBlob, works with records @@ -430,13 +445,13 @@ public: template void EmitRecordWithArray(unsigned Abbrev, SmallVectorImpl &Vals, StringRef Array) { - EmitRecordWithAbbrevImpl(Abbrev, Vals, Array); + EmitRecordWithAbbrevImpl(Abbrev, Vals, Array, None); } template void EmitRecordWithArray(unsigned Abbrev, SmallVectorImpl &Vals, const char *ArrayData, unsigned ArrayLen) { - return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(ArrayData, - ArrayLen)); + return EmitRecordWithAbbrevImpl(Abbrev, Vals, + StringRef(ArrayData, ArrayLen), None); } //===--------------------------------------------------------------------===// -- 2.34.1