diff --git a/lib/Bytecode/Reader/InstructionReader.cpp b/lib/Bytecode/Reader/InstructionReader.cpp index f3340857791..d8a6af0179c 100644 --- a/lib/Bytecode/Reader/InstructionReader.cpp +++ b/lib/Bytecode/Reader/InstructionReader.cpp @@ -161,12 +161,12 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, delete PN; return true; case 2: PN->addIncoming(getValue(Raw->Ty, Raw->Arg1), - cast(getValue(Type::LabelTy, + cast(getValue(Type::LabelTyID, Raw->Arg2))); break; default: PN->addIncoming(getValue(Raw->Ty, Raw->Arg1), - cast(getValue(Type::LabelTy, Raw->Arg2))); + cast(getValue(Type::LabelTyID, Raw->Arg2))); if (Raw->VarArgs->size() & 1) { std::cerr << "PHI Node with ODD number of arguments!\n"; delete PN; @@ -175,7 +175,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, std::vector &args = *Raw->VarArgs; for (unsigned i = 0; i < args.size(); i+=2) PN->addIncoming(getValue(Raw->Ty, args[i]), - cast(getValue(Type::LabelTy, args[i+1]))); + cast(getValue(Type::LabelTyID, args[i+1]))); } delete Raw->VarArgs; break; @@ -188,7 +188,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, case Instruction::Shr: Res = new ShiftInst((Instruction::OtherOps)Raw->Opcode, getValue(Raw->Ty, Raw->Arg1), - getValue(Type::UByteTy, Raw->Arg2)); + getValue(Type::UByteTyID, Raw->Arg2)); return false; case Instruction::Ret: if (Raw->NumOperands == 0) { @@ -200,12 +200,12 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, case Instruction::Br: if (Raw->NumOperands == 1) { - Res = new BranchInst(cast(getValue(Type::LabelTy,Raw->Arg1))); + Res = new BranchInst(cast(getValue(Type::LabelTyID,Raw->Arg1))); return false; } else if (Raw->NumOperands == 3) { - Res = new BranchInst(cast(getValue(Type::LabelTy, Raw->Arg1)), - cast(getValue(Type::LabelTy, Raw->Arg2)), - getValue(Type::BoolTy , Raw->Arg3)); + Res = new BranchInst(cast(getValue(Type::LabelTyID, Raw->Arg1)), + cast(getValue(Type::LabelTyID, Raw->Arg2)), + getValue(Type::BoolTyID , Raw->Arg3)); return false; } break; @@ -213,7 +213,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, case Instruction::Switch: { SwitchInst *I = new SwitchInst(getValue(Raw->Ty, Raw->Arg1), - cast(getValue(Type::LabelTy, Raw->Arg2))); + cast(getValue(Type::LabelTyID, Raw->Arg2))); Res = I; if (Raw->NumOperands < 3) return false; // No destinations? Weird. @@ -226,7 +226,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, std::vector &args = *Raw->VarArgs; for (unsigned i = 0; i < args.size(); i += 2) I->addCase(cast(getValue(Raw->Ty, args[i])), - cast(getValue(Type::LabelTy, args[i+1]))); + cast(getValue(Type::LabelTyID, args[i+1]))); delete Raw->VarArgs; return false; @@ -311,11 +311,11 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, if (!FTy->isVarArg()) { if (Raw->NumOperands < 3) return true; - Normal = cast(getValue(Type::LabelTy, Raw->Arg2)); + Normal = cast(getValue(Type::LabelTyID, Raw->Arg2)); if (Raw->NumOperands == 3) - Except = cast(getValue(Type::LabelTy, Raw->Arg3)); + Except = cast(getValue(Type::LabelTyID, Raw->Arg3)); else { - Except = cast(getValue(Type::LabelTy, args[0])); + Except = cast(getValue(Type::LabelTyID, args[0])); FunctionType::ParamTypes::const_iterator It = PL.begin(); for (unsigned i = 1; i < args.size(); i++) { @@ -329,13 +329,13 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, if (args.size() < 4) return true; if (getType(args[0]) != Type::LabelTy || getType(args[2]) != Type::LabelTy) return true; - Normal = cast(getValue(Type::LabelTy, args[1])); - Except = cast(getValue(Type::LabelTy, args[3])); + Normal = cast(getValue(Type::LabelTyID, args[1])); + Except = cast(getValue(Type::LabelTyID, args[3])); if ((args.size() & 1) != 0) return true; // Must be pairs of type/value for (unsigned i = 4; i < args.size(); i+=2) { - Params.push_back(getValue(getType(args[i]), args[i+1])); + Params.push_back(getValue(args[i], args[i+1])); if (Params.back() == 0) return true; } } @@ -347,7 +347,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, } case Instruction::Malloc: if (Raw->NumOperands > 2) return true; - V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0; + V = Raw->NumOperands ? getValue(Type::UIntTyID, Raw->Arg1) : 0; if (const PointerType *PTy = dyn_cast(Raw->Ty)) Res = new MallocInst(PTy->getElementType(), V); else @@ -356,7 +356,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf, case Instruction::Alloca: if (Raw->NumOperands > 2) return true; - V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0; + V = Raw->NumOperands ? getValue(Type::UIntTyID, Raw->Arg1) : 0; if (const PointerType *PTy = dyn_cast(Raw->Ty)) Res = new AllocaInst(PTy->getElementType(), V); else diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp index 727e2a91100..c3f7eb64349 100644 --- a/lib/Bytecode/Reader/Reader.cpp +++ b/lib/Bytecode/Reader/Reader.cpp @@ -55,8 +55,25 @@ const Type *BytecodeParser::getType(unsigned ID) { } //cerr << "Looking up Type ID: " << ID << "\n"; - const Value *V = getValue(Type::TypeTy, ID, false); - return cast_or_null(V); + + if (ID < Type::NumPrimitiveIDs) { + const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID); + if (T) return T; // Asked for a primitive type... + } + + // Otherwise, derived types need offset... + ID -= FirstDerivedTyID; + + // Is it a module-level type? + if (ID < ModuleTypeValues.size()) + return ModuleTypeValues[ID].get(); + + // Nope, is it a function-level type? + ID -= ModuleTypeValues.size(); + if (ID < FunctionTypeValues.size()) + return FunctionTypeValues[ID].get(); + + return 0; } int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) { @@ -95,35 +112,18 @@ void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot, ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val); } + Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { + return getValue(getTypeSlot(Ty), oNum, Create); +} + +Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) { + assert(type != Type::TypeTyID && "getValue() cannot get types!"); unsigned Num = oNum; - unsigned type = getTypeSlot(Ty); // The type plane it lives in... - - if (type == Type::TypeTyID) { // The 'type' plane has implicit values - assert(Create == false); - if (Num < Type::NumPrimitiveIDs) { - const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num); - if (T) return (Value*)T; // Asked for a primitive type... - } - - // Otherwise, derived types need offset... - Num -= FirstDerivedTyID; - - // Is it a module-level type? - if (Num < ModuleTypeValues.size()) - return (Value*)ModuleTypeValues[Num].get(); - - // Nope, is it a function-level type? - Num -= ModuleTypeValues.size(); - if (Num < FunctionTypeValues.size()) - return (Value*)FunctionTypeValues[Num].get(); - - return 0; - } if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) { if (Num == 0) - return Constant::getNullValue(Ty); + return Constant::getNullValue(getType(type)); --Num; } @@ -138,19 +138,13 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { if (!Create) return 0; // Do not create a placeholder? - Value *d = 0; - switch (Ty->getPrimitiveID()) { - case Type::LabelTyID: - d = new BBPHolder(Ty, oNum); - break; - default: - d = new ValPHolder(Ty, oNum); - break; - } + const Type *Ty = getType(type); + Value *Val = type == Type::LabelTyID ? (Value*)new BBPHolder(Ty, oNum) : + (Value*)new ValPHolder(Ty, oNum); - assert(d != 0 && "How did we not make something?"); - if (insertValue(d, LateResolveValues) == -1) return 0; - return d; + assert(Val != 0 && "How did we not make something?"); + if (insertValue(Val, LateResolveValues) == -1) return 0; + return Val; } /// getConstantValue - Just like getValue, except that it returns a null pointer @@ -252,11 +246,12 @@ void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf, if (read(Buf, EndBuf, Name, false)) // Not aligned... throw std::string("Buffer not aligned."); - Value *V = getValue(Ty, slot, false); // Find mapping... - if (V == 0) { - BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n"); - throw std::string("Failed value look-up."); - } + Value *V; + if (Typ == Type::TypeTyID) + V = (Value*)getType(slot); + else + V = getValue(Typ, slot, false); // Find mapping... + if (V == 0) throw std::string("Failed value look-up."); BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V; if (!isa(V)) std::cerr << "\n"); diff --git a/lib/Bytecode/Reader/ReaderInternals.h b/lib/Bytecode/Reader/ReaderInternals.h index f08cedf8cfb..c022519e58e 100644 --- a/lib/Bytecode/Reader/ReaderInternals.h +++ b/lib/Bytecode/Reader/ReaderInternals.h @@ -179,6 +179,7 @@ private: const unsigned char *EndBuf); Value *getValue(const Type *Ty, unsigned num, bool Create = true); + Value *getValue(unsigned TypeID, unsigned num, bool Create = true); const Type *getType(unsigned ID); Constant *getConstantValue(const Type *Ty, unsigned num);