diff options
Diffstat (limited to 'lib/Bitcode')
| -rw-r--r-- | lib/Bitcode/Reader/BitReader.cpp | 1 | ||||
| -rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 136 | ||||
| -rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.h | 11 | ||||
| -rw-r--r-- | lib/Bitcode/Reader/BitstreamReader.cpp | 15 | ||||
| -rw-r--r-- | lib/Bitcode/Writer/BitWriter.cpp | 3 | ||||
| -rw-r--r-- | lib/Bitcode/Writer/BitcodeWriter.cpp | 101 | ||||
| -rw-r--r-- | lib/Bitcode/Writer/ValueEnumerator.cpp | 2 | ||||
| -rw-r--r-- | lib/Bitcode/Writer/ValueEnumerator.h | 2 |
8 files changed, 246 insertions, 25 deletions
diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 5cd6c55..23630e5 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -10,6 +10,7 @@ #include "llvm-c/BitReader.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" #include "llvm/Support/MemoryBuffer.h" #include <cstring> #include <string> diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index f348843..e6d7b50 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/AutoUpgrade.h" +#include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/InlineAsm.h" @@ -22,6 +23,7 @@ #include "llvm/Support/DataStream.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; enum { @@ -405,7 +407,7 @@ Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { } // Create and return a placeholder, which will later be RAUW'd. - Value *V = MDNode::getTemporary(Context, ArrayRef<Value*>()); + Value *V = MDNode::getTemporary(Context, None); MDValuePtrs[Idx] = V; return V; } @@ -506,6 +508,125 @@ bool BitcodeReader::ParseAttributeBlock() { } } +bool BitcodeReader::ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { + switch (Code) { + case bitc::ATTR_KIND_ALIGNMENT: + *Kind = Attribute::Alignment; + return false; + case bitc::ATTR_KIND_ALWAYS_INLINE: + *Kind = Attribute::AlwaysInline; + return false; + case bitc::ATTR_KIND_BUILTIN: + *Kind = Attribute::Builtin; + return false; + case bitc::ATTR_KIND_BY_VAL: + *Kind = Attribute::ByVal; + return false; + case bitc::ATTR_KIND_COLD: + *Kind = Attribute::Cold; + return false; + case bitc::ATTR_KIND_INLINE_HINT: + *Kind = Attribute::InlineHint; + return false; + case bitc::ATTR_KIND_IN_REG: + *Kind = Attribute::InReg; + return false; + case bitc::ATTR_KIND_MIN_SIZE: + *Kind = Attribute::MinSize; + return false; + case bitc::ATTR_KIND_NAKED: + *Kind = Attribute::Naked; + return false; + case bitc::ATTR_KIND_NEST: + *Kind = Attribute::Nest; + return false; + case bitc::ATTR_KIND_NO_ALIAS: + *Kind = Attribute::NoAlias; + return false; + case bitc::ATTR_KIND_NO_BUILTIN: + *Kind = Attribute::NoBuiltin; + return false; + case bitc::ATTR_KIND_NO_CAPTURE: + *Kind = Attribute::NoCapture; + return false; + case bitc::ATTR_KIND_NO_DUPLICATE: + *Kind = Attribute::NoDuplicate; + return false; + case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: + *Kind = Attribute::NoImplicitFloat; + return false; + case bitc::ATTR_KIND_NO_INLINE: + *Kind = Attribute::NoInline; + return false; + case bitc::ATTR_KIND_NON_LAZY_BIND: + *Kind = Attribute::NonLazyBind; + return false; + case bitc::ATTR_KIND_NO_RED_ZONE: + *Kind = Attribute::NoRedZone; + return false; + case bitc::ATTR_KIND_NO_RETURN: + *Kind = Attribute::NoReturn; + return false; + case bitc::ATTR_KIND_NO_UNWIND: + *Kind = Attribute::NoUnwind; + return false; + case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: + *Kind = Attribute::OptimizeForSize; + return false; + case bitc::ATTR_KIND_READ_NONE: + *Kind = Attribute::ReadNone; + return false; + case bitc::ATTR_KIND_READ_ONLY: + *Kind = Attribute::ReadOnly; + return false; + case bitc::ATTR_KIND_RETURNED: + *Kind = Attribute::Returned; + return false; + case bitc::ATTR_KIND_RETURNS_TWICE: + *Kind = Attribute::ReturnsTwice; + return false; + case bitc::ATTR_KIND_S_EXT: + *Kind = Attribute::SExt; + return false; + case bitc::ATTR_KIND_STACK_ALIGNMENT: + *Kind = Attribute::StackAlignment; + return false; + case bitc::ATTR_KIND_STACK_PROTECT: + *Kind = Attribute::StackProtect; + return false; + case bitc::ATTR_KIND_STACK_PROTECT_REQ: + *Kind = Attribute::StackProtectReq; + return false; + case bitc::ATTR_KIND_STACK_PROTECT_STRONG: + *Kind = Attribute::StackProtectStrong; + return false; + case bitc::ATTR_KIND_STRUCT_RET: + *Kind = Attribute::StructRet; + return false; + case bitc::ATTR_KIND_SANITIZE_ADDRESS: + *Kind = Attribute::SanitizeAddress; + return false; + case bitc::ATTR_KIND_SANITIZE_THREAD: + *Kind = Attribute::SanitizeThread; + return false; + case bitc::ATTR_KIND_SANITIZE_MEMORY: + *Kind = Attribute::SanitizeMemory; + return false; + case bitc::ATTR_KIND_UW_TABLE: + *Kind = Attribute::UWTable; + return false; + case bitc::ATTR_KIND_Z_EXT: + *Kind = Attribute::ZExt; + return false; + default: + std::string Buf; + raw_string_ostream fmt(Buf); + fmt << "Unknown attribute kind (" << Code << ")"; + fmt.flush(); + return Error(Buf.c_str()); + } +} + bool BitcodeReader::ParseAttributeGroupBlock() { if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) return Error("Malformed block record"); @@ -545,9 +666,16 @@ bool BitcodeReader::ParseAttributeGroupBlock() { AttrBuilder B; for (unsigned i = 2, e = Record.size(); i != e; ++i) { if (Record[i] == 0) { // Enum attribute - B.addAttribute(Attribute::AttrKind(Record[++i])); + Attribute::AttrKind Kind; + if (ParseAttrKind(Record[++i], &Kind)) + return true; + + B.addAttribute(Kind); } else if (Record[i] == 1) { // Align attribute - if (Attribute::AttrKind(Record[++i]) == Attribute::Alignment) + Attribute::AttrKind Kind; + if (ParseAttrKind(Record[++i], &Kind)) + return true; + if (Kind == Attribute::Alignment) B.addAlignmentAttr(Record[++i]); else B.addStackAlignmentAttr(Record[++i]); @@ -3010,7 +3138,7 @@ bool BitcodeReader::InitLazyStream() { Stream.init(*StreamFile); unsigned char buf[16]; - if (Bytes->readBytes(0, 16, buf, NULL) == -1) + if (Bytes->readBytes(0, 16, buf) == -1) return Error("Bitcode stream must be at least 16 bytes in length"); if (!isBitcode(buf, buf + 16)) diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index 28674eb..b095447 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -258,7 +258,7 @@ private: /// getValueTypePair - Read a value/type pair out of the specified record from /// slot 'Slot'. Increment Slot past the number of slots used in the record. /// Return true on failure. - bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot, + bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, unsigned InstNum, Value *&ResVal) { if (Slot == Record.size()) return true; unsigned ValNo = (unsigned)Record[Slot++]; @@ -282,7 +282,7 @@ private: /// popValue - Read a value out of the specified record from slot 'Slot'. /// Increment Slot past the number of slots used by the value in the record. /// Return true if there is an error. - bool popValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, + bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, unsigned InstNum, Type *Ty, Value *&ResVal) { if (getValue(Record, Slot, InstNum, Ty, ResVal)) return true; @@ -292,7 +292,7 @@ private: } /// getValue -- Like popValue, but does not increment the Slot number. - bool getValue(SmallVector<uint64_t, 64> &Record, unsigned Slot, + bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, unsigned InstNum, Type *Ty, Value *&ResVal) { ResVal = getValue(Record, Slot, InstNum, Ty); return ResVal == 0; @@ -300,7 +300,7 @@ private: /// getValue -- Version of getValue that returns ResVal directly, /// or 0 if there is an error. - Value *getValue(SmallVector<uint64_t, 64> &Record, unsigned Slot, + Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, unsigned InstNum, Type *Ty) { if (Slot == Record.size()) return 0; unsigned ValNo = (unsigned)Record[Slot]; @@ -311,7 +311,7 @@ private: } /// getValueSigned -- Like getValue, but decodes signed VBRs. - Value *getValueSigned(SmallVector<uint64_t, 64> &Record, unsigned Slot, + Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, unsigned InstNum, Type *Ty) { if (Slot == Record.size()) return 0; unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); @@ -321,6 +321,7 @@ private: return getFnValueByID(ValNo, Ty); } + bool ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); bool ParseModule(bool Resume); bool ParseAttributeBlock(); bool ParseAttributeGroupBlock(); diff --git a/lib/Bitcode/Reader/BitstreamReader.cpp b/lib/Bitcode/Reader/BitstreamReader.cpp index 942346b..1fd9abd 100644 --- a/lib/Bitcode/Reader/BitstreamReader.cpp +++ b/lib/Bitcode/Reader/BitstreamReader.cpp @@ -204,7 +204,16 @@ unsigned BitstreamCursor::readRecord(unsigned AbbrevID, const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); - for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) { + // Read the record code first. + 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); @@ -264,8 +273,6 @@ unsigned BitstreamCursor::readRecord(unsigned AbbrevID, JumpToBit(NewEnd); } - unsigned Code = (unsigned)Vals[0]; - Vals.erase(Vals.begin()); return Code; } @@ -292,7 +299,7 @@ void BitstreamCursor::ReadAbbrevRecord() { Abbv->Add(BitCodeAbbrevOp(0)); continue; } - + Abbv->Add(BitCodeAbbrevOp(E, Data)); } else Abbv->Add(BitCodeAbbrevOp(E)); diff --git a/lib/Bitcode/Writer/BitWriter.cpp b/lib/Bitcode/Writer/BitWriter.cpp index 9f51c35..cd1ada2 100644 --- a/lib/Bitcode/Writer/BitWriter.cpp +++ b/lib/Bitcode/Writer/BitWriter.cpp @@ -9,6 +9,7 @@ #include "llvm-c/BitWriter.h" #include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/Module.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -17,7 +18,7 @@ using namespace llvm; int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) { std::string ErrorInfo; - raw_fd_ostream OS(Path, ErrorInfo, raw_fd_ostream::F_Binary); + raw_fd_ostream OS(Path, ErrorInfo, sys::fs::F_Binary); if (!ErrorInfo.empty()) return -1; diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 1b73f23..311c233 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -161,6 +161,89 @@ static void WriteStringRecord(unsigned Code, StringRef Str, Stream.EmitRecord(Code, Vals, AbbrevToUse); } +static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) { + switch (Kind) { + case Attribute::Alignment: + return bitc::ATTR_KIND_ALIGNMENT; + case Attribute::AlwaysInline: + return bitc::ATTR_KIND_ALWAYS_INLINE; + case Attribute::Builtin: + return bitc::ATTR_KIND_BUILTIN; + case Attribute::ByVal: + return bitc::ATTR_KIND_BY_VAL; + case Attribute::Cold: + return bitc::ATTR_KIND_COLD; + case Attribute::InlineHint: + return bitc::ATTR_KIND_INLINE_HINT; + case Attribute::InReg: + return bitc::ATTR_KIND_IN_REG; + case Attribute::MinSize: + return bitc::ATTR_KIND_MIN_SIZE; + case Attribute::Naked: + return bitc::ATTR_KIND_NAKED; + case Attribute::Nest: + return bitc::ATTR_KIND_NEST; + case Attribute::NoAlias: + return bitc::ATTR_KIND_NO_ALIAS; + case Attribute::NoBuiltin: + return bitc::ATTR_KIND_NO_BUILTIN; + case Attribute::NoCapture: + return bitc::ATTR_KIND_NO_CAPTURE; + case Attribute::NoDuplicate: + return bitc::ATTR_KIND_NO_DUPLICATE; + case Attribute::NoImplicitFloat: + return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT; + case Attribute::NoInline: + return bitc::ATTR_KIND_NO_INLINE; + case Attribute::NonLazyBind: + return bitc::ATTR_KIND_NON_LAZY_BIND; + case Attribute::NoRedZone: + return bitc::ATTR_KIND_NO_RED_ZONE; + case Attribute::NoReturn: + return bitc::ATTR_KIND_NO_RETURN; + case Attribute::NoUnwind: + return bitc::ATTR_KIND_NO_UNWIND; + case Attribute::OptimizeForSize: + return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE; + case Attribute::ReadNone: + return bitc::ATTR_KIND_READ_NONE; + case Attribute::ReadOnly: + return bitc::ATTR_KIND_READ_ONLY; + case Attribute::Returned: + return bitc::ATTR_KIND_RETURNED; + case Attribute::ReturnsTwice: + return bitc::ATTR_KIND_RETURNS_TWICE; + case Attribute::SExt: + return bitc::ATTR_KIND_S_EXT; + case Attribute::StackAlignment: + return bitc::ATTR_KIND_STACK_ALIGNMENT; + case Attribute::StackProtect: + return bitc::ATTR_KIND_STACK_PROTECT; + case Attribute::StackProtectReq: + return bitc::ATTR_KIND_STACK_PROTECT_REQ; + case Attribute::StackProtectStrong: + return bitc::ATTR_KIND_STACK_PROTECT_STRONG; + case Attribute::StructRet: + return bitc::ATTR_KIND_STRUCT_RET; + case Attribute::SanitizeAddress: + return bitc::ATTR_KIND_SANITIZE_ADDRESS; + case Attribute::SanitizeThread: + return bitc::ATTR_KIND_SANITIZE_THREAD; + case Attribute::SanitizeMemory: + return bitc::ATTR_KIND_SANITIZE_MEMORY; + case Attribute::UWTable: + return bitc::ATTR_KIND_UW_TABLE; + case Attribute::ZExt: + return bitc::ATTR_KIND_Z_EXT; + case Attribute::EndAttrKinds: + llvm_unreachable("Can not encode end-attribute kinds marker."); + case Attribute::None: + llvm_unreachable("Can not encode none-attribute."); + } + + llvm_unreachable("Trying to encode unknown attribute"); +} + static void WriteAttributeGroupTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); @@ -182,10 +265,10 @@ static void WriteAttributeGroupTable(const ValueEnumerator &VE, Attribute Attr = *I; if (Attr.isEnumAttribute()) { Record.push_back(0); - Record.push_back(Attr.getKindAsEnum()); + Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); } else if (Attr.isAlignAttribute()) { Record.push_back(1); - Record.push_back(Attr.getKindAsEnum()); + Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); Record.push_back(Attr.getValueAsInt()); } else { StringRef Kind = Attr.getKindAsString(); @@ -614,7 +697,7 @@ static uint64_t GetOptimizationFlags(const Value *V) { static void WriteMDNode(const MDNode *N, const ValueEnumerator &VE, BitstreamWriter &Stream, - SmallVector<uint64_t, 64> &Record) { + SmallVectorImpl<uint64_t> &Record) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { if (N->getOperand(i)) { Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); @@ -701,7 +784,7 @@ static void WriteFunctionLocalMetadata(const Function &F, BitstreamWriter &Stream) { bool StartedMetadataBlock = false; SmallVector<uint64_t, 64> Record; - const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); + const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues(); for (unsigned i = 0, e = Vals.size(); i != e; ++i) if (const MDNode *N = Vals[i]) if (N->isFunctionLocal() && N->getFunction() == &F) { @@ -1078,7 +1161,7 @@ static void WriteModuleConstants(const ValueEnumerator &VE, /// instruction ID, then it is a forward reference, and it also includes the /// type ID. The value ID that is written is encoded relative to the InstID. static bool PushValueAndType(const Value *V, unsigned InstID, - SmallVector<unsigned, 64> &Vals, + SmallVectorImpl<unsigned> &Vals, ValueEnumerator &VE) { unsigned ValID = VE.getValueID(V); // Make encoding relative to the InstID. @@ -1093,21 +1176,21 @@ static bool PushValueAndType(const Value *V, unsigned InstID, /// pushValue - Like PushValueAndType, but where the type of the value is /// omitted (perhaps it was already encoded in an earlier operand). static void pushValue(const Value *V, unsigned InstID, - SmallVector<unsigned, 64> &Vals, + SmallVectorImpl<unsigned> &Vals, ValueEnumerator &VE) { unsigned ValID = VE.getValueID(V); Vals.push_back(InstID - ValID); } static void pushValue64(const Value *V, unsigned InstID, - SmallVector<uint64_t, 128> &Vals, + SmallVectorImpl<uint64_t> &Vals, ValueEnumerator &VE) { uint64_t ValID = VE.getValueID(V); Vals.push_back(InstID - ValID); } static void pushValueSigned(const Value *V, unsigned InstID, - SmallVector<uint64_t, 128> &Vals, + SmallVectorImpl<uint64_t> &Vals, ValueEnumerator &VE) { unsigned ValID = VE.getValueID(V); int64_t diff = ((int32_t)InstID - (int32_t)ValID); @@ -1117,7 +1200,7 @@ static void pushValueSigned(const Value *V, unsigned InstID, /// WriteInstruction - Emit an instruction to the specified stream. static void WriteInstruction(const Instruction &I, unsigned InstID, ValueEnumerator &VE, BitstreamWriter &Stream, - SmallVector<unsigned, 64> &Vals) { + SmallVectorImpl<unsigned> &Vals) { unsigned Code = 0; unsigned AbbrevToUse = 0; VE.setInstructionID(&I); diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index 4f19dd0..8bac6da 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -60,7 +60,7 @@ ValueEnumerator::ValueEnumerator(const Module *M) { I != E; ++I) EnumerateValue(I->getAliasee()); - // Insert constants and metadata that are named at module level into the slot + // Insert constants and metadata that are named at module level into the slot // pool so that the module symbol table can refer to them... EnumerateValueSymbolTable(M->getValueSymbolTable()); EnumerateNamedMetadata(M); diff --git a/lib/Bitcode/Writer/ValueEnumerator.h b/lib/Bitcode/Writer/ValueEnumerator.h index 0af6164..d1ca15f 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.h +++ b/lib/Bitcode/Writer/ValueEnumerator.h @@ -125,7 +125,7 @@ public: const ValueList &getValues() const { return Values; } const ValueList &getMDValues() const { return MDValues; } - const SmallVector<const MDNode *, 8> &getFunctionLocalMDValues() const { + const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const { return FunctionLocalMDs; } const TypeList &getTypes() const { return Types; } |
