2001-10-13 06:47:01 +00:00
|
|
|
//===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file defines the mechanism to read an instruction from a bytecode
|
|
|
|
// stream.
|
|
|
|
//
|
|
|
|
// Note that this library should be as fast as possible, reentrant, and
|
|
|
|
// threadsafe!!
|
|
|
|
//
|
|
|
|
// TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1)
|
|
|
|
// Make it check type, so that casts are checked.
|
|
|
|
//
|
2001-10-13 06:47:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "ReaderInternals.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/iMemory.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
|
|
|
#include "llvm/iOther.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
std::auto_ptr<RawInst>
|
|
|
|
BytecodeParser::ParseRawInst(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned Op, Typ;
|
2003-09-23 16:17:50 +00:00
|
|
|
std::auto_ptr<RawInst> Result = std::auto_ptr<RawInst>(new RawInst());
|
|
|
|
if (read(Buf, EndBuf, Op))
|
|
|
|
throw std::string("Error reading from buffer.");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-23 03:21:10 +00:00
|
|
|
// bits Instruction format: Common to all formats
|
|
|
|
// --------------------------
|
|
|
|
// 01-00: Opcode type, fixed to 1.
|
|
|
|
// 07-02: Opcode
|
2003-09-23 16:17:50 +00:00
|
|
|
Result->NumOperands = (Op >> 0) & 03;
|
|
|
|
Result->Opcode = (Op >> 2) & 63;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
switch (Result->NumOperands) {
|
2001-06-06 20:29:01 +00:00
|
|
|
case 1:
|
2001-10-23 03:21:10 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 19-08: Resulting type plane
|
|
|
|
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
|
|
|
|
//
|
2003-09-23 16:17:50 +00:00
|
|
|
Result->Ty = getType((Op >> 8) & 4095);
|
|
|
|
Result->Arg1 = (Op >> 20) & 4095;
|
|
|
|
if (Result->Arg1 == 4095) // Handle special encoding for 0 operands...
|
|
|
|
Result->NumOperands = 0;
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2001-10-23 03:21:10 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 15-08: Resulting type plane
|
|
|
|
// 23-16: Operand #1
|
|
|
|
// 31-24: Operand #2
|
|
|
|
//
|
2003-09-23 16:17:50 +00:00
|
|
|
Result->Ty = getType((Op >> 8) & 255);
|
|
|
|
Result->Arg1 = (Op >> 16) & 255;
|
|
|
|
Result->Arg2 = (Op >> 24) & 255;
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2001-10-23 03:21:10 +00:00
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 13-08: Resulting type plane
|
|
|
|
// 19-14: Operand #1
|
|
|
|
// 25-20: Operand #2
|
|
|
|
// 31-26: Operand #3
|
|
|
|
//
|
2003-09-23 16:17:50 +00:00
|
|
|
Result->Ty = getType((Op >> 8) & 63);
|
|
|
|
Result->Arg1 = (Op >> 14) & 63;
|
|
|
|
Result->Arg2 = (Op >> 20) & 63;
|
|
|
|
Result->Arg3 = (Op >> 26) & 63;
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
Buf -= 4; // Hrm, try this again...
|
2003-09-23 16:17:50 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Result->Opcode))
|
|
|
|
throw std::string("Error reading from buffer.");
|
|
|
|
Result->Opcode >>= 2;
|
|
|
|
if (read_vbr(Buf, EndBuf, Typ))
|
|
|
|
throw std::string("Error reading from buffer.");
|
|
|
|
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) {
|
2001-06-06 20:29:01 +00:00
|
|
|
case 0:
|
2003-09-23 16:17:50 +00:00
|
|
|
throw std::string("Zero-argument instruction found; this is invalid.");
|
2001-06-06 20:29:01 +00:00
|
|
|
case 1:
|
2003-09-23 16:17:50 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Result->Arg1))
|
|
|
|
throw std::string("Error reading from buffer");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2003-09-23 16:17:50 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
|
|
|
read_vbr(Buf, EndBuf, Result->Arg2))
|
|
|
|
throw std::string("Error reading from buffer");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2003-09-23 16:17:50 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
|
|
|
read_vbr(Buf, EndBuf, Result->Arg2) ||
|
|
|
|
read_vbr(Buf, EndBuf, Result->Arg3))
|
|
|
|
throw std::string("Error reading from buffer");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-09-23 16:17:50 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, Result->Arg1) ||
|
|
|
|
read_vbr(Buf, EndBuf, Result->Arg2))
|
|
|
|
throw std::string("Error reading from buffer");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
|
2003-09-23 16:17:50 +00:00
|
|
|
Result->VarArgs = new std::vector<unsigned>(Result->NumOperands-2);
|
|
|
|
for (unsigned a = 0; a < Result->NumOperands-2; a++)
|
|
|
|
if (read_vbr(Buf, EndBuf, (*Result->VarArgs)[a]))
|
|
|
|
throw std::string("Error reading from buffer");
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-09-23 16:17:50 +00:00
|
|
|
if (align32(Buf, EndBuf))
|
|
|
|
throw std::string("Unaligned bytecode buffer.");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-07-07 08:36:50 +00:00
|
|
|
#if 0
|
2003-09-23 16:17:50 +00:00
|
|
|
std::cerr << "NO: " << Result->NumOperands << " opcode: " << Result->Opcode
|
|
|
|
<< " Ty: "<< Result->Ty->getDescription()<< " arg1: "<< Result->Arg1
|
|
|
|
<< " arg2: " << Result->Arg2 << " arg3: " << Result->Arg3 << "\n";
|
2001-07-07 08:36:50 +00:00
|
|
|
#endif
|
2003-09-23 16:17:50 +00:00
|
|
|
return Result;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
Instruction *BytecodeParser::ParseInstruction(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
2003-09-23 16:17:50 +00:00
|
|
|
std::auto_ptr<RawInst> Raw = ParseRawInst(Buf, EndBuf);
|
|
|
|
|
|
|
|
if (Raw->Opcode >= Instruction::BinaryOpsBegin &&
|
2003-10-09 18:25:19 +00:00
|
|
|
Raw->Opcode < Instruction::BinaryOpsEnd && Raw->NumOperands == 2)
|
|
|
|
return BinaryOperator::create((Instruction::BinaryOps)Raw->Opcode,
|
|
|
|
getValue(Raw->Ty, Raw->Arg1),
|
|
|
|
getValue(Raw->Ty, Raw->Arg2));
|
2001-07-08 21:10:27 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
switch (Raw->Opcode) {
|
2003-05-08 02:44:12 +00:00
|
|
|
case Instruction::VarArg:
|
2001-10-21 00:14:44 +00:00
|
|
|
case Instruction::Cast: {
|
2003-10-09 18:25:19 +00:00
|
|
|
Value *V = getValue(Raw->Ty, Raw->Arg1);
|
2003-09-23 16:17:50 +00:00
|
|
|
const Type *Ty = getType(Raw->Arg2);
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Ty == 0) throw std::string("Invalid cast!\n");
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->Opcode == Instruction::Cast)
|
2003-10-09 18:25:19 +00:00
|
|
|
return new CastInst(V, Ty);
|
2003-05-08 02:44:12 +00:00
|
|
|
else
|
2003-10-09 18:25:19 +00:00
|
|
|
return new VarArgInst(V, Ty);
|
2001-10-21 00:14:44 +00:00
|
|
|
}
|
2001-07-08 21:10:27 +00:00
|
|
|
case Instruction::PHINode: {
|
2003-09-23 16:17:50 +00:00
|
|
|
PHINode *PN = new PHINode(Raw->Ty);
|
|
|
|
switch (Raw->NumOperands) {
|
2001-06-11 15:04:40 +00:00
|
|
|
case 0:
|
|
|
|
case 1:
|
2003-10-09 18:25:19 +00:00
|
|
|
case 3:
|
|
|
|
delete PN;
|
|
|
|
throw std::string("Invalid phi node encountered!\n");
|
|
|
|
case 2:
|
|
|
|
PN->addIncoming(getValue(Raw->Ty, Raw->Arg1), getBasicBlock(Raw->Arg2));
|
2001-06-11 15:04:40 +00:00
|
|
|
break;
|
2001-06-06 20:29:01 +00:00
|
|
|
default:
|
2003-10-09 18:25:19 +00:00
|
|
|
PN->addIncoming(getValue(Raw->Ty, Raw->Arg1), getBasicBlock(Raw->Arg2));
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->VarArgs->size() & 1) {
|
2001-06-11 15:04:40 +00:00
|
|
|
delete PN;
|
2003-10-09 18:25:19 +00:00
|
|
|
throw std::string("PHI Node with ODD number of arguments!\n");
|
2001-06-11 15:04:40 +00:00
|
|
|
} else {
|
2003-09-23 16:17:50 +00:00
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2001-06-11 15:04:40 +00:00
|
|
|
for (unsigned i = 0; i < args.size(); i+=2)
|
2003-10-08 22:52:54 +00:00
|
|
|
PN->addIncoming(getValue(Raw->Ty, args[i]), getBasicBlock(args[i+1]));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2003-09-23 16:17:50 +00:00
|
|
|
delete Raw->VarArgs;
|
2001-06-11 15:04:40 +00:00
|
|
|
break;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2003-10-09 18:25:19 +00:00
|
|
|
return PN;
|
2001-07-08 21:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::Shr:
|
2003-10-09 18:25:19 +00:00
|
|
|
return new ShiftInst((Instruction::OtherOps)Raw->Opcode,
|
|
|
|
getValue(Raw->Ty, Raw->Arg1),
|
|
|
|
getValue(Type::UByteTyID, Raw->Arg2));
|
2001-07-08 21:10:27 +00:00
|
|
|
case Instruction::Ret:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands == 0)
|
|
|
|
return new ReturnInst();
|
|
|
|
else if (Raw->NumOperands == 1)
|
|
|
|
return new ReturnInst(getValue(Raw->Ty, Raw->Arg1));
|
2001-07-08 21:10:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Br:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands == 1)
|
|
|
|
return new BranchInst(getBasicBlock(Raw->Arg1));
|
|
|
|
else if (Raw->NumOperands == 3)
|
|
|
|
return new BranchInst(getBasicBlock(Raw->Arg1), getBasicBlock(Raw->Arg2),
|
|
|
|
getValue(Type::BoolTyID , Raw->Arg3));
|
|
|
|
throw std::string("Invalid number of operands for a 'br' instruction!");
|
2001-07-08 21:10:27 +00:00
|
|
|
|
|
|
|
case Instruction::Switch: {
|
2003-10-09 18:25:19 +00:00
|
|
|
SwitchInst *I = new SwitchInst(getValue(Raw->Ty, Raw->Arg1),
|
|
|
|
getBasicBlock(Raw->Arg2));
|
|
|
|
if (Raw->NumOperands < 3)
|
|
|
|
return I;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->NumOperands == 3 || Raw->VarArgs->size() & 1) {
|
2001-06-06 20:29:01 +00:00
|
|
|
delete I;
|
2003-10-09 18:25:19 +00:00
|
|
|
throw std::string("Switch statement with odd number of arguments!");
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2001-06-06 20:29:01 +00:00
|
|
|
for (unsigned i = 0; i < args.size(); i += 2)
|
2003-09-23 16:17:50 +00:00
|
|
|
I->addCase(cast<Constant>(getValue(Raw->Ty, args[i])),
|
2003-10-08 22:52:54 +00:00
|
|
|
getBasicBlock(args[i+1]));
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
delete Raw->VarArgs;
|
2003-10-09 18:25:19 +00:00
|
|
|
return I;
|
2001-07-08 21:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Call: {
|
2003-09-23 16:17:50 +00:00
|
|
|
Value *F = getValue(Raw->Ty, Raw->Arg1);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
// Check to make sure we have a pointer to function type
|
2003-09-05 18:25:29 +00:00
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(F->getType());
|
2003-10-09 18:25:19 +00:00
|
|
|
if (PTy == 0) throw std::string("Call to non function pointer value!");
|
2003-09-05 18:25:29 +00:00
|
|
|
const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
|
2003-10-09 18:25:19 +00:00
|
|
|
if (FTy == 0) throw std::string("Call to non function pointer value!");
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2003-03-06 16:32:25 +00:00
|
|
|
std::vector<Value *> Params;
|
2003-09-05 18:25:29 +00:00
|
|
|
const FunctionType::ParamTypes &PL = FTy->getParamTypes();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-05 18:25:29 +00:00
|
|
|
if (!FTy->isVarArg()) {
|
2002-04-04 22:19:18 +00:00
|
|
|
FunctionType::ParamTypes::const_iterator It = PL.begin();
|
2001-07-25 22:47:55 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
switch (Raw->NumOperands) {
|
2003-10-09 18:25:19 +00:00
|
|
|
case 0: throw std::string("Invalid call instruction encountered!");
|
2001-07-25 22:47:55 +00:00
|
|
|
case 1: break;
|
2003-09-23 16:17:50 +00:00
|
|
|
case 2: Params.push_back(getValue(*It++, Raw->Arg2)); break;
|
|
|
|
case 3: Params.push_back(getValue(*It++, Raw->Arg2));
|
2003-10-09 18:25:19 +00:00
|
|
|
if (It == PL.end()) throw std::string("Invalid call instruction!");
|
2003-09-23 16:17:50 +00:00
|
|
|
Params.push_back(getValue(*It++, Raw->Arg3)); break;
|
2001-07-25 22:47:55 +00:00
|
|
|
default:
|
2003-09-23 16:17:50 +00:00
|
|
|
Params.push_back(getValue(*It++, Raw->Arg2));
|
2001-07-25 22:47:55 +00:00
|
|
|
{
|
2003-09-23 16:17:50 +00:00
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2001-07-25 22:47:55 +00:00
|
|
|
for (unsigned i = 0; i < args.size(); i++) {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (It == PL.end()) throw std::string("Invalid call instruction!");
|
2001-07-25 22:47:55 +00:00
|
|
|
Params.push_back(getValue(*It++, args[i]));
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2003-09-23 16:17:50 +00:00
|
|
|
delete Raw->VarArgs;
|
2001-07-25 22:47:55 +00:00
|
|
|
}
|
2003-10-09 18:25:19 +00:00
|
|
|
if (It != PL.end()) throw std::string("Invalid call instruction!");
|
2001-07-25 22:47:55 +00:00
|
|
|
} else {
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->NumOperands > 2) {
|
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2003-10-09 18:25:19 +00:00
|
|
|
if (args.size() < 1) throw std::string("Invalid call instruction!");
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
if ((args.size() & 1) != 0) // Must be pairs of type/value
|
|
|
|
throw std::string("Invalid call instruction!");
|
2001-10-13 06:47:01 +00:00
|
|
|
for (unsigned i = 0; i < args.size(); i+=2) {
|
|
|
|
const Type *Ty = getType(args[i]);
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Ty == 0) throw std::string("Invalid call instruction!");
|
|
|
|
Params.push_back(getValue(Ty, args[i+1]));
|
2001-10-13 06:47:01 +00:00
|
|
|
}
|
2003-09-23 16:17:50 +00:00
|
|
|
delete Raw->VarArgs;
|
2001-10-13 06:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
return new CallInst(F, Params);
|
2001-10-13 06:47:01 +00:00
|
|
|
}
|
|
|
|
case Instruction::Invoke: {
|
2003-09-23 16:17:50 +00:00
|
|
|
Value *F = getValue(Raw->Ty, Raw->Arg1);
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
// Check to make sure we have a pointer to function type
|
2003-09-05 18:25:29 +00:00
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(F->getType());
|
2003-10-09 18:25:19 +00:00
|
|
|
if (PTy == 0) throw std::string("Invoke to non function pointer value!");
|
2003-09-05 18:25:29 +00:00
|
|
|
const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
|
2003-10-09 18:25:19 +00:00
|
|
|
if (FTy == 0) throw std::string("Invoke to non function pointer value!");
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2003-03-06 16:32:25 +00:00
|
|
|
std::vector<Value *> Params;
|
2003-09-05 18:25:29 +00:00
|
|
|
const FunctionType::ParamTypes &PL = FTy->getParamTypes();
|
2003-09-23 16:17:50 +00:00
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2001-10-13 06:47:01 +00:00
|
|
|
|
|
|
|
BasicBlock *Normal, *Except;
|
|
|
|
|
2003-09-05 18:25:29 +00:00
|
|
|
if (!FTy->isVarArg()) {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands < 3) throw std::string("Invalid call instruction!");
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
Normal = getBasicBlock(Raw->Arg2);
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->NumOperands == 3)
|
2003-10-08 22:52:54 +00:00
|
|
|
Except = getBasicBlock(Raw->Arg3);
|
2003-06-17 13:31:10 +00:00
|
|
|
else {
|
2003-10-08 22:52:54 +00:00
|
|
|
Except = getBasicBlock(args[0]);
|
2003-06-17 13:31:10 +00:00
|
|
|
|
|
|
|
FunctionType::ParamTypes::const_iterator It = PL.begin();
|
|
|
|
for (unsigned i = 1; i < args.size(); i++) {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (It == PL.end()) throw std::string("Invalid invoke instruction!");
|
2003-06-17 13:31:10 +00:00
|
|
|
Params.push_back(getValue(*It++, args[i]));
|
|
|
|
}
|
2003-10-09 18:25:19 +00:00
|
|
|
if (It != PL.end()) throw std::string("Invalid invoke instruction!");
|
2001-10-13 06:47:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (args.size() < 4) throw std::string("Invalid invoke instruction!");
|
2003-10-08 22:52:54 +00:00
|
|
|
if (args[0] != Type::LabelTyID || args[2] != Type::LabelTyID)
|
2003-10-09 18:25:19 +00:00
|
|
|
throw std::string("Invalid invoke instruction!");
|
2003-10-08 22:52:54 +00:00
|
|
|
|
|
|
|
Normal = getBasicBlock(args[1]);
|
|
|
|
Except = getBasicBlock(args[3]);
|
2001-07-25 22:47:55 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
if ((args.size() & 1) != 0) // Must be pairs of type/value
|
|
|
|
throw std::string("Invalid invoke instruction!");
|
|
|
|
|
|
|
|
for (unsigned i = 4; i < args.size(); i += 2)
|
2003-10-08 21:18:57 +00:00
|
|
|
Params.push_back(getValue(args[i], args[i+1]));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
if (Raw->NumOperands > 3)
|
|
|
|
delete Raw->VarArgs;
|
2003-10-09 18:25:19 +00:00
|
|
|
return new InvokeInst(F, Normal, Except, Params);
|
2001-07-08 21:10:27 +00:00
|
|
|
}
|
|
|
|
case Instruction::Malloc:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands > 2) throw std::string("Invalid malloc instruction!");
|
|
|
|
if (!isa<PointerType>(Raw->Ty))
|
|
|
|
throw std::string("Invalid malloc instruction!");
|
|
|
|
|
|
|
|
return new MallocInst(cast<PointerType>(Raw->Ty)->getElementType(),
|
|
|
|
Raw->NumOperands ? getValue(Type::UIntTyID,
|
|
|
|
Raw->Arg1) : 0);
|
2001-07-08 21:10:27 +00:00
|
|
|
|
|
|
|
case Instruction::Alloca:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands > 2) throw std::string("Invalid alloca instruction!");
|
|
|
|
if (!isa<PointerType>(Raw->Ty))
|
|
|
|
throw std::string("Invalid alloca instruction!");
|
2001-07-08 21:10:27 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
return new AllocaInst(cast<PointerType>(Raw->Ty)->getElementType(),
|
|
|
|
Raw->NumOperands ? getValue(Type::UIntTyID,
|
|
|
|
Raw->Arg1) : 0);
|
2001-07-08 21:10:27 +00:00
|
|
|
case Instruction::Free:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!isa<PointerType>(Raw->Ty))
|
|
|
|
throw std::string("Invalid free instruction!");
|
|
|
|
return new FreeInst(getValue(Raw->Ty, Raw->Arg1));
|
2001-07-08 21:10:27 +00:00
|
|
|
|
2001-07-08 23:22:50 +00:00
|
|
|
case Instruction::GetElementPtr: {
|
2003-03-06 16:32:25 +00:00
|
|
|
std::vector<Value*> Idx;
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!isa<PointerType>(Raw->Ty))
|
|
|
|
throw std::string("Invalid getelementptr instruction!");
|
2003-09-23 16:17:50 +00:00
|
|
|
const CompositeType *TopTy = dyn_cast<CompositeType>(Raw->Ty);
|
2001-11-26 16:54:55 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
switch (Raw->NumOperands) {
|
2003-10-09 18:25:19 +00:00
|
|
|
case 0: throw std::string("Invalid getelementptr instruction!");
|
2001-07-08 21:10:27 +00:00
|
|
|
case 1: break;
|
2001-11-26 16:54:55 +00:00
|
|
|
case 2:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!TopTy) throw std::string("Invalid getelementptr instruction!");
|
|
|
|
Idx.push_back(getValue(TopTy->getIndexType(), Raw->Arg2));
|
2001-11-26 16:54:55 +00:00
|
|
|
break;
|
|
|
|
case 3: {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!TopTy) throw std::string("Invalid getelementptr instruction!");
|
|
|
|
Idx.push_back(getValue(TopTy->getIndexType(), Raw->Arg2));
|
2001-11-26 16:54:55 +00:00
|
|
|
|
2002-08-22 23:37:20 +00:00
|
|
|
const Type *ETy = GetElementPtrInst::getIndexedType(TopTy, Idx, true);
|
2001-11-26 16:54:55 +00:00
|
|
|
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!ElTy) throw std::string("Invalid getelementptr instruction!");
|
2001-11-26 16:54:55 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
Idx.push_back(getValue(ElTy->getIndexType(), Raw->Arg3));
|
2001-11-26 16:54:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-08 21:10:27 +00:00
|
|
|
default:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!TopTy) throw std::string("Invalid getelementptr instruction!");
|
|
|
|
Idx.push_back(getValue(TopTy->getIndexType(), Raw->Arg2));
|
2001-11-26 16:54:55 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
std::vector<unsigned> &args = *Raw->VarArgs;
|
2001-07-08 21:10:27 +00:00
|
|
|
for (unsigned i = 0, E = args.size(); i != E; ++i) {
|
2003-09-23 16:17:50 +00:00
|
|
|
const Type *ETy = GetElementPtrInst::getIndexedType(Raw->Ty, Idx, true);
|
2001-11-26 16:54:55 +00:00
|
|
|
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!ElTy) throw std::string("Invalid getelementptr instruction!");
|
|
|
|
Idx.push_back(getValue(ElTy->getIndexType(), args[i]));
|
2001-07-08 21:10:27 +00:00
|
|
|
}
|
2003-09-23 16:17:50 +00:00
|
|
|
delete Raw->VarArgs;
|
2001-07-08 21:10:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-12 21:48:38 +00:00
|
|
|
|
2003-10-09 18:25:19 +00:00
|
|
|
return new GetElementPtrInst(getValue(Raw->Ty, Raw->Arg1), Idx);
|
2001-07-08 23:22:50 +00:00
|
|
|
}
|
2001-11-26 16:54:55 +00:00
|
|
|
|
2003-09-08 18:20:14 +00:00
|
|
|
case 62: // volatile load
|
2003-09-08 18:04:16 +00:00
|
|
|
case Instruction::Load:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands != 1 || !isa<PointerType>(Raw->Ty))
|
|
|
|
throw std::string("Invalid load instruction!");
|
|
|
|
return new LoadInst(getValue(Raw->Ty, Raw->Arg1), "", Raw->Opcode == 62);
|
2001-12-14 16:30:09 +00:00
|
|
|
|
2003-09-08 18:20:14 +00:00
|
|
|
case 63: // volatile store
|
2003-09-08 18:04:16 +00:00
|
|
|
case Instruction::Store: {
|
2003-10-09 18:25:19 +00:00
|
|
|
if (!isa<PointerType>(Raw->Ty) || Raw->NumOperands != 2)
|
|
|
|
throw std::string("Invalid store instruction!");
|
2001-07-08 23:22:50 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
Value *Ptr = getValue(Raw->Ty, Raw->Arg2);
|
2002-08-21 22:55:27 +00:00
|
|
|
const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
|
2003-10-09 18:25:19 +00:00
|
|
|
return new StoreInst(getValue(ValTy, Raw->Arg1), Ptr, Raw->Opcode == 63);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2003-09-08 18:54:55 +00:00
|
|
|
case Instruction::Unwind:
|
2003-10-09 18:25:19 +00:00
|
|
|
if (Raw->NumOperands != 0) throw std::string("Invalid unwind instruction!");
|
|
|
|
return new UnwindInst();
|
2003-09-23 16:17:50 +00:00
|
|
|
} // end switch(Raw->Opcode)
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-23 16:17:50 +00:00
|
|
|
std::cerr << "Unrecognized instruction! " << Raw->Opcode
|
2003-03-06 16:32:25 +00:00
|
|
|
<< " ADDR = 0x" << (void*)Buf << "\n";
|
2003-10-09 18:25:19 +00:00
|
|
|
throw std::string("Unrecognized instruction!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|