mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-02 22:04:55 +00:00
Stop returning bool and pass Instruction by reference;
return std::auto_ptr and use exceptions git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8684 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
e0dd0d47cb
commit
d554ebfcf7
@ -17,30 +17,32 @@
|
|||||||
#include "llvm/iPHINode.h"
|
#include "llvm/iPHINode.h"
|
||||||
#include "llvm/iOther.h"
|
#include "llvm/iOther.h"
|
||||||
|
|
||||||
bool BytecodeParser::ParseRawInst(const unsigned char *&Buf,
|
std::auto_ptr<RawInst>
|
||||||
const unsigned char *EndBuf,
|
BytecodeParser::ParseRawInst(const unsigned char *&Buf,
|
||||||
RawInst &Result) {
|
const unsigned char *EndBuf) {
|
||||||
unsigned Op, Typ;
|
unsigned Op, Typ;
|
||||||
if (read(Buf, EndBuf, Op)) return true;
|
std::auto_ptr<RawInst> Result = std::auto_ptr<RawInst>(new RawInst());
|
||||||
|
if (read(Buf, EndBuf, Op))
|
||||||
|
throw std::string("Error reading from buffer.");
|
||||||
|
|
||||||
// bits Instruction format: Common to all formats
|
// bits Instruction format: Common to all formats
|
||||||
// --------------------------
|
// --------------------------
|
||||||
// 01-00: Opcode type, fixed to 1.
|
// 01-00: Opcode type, fixed to 1.
|
||||||
// 07-02: Opcode
|
// 07-02: Opcode
|
||||||
Result.NumOperands = (Op >> 0) & 03;
|
Result->NumOperands = (Op >> 0) & 03;
|
||||||
Result.Opcode = (Op >> 2) & 63;
|
Result->Opcode = (Op >> 2) & 63;
|
||||||
|
|
||||||
switch (Result.NumOperands) {
|
switch (Result->NumOperands) {
|
||||||
case 1:
|
case 1:
|
||||||
// bits Instruction format:
|
// bits Instruction format:
|
||||||
// --------------------------
|
// --------------------------
|
||||||
// 19-08: Resulting type plane
|
// 19-08: Resulting type plane
|
||||||
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
|
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
|
||||||
//
|
//
|
||||||
Result.Ty = getType((Op >> 8) & 4095);
|
Result->Ty = getType((Op >> 8) & 4095);
|
||||||
Result.Arg1 = (Op >> 20) & 4095;
|
Result->Arg1 = (Op >> 20) & 4095;
|
||||||
if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
|
if (Result->Arg1 == 4095) // Handle special encoding for 0 operands...
|
||||||
Result.NumOperands = 0;
|
Result->NumOperands = 0;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// bits Instruction format:
|
// bits Instruction format:
|
||||||
@ -49,9 +51,9 @@ bool BytecodeParser::ParseRawInst(const unsigned char *&Buf,
|
|||||||
// 23-16: Operand #1
|
// 23-16: Operand #1
|
||||||
// 31-24: Operand #2
|
// 31-24: Operand #2
|
||||||
//
|
//
|
||||||
Result.Ty = getType((Op >> 8) & 255);
|
Result->Ty = getType((Op >> 8) & 255);
|
||||||
Result.Arg1 = (Op >> 16) & 255;
|
Result->Arg1 = (Op >> 16) & 255;
|
||||||
Result.Arg2 = (Op >> 24) & 255;
|
Result->Arg2 = (Op >> 24) & 255;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
// bits Instruction format:
|
// bits Instruction format:
|
||||||
@ -61,112 +63,121 @@ bool BytecodeParser::ParseRawInst(const unsigned char *&Buf,
|
|||||||
// 25-20: Operand #2
|
// 25-20: Operand #2
|
||||||
// 31-26: Operand #3
|
// 31-26: Operand #3
|
||||||
//
|
//
|
||||||
Result.Ty = getType((Op >> 8) & 63);
|
Result->Ty = getType((Op >> 8) & 63);
|
||||||
Result.Arg1 = (Op >> 14) & 63;
|
Result->Arg1 = (Op >> 14) & 63;
|
||||||
Result.Arg2 = (Op >> 20) & 63;
|
Result->Arg2 = (Op >> 20) & 63;
|
||||||
Result.Arg3 = (Op >> 26) & 63;
|
Result->Arg3 = (Op >> 26) & 63;
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
Buf -= 4; // Hrm, try this again...
|
Buf -= 4; // Hrm, try this again...
|
||||||
if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
|
if (read_vbr(Buf, EndBuf, Result->Opcode))
|
||||||
Result.Opcode >>= 2;
|
throw std::string("Error reading from buffer.");
|
||||||
if (read_vbr(Buf, EndBuf, Typ)) return true;
|
Result->Opcode >>= 2;
|
||||||
Result.Ty = getType(Typ);
|
if (read_vbr(Buf, EndBuf, Typ))
|
||||||
if (Result.Ty == 0) return true;
|
throw std::string("Error reading from buffer.");
|
||||||
if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
|
Result->Ty = getType(Typ);
|
||||||
|
if (Result->Ty == 0)
|
||||||
|
throw std::string("Invalid type read in instruction.");
|
||||||
|
if (read_vbr(Buf, EndBuf, Result->NumOperands))
|
||||||
|
throw std::string("Error reading from buffer.");
|
||||||
|
|
||||||
switch (Result.NumOperands) {
|
switch (Result->NumOperands) {
|
||||||
case 0:
|
case 0:
|
||||||
std::cerr << "Zero Arg instr found!\n";
|
throw std::string("Zero-argument instruction found; this is invalid.");
|
||||||
return true; // This encoding is invalid!
|
|
||||||
case 1:
|
case 1:
|
||||||
if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
|
if (read_vbr(Buf, EndBuf, Result->Arg1))
|
||||||
|
throw std::string("Error reading from buffer");
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
||||||
read_vbr(Buf, EndBuf, Result.Arg2)) return true;
|
read_vbr(Buf, EndBuf, Result->Arg2))
|
||||||
|
throw std::string("Error reading from buffer");
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
||||||
read_vbr(Buf, EndBuf, Result.Arg2) ||
|
read_vbr(Buf, EndBuf, Result->Arg2) ||
|
||||||
read_vbr(Buf, EndBuf, Result.Arg3)) return true;
|
read_vbr(Buf, EndBuf, Result->Arg3))
|
||||||
|
throw std::string("Error reading from buffer");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
||||||
read_vbr(Buf, EndBuf, Result.Arg2)) return true;
|
read_vbr(Buf, EndBuf, Result->Arg2))
|
||||||
|
throw std::string("Error reading from buffer");
|
||||||
|
|
||||||
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
|
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
|
||||||
Result.VarArgs = new std::vector<unsigned>(Result.NumOperands-2);
|
Result->VarArgs = new std::vector<unsigned>(Result->NumOperands-2);
|
||||||
for (unsigned a = 0; a < Result.NumOperands-2; a++)
|
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]))
|
||||||
|
throw std::string("Error reading from buffer");
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (align32(Buf, EndBuf)) return true;
|
if (align32(Buf, EndBuf))
|
||||||
|
throw std::string("Unaligned bytecode buffer.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
std::cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
|
std::cerr << "NO: " << Result->NumOperands << " opcode: " << Result->Opcode
|
||||||
<< " Ty: " << Result.Ty->getDescription() << " arg1: "<< Result.Arg1
|
<< " Ty: "<< Result->Ty->getDescription()<< " arg1: "<< Result->Arg1
|
||||||
<< " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
|
<< " arg2: " << Result->Arg2 << " arg3: " << Result->Arg3 << "\n";
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
||||||
const unsigned char *EndBuf,
|
const unsigned char *EndBuf,
|
||||||
Instruction *&Res) {
|
Instruction *&Res) {
|
||||||
RawInst Raw;
|
std::auto_ptr<RawInst> Raw = ParseRawInst(Buf, EndBuf);
|
||||||
if (ParseRawInst(Buf, EndBuf, Raw))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (Raw.Opcode >= Instruction::BinaryOpsBegin &&
|
if (Raw->Opcode >= Instruction::BinaryOpsBegin &&
|
||||||
Raw.Opcode < Instruction::BinaryOpsEnd && Raw.NumOperands == 2) {
|
Raw->Opcode < Instruction::BinaryOpsEnd && Raw->NumOperands == 2) {
|
||||||
Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
|
Res = BinaryOperator::create((Instruction::BinaryOps)Raw->Opcode,
|
||||||
getValue(Raw.Ty, Raw.Arg1),
|
getValue(Raw->Ty, Raw->Arg1),
|
||||||
getValue(Raw.Ty, Raw.Arg2));
|
getValue(Raw->Ty, Raw->Arg2));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *V;
|
Value *V;
|
||||||
switch (Raw.Opcode) {
|
switch (Raw->Opcode) {
|
||||||
case Instruction::VarArg:
|
case Instruction::VarArg:
|
||||||
case Instruction::Cast: {
|
case Instruction::Cast: {
|
||||||
V = getValue(Raw.Ty, Raw.Arg1);
|
V = getValue(Raw->Ty, Raw->Arg1);
|
||||||
const Type *Ty = getType(Raw.Arg2);
|
const Type *Ty = getType(Raw->Arg2);
|
||||||
if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
|
if (V == 0 || Ty == 0) { std::cerr << "Invalid cast!\n"; return true; }
|
||||||
if (Raw.Opcode == Instruction::Cast)
|
if (Raw->Opcode == Instruction::Cast)
|
||||||
Res = new CastInst(V, Ty);
|
Res = new CastInst(V, Ty);
|
||||||
else
|
else
|
||||||
Res = new VarArgInst(V, Ty);
|
Res = new VarArgInst(V, Ty);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case Instruction::PHINode: {
|
case Instruction::PHINode: {
|
||||||
PHINode *PN = new PHINode(Raw.Ty);
|
PHINode *PN = new PHINode(Raw->Ty);
|
||||||
switch (Raw.NumOperands) {
|
switch (Raw->NumOperands) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
case 3: std::cerr << "Invalid phi node encountered!\n";
|
case 3: std::cerr << "Invalid phi node encountered!\n";
|
||||||
delete PN;
|
delete PN;
|
||||||
return true;
|
return true;
|
||||||
case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
|
case 2: PN->addIncoming(getValue(Raw->Ty, Raw->Arg1),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
|
cast<BasicBlock>(getValue(Type::LabelTy,
|
||||||
|
Raw->Arg2)));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
|
PN->addIncoming(getValue(Raw->Ty, Raw->Arg1),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
|
cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)));
|
||||||
if (Raw.VarArgs->size() & 1) {
|
if (Raw->VarArgs->size() & 1) {
|
||||||
std::cerr << "PHI Node with ODD number of arguments!\n";
|
std::cerr << "PHI Node with ODD number of arguments!\n";
|
||||||
delete PN;
|
delete PN;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
for (unsigned i = 0; i < args.size(); i+=2)
|
for (unsigned i = 0; i < args.size(); i+=2)
|
||||||
PN->addIncoming(getValue(Raw.Ty, args[i]),
|
PN->addIncoming(getValue(Raw->Ty, args[i]),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
|
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
|
||||||
}
|
}
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Res = PN;
|
Res = PN;
|
||||||
@ -175,54 +186,54 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
|
|
||||||
case Instruction::Shl:
|
case Instruction::Shl:
|
||||||
case Instruction::Shr:
|
case Instruction::Shr:
|
||||||
Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
|
Res = new ShiftInst((Instruction::OtherOps)Raw->Opcode,
|
||||||
getValue(Raw.Ty, Raw.Arg1),
|
getValue(Raw->Ty, Raw->Arg1),
|
||||||
getValue(Type::UByteTy, Raw.Arg2));
|
getValue(Type::UByteTy, Raw->Arg2));
|
||||||
return false;
|
return false;
|
||||||
case Instruction::Ret:
|
case Instruction::Ret:
|
||||||
if (Raw.NumOperands == 0) {
|
if (Raw->NumOperands == 0) {
|
||||||
Res = new ReturnInst(); return false;
|
Res = new ReturnInst(); return false;
|
||||||
} else if (Raw.NumOperands == 1) {
|
} else if (Raw->NumOperands == 1) {
|
||||||
Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
|
Res = new ReturnInst(getValue(Raw->Ty, Raw->Arg1)); return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Instruction::Br:
|
case Instruction::Br:
|
||||||
if (Raw.NumOperands == 1) {
|
if (Raw->NumOperands == 1) {
|
||||||
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
|
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy,Raw->Arg1)));
|
||||||
return false;
|
return false;
|
||||||
} else if (Raw.NumOperands == 3) {
|
} else if (Raw->NumOperands == 3) {
|
||||||
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
|
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg1)),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
|
cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)),
|
||||||
getValue(Type::BoolTy , Raw.Arg3));
|
getValue(Type::BoolTy , Raw->Arg3));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Instruction::Switch: {
|
case Instruction::Switch: {
|
||||||
SwitchInst *I =
|
SwitchInst *I =
|
||||||
new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
|
new SwitchInst(getValue(Raw->Ty, Raw->Arg1),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
|
cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2)));
|
||||||
Res = I;
|
Res = I;
|
||||||
if (Raw.NumOperands < 3) return false; // No destinations? Weird.
|
if (Raw->NumOperands < 3) return false; // No destinations? Weird.
|
||||||
|
|
||||||
if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
|
if (Raw->NumOperands == 3 || Raw->VarArgs->size() & 1) {
|
||||||
std::cerr << "Switch statement with odd number of arguments!\n";
|
std::cerr << "Switch statement with odd number of arguments!\n";
|
||||||
delete I;
|
delete I;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
for (unsigned i = 0; i < args.size(); i += 2)
|
for (unsigned i = 0; i < args.size(); i += 2)
|
||||||
I->addCase(cast<Constant>(getValue(Raw.Ty, args[i])),
|
I->addCase(cast<Constant>(getValue(Raw->Ty, args[i])),
|
||||||
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
|
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
|
||||||
|
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
case Instruction::Call: {
|
case Instruction::Call: {
|
||||||
Value *F = getValue(Raw.Ty, Raw.Arg1);
|
Value *F = getValue(Raw->Ty, Raw->Arg1);
|
||||||
if (F == 0) return true;
|
if (F == 0) return true;
|
||||||
|
|
||||||
// Check to make sure we have a pointer to method type
|
// Check to make sure we have a pointer to method type
|
||||||
@ -237,30 +248,30 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
if (!FTy->isVarArg()) {
|
if (!FTy->isVarArg()) {
|
||||||
FunctionType::ParamTypes::const_iterator It = PL.begin();
|
FunctionType::ParamTypes::const_iterator It = PL.begin();
|
||||||
|
|
||||||
switch (Raw.NumOperands) {
|
switch (Raw->NumOperands) {
|
||||||
case 0: std::cerr << "Invalid call instruction encountered!\n";
|
case 0: std::cerr << "Invalid call instruction encountered!\n";
|
||||||
return true;
|
return true;
|
||||||
case 1: break;
|
case 1: break;
|
||||||
case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
|
case 2: Params.push_back(getValue(*It++, Raw->Arg2)); break;
|
||||||
case 3: Params.push_back(getValue(*It++, Raw.Arg2));
|
case 3: Params.push_back(getValue(*It++, Raw->Arg2));
|
||||||
if (It == PL.end()) return true;
|
if (It == PL.end()) return true;
|
||||||
Params.push_back(getValue(*It++, Raw.Arg3)); break;
|
Params.push_back(getValue(*It++, Raw->Arg3)); break;
|
||||||
default:
|
default:
|
||||||
Params.push_back(getValue(*It++, Raw.Arg2));
|
Params.push_back(getValue(*It++, Raw->Arg2));
|
||||||
{
|
{
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
for (unsigned i = 0; i < args.size(); i++) {
|
for (unsigned i = 0; i < args.size(); i++) {
|
||||||
if (It == PL.end()) return true;
|
if (It == PL.end()) return true;
|
||||||
Params.push_back(getValue(*It++, args[i]));
|
Params.push_back(getValue(*It++, args[i]));
|
||||||
if (Params.back() == 0) return true;
|
if (Params.back() == 0) return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
}
|
}
|
||||||
if (It != PL.end()) return true;
|
if (It != PL.end()) return true;
|
||||||
} else {
|
} else {
|
||||||
if (Raw.NumOperands > 2) {
|
if (Raw->NumOperands > 2) {
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
if (args.size() < 1) return true;
|
if (args.size() < 1) return true;
|
||||||
|
|
||||||
if ((args.size() & 1) != 0)
|
if ((args.size() & 1) != 0)
|
||||||
@ -274,7 +285,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
if (V == 0) return true;
|
if (V == 0) return true;
|
||||||
Params.push_back(V);
|
Params.push_back(V);
|
||||||
}
|
}
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,7 +293,7 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case Instruction::Invoke: {
|
case Instruction::Invoke: {
|
||||||
Value *F = getValue(Raw.Ty, Raw.Arg1);
|
Value *F = getValue(Raw->Ty, Raw->Arg1);
|
||||||
if (F == 0) return true;
|
if (F == 0) return true;
|
||||||
|
|
||||||
// Check to make sure we have a pointer to method type
|
// Check to make sure we have a pointer to method type
|
||||||
@ -293,16 +304,16 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
|
|
||||||
std::vector<Value *> Params;
|
std::vector<Value *> Params;
|
||||||
const FunctionType::ParamTypes &PL = FTy->getParamTypes();
|
const FunctionType::ParamTypes &PL = FTy->getParamTypes();
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
|
|
||||||
BasicBlock *Normal, *Except;
|
BasicBlock *Normal, *Except;
|
||||||
|
|
||||||
if (!FTy->isVarArg()) {
|
if (!FTy->isVarArg()) {
|
||||||
if (Raw.NumOperands < 3) return true;
|
if (Raw->NumOperands < 3) return true;
|
||||||
|
|
||||||
Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
|
Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg2));
|
||||||
if (Raw.NumOperands == 3)
|
if (Raw->NumOperands == 3)
|
||||||
Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg3));
|
Except = cast<BasicBlock>(getValue(Type::LabelTy, Raw->Arg3));
|
||||||
else {
|
else {
|
||||||
Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
|
Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
|
||||||
|
|
||||||
@ -329,105 +340,105 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Raw.NumOperands > 3)
|
if (Raw->NumOperands > 3)
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
Res = new InvokeInst(F, Normal, Except, Params);
|
Res = new InvokeInst(F, Normal, Except, Params);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case Instruction::Malloc:
|
case Instruction::Malloc:
|
||||||
if (Raw.NumOperands > 2) return true;
|
if (Raw->NumOperands > 2) return true;
|
||||||
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
|
V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0;
|
||||||
if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
|
if (const PointerType *PTy = dyn_cast<PointerType>(Raw->Ty))
|
||||||
Res = new MallocInst(PTy->getElementType(), V);
|
Res = new MallocInst(PTy->getElementType(), V);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case Instruction::Alloca:
|
case Instruction::Alloca:
|
||||||
if (Raw.NumOperands > 2) return true;
|
if (Raw->NumOperands > 2) return true;
|
||||||
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
|
V = Raw->NumOperands ? getValue(Type::UIntTy, Raw->Arg1) : 0;
|
||||||
if (const PointerType *PTy = dyn_cast<PointerType>(Raw.Ty))
|
if (const PointerType *PTy = dyn_cast<PointerType>(Raw->Ty))
|
||||||
Res = new AllocaInst(PTy->getElementType(), V);
|
Res = new AllocaInst(PTy->getElementType(), V);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case Instruction::Free:
|
case Instruction::Free:
|
||||||
V = getValue(Raw.Ty, Raw.Arg1);
|
V = getValue(Raw->Ty, Raw->Arg1);
|
||||||
if (!isa<PointerType>(V->getType())) return true;
|
if (!isa<PointerType>(V->getType())) return true;
|
||||||
Res = new FreeInst(V);
|
Res = new FreeInst(V);
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case Instruction::GetElementPtr: {
|
case Instruction::GetElementPtr: {
|
||||||
std::vector<Value*> Idx;
|
std::vector<Value*> Idx;
|
||||||
if (!isa<PointerType>(Raw.Ty)) return true;
|
if (!isa<PointerType>(Raw->Ty)) return true;
|
||||||
const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
|
const CompositeType *TopTy = dyn_cast<CompositeType>(Raw->Ty);
|
||||||
|
|
||||||
switch (Raw.NumOperands) {
|
switch (Raw->NumOperands) {
|
||||||
case 0: std::cerr << "Invalid getelementptr encountered!\n"; return true;
|
case 0: std::cerr << "Invalid getelementptr encountered!\n"; return true;
|
||||||
case 1: break;
|
case 1: break;
|
||||||
case 2:
|
case 2:
|
||||||
if (!TopTy) return true;
|
if (!TopTy) return true;
|
||||||
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
|
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
|
||||||
if (!V) return true;
|
if (!V) return true;
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case 3: {
|
||||||
if (!TopTy) return true;
|
if (!TopTy) return true;
|
||||||
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
|
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
|
||||||
if (!V) return true;
|
if (!V) return true;
|
||||||
|
|
||||||
const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
|
const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
|
||||||
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
||||||
if (!ElTy) return true;
|
if (!ElTy) return true;
|
||||||
|
|
||||||
Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
|
Idx.push_back(V = getValue(ElTy->getIndexType(), Raw->Arg3));
|
||||||
if (!V) return true;
|
if (!V) return true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if (!TopTy) return true;
|
if (!TopTy) return true;
|
||||||
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
|
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw->Arg2));
|
||||||
if (!V) return true;
|
if (!V) return true;
|
||||||
|
|
||||||
std::vector<unsigned> &args = *Raw.VarArgs;
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
||||||
for (unsigned i = 0, E = args.size(); i != E; ++i) {
|
for (unsigned i = 0, E = args.size(); i != E; ++i) {
|
||||||
const Type *ETy = GetElementPtrInst::getIndexedType(Raw.Ty, Idx, true);
|
const Type *ETy = GetElementPtrInst::getIndexedType(Raw->Ty, Idx, true);
|
||||||
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
||||||
if (!ElTy) return true;
|
if (!ElTy) return true;
|
||||||
Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
|
Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
|
||||||
if (!V) return true;
|
if (!V) return true;
|
||||||
}
|
}
|
||||||
delete Raw.VarArgs;
|
delete Raw->VarArgs;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
|
Res = new GetElementPtrInst(getValue(Raw->Ty, Raw->Arg1), Idx);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 62: // volatile load
|
case 62: // volatile load
|
||||||
case Instruction::Load:
|
case Instruction::Load:
|
||||||
if (Raw.NumOperands != 1) return true;
|
if (Raw->NumOperands != 1) return true;
|
||||||
if (!isa<PointerType>(Raw.Ty)) return true;
|
if (!isa<PointerType>(Raw->Ty)) return true;
|
||||||
Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), "", Raw.Opcode == 62);
|
Res = new LoadInst(getValue(Raw->Ty, Raw->Arg1), "", Raw->Opcode == 62);
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case 63: // volatile store
|
case 63: // volatile store
|
||||||
case Instruction::Store: {
|
case Instruction::Store: {
|
||||||
if (!isa<PointerType>(Raw.Ty) || Raw.NumOperands != 2) return true;
|
if (!isa<PointerType>(Raw->Ty) || Raw->NumOperands != 2) return true;
|
||||||
|
|
||||||
Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
|
Value *Ptr = getValue(Raw->Ty, Raw->Arg2);
|
||||||
const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
|
const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
|
||||||
Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr, Raw.Opcode == 63);
|
Res = new StoreInst(getValue(ValTy, Raw->Arg1), Ptr, Raw->Opcode == 63);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case Instruction::Unwind:
|
case Instruction::Unwind:
|
||||||
if (Raw.NumOperands != 0) return true;
|
if (Raw->NumOperands != 0) return true;
|
||||||
Res = new UnwindInst();
|
Res = new UnwindInst();
|
||||||
return false;
|
return false;
|
||||||
} // end switch(Raw.Opcode)
|
} // end switch(Raw->Opcode)
|
||||||
|
|
||||||
std::cerr << "Unrecognized instruction! " << Raw.Opcode
|
std::cerr << "Unrecognized instruction! " << Raw->Opcode
|
||||||
<< " ADDR = 0x" << (void*)Buf << "\n";
|
<< " ADDR = 0x" << (void*)Buf << "\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -164,8 +164,8 @@ private:
|
|||||||
|
|
||||||
bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End,
|
bool ParseInstruction (const unsigned char *&Buf, const unsigned char *End,
|
||||||
Instruction *&);
|
Instruction *&);
|
||||||
bool ParseRawInst (const unsigned char *&Buf, const unsigned char *End,
|
std::auto_ptr<RawInst> ParseRawInst(const unsigned char *&Buf,
|
||||||
RawInst &);
|
const unsigned char *End);
|
||||||
|
|
||||||
void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
|
void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
|
||||||
ValueTable &Tab, TypeValuesListTy &TypeTab);
|
ValueTable &Tab, TypeValuesListTy &TypeTab);
|
||||||
|
Loading…
Reference in New Issue
Block a user