Commit 14d377b9 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Fix a PHI-handling bug in the interpreter

parent d5fe4b7b
......@@ -337,13 +337,18 @@ static void emitBBs(IRGenState* irstate, const char* bb_type, GuardList& out_gua
arg_num++;
if (arg_num < 3) {
from_arg = func_args[arg_num];
assert(from_arg->getType() == p.second->llvmType());
} else {
ASSERT(func_args.size() == 4, "%ld", func_args.size());
llvm::Value* ptr = entry_emitter->getBuilder()->CreateConstGEP1_32(func_args[3], arg_num - 3);
if (p.second == INT) {
ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.i64->getPointerTo());
} else if (p.second == BOOL) {
ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.i1->getPointerTo());
} else if (p.second == FLOAT) {
ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.double_->getPointerTo());
} else {
assert(p.second->llvmType() == g.llvm_value_type_ptr);
}
from_arg = entry_emitter->getBuilder()->CreateLoad(ptr);
assert(from_arg->getType() == p.second->llvmType());
......
......@@ -1823,7 +1823,7 @@ private:
} else {
llvm::Value* ptr = emitter.getBuilder()->CreateConstGEP1_32(arg_array, arg_num - 3);
if (var->getType() == INT) {
if (var->getType() == INT || var->getType() == BOOL) {
val = emitter.getBuilder()->CreateIntToPtr(val, g.llvm_value_type_ptr);
} else if (var->getType() == FLOAT) {
// val = emitter.getBuilder()->CreateBitCast(val, g.llvm_value_type_ptr);
......
......@@ -102,6 +102,12 @@ Val fetch(llvm::Value* v, const llvm::DataLayout& dl, const SymMap& symbols) {
case llvm::Value::ConstantExprVal: {
llvm::ConstantExpr* ce = llvm::cast<llvm::ConstantExpr>(v);
if (ce->isCast()) {
if (ce->getOpcode() == llvm::Instruction::IntToPtr && ce->getOperand(0)->getType() == g.i1) {
// inttoptr is specified to zero-extend
Val o = fetch(ce->getOperand(0), dl, symbols);
return o.n & 0x1;
}
assert(width(ce->getOperand(0), dl) == 8 && width(ce, dl) == 8);
Val o = fetch(ce->getOperand(0), dl, symbols);
......@@ -305,6 +311,14 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
llvm::BasicBlock* prevblock = NULL;
llvm::BasicBlock* curblock = &f->getEntryBlock();
// The symbol table at the end of the previous BB
// This is important for the following case:
// %a = phi [0, %l1], [1, %l2]
// %b = phi [0, %l1], [%a, %l2]
// The reference to %a in the definition of %b resolves to the *previous* value of %a,
// not the value of %a that we just set in the phi.
SymMap prev_symbols;
struct {
Box* exc_obj;
int64_t exc_selector;
......@@ -516,8 +530,12 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
SET(fetch(bc->getOperand(0), dl, symbols));
continue;
} else if (llvm::IntToPtrInst* bc = llvm::dyn_cast<llvm::IntToPtrInst>(inst)) {
if (bc->getOperand(0)->getType() == g.i1) {
SET(fetch(bc->getOperand(0), dl, symbols).n & 0xff);
} else {
assert(width(bc->getOperand(0), dl) == 8);
SET(fetch(bc->getOperand(0), dl, symbols));
}
continue;
//} else if (llvm::CallInst* ci = llvm::dyn_cast<llvm::CallInst>(inst)) {
} else if (llvm::isa<llvm::CallInst>(inst) || llvm::isa<llvm::InvokeInst>(inst)) {
......@@ -662,6 +680,7 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
if (invoke != nullptr) {
prevblock = curblock;
curblock = invoke->getNormalDest();
prev_symbols = symbols;
}
} catch (Box* e) {
if (VERBOSITY("interpreter") >= 2) {
......@@ -673,6 +692,7 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
prevblock = curblock;
curblock = invoke->getUnwindDest();
prev_symbols = symbols;
landingpad_value.exc_obj = e;
landingpad_value.exc_selector
......@@ -695,7 +715,7 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
continue;
} else if (llvm::PHINode* phi = llvm::dyn_cast<llvm::PHINode>(inst)) {
assert(prevblock);
SET(fetch(phi->getIncomingValueForBlock(prevblock), dl, symbols));
SET(fetch(phi->getIncomingValueForBlock(prevblock), dl, prev_symbols));
continue;
} else if (llvm::BranchInst* br = llvm::dyn_cast<llvm::BranchInst>(inst)) {
prevblock = curblock;
......@@ -709,6 +729,7 @@ Box* interpretFunction(llvm::Function* f, int nargs, Box* closure, Box* arg1, Bo
} else {
curblock = br->getSuccessor(0);
}
prev_symbols = symbols;
// if (VERBOSITY()) {
// printf("jumped to %s\n", curblock->getName().data());
//}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment