2001-08-24 01:05:04 +08:00
|
|
|
//===-- Execution.cpp - Implement code to simulate the program ------------===//
|
|
|
|
//
|
|
|
|
// This file contains the actual instruction interpreter.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Interpreter.h"
|
|
|
|
#include "ExecutionAnnotations.h"
|
2002-12-24 07:59:41 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Function.h"
|
2001-12-04 02:02:31 +08:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-08-24 01:05:04 +08:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/iTerminators.h"
|
2001-08-27 13:16:50 +08:00
|
|
|
#include "llvm/iMemory.h"
|
2002-04-30 02:56:45 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-04-29 03:55:58 +08:00
|
|
|
#include "llvm/Constants.h"
|
2001-08-24 01:05:04 +08:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2001-12-15 00:49:29 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2002-12-08 13:51:08 +08:00
|
|
|
#include "Support/Statistic.h"
|
2001-10-31 04:27:31 +08:00
|
|
|
#include <math.h> // For fmod
|
2001-11-07 12:23:00 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
2001-10-15 13:51:48 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
Interpreter *TheEE = 0;
|
|
|
|
|
2002-12-08 13:51:08 +08:00
|
|
|
namespace {
|
|
|
|
Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
|
|
|
|
|
2002-12-08 14:01:34 +08:00
|
|
|
cl::opt<bool>
|
2002-12-24 07:59:41 +08:00
|
|
|
QuietMode("quiet", cl::desc("Do not emit any non-program output"),
|
|
|
|
cl::init(true));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-08 14:01:34 +08:00
|
|
|
cl::alias
|
|
|
|
QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-08 14:01:34 +08:00
|
|
|
cl::opt<bool>
|
|
|
|
ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2002-12-08 14:01:34 +08:00
|
|
|
cl::opt<bool>
|
|
|
|
AbortOnExceptions("abort-on-exception",
|
|
|
|
cl::desc("Halt execution on a machine exception"));
|
|
|
|
}
|
2001-12-04 06:26:30 +08:00
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
// Create a TargetData structure to handle memory addressing and size/alignment
|
|
|
|
// computations
|
|
|
|
//
|
2001-11-08 03:46:27 +08:00
|
|
|
CachedWriter CW; // Object to accelerate printing of LLVM
|
2001-11-07 12:23:00 +08:00
|
|
|
|
2001-11-13 00:19:45 +08:00
|
|
|
#ifdef PROFILE_STRUCTURE_FIELDS
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ProfileStructureFields("profilestructfields",
|
|
|
|
cl::desc("Profile Structure Field Accesses"));
|
2001-11-13 00:19:45 +08:00
|
|
|
#include <map>
|
2003-04-23 05:22:33 +08:00
|
|
|
static std::map<const StructType *, std::vector<unsigned> > FieldAccessCounts;
|
2001-11-13 00:19:45 +08:00
|
|
|
#endif
|
|
|
|
|
2001-11-07 12:23:00 +08:00
|
|
|
sigjmp_buf SignalRecoverBuffer;
|
2001-11-07 13:31:27 +08:00
|
|
|
static bool InInstruction = false;
|
2001-11-07 12:23:00 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
static void SigHandler(int Signal) {
|
2001-11-07 13:31:27 +08:00
|
|
|
if (InInstruction)
|
|
|
|
siglongjmp(SignalRecoverBuffer, Signal);
|
2001-11-07 12:23:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initializeSignalHandlers() {
|
|
|
|
struct sigaction Action;
|
|
|
|
Action.sa_handler = SigHandler;
|
|
|
|
Action.sa_flags = SA_SIGINFO;
|
|
|
|
sigemptyset(&Action.sa_mask);
|
|
|
|
sigaction(SIGSEGV, &Action, 0);
|
|
|
|
sigaction(SIGBUS, &Action, 0);
|
2001-11-07 13:31:27 +08:00
|
|
|
sigaction(SIGINT, &Action, 0);
|
2001-11-08 03:46:27 +08:00
|
|
|
sigaction(SIGFPE, &Action, 0);
|
2001-11-07 12:23:00 +08:00
|
|
|
}
|
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
|
2001-10-15 21:25:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Manipulation code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static unsigned getOperandSlot(Value *V) {
|
|
|
|
SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
|
|
|
|
assert(SN && "Operand does not have a slot number annotation!");
|
|
|
|
return SN->SlotNum;
|
|
|
|
}
|
|
|
|
|
2002-08-28 06:33:45 +08:00
|
|
|
// Operations used by constant expr implementations...
|
|
|
|
static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
|
|
|
|
ExecutionContext &SF);
|
|
|
|
static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty);
|
2002-08-28 06:33:45 +08:00
|
|
|
|
2002-10-16 04:34:05 +08:00
|
|
|
|
2001-10-15 21:25:40 +08:00
|
|
|
static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
|
2002-08-28 06:33:45 +08:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::Cast:
|
|
|
|
return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
|
|
|
|
case Instruction::GetElementPtr:
|
2002-12-24 07:59:41 +08:00
|
|
|
return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
|
|
|
|
CE->op_end(), SF);
|
2002-08-28 06:33:45 +08:00
|
|
|
case Instruction::Add:
|
|
|
|
return executeAddInst(getOperandValue(CE->getOperand(0), SF),
|
|
|
|
getOperandValue(CE->getOperand(1), SF),
|
2003-04-23 04:37:39 +08:00
|
|
|
CE->getType());
|
2002-08-28 06:33:45 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
|
2002-08-28 06:33:45 +08:00
|
|
|
abort();
|
2003-04-22 06:43:32 +08:00
|
|
|
return GenericValue();
|
2002-08-28 06:33:45 +08:00
|
|
|
}
|
|
|
|
} else if (Constant *CPV = dyn_cast<Constant>(V)) {
|
2002-12-24 07:59:41 +08:00
|
|
|
return TheEE->getConstantValue(CPV);
|
2001-10-15 21:25:40 +08:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2002-12-24 07:59:41 +08:00
|
|
|
return PTOGV(TheEE->getPointerToGlobal(GV));
|
2001-10-15 21:25:40 +08:00
|
|
|
} else {
|
|
|
|
unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
|
2001-10-31 04:27:31 +08:00
|
|
|
unsigned OpSlot = getOperandSlot(V);
|
|
|
|
assert(TyP < SF.Values.size() &&
|
|
|
|
OpSlot < SF.Values[TyP].size() && "Value out of range!");
|
2001-10-15 21:25:40 +08:00
|
|
|
return SF.Values[TyP][getOperandSlot(V)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printOperandInfo(Value *V, ExecutionContext &SF) {
|
2001-12-04 06:26:30 +08:00
|
|
|
if (isa<Constant>(V)) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Constant Pool Value\n";
|
2001-10-15 21:25:40 +08:00
|
|
|
} else if (isa<GlobalValue>(V)) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Global Value\n";
|
2001-10-15 21:25:40 +08:00
|
|
|
} else {
|
|
|
|
unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
|
|
|
|
unsigned Slot = getOperandSlot(V);
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
|
|
|
|
<< " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
|
|
|
|
<< " Contents=0x";
|
2001-11-08 03:46:27 +08:00
|
|
|
|
|
|
|
const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
|
|
|
|
for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
|
|
|
|
unsigned char Cur = Buf[i];
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
|
|
|
|
<< ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
|
2001-11-08 03:46:27 +08:00
|
|
|
}
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-10-15 21:25:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
|
|
|
|
unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
|
|
|
|
|
2003-04-23 05:22:33 +08:00
|
|
|
//std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
|
2001-10-15 21:25:40 +08:00
|
|
|
SF.Values[TyP][getOperandSlot(V)] = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Annotation Wrangling code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Interpreter::initializeExecutionEngine() {
|
2002-12-24 07:59:41 +08:00
|
|
|
TheEE = this;
|
2001-10-15 13:51:48 +08:00
|
|
|
AnnotationManager::registerAnnotationFactory(MethodInfoAID,
|
|
|
|
&MethodInfo::Create);
|
2001-11-07 12:23:00 +08:00
|
|
|
initializeSignalHandlers();
|
2001-10-15 13:51:48 +08:00
|
|
|
}
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Binary Instruction Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
|
|
|
|
case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
|
|
|
|
|
|
|
|
static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, Float);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(+, Double);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, Float);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(-, Double);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2001-10-27 16:28:11 +08:00
|
|
|
static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-10-27 16:28:11 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, Long);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, Float);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(*, Double);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
|
|
|
|
abort();
|
2001-10-27 16:28:11 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-10-27 16:28:11 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, Long);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, Float);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(/, Double);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-10-31 04:27:31 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-10-31 04:27:31 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(%, Long);
|
|
|
|
case Type::FloatTyID:
|
|
|
|
Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
|
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
|
|
|
|
break;
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-10-27 16:28:11 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2001-11-08 03:46:27 +08:00
|
|
|
static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-10-31 04:54:36 +08:00
|
|
|
GenericValue Dest;
|
2001-11-08 03:46:27 +08:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
2003-04-24 03:21:00 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, Bool);
|
2001-11-08 03:46:27 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(&, Long);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-11-08 03:46:27 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-11-08 03:46:27 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
2003-04-24 03:21:00 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, Bool);
|
2001-11-08 03:46:27 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(|, Long);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-11-08 03:46:27 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-11-08 03:46:27 +08:00
|
|
|
GenericValue Dest;
|
2001-10-31 04:54:36 +08:00
|
|
|
switch (Ty->getPrimitiveID()) {
|
2003-04-24 03:21:00 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, Bool);
|
2001-10-31 04:54:36 +08:00
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, UByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, SByte);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, UShort);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, Short);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, UInt);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, Int);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, ULong);
|
|
|
|
IMPLEMENT_BINARY_OPERATOR(^, Long);
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-10-31 04:54:36 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
#define IMPLEMENT_SETCC(OP, TY) \
|
|
|
|
case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
|
|
|
|
|
|
|
|
static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(==, UByte);
|
|
|
|
IMPLEMENT_SETCC(==, SByte);
|
|
|
|
IMPLEMENT_SETCC(==, UShort);
|
|
|
|
IMPLEMENT_SETCC(==, Short);
|
|
|
|
IMPLEMENT_SETCC(==, UInt);
|
|
|
|
IMPLEMENT_SETCC(==, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(==, ULong);
|
|
|
|
IMPLEMENT_SETCC(==, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(==, Float);
|
|
|
|
IMPLEMENT_SETCC(==, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(==, Pointer);
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(!=, UByte);
|
|
|
|
IMPLEMENT_SETCC(!=, SByte);
|
|
|
|
IMPLEMENT_SETCC(!=, UShort);
|
|
|
|
IMPLEMENT_SETCC(!=, Short);
|
|
|
|
IMPLEMENT_SETCC(!=, UInt);
|
|
|
|
IMPLEMENT_SETCC(!=, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(!=, ULong);
|
|
|
|
IMPLEMENT_SETCC(!=, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(!=, Float);
|
|
|
|
IMPLEMENT_SETCC(!=, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(!=, Pointer);
|
2001-11-08 03:46:27 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(<=, UByte);
|
|
|
|
IMPLEMENT_SETCC(<=, SByte);
|
|
|
|
IMPLEMENT_SETCC(<=, UShort);
|
|
|
|
IMPLEMENT_SETCC(<=, Short);
|
|
|
|
IMPLEMENT_SETCC(<=, UInt);
|
|
|
|
IMPLEMENT_SETCC(<=, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(<=, ULong);
|
|
|
|
IMPLEMENT_SETCC(<=, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(<=, Float);
|
|
|
|
IMPLEMENT_SETCC(<=, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(<=, Pointer);
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(>=, UByte);
|
|
|
|
IMPLEMENT_SETCC(>=, SByte);
|
|
|
|
IMPLEMENT_SETCC(>=, UShort);
|
|
|
|
IMPLEMENT_SETCC(>=, Short);
|
|
|
|
IMPLEMENT_SETCC(>=, UInt);
|
|
|
|
IMPLEMENT_SETCC(>=, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(>=, ULong);
|
|
|
|
IMPLEMENT_SETCC(>=, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(>=, Float);
|
|
|
|
IMPLEMENT_SETCC(>=, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(>=, Pointer);
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(<, UByte);
|
|
|
|
IMPLEMENT_SETCC(<, SByte);
|
|
|
|
IMPLEMENT_SETCC(<, UShort);
|
|
|
|
IMPLEMENT_SETCC(<, Short);
|
|
|
|
IMPLEMENT_SETCC(<, UInt);
|
|
|
|
IMPLEMENT_SETCC(<, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(<, ULong);
|
|
|
|
IMPLEMENT_SETCC(<, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(<, Float);
|
|
|
|
IMPLEMENT_SETCC(<, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(<, Pointer);
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
|
2003-04-23 04:37:39 +08:00
|
|
|
const Type *Ty) {
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SETCC(>, UByte);
|
|
|
|
IMPLEMENT_SETCC(>, SByte);
|
|
|
|
IMPLEMENT_SETCC(>, UShort);
|
|
|
|
IMPLEMENT_SETCC(>, Short);
|
|
|
|
IMPLEMENT_SETCC(>, UInt);
|
|
|
|
IMPLEMENT_SETCC(>, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SETCC(>, ULong);
|
|
|
|
IMPLEMENT_SETCC(>, Long);
|
2001-08-24 01:05:04 +08:00
|
|
|
IMPLEMENT_SETCC(>, Float);
|
|
|
|
IMPLEMENT_SETCC(>, Double);
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_SETCC(>, Pointer);
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
|
|
|
|
const Type *Ty = I.getOperand(0)->getType();
|
|
|
|
GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
|
|
|
|
GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
|
2001-08-24 01:05:04 +08:00
|
|
|
GenericValue R; // Result
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
switch (I.getOpcode()) {
|
2003-04-23 04:37:39 +08:00
|
|
|
case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
|
|
|
|
case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Don't know how to handle this binary operator!\n-->" << I;
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, R, SF);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Terminator Instruction Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-11-13 00:28:48 +08:00
|
|
|
static void PerformExitStuff() {
|
|
|
|
#ifdef PROFILE_STRUCTURE_FIELDS
|
|
|
|
// Print out structure field accounting information...
|
|
|
|
if (!FieldAccessCounts.empty()) {
|
2001-11-13 04:13:14 +08:00
|
|
|
CW << "Profile Field Access Counts:\n";
|
2003-04-23 05:22:33 +08:00
|
|
|
std::map<const StructType *, std::vector<unsigned> >::iterator
|
2001-11-13 00:28:48 +08:00
|
|
|
I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
|
|
|
|
for (; I != E; ++I) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::vector<unsigned> &OfC = I->second;
|
2001-11-13 00:28:48 +08:00
|
|
|
CW << " '" << (Value*)I->first << "'\t- Sum=";
|
|
|
|
|
|
|
|
unsigned Sum = 0;
|
|
|
|
for (unsigned i = 0; i < OfC.size(); ++i)
|
|
|
|
Sum += OfC[i];
|
|
|
|
CW << Sum << " - ";
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < OfC.size(); ++i) {
|
|
|
|
if (i) CW << ", ";
|
|
|
|
CW << OfC[i];
|
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
CW << "\n";
|
2001-11-13 00:28:48 +08:00
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
CW << "\n";
|
2001-11-13 04:13:14 +08:00
|
|
|
|
|
|
|
CW << "Profile Field Access Percentages:\n";
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout.precision(3);
|
2001-11-13 04:13:14 +08:00
|
|
|
for (I = FieldAccessCounts.begin(); I != E; ++I) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::vector<unsigned> &OfC = I->second;
|
2001-11-13 04:13:14 +08:00
|
|
|
unsigned Sum = 0;
|
|
|
|
for (unsigned i = 0; i < OfC.size(); ++i)
|
|
|
|
Sum += OfC[i];
|
|
|
|
|
|
|
|
CW << " '" << (Value*)I->first << "'\t- ";
|
|
|
|
for (unsigned i = 0; i < OfC.size(); ++i) {
|
|
|
|
if (i) CW << ", ";
|
|
|
|
CW << double(OfC[i])/Sum;
|
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
CW << "\n";
|
2001-11-13 04:13:14 +08:00
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
CW << "\n";
|
2001-11-13 04:13:14 +08:00
|
|
|
|
2001-11-13 00:28:48 +08:00
|
|
|
FieldAccessCounts.clear();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:15:57 +08:00
|
|
|
void Interpreter::exitCalled(GenericValue GV) {
|
2001-12-15 00:49:29 +08:00
|
|
|
if (!QuietMode) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Program returned ";
|
2001-12-15 00:49:29 +08:00
|
|
|
print(Type::IntTy, GV);
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << " via 'void exit(int)'\n";
|
2001-12-15 00:49:29 +08:00
|
|
|
}
|
2001-10-27 12:15:57 +08:00
|
|
|
|
|
|
|
ExitCode = GV.SByteVal;
|
|
|
|
ECStack.clear();
|
2001-11-13 00:28:48 +08:00
|
|
|
PerformExitStuff();
|
2001-10-27 12:15:57 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
|
2001-08-24 01:05:04 +08:00
|
|
|
const Type *RetTy = 0;
|
|
|
|
GenericValue Result;
|
|
|
|
|
|
|
|
// Save away the return value... (if we are not 'ret void')
|
2002-06-26 00:13:21 +08:00
|
|
|
if (I.getNumOperands()) {
|
|
|
|
RetTy = I.getReturnValue()->getType();
|
|
|
|
Result = getOperandValue(I.getReturnValue(), SF);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save previously executing meth
|
2002-04-08 04:49:59 +08:00
|
|
|
const Function *M = ECStack.back().CurMethod;
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// Pop the current stack frame... this invalidates SF
|
|
|
|
ECStack.pop_back();
|
|
|
|
|
|
|
|
if (ECStack.empty()) { // Finished main. Put result into exit code...
|
|
|
|
if (RetTy) { // Nonvoid return type?
|
2001-12-15 00:49:29 +08:00
|
|
|
if (!QuietMode) {
|
2002-04-08 04:49:59 +08:00
|
|
|
CW << "Function " << M->getType() << " \"" << M->getName()
|
2001-12-15 00:49:29 +08:00
|
|
|
<< "\" returned ";
|
|
|
|
print(RetTy, Result);
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-12-15 00:49:29 +08:00
|
|
|
}
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
if (RetTy->isIntegral())
|
2002-05-03 03:28:45 +08:00
|
|
|
ExitCode = Result.IntVal; // Capture the exit code of the program
|
2001-08-24 01:05:04 +08:00
|
|
|
} else {
|
|
|
|
ExitCode = 0;
|
|
|
|
}
|
2001-11-13 00:19:45 +08:00
|
|
|
|
2001-11-13 00:28:48 +08:00
|
|
|
PerformExitStuff();
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a previous stack frame, and we have a previous call, fill in
|
|
|
|
// the return value...
|
|
|
|
//
|
|
|
|
ExecutionContext &NewSF = ECStack.back();
|
|
|
|
if (NewSF.Caller) {
|
|
|
|
if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
|
|
|
|
SetValue(NewSF.Caller, Result, NewSF);
|
|
|
|
|
|
|
|
NewSF.Caller = 0; // We returned from the call...
|
2001-12-15 00:49:29 +08:00
|
|
|
} else if (!QuietMode) {
|
2001-09-10 12:49:44 +08:00
|
|
|
// This must be a function that is executing because of a user 'call'
|
|
|
|
// instruction.
|
2002-04-08 04:49:59 +08:00
|
|
|
CW << "Function " << M->getType() << " \"" << M->getName()
|
2001-11-07 12:23:00 +08:00
|
|
|
<< "\" returned ";
|
2001-10-15 13:51:48 +08:00
|
|
|
print(RetTy, Result);
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
|
2001-08-24 01:05:04 +08:00
|
|
|
SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
|
|
|
|
BasicBlock *Dest;
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
|
|
|
|
if (!I.isUnconditional()) {
|
|
|
|
Value *Cond = I.getCondition();
|
2001-10-31 04:27:31 +08:00
|
|
|
GenericValue CondVal = getOperandValue(Cond, SF);
|
|
|
|
if (CondVal.BoolVal == 0) // If false cond...
|
2002-06-26 00:13:21 +08:00
|
|
|
Dest = I.getSuccessor(1);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
SF.CurBB = Dest; // Update CurBB to branch destination
|
|
|
|
SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
|
|
|
|
}
|
|
|
|
|
2003-04-23 04:34:47 +08:00
|
|
|
static void executeSwitch(SwitchInst &I, ExecutionContext &SF) {
|
|
|
|
GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
|
|
|
|
const Type *ElTy = I.getOperand(0)->getType();
|
|
|
|
SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
|
|
|
|
BasicBlock *Dest = 0;
|
|
|
|
|
|
|
|
// Check to see if any of the cases match...
|
|
|
|
for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) {
|
|
|
|
if (executeSetEQInst(CondVal,
|
2003-04-23 04:37:39 +08:00
|
|
|
getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
|
2003-04-23 04:34:47 +08:00
|
|
|
Dest = cast<BasicBlock>(I.getOperand(i+1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
|
|
|
|
SF.CurBB = Dest; // Update CurBB to branch destination
|
|
|
|
SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-27 13:16:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Memory Instruction Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
|
|
|
|
const Type *Ty = I.getType()->getElementType(); // Type to be allocated
|
2001-08-27 13:16:50 +08:00
|
|
|
|
2002-04-29 05:57:33 +08:00
|
|
|
// Get the number of elements being allocated by the array...
|
2002-06-26 00:13:21 +08:00
|
|
|
unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
|
2001-08-27 13:16:50 +08:00
|
|
|
|
|
|
|
// Allocate enough memory to hold the type...
|
2001-11-08 03:46:27 +08:00
|
|
|
// FIXME: Don't use CALLOC, use a tainted malloc.
|
2002-02-20 02:50:09 +08:00
|
|
|
void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
GenericValue Result = PTOGV(Memory);
|
2001-11-08 03:46:27 +08:00
|
|
|
assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, Result, SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
if (I.getOpcode() == Instruction::Alloca)
|
2002-02-20 02:50:09 +08:00
|
|
|
ECStack.back().Allocas.add(Memory);
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
|
|
|
|
assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
|
|
|
|
GenericValue Value = getOperandValue(I.getOperand(0), SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
// TODO: Check to make sure memory is allocated
|
2002-12-24 07:59:41 +08:00
|
|
|
free(GVTOP(Value)); // Free memory
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
2001-10-30 03:32:19 +08:00
|
|
|
|
2002-08-28 06:33:45 +08:00
|
|
|
// getElementOffset - The workhorse for getelementptr.
|
2001-10-30 03:32:19 +08:00
|
|
|
//
|
2002-12-24 07:59:41 +08:00
|
|
|
GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
|
|
|
|
User::op_iterator E,
|
|
|
|
ExecutionContext &SF) {
|
2002-08-28 06:33:45 +08:00
|
|
|
assert(isa<PointerType>(Ptr->getType()) &&
|
2001-10-30 03:32:19 +08:00
|
|
|
"Cannot getElementOffset of a nonpointer type!");
|
|
|
|
|
2001-11-08 03:46:27 +08:00
|
|
|
PointerTy Total = 0;
|
2002-08-28 06:33:45 +08:00
|
|
|
const Type *Ty = Ptr->getType();
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
2001-11-27 02:18:18 +08:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
|
|
|
const StructLayout *SLO = TD.getStructLayout(STy);
|
|
|
|
|
|
|
|
// Indicies must be ubyte constants...
|
2002-08-28 06:33:45 +08:00
|
|
|
const ConstantUInt *CPU = cast<ConstantUInt>(*I);
|
2001-11-27 02:18:18 +08:00
|
|
|
assert(CPU->getType() == Type::UByteTy);
|
|
|
|
unsigned Index = CPU->getValue();
|
|
|
|
|
2001-11-13 00:19:45 +08:00
|
|
|
#ifdef PROFILE_STRUCTURE_FIELDS
|
2001-11-27 02:18:18 +08:00
|
|
|
if (ProfileStructureFields) {
|
|
|
|
// Do accounting for this field...
|
2003-04-23 05:22:33 +08:00
|
|
|
std::vector<unsigned> &OfC = FieldAccessCounts[STy];
|
2001-11-27 02:18:18 +08:00
|
|
|
if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
|
|
|
|
OfC[Index]++;
|
|
|
|
}
|
2001-11-13 00:19:45 +08:00
|
|
|
#endif
|
2001-11-27 02:18:18 +08:00
|
|
|
|
|
|
|
Total += SLO->MemberOffsets[Index];
|
|
|
|
Ty = STy->getElementTypes()[Index];
|
2001-12-15 00:49:29 +08:00
|
|
|
} else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
|
2001-11-27 02:18:18 +08:00
|
|
|
|
2003-02-26 05:14:59 +08:00
|
|
|
// Get the index number for the array... which must be long type...
|
2002-09-11 09:21:35 +08:00
|
|
|
assert((*I)->getType() == Type::LongTy);
|
2002-09-14 07:30:42 +08:00
|
|
|
unsigned Idx = getOperandValue(*I, SF).LongVal;
|
2001-12-15 00:49:29 +08:00
|
|
|
if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
|
2002-02-12 04:19:16 +08:00
|
|
|
if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cerr << "Out of range memory access to element #" << Idx
|
|
|
|
<< " of a " << AT->getNumElements() << " element array."
|
|
|
|
<< " Subscript #" << *I << "\n";
|
2001-12-15 00:49:29 +08:00
|
|
|
// Get outta here!!!
|
2002-02-12 23:47:23 +08:00
|
|
|
siglongjmp(SignalRecoverBuffer, SIGTRAP);
|
2001-12-15 00:49:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ty = ST->getElementType();
|
2001-11-27 02:18:18 +08:00
|
|
|
unsigned Size = TD.getTypeSize(Ty);
|
|
|
|
Total += Size*Idx;
|
|
|
|
}
|
2001-10-30 03:32:19 +08:00
|
|
|
}
|
|
|
|
|
2002-08-28 06:33:45 +08:00
|
|
|
GenericValue Result;
|
|
|
|
Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
|
|
|
|
return Result;
|
2001-10-30 03:32:19 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
|
2002-12-24 07:59:41 +08:00
|
|
|
SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
|
2002-08-28 06:33:45 +08:00
|
|
|
I.idx_begin(), I.idx_end(), SF), SF);
|
2001-10-30 03:32:19 +08:00
|
|
|
}
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) {
|
2002-06-26 00:13:21 +08:00
|
|
|
GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
|
2002-12-24 07:59:41 +08:00
|
|
|
GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
|
2001-08-27 13:16:50 +08:00
|
|
|
GenericValue Result;
|
|
|
|
|
2002-10-16 04:34:05 +08:00
|
|
|
if (TD.isLittleEndian()) {
|
|
|
|
switch (I.getType()->getPrimitiveID()) {
|
|
|
|
case Type::BoolTyID:
|
|
|
|
case Type::UByteTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
|
2002-10-16 04:34:05 +08:00
|
|
|
case Type::UShortTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
|
|
|
|
((unsigned)Ptr->Untyped[1] << 8);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::UIntTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
|
|
|
|
((unsigned)Ptr->Untyped[1] << 8) |
|
|
|
|
((unsigned)Ptr->Untyped[2] << 16) |
|
|
|
|
((unsigned)Ptr->Untyped[3] << 24);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
case Type::ULongTyID:
|
|
|
|
case Type::LongTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
|
|
|
|
((uint64_t)Ptr->Untyped[1] << 8) |
|
|
|
|
((uint64_t)Ptr->Untyped[2] << 16) |
|
|
|
|
((uint64_t)Ptr->Untyped[3] << 24) |
|
|
|
|
((uint64_t)Ptr->Untyped[4] << 32) |
|
|
|
|
((uint64_t)Ptr->Untyped[5] << 40) |
|
|
|
|
((uint64_t)Ptr->Untyped[6] << 48) |
|
|
|
|
((uint64_t)Ptr->Untyped[7] << 56);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Cannot load value of type " << *I.getType() << "!\n";
|
|
|
|
abort();
|
2002-10-16 04:34:05 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (I.getType()->getPrimitiveID()) {
|
|
|
|
case Type::BoolTyID:
|
|
|
|
case Type::UByteTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
|
2002-10-16 04:34:05 +08:00
|
|
|
case Type::UShortTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
|
|
|
|
((unsigned)Ptr->Untyped[0] << 8);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::UIntTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
|
|
|
|
((unsigned)Ptr->Untyped[2] << 8) |
|
|
|
|
((unsigned)Ptr->Untyped[1] << 16) |
|
|
|
|
((unsigned)Ptr->Untyped[0] << 24);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
case Type::ULongTyID:
|
|
|
|
case Type::LongTyID:
|
2002-10-31 05:47:57 +08:00
|
|
|
case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
|
|
|
|
((uint64_t)Ptr->Untyped[6] << 8) |
|
|
|
|
((uint64_t)Ptr->Untyped[5] << 16) |
|
|
|
|
((uint64_t)Ptr->Untyped[4] << 24) |
|
|
|
|
((uint64_t)Ptr->Untyped[3] << 32) |
|
|
|
|
((uint64_t)Ptr->Untyped[2] << 40) |
|
|
|
|
((uint64_t)Ptr->Untyped[1] << 48) |
|
|
|
|
((uint64_t)Ptr->Untyped[0] << 56);
|
2002-10-16 04:34:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Cannot load value of type " << *I.getType() << "!\n";
|
|
|
|
abort();
|
2002-10-16 04:34:05 +08:00
|
|
|
}
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, Result, SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) {
|
2002-06-26 00:13:21 +08:00
|
|
|
GenericValue Val = getOperandValue(I.getOperand(0), SF);
|
2002-10-16 04:34:05 +08:00
|
|
|
GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
|
2002-12-24 07:59:41 +08:00
|
|
|
StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
|
2002-10-26 09:57:15 +08:00
|
|
|
I.getOperand(0)->getType());
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-08 03:29:31 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Miscellaneous Instruction Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
|
|
|
|
ECStack.back().Caller = &I;
|
2003-04-23 05:22:33 +08:00
|
|
|
std::vector<GenericValue> ArgVals;
|
2002-06-26 00:13:21 +08:00
|
|
|
ArgVals.reserve(I.getNumOperands()-1);
|
2003-01-13 08:58:52 +08:00
|
|
|
for (unsigned i = 1; i < I.getNumOperands(); ++i) {
|
2002-06-26 00:13:21 +08:00
|
|
|
ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
|
2003-01-13 08:58:52 +08:00
|
|
|
// Promote all integral types whose size is < sizeof(int) into ints. We do
|
|
|
|
// this by zero or sign extending the value as appropriate according to the
|
|
|
|
// source type.
|
|
|
|
if (I.getOperand(i)->getType()->isIntegral() &&
|
|
|
|
I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
|
|
|
|
const Type *Ty = I.getOperand(i)->getType();
|
|
|
|
if (Ty == Type::ShortTy)
|
|
|
|
ArgVals.back().IntVal = ArgVals.back().ShortVal;
|
|
|
|
else if (Ty == Type::UShortTy)
|
|
|
|
ArgVals.back().UIntVal = ArgVals.back().UShortVal;
|
|
|
|
else if (Ty == Type::SByteTy)
|
|
|
|
ArgVals.back().IntVal = ArgVals.back().SByteVal;
|
|
|
|
else if (Ty == Type::UByteTy)
|
|
|
|
ArgVals.back().UIntVal = ArgVals.back().UByteVal;
|
|
|
|
else if (Ty == Type::BoolTy)
|
|
|
|
ArgVals.back().UIntVal = ArgVals.back().BoolVal;
|
|
|
|
else
|
|
|
|
assert(0 && "Unknown type!");
|
|
|
|
}
|
|
|
|
}
|
2001-09-10 12:49:44 +08:00
|
|
|
|
2001-11-08 04:12:30 +08:00
|
|
|
// To handle indirect calls, we must get the pointer value from the argument
|
2002-04-08 04:49:59 +08:00
|
|
|
// and treat it as a function pointer.
|
2002-06-26 00:13:21 +08:00
|
|
|
GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
|
2001-11-08 04:12:30 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
callMethod((Function*)GVTOP(SRC), ArgVals);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executePHINode(PHINode &I, ExecutionContext &SF) {
|
2001-08-24 01:05:04 +08:00
|
|
|
BasicBlock *PrevBB = SF.PrevBB;
|
|
|
|
Value *IncomingValue = 0;
|
|
|
|
|
|
|
|
// Search for the value corresponding to this previous bb...
|
2002-06-26 00:13:21 +08:00
|
|
|
for (unsigned i = I.getNumIncomingValues(); i > 0;) {
|
|
|
|
if (I.getIncomingBlock(--i) == PrevBB) {
|
|
|
|
IncomingValue = I.getIncomingValue(i);
|
2001-08-24 01:05:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
|
|
|
|
|
|
|
|
// Found the value, set as the result...
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, getOperandValue(IncomingValue, SF), SF);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
2001-08-27 13:16:50 +08:00
|
|
|
#define IMPLEMENT_SHIFT(OP, TY) \
|
|
|
|
case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
|
|
|
|
const Type *Ty = I.getOperand(0)->getType();
|
|
|
|
GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
|
|
|
|
GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SHIFT(<<, UByte);
|
|
|
|
IMPLEMENT_SHIFT(<<, SByte);
|
|
|
|
IMPLEMENT_SHIFT(<<, UShort);
|
|
|
|
IMPLEMENT_SHIFT(<<, Short);
|
|
|
|
IMPLEMENT_SHIFT(<<, UInt);
|
|
|
|
IMPLEMENT_SHIFT(<<, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SHIFT(<<, ULong);
|
|
|
|
IMPLEMENT_SHIFT(<<, Long);
|
2001-08-27 13:16:50 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, Dest, SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
|
|
|
|
const Type *Ty = I.getOperand(0)->getType();
|
|
|
|
GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
|
|
|
|
GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
GenericValue Dest;
|
|
|
|
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
IMPLEMENT_SHIFT(>>, UByte);
|
|
|
|
IMPLEMENT_SHIFT(>>, SByte);
|
|
|
|
IMPLEMENT_SHIFT(>>, UShort);
|
|
|
|
IMPLEMENT_SHIFT(>>, Short);
|
|
|
|
IMPLEMENT_SHIFT(>>, UInt);
|
|
|
|
IMPLEMENT_SHIFT(>>, Int);
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_SHIFT(>>, ULong);
|
|
|
|
IMPLEMENT_SHIFT(>>, Long);
|
2001-08-27 13:16:50 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
|
|
|
|
abort();
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
2002-06-26 00:13:21 +08:00
|
|
|
SetValue(&I, Dest, SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define IMPLEMENT_CAST(DTY, DCTY, STY) \
|
2001-11-08 03:46:27 +08:00
|
|
|
case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
|
2001-08-27 13:16:50 +08:00
|
|
|
|
|
|
|
#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
|
|
|
|
case Type::DESTTY##TyID: \
|
|
|
|
switch (SrcTy->getPrimitiveID()) { \
|
2002-05-03 03:28:45 +08:00
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
|
2001-08-27 13:16:50 +08:00
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
|
2001-10-16 03:18:26 +08:00
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
|
2001-10-27 16:28:11 +08:00
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
|
2001-08-27 13:16:50 +08:00
|
|
|
|
|
|
|
#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
|
|
|
|
IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
|
|
|
|
|
|
|
|
#define IMPLEMENT_CAST_CASE_END() \
|
2003-04-23 05:22:33 +08:00
|
|
|
default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
|
|
|
|
abort(); \
|
2001-08-27 13:16:50 +08:00
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
|
|
|
#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
|
|
|
|
IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
|
|
|
|
IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
|
|
|
|
IMPLEMENT_CAST_CASE_END()
|
|
|
|
|
2002-08-28 06:33:45 +08:00
|
|
|
static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
|
|
|
|
ExecutionContext &SF) {
|
|
|
|
const Type *SrcTy = SrcVal->getType();
|
|
|
|
GenericValue Dest, Src = getOperandValue(SrcVal, SF);
|
2001-08-27 13:16:50 +08:00
|
|
|
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
2001-11-08 03:46:27 +08:00
|
|
|
IMPLEMENT_CAST_CASE(UByte , (unsigned char));
|
|
|
|
IMPLEMENT_CAST_CASE(SByte , ( signed char));
|
|
|
|
IMPLEMENT_CAST_CASE(UShort , (unsigned short));
|
2002-08-03 06:06:04 +08:00
|
|
|
IMPLEMENT_CAST_CASE(Short , ( signed short));
|
2001-11-08 03:46:27 +08:00
|
|
|
IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
|
|
|
|
IMPLEMENT_CAST_CASE(Int , ( signed int ));
|
|
|
|
IMPLEMENT_CAST_CASE(ULong , (uint64_t));
|
|
|
|
IMPLEMENT_CAST_CASE(Long , ( int64_t));
|
2002-10-31 05:47:57 +08:00
|
|
|
IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
|
2001-11-08 03:46:27 +08:00
|
|
|
IMPLEMENT_CAST_CASE(Float , (float));
|
|
|
|
IMPLEMENT_CAST_CASE(Double , (double));
|
2003-04-23 05:15:56 +08:00
|
|
|
IMPLEMENT_CAST_CASE(Bool , (bool));
|
2001-08-27 13:16:50 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
|
2003-04-23 05:15:56 +08:00
|
|
|
abort();
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
2002-08-28 06:33:45 +08:00
|
|
|
|
|
|
|
return Dest;
|
2001-08-27 13:16:50 +08:00
|
|
|
}
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
|
2002-08-28 06:33:45 +08:00
|
|
|
static void executeCastInst(CastInst &I, ExecutionContext &SF) {
|
|
|
|
SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
|
|
|
|
}
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dispatch and Execution Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
|
2002-04-08 04:49:59 +08:00
|
|
|
// Assign slot numbers to the function arguments...
|
2002-06-26 00:13:21 +08:00
|
|
|
for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
|
|
|
|
AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// Iterate over all of the instructions...
|
|
|
|
unsigned InstNum = 0;
|
2002-06-26 00:13:21 +08:00
|
|
|
for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
|
|
|
|
// For each instruction... Add Annote
|
|
|
|
II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MethodInfo::getValueSlot(const Value *V) {
|
|
|
|
unsigned Plane = V->getType()->getUniqueID();
|
|
|
|
if (Plane >= NumPlaneElements.size())
|
|
|
|
NumPlaneElements.resize(Plane+1, 0);
|
|
|
|
return NumPlaneElements[Plane]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-08 04:49:59 +08:00
|
|
|
// callMethod - Execute the specified function...
|
2001-08-24 01:05:04 +08:00
|
|
|
//
|
2003-04-23 05:22:33 +08:00
|
|
|
void Interpreter::callMethod(Function *M,
|
|
|
|
const std::vector<GenericValue> &ArgVals) {
|
2001-09-10 12:49:44 +08:00
|
|
|
assert((ECStack.empty() || ECStack.back().Caller == 0 ||
|
|
|
|
ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
|
|
|
|
"Incorrect number of arguments passed into function call!");
|
2001-08-24 01:05:04 +08:00
|
|
|
if (M->isExternal()) {
|
2001-10-31 04:27:31 +08:00
|
|
|
GenericValue Result = callExternalMethod(M, ArgVals);
|
|
|
|
const Type *RetTy = M->getReturnType();
|
|
|
|
|
|
|
|
// Copy the result back into the result variable if we are not returning
|
|
|
|
// void.
|
|
|
|
if (RetTy != Type::VoidTy) {
|
|
|
|
if (!ECStack.empty() && ECStack.back().Caller) {
|
|
|
|
ExecutionContext &SF = ECStack.back();
|
|
|
|
SetValue(SF.Caller, Result, SF);
|
|
|
|
|
|
|
|
SF.Caller = 0; // We returned from the call...
|
2001-12-15 00:49:29 +08:00
|
|
|
} else if (!QuietMode) {
|
2001-10-31 04:27:31 +08:00
|
|
|
// print it.
|
2002-04-08 04:49:59 +08:00
|
|
|
CW << "Function " << M->getType() << " \"" << M->getName()
|
2001-11-07 12:23:00 +08:00
|
|
|
<< "\" returned ";
|
2001-10-31 04:27:31 +08:00
|
|
|
print(RetTy, Result);
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-10-31 04:27:31 +08:00
|
|
|
|
|
|
|
if (RetTy->isIntegral())
|
2002-09-03 09:08:28 +08:00
|
|
|
ExitCode = Result.IntVal; // Capture the exit code of the program
|
2001-10-31 04:27:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-08 04:49:59 +08:00
|
|
|
// Process the function, assigning instruction numbers to the instructions in
|
|
|
|
// the function. Also calculate the number of values for each type slot
|
|
|
|
// active.
|
2001-08-24 01:05:04 +08:00
|
|
|
//
|
|
|
|
MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
|
|
|
|
ECStack.push_back(ExecutionContext()); // Make a new stack frame...
|
2001-08-27 13:16:50 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
|
|
|
|
StackFrame.CurMethod = M;
|
2002-06-26 00:13:21 +08:00
|
|
|
StackFrame.CurBB = M->begin();
|
2001-08-24 01:05:04 +08:00
|
|
|
StackFrame.CurInst = StackFrame.CurBB->begin();
|
|
|
|
StackFrame.MethInfo = MethInfo;
|
|
|
|
|
|
|
|
// Initialize the values to nothing...
|
|
|
|
StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
|
2001-11-08 03:46:27 +08:00
|
|
|
for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
|
2001-08-24 01:05:04 +08:00
|
|
|
StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
|
|
|
|
|
2001-11-08 03:46:27 +08:00
|
|
|
// Taint the initial values of stuff
|
|
|
|
memset(&StackFrame.Values[i][0], 42,
|
|
|
|
MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
|
|
|
|
}
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
|
|
|
|
|
2001-09-10 12:49:44 +08:00
|
|
|
|
2002-04-08 04:49:59 +08:00
|
|
|
// Run through the function arguments and initialize their values...
|
2002-06-26 00:13:21 +08:00
|
|
|
assert(ArgVals.size() == M->asize() &&
|
2002-04-08 04:49:59 +08:00
|
|
|
"Invalid number of values passed to function invocation!");
|
2001-09-10 12:49:44 +08:00
|
|
|
unsigned i = 0;
|
2002-06-26 00:13:21 +08:00
|
|
|
for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
|
|
|
|
SetValue(AI, ArgVals[i], StackFrame);
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// executeInstruction - Interpret a single instruction, increment the "PC", and
|
|
|
|
// return true if the next instruction is a breakpoint...
|
|
|
|
//
|
|
|
|
bool Interpreter::executeInstruction() {
|
|
|
|
assert(!ECStack.empty() && "No program running, cannot execute inst!");
|
|
|
|
|
|
|
|
ExecutionContext &SF = ECStack.back(); // Current stack frame
|
2002-06-26 00:13:21 +08:00
|
|
|
Instruction &I = *SF.CurInst++; // Increment before execute
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2001-10-27 16:43:52 +08:00
|
|
|
if (Trace)
|
2001-11-07 12:23:00 +08:00
|
|
|
CW << "Run:" << I;
|
|
|
|
|
2002-12-08 13:51:08 +08:00
|
|
|
// Track the number of dynamic instructions executed.
|
|
|
|
++NumDynamicInsts;
|
|
|
|
|
2001-11-07 12:23:00 +08:00
|
|
|
// Set a sigsetjmp buffer so that we can recover if an error happens during
|
|
|
|
// instruction execution...
|
|
|
|
//
|
|
|
|
if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
|
|
|
|
--SF.CurInst; // Back up to erroring instruction
|
2002-02-12 23:47:23 +08:00
|
|
|
if (SigNo != SIGINT) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
|
2001-11-07 13:31:27 +08:00
|
|
|
printStackTrace();
|
2002-02-12 23:47:23 +08:00
|
|
|
// If -abort-on-exception was specified, terminate LLI instead of trying
|
|
|
|
// to debug it.
|
|
|
|
//
|
|
|
|
if (AbortOnExceptions) exit(1);
|
2001-11-27 02:18:18 +08:00
|
|
|
} else if (SigNo == SIGINT) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "CTRL-C Detected, execution halted.\n";
|
2001-11-07 13:31:27 +08:00
|
|
|
}
|
|
|
|
InInstruction = false;
|
2001-11-07 12:23:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2001-10-27 16:43:52 +08:00
|
|
|
|
2001-11-07 13:31:27 +08:00
|
|
|
InInstruction = true;
|
2002-06-26 00:13:21 +08:00
|
|
|
if (I.isBinaryOp()) {
|
2001-10-31 04:27:31 +08:00
|
|
|
executeBinaryInst(cast<BinaryOperator>(I), SF);
|
2001-08-24 01:05:04 +08:00
|
|
|
} else {
|
2002-06-26 00:13:21 +08:00
|
|
|
switch (I.getOpcode()) {
|
2001-08-27 13:16:50 +08:00
|
|
|
// Terminators
|
2001-10-31 04:27:31 +08:00
|
|
|
case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
|
|
|
|
case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
|
2003-04-23 04:34:47 +08:00
|
|
|
case Instruction::Switch: executeSwitch (cast<SwitchInst>(I), SF); break;
|
2001-08-27 13:16:50 +08:00
|
|
|
// Memory Instructions
|
|
|
|
case Instruction::Alloca:
|
2002-06-26 00:13:21 +08:00
|
|
|
case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
|
2001-10-31 04:27:31 +08:00
|
|
|
case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
|
|
|
|
case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
|
|
|
|
case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
|
2001-10-30 03:32:19 +08:00
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
|
2001-08-27 13:16:50 +08:00
|
|
|
|
|
|
|
// Miscellaneous Instructions
|
2001-10-31 04:27:31 +08:00
|
|
|
case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
|
|
|
|
case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
|
|
|
|
case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
|
|
|
|
case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
|
|
|
|
case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Don't know how to execute this instruction!\n-->" << I;
|
|
|
|
abort();
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
2001-11-07 13:31:27 +08:00
|
|
|
InInstruction = false;
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// Reset the current frame location to the top of stack
|
|
|
|
CurFrame = ECStack.size()-1;
|
|
|
|
|
|
|
|
if (CurFrame == -1) return false; // No breakpoint if no code
|
|
|
|
|
|
|
|
// Return true if there is a breakpoint annotation on the instruction...
|
2002-06-26 00:13:21 +08:00
|
|
|
return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::stepInstruction() { // Do the 'step' command
|
|
|
|
if (ECStack.empty()) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Error: no program running, cannot step!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run an instruction...
|
|
|
|
executeInstruction();
|
|
|
|
|
|
|
|
// Print the next instruction to execute...
|
|
|
|
printCurrentInstruction();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- UI Stuff...
|
|
|
|
void Interpreter::nextInstruction() { // Do the 'next' command
|
|
|
|
if (ECStack.empty()) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Error: no program running, cannot 'next'!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a call instruction, step over the call instruction...
|
|
|
|
// TODO: ICALL, CALL WITH, ...
|
2002-06-26 00:13:21 +08:00
|
|
|
if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
|
2001-10-29 22:08:33 +08:00
|
|
|
unsigned StackSize = ECStack.size();
|
2001-08-24 01:05:04 +08:00
|
|
|
// Step into the function...
|
|
|
|
if (executeInstruction()) {
|
|
|
|
// Hit a breakpoint, print current instruction, then return to user...
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Breakpoint hit!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
printCurrentInstruction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-29 22:08:33 +08:00
|
|
|
// If we we able to step into the function, finish it now. We might not be
|
|
|
|
// able the step into a function, if it's external for example.
|
|
|
|
if (ECStack.size() != StackSize)
|
|
|
|
finish(); // Finish executing the function...
|
2001-10-30 00:05:19 +08:00
|
|
|
else
|
|
|
|
printCurrentInstruction();
|
2001-10-29 22:08:33 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
} else {
|
|
|
|
// Normal instruction, just step...
|
|
|
|
stepInstruction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::run() {
|
|
|
|
if (ECStack.empty()) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Error: no program running, cannot run!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HitBreakpoint = false;
|
|
|
|
while (!ECStack.empty() && !HitBreakpoint) {
|
|
|
|
// Run an instruction...
|
|
|
|
HitBreakpoint = executeInstruction();
|
|
|
|
}
|
|
|
|
|
2003-04-23 05:22:33 +08:00
|
|
|
if (HitBreakpoint)
|
|
|
|
std::cout << "Breakpoint hit!\n";
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
// Print the next instruction to execute...
|
|
|
|
printCurrentInstruction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::finish() {
|
|
|
|
if (ECStack.empty()) {
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Error: no program running, cannot run!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned StackSize = ECStack.size();
|
|
|
|
bool HitBreakpoint = false;
|
|
|
|
while (ECStack.size() >= StackSize && !HitBreakpoint) {
|
|
|
|
// Run an instruction...
|
|
|
|
HitBreakpoint = executeInstruction();
|
|
|
|
}
|
|
|
|
|
2003-04-23 05:22:33 +08:00
|
|
|
if (HitBreakpoint)
|
|
|
|
std::cout << "Breakpoint hit!\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// Print the next instruction to execute...
|
|
|
|
printCurrentInstruction();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// printCurrentInstruction - Print out the instruction that the virtual PC is
|
|
|
|
// at, or fail silently if no program is running.
|
|
|
|
//
|
|
|
|
void Interpreter::printCurrentInstruction() {
|
|
|
|
if (!ECStack.empty()) {
|
2001-10-30 04:44:34 +08:00
|
|
|
if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
|
2003-04-23 05:22:33 +08:00
|
|
|
WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
|
2001-10-30 04:44:34 +08:00
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
Instruction &I = *ECStack.back().CurInst;
|
|
|
|
InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
|
2001-08-24 01:05:04 +08:00
|
|
|
assert(IN && "Instruction has no numbering annotation!");
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "#" << IN->InstNum << I;
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::printValue(const Type *Ty, GenericValue V) {
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
2003-04-23 05:22:33 +08:00
|
|
|
case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
|
2002-08-14 04:45:11 +08:00
|
|
|
case Type::SByteTyID:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
|
2002-08-14 04:45:11 +08:00
|
|
|
case Type::UByteTyID:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
|
|
|
|
case Type::ShortTyID: std::cout << V.ShortVal; break;
|
|
|
|
case Type::UShortTyID: std::cout << V.UShortVal; break;
|
|
|
|
case Type::IntTyID: std::cout << V.IntVal; break;
|
|
|
|
case Type::UIntTyID: std::cout << V.UIntVal; break;
|
|
|
|
case Type::LongTyID: std::cout << (long)V.LongVal; break;
|
|
|
|
case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
|
|
|
|
case Type::FloatTyID: std::cout << V.FloatVal; break;
|
|
|
|
case Type::DoubleTyID: std::cout << V.DoubleVal; break;
|
|
|
|
case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
|
2001-08-24 01:05:04 +08:00
|
|
|
default:
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "- Don't know how to print value of this type!";
|
2001-08-24 01:05:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-15 13:51:48 +08:00
|
|
|
void Interpreter::print(const Type *Ty, GenericValue V) {
|
2001-11-07 12:23:00 +08:00
|
|
|
CW << Ty << " ";
|
2001-10-15 13:51:48 +08:00
|
|
|
printValue(Ty, V);
|
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
void Interpreter::print(const std::string &Name) {
|
2001-08-24 01:05:04 +08:00
|
|
|
Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
|
|
|
|
if (!PickedVal) return;
|
|
|
|
|
2002-04-08 04:49:59 +08:00
|
|
|
if (const Function *F = dyn_cast<const Function>(PickedVal)) {
|
|
|
|
CW << F; // Print the function
|
2001-11-08 03:46:27 +08:00
|
|
|
} else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
|
2002-01-21 06:54:45 +08:00
|
|
|
CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
|
2001-11-08 03:46:27 +08:00
|
|
|
} else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
|
|
|
|
CW << BB; // Print the basic block
|
2001-08-24 01:05:04 +08:00
|
|
|
} else { // Otherwise there should be an annotation for the slot#
|
2001-10-15 13:51:48 +08:00
|
|
|
print(PickedVal->getType(),
|
|
|
|
getOperandValue(PickedVal, ECStack[CurFrame]));
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
void Interpreter::infoValue(const std::string &Name) {
|
2001-08-27 13:16:50 +08:00
|
|
|
Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
|
|
|
|
if (!PickedVal) return;
|
|
|
|
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "Value: ";
|
2001-10-15 13:51:48 +08:00
|
|
|
print(PickedVal->getType(),
|
|
|
|
getOperandValue(PickedVal, ECStack[CurFrame]));
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << "\n";
|
2001-08-27 13:16:50 +08:00
|
|
|
printOperandInfo(PickedVal, ECStack[CurFrame]);
|
|
|
|
}
|
|
|
|
|
2001-11-07 13:31:27 +08:00
|
|
|
// printStackFrame - Print information about the specified stack frame, or -1
|
|
|
|
// for the default one.
|
|
|
|
//
|
2002-07-26 01:37:05 +08:00
|
|
|
void Interpreter::printStackFrame(int FrameNo) {
|
2001-11-07 13:31:27 +08:00
|
|
|
if (FrameNo == -1) FrameNo = CurFrame;
|
2002-06-26 00:13:21 +08:00
|
|
|
Function *F = ECStack[FrameNo].CurMethod;
|
|
|
|
const Type *RetTy = F->getReturnType();
|
2001-11-08 03:46:27 +08:00
|
|
|
|
|
|
|
CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
|
2002-06-26 00:13:21 +08:00
|
|
|
<< (Value*)RetTy << " \"" << F->getName() << "\"(";
|
2001-11-07 13:31:27 +08:00
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
|
2003-04-23 05:22:33 +08:00
|
|
|
if (i != 0) std::cout << ", ";
|
2002-06-26 00:13:21 +08:00
|
|
|
CW << *I << "=";
|
2001-11-07 13:31:27 +08:00
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
2001-11-07 13:31:27 +08:00
|
|
|
|
2003-04-23 05:22:33 +08:00
|
|
|
std::cout << ")\n";
|
2002-06-26 00:13:21 +08:00
|
|
|
|
|
|
|
if (FrameNo != int(ECStack.size()-1)) {
|
|
|
|
BasicBlock::iterator I = ECStack[FrameNo].CurInst;
|
|
|
|
CW << --I;
|
|
|
|
} else {
|
|
|
|
CW << *ECStack[FrameNo].CurInst;
|
|
|
|
}
|
2001-08-24 01:05:04 +08:00
|
|
|
}
|
2001-11-07 13:31:27 +08:00
|
|
|
|