const Type *Val = 0;
unsigned PrimType;
- if (read_vbr(Buf, EndBuf, PrimType)) return true;
+ if (read_vbr(Buf, EndBuf, PrimType)) return failure(true);
if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType))) {
V = new ConstPoolType(Val); // It's just a primitive ID.
switch (PrimType) {
case Type::MethodTyID: {
unsigned Typ;
- if (read_vbr(Buf, EndBuf, Typ)) return true;
+ if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *RetType = getType(Typ);
- if (RetType == 0) return true;
+ if (RetType == 0) return failure(true);
unsigned NumParams;
- if (read_vbr(Buf, EndBuf, NumParams)) return true;
+ if (read_vbr(Buf, EndBuf, NumParams)) return failure(true);
MethodType::ParamTypes Params;
while (NumParams--) {
- if (read_vbr(Buf, EndBuf, Typ)) return true;
+ if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
- if (Ty == 0) return true;
+ if (Ty == 0) return failure(true);
Params.push_back(Ty);
}
}
case Type::ArrayTyID: {
unsigned ElTyp;
- if (read_vbr(Buf, EndBuf, ElTyp)) return true;
+ if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
const Type *ElementType = getType(ElTyp);
- if (ElementType == 0) return true;
+ if (ElementType == 0) return failure(true);
int NumElements;
- if (read_vbr(Buf, EndBuf, NumElements)) return true;
+ if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
Val = ArrayType::getArrayType(ElementType, NumElements);
break;
}
unsigned Typ;
StructType::ElementTypes Elements;
- if (read_vbr(Buf, EndBuf, Typ)) return true;
+ if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
while (Typ) { // List is terminated by void/0 typeid
const Type *Ty = getType(Typ);
- if (Ty == 0) return true;
+ if (Ty == 0) return failure(true);
Elements.push_back(Ty);
- if (read_vbr(Buf, EndBuf, Typ)) return true;
+ if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
}
Val = StructType::getStructType(Elements);
}
case Type::PointerTyID: {
unsigned ElTyp;
- if (read_vbr(Buf, EndBuf, ElTyp)) return true;
+ if (read_vbr(Buf, EndBuf, ElTyp)) return failure(true);
const Type *ElementType = getType(ElTyp);
- if (ElementType == 0) return true;
+ if (ElementType == 0) return failure(true);
Val = PointerType::getPointerType(ElementType);
break;
}
default:
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize"
<< " primitive Type " << PrimType << "\n";
- return true;
+ return failure(true);
}
V = new ConstPoolType(Val);
switch (Ty->getPrimitiveID()) {
case Type::BoolTyID: {
unsigned Val;
- if (read_vbr(Buf, EndBuf, Val)) return true;
- if (Val != 0 && Val != 1) return true;
+ if (read_vbr(Buf, EndBuf, Val)) return failure(true);
+ if (Val != 0 && Val != 1) return failure(true);
V = new ConstPoolBool(Val == 1);
break;
}
case Type::UShortTyID:
case Type::UIntTyID: {
unsigned Val;
- if (read_vbr(Buf, EndBuf, Val)) return true;
- if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return true;
+ if (read_vbr(Buf, EndBuf, Val)) return failure(true);
+ if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return failure(true);
V = new ConstPoolUInt(Ty, Val);
break;
}
case Type::ULongTyID: {
uint64_t Val;
- if (read_vbr(Buf, EndBuf, Val)) return true;
+ if (read_vbr(Buf, EndBuf, Val)) return failure(true);
V = new ConstPoolUInt(Ty, Val);
break;
}
case Type::ShortTyID:
case Type::IntTyID: {
int Val;
- if (read_vbr(Buf, EndBuf, Val)) return true;
- if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return 0;
+ if (read_vbr(Buf, EndBuf, Val)) return failure(true);
+ if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return failure(true);
V = new ConstPoolSInt(Ty, Val);
break;
}
case Type::LongTyID: {
int64_t Val;
- if (read_vbr(Buf, EndBuf, Val)) return true;
+ if (read_vbr(Buf, EndBuf, Val)) return failure(true);
V = new ConstPoolSInt(Ty, Val);
break;
}
case Type::FloatTyID: {
float F;
- if (input_data(Buf, EndBuf, &F, &F+1)) return true;
+ if (input_data(Buf, EndBuf, &F, &F+1)) return failure(true);
V = new ConstPoolFP(Ty, F);
break;
}
case Type::DoubleTyID: {
double Val;
- if (input_data(Buf, EndBuf, &Val, &Val+1)) return true;
+ if (input_data(Buf, EndBuf, &Val, &Val+1)) return failure(true);
V = new ConstPoolFP(Ty, Val);
break;
}
case Type::TypeTyID:
- if (parseTypeConstant(Buf, EndBuf, V)) return true;
+ if (parseTypeConstant(Buf, EndBuf, V)) return failure(true);
break;
case Type::ArrayTyID: {
if (AT->isSized()) // Sized array, # elements stored in type!
NumElements = (unsigned)AT->getNumElements();
else // Unsized array, # elements stored in stream!
- if (read_vbr(Buf, EndBuf, NumElements)) return true;
+ if (read_vbr(Buf, EndBuf, NumElements)) return failure(true);
vector<ConstPoolVal *> Elements;
while (NumElements--) { // Read all of the elements of the constant.
unsigned Slot;
- if (read_vbr(Buf, EndBuf, Slot)) return true;
+ if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Value *V = getValue(AT->getElementType(), Slot, false);
- if (!V || !V->isConstant())
- return true;
+ if (!V || !V->isConstant()) return failure(true);
Elements.push_back((ConstPoolVal*)V);
}
V = new ConstPoolArray(AT, Elements);
vector<ConstPoolVal *> Elements;
for (unsigned i = 0; i < ET.size(); ++i) {
unsigned Slot;
- if (read_vbr(Buf, EndBuf, Slot)) return true;
+ if (read_vbr(Buf, EndBuf, Slot)) return failure(true);
Value *V = getValue(ET[i], Slot, false);
if (!V || !V->isConstant())
- return true;
+ return failure(true);
Elements.push_back((ConstPoolVal*)V);
}
cerr << __FILE__ << ":" << __LINE__
<< ": Don't know how to deserialize constant value of type '"
<< Ty->getName() << "'\n";
- return true;
+ return failure(true);
}
return false;
}
unsigned NumEntries, Typ;
if (read_vbr(Buf, EndBuf, NumEntries) ||
- read_vbr(Buf, EndBuf, Typ)) return true;
+ read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
- if (Ty == 0) return true;
+ if (Ty == 0) return failure(true);
for (unsigned i = 0; i < NumEntries; i++) {
ConstPoolVal *I;
- if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return true;
+ if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return failure(true);
#if 0
cerr << " Read const value: <" << I->getType()->getName()
<< ">: " << I->getStrValue() << endl;
}
}
- return Buf > EndBuf;
+ if (Buf > EndBuf) return failure(true);
+ return false;
}
bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
RawInst &Result) {
unsigned Op, Typ;
- if (read(Buf, EndBuf, Op)) return true;
+ if (read(Buf, EndBuf, Op)) return failure(true);
Result.NumOperands = Op >> 30;
Result.Opcode = (Op >> 24) & 63;
break;
case 0:
Buf -= 4; // Hrm, try this again...
- if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
- if (read_vbr(Buf, EndBuf, Typ)) return true;
+ if (read_vbr(Buf, EndBuf, Result.Opcode)) return failure(true);
+ if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
Result.Ty = getType(Typ);
- if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
+ if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
switch (Result.NumOperands) {
case 0:
cerr << "Zero Arg instr found!\n";
- return true; // This encoding is invalid!
+ return failure(true); // This encoding is invalid!
case 1:
- if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
+ if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
break;
case 2:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
- read_vbr(Buf, EndBuf, Result.Arg2)) return true;
+ read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
break;
case 3:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2) ||
- read_vbr(Buf, EndBuf, Result.Arg3)) return true;
+ read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
break;
default:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
- read_vbr(Buf, EndBuf, Result.Arg2)) return true;
+ read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
for (unsigned a = 0; a < Result.NumOperands-2; a++)
- if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
+ if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
break;
}
- if (align32(Buf, EndBuf)) return true;
+ if (align32(Buf, EndBuf)) return failure(true);
break;
}
bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
Instruction *&Res) {
RawInst Raw;
- if (ParseRawInst(Buf, EndBuf, Raw)) return true;;
+ if (ParseRawInst(Buf, EndBuf, Raw)) return failure(true);
if (Raw.Opcode >= Instruction::FirstUnaryOp &&
Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
case 1:
case 3: cerr << "Invalid phi node encountered!\n";
delete PN;
- return true;
+ return failure(true);
case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
(BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
break;
if (Raw.VarArgs->size() & 1) {
cerr << "PHI Node with ODD number of arguments!\n";
delete PN;
- return true;
+ return failure(true);
} else {
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i+=2)
if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
cerr << "Switch statement with odd number of arguments!\n";
delete I;
- return true;
+ return failure(true);
}
vector<unsigned> &args = *Raw.VarArgs;
case Instruction::Call: {
Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
- if (M == 0) return true;
+ if (M == 0) return failure(true);
vector<Value *> Params;
const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes();
switch (Raw.NumOperands) {
case 0: cerr << "Invalid call instruction encountered!\n";
- return true;
+ return failure(true);
case 1: break;
case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
case 3: Params.push_back(getValue(*It++, Raw.Arg2));
- if (It == PL.end()) return true;
+ if (It == PL.end()) return failure(true);
Params.push_back(getValue(*It++, Raw.Arg3)); break;
default:
Params.push_back(getValue(*It++, Raw.Arg2));
{
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i++) {
- if (It == PL.end()) return true;
+ if (It == PL.end()) return failure(true);
// TODO: Check getValue for null!
Params.push_back(getValue(*It++, args[i]));
}
}
delete Raw.VarArgs;
}
- if (It != PL.end()) return true;
+ if (It != PL.end()) return failure(true);
} else {
// The first parameter does not have a type specifier... because there
// must be at least one concrete argument to a vararg type...
Params.push_back(getValue(PL.front(), Raw.Arg2));
vector<unsigned> &args = *Raw.VarArgs;
- if ((args.size() & 1) != 0) return true; // Must be pairs of type/value
+ if ((args.size() & 1) != 0)
+ return failure(true); // Must be pairs of type/value
for (unsigned i = 0; i < args.size(); i+=2) {
- Value *Ty = getValue(Type::TypeTy, args[i]);
- if (!Ty) return true;
// TODO: Check getValue for null!
- Params.push_back(getValue(Ty->castTypeAsserting(), args[i+1]));
+ Params.push_back(getValue(getType(args[i]), args[i+1]));
}
delete Raw.VarArgs;
}
return false;
}
case Instruction::Malloc:
- if (Raw.NumOperands > 2) return true;
+ if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new MallocInst(Raw.Ty, V);
return false;
case Instruction::Alloca:
- if (Raw.NumOperands > 2) return true;
+ if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new AllocaInst(Raw.Ty, V);
return false;
case Instruction::Free:
V = getValue(Raw.Ty, Raw.Arg1);
- if (!V->getType()->isPointerType()) return true;
+ if (!V->getType()->isPointerType()) return failure(true);
Res = new FreeInst(V);
return false;
case Instruction::GetElementPtr: {
vector<ConstPoolVal*> Idx;
switch (Raw.NumOperands) {
- case 0: cerr << "Invalid load encountered!\n"; return true;
+ case 0: cerr << "Invalid load encountered!\n"; return failure(true);
case 1: break;
case 2: V = getValue(Type::UByteTy, Raw.Arg2);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
case 3: V = getValue(Type::UByteTy, Raw.Arg2);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
V = getValue(Type::UByteTy, Raw.Arg3);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
default:
V = getValue(Type::UByteTy, Raw.Arg2);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
V = getValue(Type::UByteTy, args[i]);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
}
delete Raw.VarArgs;
vector<ConstPoolVal*> Idx;
switch (Raw.NumOperands) {
case 0:
- case 1: cerr << "Invalid store encountered!\n"; return true;
+ case 1: cerr << "Invalid store encountered!\n"; return failure(true);
case 2: break;
case 3: V = getValue(Type::UByteTy, Raw.Arg3);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
break;
default:
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
V = getValue(Type::UByteTy, args[i]);
- if (!V->isConstant()) return true;
+ if (!V->isConstant()) return failure(true);
Idx.push_back(V->castConstant());
}
delete Raw.VarArgs;
}
const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
- if (ElType == 0) return true;
+ if (ElType == 0) return failure(true);
Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
Idx);
return false;
cerr << "Unrecognized instruction! " << Raw.Opcode
<< " ADDR = 0x" << (void*)Buf << endl;
- return true;
+ return failure(true);
}
Slot = Ty->getPrimitiveID();
} else {
TypeMapType::iterator I = TypeMap.find(Ty);
- if (I == TypeMap.end()) return true; // Didn't find type!
+ if (I == TypeMap.end()) return failure(true); // Didn't find type!
Slot = I->second;
}
//cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
//cerr << "Looking up Type ID: " << ID << endl;
const Value *D = getValue(Type::TypeTy, ID, false);
- if (D == 0) return 0;
+ if (D == 0) return failure<const Type*>(0);
assert(D->getType() == Type::TypeTy);
return ((const ConstPoolType*)D->castConstantAsserting())->getValue();
bool BytecodeParser::insertValue(Value *Def, vector<ValueList> &ValueTab) {
unsigned type;
- if (getTypeSlot(Def->getType(), type)) return true;
+ if (getTypeSlot(Def->getType(), type)) return failure(true);
if (ValueTab.size() <= type)
ValueTab.resize(type+1, ValueList());
unsigned Num = oNum;
unsigned type; // The type plane it lives in...
- if (getTypeSlot(Ty, type)) return 0; // TODO: true
+ if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
if (type == Type::TypeTyID) { // The 'type' plane has implicit values
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
if (Values.size() > type && Values[type].size() > Num)
return Values[type][Num];
- if (!Create) return 0; // Do not create a placeholder?
+ if (!Create) return failure<Value*>(0); // Do not create a placeholder?
Value *d = 0;
switch (Ty->getPrimitiveID()) {
}
assert(d != 0 && "How did we not make something?");
- if (insertValue(d, LateResolveValues)) return 0;
+ if (insertValue(d, LateResolveValues)) return failure<Value*>(0);
return d;
}
Instruction *Def;
if (ParseInstruction(Buf, EndBuf, Def)) {
delete BB;
- return true;
+ return failure(true);
}
- if (Def == 0) { delete BB; return true; }
- if (insertValue(Def, Values)) { delete BB; return true; }
+ if (Def == 0) { delete BB; return failure(true); }
+ if (insertValue(Def, Values)) { delete BB; return failure(true); }
BB->getInstList().push_back(Def);
}
// Symtab block header: [num entries][type id number]
unsigned NumEntries, Typ;
if (read_vbr(Buf, EndBuf, NumEntries) ||
- read_vbr(Buf, EndBuf, Typ)) return true;
+ read_vbr(Buf, EndBuf, Typ)) return failure(true);
const Type *Ty = getType(Typ);
- if (Ty == 0) return true;
+ if (Ty == 0) return failure(true);
for (unsigned i = 0; i < NumEntries; ++i) {
// Symtab entry: [def slot #][name]
unsigned slot;
- if (read_vbr(Buf, EndBuf, slot)) return true;
+ if (read_vbr(Buf, EndBuf, slot)) return failure(true);
string Name;
if (read(Buf, EndBuf, Name, false)) // Not aligned...
- return true;
+ return failure(true);
Value *D = getValue(Ty, slot, false); // Find mapping...
- if (D == 0) return true;
+ if (D == 0) return failure(true);
D->setName(Name);
}
}
- return Buf > EndBuf;
+ if (Buf > EndBuf) return failure(true);
+ return false;
}
Module *C) {
// Clear out the local values table...
Values.clear();
- if (MethodSignatureList.empty()) return true; // Unexpected method!
+ if (MethodSignatureList.empty()) return failure(true); // Unexpected method!
const MethodType *MTy = MethodSignatureList.front().first;
unsigned MethSlot = MethodSignatureList.front().second;
for (MethodType::ParamTypes::const_iterator It = Params.begin();
It != Params.end(); ++It) {
MethodArgument *MA = new MethodArgument(*It);
- if (insertValue(MA, Values)) { delete M; return true; }
+ if (insertValue(MA, Values)) { delete M; return failure(true); }
M->getArgumentList().push_back(MA);
}
while (Buf < EndBuf) {
unsigned Type, Size;
const uchar *OldBuf = Buf;
- if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return true; }
+ if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return failure(true); }
switch (Type) {
case BytecodeFormat::ConstantPool:
if (ParseConstantPool(Buf, Buf+Size, M->getConstantPool(), Values)) {
cerr << "Error reading constant pool!\n";
- delete M; return true;
+ delete M; return failure(true);
}
break;
if (ParseBasicBlock(Buf, Buf+Size, BB) ||
insertValue(BB, Values)) {
cerr << "Error parsing basic block!\n";
- delete M; return true; // Parse error... :(
+ delete M; return failure(true); // Parse error... :(
}
M->getBasicBlocks().push_back(BB);
case BytecodeFormat::SymbolTable:
if (ParseSymbolTable(Buf, Buf+Size)) {
cerr << "Error reading method symbol table!\n";
- delete M; return true;
+ delete M; return failure(true);
}
break;
default:
Buf += Size;
- if (OldBuf > Buf) return true; // Wrap around!
+ if (OldBuf > Buf) return failure(true); // Wrap around!
break;
}
if (align32(Buf, EndBuf)) {
delete M; // Malformed bc file, read past end of block.
- return true;
+ return failure(true);
}
}
if (postResolveValues(LateResolveValues) ||
postResolveValues(LateResolveModuleValues)) {
- delete M; return true; // Unresolvable references!
+ delete M; return failure(true); // Unresolvable references!
}
Value *MethPHolder = getValue(MTy, MethSlot, false);
bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
Module *C) {
- if (!MethodSignatureList.empty()) return true; // Two ModuleGlobal blocks?
+ if (!MethodSignatureList.empty())
+ return failure(true); // Two ModuleGlobal blocks?
// Read the method signatures for all of the methods that are coming, and
// create fillers in the Value tables.
unsigned MethSignature;
- if (read_vbr(Buf, End, MethSignature)) return true;
+ if (read_vbr(Buf, End, MethSignature)) return failure(true);
while (MethSignature != Type::VoidTyID) { // List is terminated by Void
const Type *Ty = getType(MethSignature);
if (!Ty || !Ty->isMethodType()) {
cerr << "Method not meth type! ";
if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl;
- return true;
+ return failure(true);
}
// When the ModuleGlobalInfo section is read, we load the type of each method
// Figure out which entry of its typeslot it went into...
unsigned TypeSlot;
- if (getTypeSlot(Def->getType(), TypeSlot)) return true;
+ if (getTypeSlot(Def->getType(), TypeSlot)) return failure(true);
unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
// methods are loaded...
//
MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
- if (read_vbr(Buf, End, MethSignature)) return true;
+ if (read_vbr(Buf, End, MethSignature)) return failure(true);
}
- if (align32(Buf, End)) return true;
+ if (align32(Buf, End)) return failure(true);
// This is for future proofing... in the future extra fields may be added that
// we don't understand, so we transparently ignore them.
Module *&C) {
unsigned Type, Size;
- if (readBlock(Buf, EndBuf, Type, Size)) return true;
+ if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
- return true; // Hrm, not a class?
+ return failure(true); // Hrm, not a class?
MethodSignatureList.clear(); // Just in case...
// Read into instance variables...
- if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true;
- if (align32(Buf, EndBuf)) return true;
+ if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
+ if (align32(Buf, EndBuf)) return failure(true);
C = new Module();
while (Buf < EndBuf) {
const uchar *OldBuf = Buf;
- if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return true; }
+ if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
switch (Type) {
case BytecodeFormat::ModuleGlobalInfo:
if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
cerr << "Error reading class global info section!\n";
- delete C; return true;
+ delete C; return failure(true);
}
break;
case BytecodeFormat::ConstantPool:
if (ParseConstantPool(Buf, Buf+Size, C->getConstantPool(), ModuleValues)) {
cerr << "Error reading class constant pool!\n";
- delete C; return true;
+ delete C; return failure(true);
}
break;
case BytecodeFormat::Method: {
if (ParseMethod(Buf, Buf+Size, C)) {
- delete C; return true; // Error parsing method
+ delete C; return failure(true); // Error parsing method
}
break;
}
case BytecodeFormat::SymbolTable:
if (ParseSymbolTable(Buf, Buf+Size)) {
cerr << "Error reading class symbol table!\n";
- delete C; return true;
+ delete C; return failure(true);
}
break;
default:
cerr << "Unknown class block: " << Type << endl;
Buf += Size;
- if (OldBuf > Buf) return true; // Wrap around!
+ if (OldBuf > Buf) return failure(true); // Wrap around!
break;
}
- if (align32(Buf, EndBuf)) { delete C; return true; }
+ if (align32(Buf, EndBuf)) { delete C; return failure(true); }
}
if (!MethodSignatureList.empty()) // Expected more methods!
- return true;
+ return failure(true);
return false;
}
// Read and check signature...
if (read(Buf, EndBuf, Sig) ||
Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
- return 0; // Invalid signature!
+ return failure<Module*>(0); // Invalid signature!
Module *Result;
if (ParseModule(Buf, EndBuf, Result)) return 0;
if (Filename != string("-")) { // Read from a file...
int FD = open(Filename.c_str(), O_RDONLY);
- if (FD == -1) return 0;
+ if (FD == -1) return failure<Module*>(0);
- if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; }
+ if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
int Length = StatBuf.st_size;
- if (Length == 0) { close(FD); return 0; }
+ if (Length == 0) { close(FD); return failure<Module*>(0); }
uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
MAP_PRIVATE, FD, 0);
- if (Buffer == (uchar*)-1) { close(FD); return 0; }
+ if (Buffer == (uchar*)-1) { close(FD); return failure<Module*>(0); }
BytecodeParser Parser;
Result = Parser.ParseBytecode(Buffer, Buffer+Length);
int BlockSize;
uchar Buffer[4096], *FileData = 0;
while ((BlockSize = read(0, Buffer, 4))) {
- if (BlockSize == -1) { free(FileData); return 0; }
+ if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
memcpy(FileData+FileSize, Buffer, BlockSize);
FileSize += BlockSize;
}
- if (FileSize == 0) { free(FileData); return 0; }
+ if (FileSize == 0) { free(FileData); return failure<Module*>(0); }
#define ALIGN_PTRS 1
#if ALIGN_PTRS
#endif
}
+
+// failure Template - This template function is used as a place to put
+// breakpoints in to debug failures of the bytecode parser.
+//
+template <typename X>
+static X failure(X Value) {
+ return Value;
+}
+
#endif