diff options
| author | Nowar Gu <nowar100@gmail.com> | 2011-06-25 14:24:15 +0800 |
|---|---|---|
| committer | Nowar Gu <nowar100@gmail.com> | 2011-06-25 14:33:30 +0800 |
| commit | c7724cf3957f39d927f430969af25eddc2875df4 (patch) | |
| tree | 5cbcc9dbb92af67f738797720067412a3fd92494 | |
| parent | 8012bd3687ad40b9bb18505c51ab5cf88eb00c45 (diff) | |
| download | external_llvm-c7724cf3957f39d927f430969af25eddc2875df4.zip external_llvm-c7724cf3957f39d927f430969af25eddc2875df4.tar.gz external_llvm-c7724cf3957f39d927f430969af25eddc2875df4.tar.bz2 | |
Recover LLVM 2.7 parse bitcode backward-compatible.
Since LLVM 3.0 modify the bitcode format, it will break old bitcode.
I fix the following commit by Chris Lattner.
020a5a449f297ced1f0fed08fb81c5da87fb7c9a
9d61dd9a088be975df4beb4632a39ed009bd0ef5
96a74c57d9e8fe0595ba8308eec1276cf8bcf6b0
4f6bab98c54a93276b8370d3f61f63bf765f7e1f
Now LLVM can support *READ* either old or new version bitcode.
That won't make trouble on Android legacy code.
| -rw-r--r-- | include/llvm/Bitcode/LLVMBitCodes.h | 10 | ||||
| -rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 104 | ||||
| -rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.h | 9 |
3 files changed, 115 insertions, 8 deletions
diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index d3fee54..d202b78 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -112,11 +112,15 @@ namespace bitc { enum MetadataCodes { METADATA_STRING = 1, // MDSTRING: [values] + METADATA_NODE_27 = 2, + METADATA_FN_NODE_27 = 3, // 2 is unused. // 3 is unused. METADATA_NAME = 4, // STRING: [values] + METADATA_NAMED_NODE_27 = 5, // 5 is unused. METADATA_KIND = 6, // [n x [id, name]] + METADATA_ATTACHMENT_27 = 7, // 7 is unused. METADATA_NODE = 8, // NODE: [n x (type num, value num)] METADATA_FN_NODE = 9, // FN_NODE: [n x (type num, value num)] @@ -223,10 +227,14 @@ namespace bitc { FUNC_CODE_INST_UNREACHABLE = 15, // UNREACHABLE FUNC_CODE_INST_PHI = 16, // PHI: [ty, val0,bb0, ...] + FUNC_CODE_INST_MALLOC_27 = 17, + FUNC_CODE_INST_FREE_27 = 18, // 17 is unused. // 18 is unused. FUNC_CODE_INST_ALLOCA = 19, // ALLOCA: [instty, op, align] FUNC_CODE_INST_LOAD = 20, // LOAD: [opty, op, align, vol] + FUNC_CODE_INST_STORE_27 = 21, + FUNC_CODE_INST_CALL_27 = 22, // 21 is unused. // 22 is unused. FUNC_CODE_INST_VAARG = 23, // VAARG: [valistty, valist, instty] @@ -234,6 +242,7 @@ namespace bitc { // this is so information only available in the pointer type (e.g. address // spaces) is retained. FUNC_CODE_INST_STORE = 24, // STORE: [ptrty,ptr,val, align, vol] + FUNC_CODE_INST_GETRESULT_27 = 25, // 25 is unused. FUNC_CODE_INST_EXTRACTVAL = 26, // EXTRACTVAL: [n x operands] FUNC_CODE_INST_INSERTVAL = 27, // INSERTVAL: [n x operands] @@ -244,6 +253,7 @@ namespace bitc { FUNC_CODE_INST_VSELECT = 29, // VSELECT: [ty,opval,opval,predty,pred] FUNC_CODE_INST_INBOUNDS_GEP= 30, // INBOUNDS_GEP: [n x operands] FUNC_CODE_INST_INDIRECTBR = 31, // INDIRECTBR: [opty, op0, op1, ...] + FUNC_CODE_DEBUG_LOC_27 = 32, // 32 is unused. FUNC_CODE_DEBUG_LOC_AGAIN = 33, // DEBUG_LOC_AGAIN diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 963791f..79db299 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -790,7 +790,11 @@ bool BitcodeReader::ParseMetadata() { // METADATA_NAME is always followed by METADATA_NAMED_NODE. unsigned NextBitCode = Stream.ReadRecord(Code, Record); - assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode; + if (NextBitCode == bitc::METADATA_NAMED_NODE_27) { + LLVM2_7MetadataDetected = true; + } else if (NextBitCode != bitc::METADATA_NAMED_NODE) { + assert(!"Invalid Named Metadata record."); (void)NextBitCode; + } // Read named metadata elements. unsigned Size = Record.size(); @@ -801,12 +805,23 @@ bool BitcodeReader::ParseMetadata() { return Error("Malformed metadata record"); NMD->addOperand(MD); } + + if (LLVM2_7MetadataDetected) { + MDValueList.AssignValue(0, NextMDValueNo++); + } break; } + case bitc::METADATA_FN_NODE_27: case bitc::METADATA_FN_NODE: IsFunctionLocal = true; // fall-through + case bitc::METADATA_NODE_27: case bitc::METADATA_NODE: { + if (Code == bitc::METADATA_FN_NODE_27 || + Code == bitc::METADATA_NODE_27) { + LLVM2_7MetadataDetected = true; + } + if (Record.size() % 2 == 1) return Error("Invalid METADATA_NODE record"); @@ -1734,6 +1749,8 @@ bool BitcodeReader::ParseMetadataAttachment() { switch (Stream.ReadRecord(Code, Record)) { default: // Default behavior: ignore. break; + case bitc::METADATA_ATTACHMENT_27: + LLVM2_7MetadataDetected = true; case bitc::METADATA_ATTACHMENT: { unsigned RecordLength = Record.size(); if (Record.empty() || (RecordLength - 1) % 2 == 1) @@ -1846,6 +1863,8 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { I = 0; continue; + case bitc::FUNC_CODE_DEBUG_LOC_27: + LLVM2_7MetadataDetected = true; case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] I = 0; // Get the last instruction emitted. if (CurBB && !CurBB->empty()) @@ -2085,6 +2104,19 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { break; } + case bitc::FUNC_CODE_INST_GETRESULT_27: { + if (Record.size() != 2) { + return Error("Invalid GETRESULT record"); + } + unsigned OpNum = 0; + Value *Op; + getValueTypePair(Record, OpNum, NextValueNo, Op); + unsigned Index = Record[1]; + I = ExtractValueInst::Create(Op, Index); + InstructionList.push_back(I); + break; + } + case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] { unsigned Size = Record.size(); @@ -2248,14 +2280,51 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { break; } + case bitc::FUNC_CODE_INST_MALLOC_27: { // MALLOC: [instty, op, align] + // Autoupgrade malloc instruction to malloc call. + // FIXME: Remove in LLVM 3.0. + if (Record.size() < 3) { + return Error("Invalid MALLOC record"); + } + const PointerType *Ty = + dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); + Value *Size = getFnValueByID(Record[1], Type::getInt32Ty(Context)); + if (!Ty || !Size) return Error("Invalid MALLOC record"); + if (!CurBB) return Error("Invalid malloc instruction with no BB"); + const Type *Int32Ty = IntegerType::getInt32Ty(CurBB->getContext()); + Constant *AllocSize = ConstantExpr::getSizeOf(Ty->getElementType()); + AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, Int32Ty); + I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(), + AllocSize, Size, NULL); + InstructionList.push_back(I); + break; + } + case bitc::FUNC_CODE_INST_FREE_27: { // FREE: [op, opty] + unsigned OpNum = 0; + Value *Op; + if (getValueTypePair(Record, OpNum, NextValueNo, Op) || + OpNum != Record.size()) { + return Error("Invalid FREE record"); + } + if (!CurBB) return Error("Invalid free instruction with no BB"); + I = CallInst::CreateFree(Op, CurBB); + InstructionList.push_back(I); + break; + } + case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] - if (Record.size() != 4) + // For backward compatibility, tolerate a lack of an opty, and use i32. + // Remove this in LLVM 3.0. + if (Record.size() < 3 || Record.size() > 4) { return Error("Invalid ALLOCA record"); + } + unsigned OpNum = 0; const PointerType *Ty = - dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); - const Type *OpTy = getTypeByID(Record[1]); - Value *Size = getFnValueByID(Record[2], OpTy); - unsigned Align = Record[3]; + dyn_cast_or_null<PointerType>(getTypeByID(Record[OpNum++])); + const Type *OpTy = Record.size() == 4 ? getTypeByID(Record[OpNum++]) : + Type::getInt32Ty(Context); + Value *Size = getFnValueByID(Record[OpNum++], OpTy); + unsigned Align = Record[OpNum++]; if (!Ty || !Size) return Error("Invalid ALLOCA record"); I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); InstructionList.push_back(I); @@ -2285,6 +2354,21 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { InstructionList.push_back(I); break; } + case bitc::FUNC_CODE_INST_STORE_27: { + unsigned OpNum = 0; + Value *Val, *Ptr; + if (getValueTypePair(Record, OpNum, NextValueNo, Val) || + getValue(Record, OpNum, + PointerType::getUnqual(Val->getType()), Ptr)|| + OpNum+2 != Record.size()) { + return Error("Invalid STORE record"); + } + I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); + InstructionList.push_back(I); + break; + } + case bitc::FUNC_CODE_INST_CALL_27: + LLVM2_7MetadataDetected = true; case bitc::FUNC_CODE_INST_CALL: { // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] if (Record.size() < 3) @@ -2403,10 +2487,16 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { BlockAddrFwdRefs.erase(BAFRI); } - + + unsigned NewMDValueListSize = MDValueList.size(); // Trim the value list down to the size it was before we parsed this function. ValueList.shrinkTo(ModuleValueListSize); MDValueList.shrinkTo(ModuleMDValueListSize); + + if (LLVM2_7MetadataDetected) { + MDValueList.resize(NewMDValueListSize); + } + std::vector<BasicBlock*>().swap(FunctionBBs); return false; } diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index 9bab00e..a217fe0 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -174,10 +174,17 @@ class BitcodeReader : public GVMaterializer { typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; + /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or + /// earlier was detected, in which case we behave slightly differently, + /// for compatibility. + /// FIXME: Remove in LLVM 3.0. + bool LLVM2_7MetadataDetected; + public: explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false), - ErrorString(0), ValueList(C), MDValueList(C) { + ErrorString(0), ValueList(C), MDValueList(C), + LLVM2_7MetadataDetected(false) { HasReversedFunctionsWithBodies = false; } ~BitcodeReader() { |
