fix bug in emit GETTABLE_S and exerimental version of ARITH

gccjit-ravi534
Dibyendu Majumdar 7 years ago
parent d952284929
commit e8d109dacb

@ -1,4 +1,6 @@
mkdir llvm64
cd llvm64
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -G "Visual Studio 14 Win64" -DLLVM_JIT=ON -DLLVM_DIR=c:\LLVM37\share\llvm\cmake ..
rem pre LLVM 3.9
rem cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -G "Visual Studio 14 Win64" -DLLVM_JIT=ON -DLLVM_DIR=c:\LLVM37\share\llvm\cmake ..
cmake -DCMAKE_INSTALL_PREFIX=c:\ravi -G "Visual Studio 14 Win64" -DLLVM_JIT=ON -DLLVM_DIR=c:\d\LLVM39_64\lib\cmake\llvm ..
cd ..

@ -1015,6 +1015,10 @@ class RaviCodeGenerator {
void emit_ARITH(RaviFunctionDef *def, int A, int B, int C, OpCode op, TMS tms,
int pc);
void emit_ARITH_new(RaviFunctionDef *def, int A, int B, int C, OpCode op, TMS tms,
int pc);
void emit_MOD(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_IDIV(RaviFunctionDef *def, int A, int B, int C, int pc);

@ -252,6 +252,286 @@ void RaviCodeGenerator::emit_ARITH(RaviFunctionDef *def, int A, int B, int C,
#endif
}
// OP_ADD, OP_SUB, OP_MUL and OP_DIV
void RaviCodeGenerator::emit_ARITH_new(RaviFunctionDef *def, int A, int B, int C,
OpCode op, TMS tms, int pc) {
//TValue *rb = RKB(i);
//TValue *rc = RKC(i);
//lua_Number nb; lua_Number nc;
//if (ttisfloat(rb)) {
// if (ttisfloat(rc)) {
// nb = fltvalue(rb); nc = fltvalue(rc);
// setfltvalue(ra, luai_numadd(L, nb, nc));
// }
// else if (ttisinteger(rc)) {
// nb = fltvalue(rb); nc = cast_num(ivalue(rc));
// setfltvalue(ra, luai_numadd(L, nb, nc));
// }
// else slowpath
//}
//else if (ttisinteger(rb)) {
// if (ttisfloat(rc)) {
// nb = cast_num(ivalue(rb)); nc = fltvalue(rc);
// setfltvalue(ra, luai_numadd(L, nb, nc));
// }
// else if (ttisinteger(rc)) {
// lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
// setivalue(ra, intop(+, ib, ic));
// }
// else slowpath
//else slowpath
bool traced = emit_debug_trace(def, op, pc);
emit_load_base(def);
llvm::Value *ra = emit_gep_register(def, A);
llvm::Value *rb = emit_gep_register_or_constant(def, B);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
llvm::Value *rb_type = emit_load_type(def, rb);
llvm::Value *rc_type = emit_load_type(def, rc);
// PART A - Is RB floating?
llvm::Value *rb_is_float =
emit_is_value_of_type(def, rb_type, LUA__TNUMFLT, "rb.is.float");
llvm::BasicBlock *done =
llvm::BasicBlock::Create(def->jitState->context(), "done");
llvm::BasicBlock *slowpath =
llvm::BasicBlock::Create(def->jitState->context(), "slowpath");
llvm::BasicBlock *check_rc_is_float =
llvm::BasicBlock::Create(def->jitState->context(), "check.rc.is.float");
llvm::BasicBlock *check_rb_is_int =
llvm::BasicBlock::Create(def->jitState->context(), "check.rb.is.int");
// If RB is floating, check RC else check RB is int
def->builder->CreateCondBr(rb_is_float, check_rc_is_float, check_rb_is_int);
// Check if rc is float
def->f->getBasicBlockList().push_back(check_rc_is_float);
def->builder->SetInsertPoint(check_rc_is_float);
llvm::Value *rc_is_float =
emit_is_value_of_type(def, rc_type, LUA__TNUMFLT, "rb.float.is.rc.float");
llvm::BasicBlock *float_op =
llvm::BasicBlock::Create(def->jitState->context(), "float.op");
llvm::BasicBlock *rb_float_check_rc_is_int =
llvm::BasicBlock::Create(def->jitState->context(), "rb.float.check.rc.int");
// RB is floating - so check if RC is floating
def->builder->CreateCondBr(rc_is_float, float_op, rb_float_check_rc_is_int);
// Both rb and rc are floats
def->f->getBasicBlockList().push_back(float_op);
def->builder->SetInsertPoint(float_op);
llvm::Instruction *lhs = emit_load_reg_n(def, rb);
llvm::Instruction *rhs = emit_load_reg_n(def, rc);
llvm::Value *result = nullptr;
// Add and set RA
switch (op) {
case OP_ADD:
result = def->builder->CreateFAdd(lhs, rhs);
break;
case OP_SUB:
result = def->builder->CreateFSub(lhs, rhs);
break;
case OP_MUL:
result = def->builder->CreateFMul(lhs, rhs);
break;
case OP_DIV:
result = def->builder->CreateFDiv(lhs, rhs);
break;
default:
lua_assert(0);
}
emit_store_reg_n_withtype(def, result, ra);
// We are done
def->builder->CreateBr(done);
// RB is float - but RC is not, so check if RC is int
def->f->getBasicBlockList().push_back(rb_float_check_rc_is_int);
def->builder->SetInsertPoint(rb_float_check_rc_is_int);
llvm::Value *rb_float_is_rc_int =
emit_is_value_of_type(def, rc_type, LUA__TNUMINT, "rb.float.is.rc.int");
llvm::BasicBlock *float_int_op =
llvm::BasicBlock::Create(def->jitState->context(), "float.int.op");
// RB is floating, check if RC in int
def->builder->CreateCondBr(rb_float_is_rc_int, float_int_op, slowpath);
// RB is float - but RC is int
def->f->getBasicBlockList().push_back(float_int_op);
def->builder->SetInsertPoint(float_int_op);
lhs = emit_load_reg_n(def, rb);
rhs = emit_load_reg_i(def, rc);
result = nullptr;
// Add and set RA
switch (op) {
case OP_ADD:
result = def->builder->CreateFAdd(lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
break;
case OP_SUB:
result = def->builder->CreateFSub(lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
break;
case OP_MUL:
result = def->builder->CreateFMul(lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
break;
case OP_DIV:
result = def->builder->CreateFDiv(lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
break;
default:
lua_assert(0);
}
emit_store_reg_n_withtype(def, result, ra);
// We are done
def->builder->CreateBr(done);
// PART B - This section starts by checking whether RB is integer
def->f->getBasicBlockList().push_back(check_rb_is_int);
def->builder->SetInsertPoint(check_rb_is_int);
llvm::Value *rb_is_int =
emit_is_value_of_type(def, rb_type, LUA__TNUMINT, "rb.is.integer");
llvm::BasicBlock *rb_int_check_rc_int = llvm::BasicBlock::Create(
def->jitState->context(), "rb.int.check.rc.int");
llvm::BasicBlock *rb_int_check_rc_float =
llvm::BasicBlock::Create(def->jitState->context(), "rb.int.check.rc.float");
// Check if RB is INT
def->builder->CreateCondBr(rb_is_int, rb_int_check_rc_int, slowpath);
// RB is int, check if RC is also int
def->f->getBasicBlockList().push_back(rb_int_check_rc_int);
def->builder->SetInsertPoint(rb_int_check_rc_int);
llvm::Value *rb_int_is_rc_int =
emit_is_value_of_type(def, rc_type, LUA__TNUMINT, "rb.int.is.rc.int");
llvm::BasicBlock *int_int_op = llvm::BasicBlock::Create(
def->jitState->context(), "int.int.op");
// Check if RB is INT
def->builder->CreateCondBr(rb_int_is_rc_int, int_int_op, rb_int_check_rc_float);
// RB is int, RC int
def->f->getBasicBlockList().push_back(int_int_op);
def->builder->SetInsertPoint(int_int_op);
// Both are integers
lhs = emit_load_reg_i(def, rb);
rhs = emit_load_reg_i(def, rc);
result = nullptr;
switch (op) {
case OP_ADD:
result = def->builder->CreateAdd(lhs, rhs, "", false, true);
break;
case OP_SUB:
result = def->builder->CreateSub(lhs, rhs, "", false, true);
break;
case OP_MUL:
result = def->builder->CreateMul(lhs, rhs, "", false, true);
break;
case OP_DIV:
result = def->builder->CreateFDiv(def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
default:
lua_assert(0);
}
if (op != OP_DIV)
emit_store_reg_i_withtype(def, result, ra);
else
emit_store_reg_n_withtype(def, result, ra);
def->builder->CreateBr(done);
// RB is int, check if RC is float
def->f->getBasicBlockList().push_back(rb_int_check_rc_float);
def->builder->SetInsertPoint(rb_int_check_rc_float);
llvm::Value *rb_int_is_rc_float =
emit_is_value_of_type(def, rc_type, LUA__TNUMFLT, "rb.int.is.rc.float");
llvm::BasicBlock *int_float_op =
llvm::BasicBlock::Create(def->jitState->context(), "int.float.op");
// RB is int, check if RC is float
def->builder->CreateCondBr(rb_int_is_rc_float, int_float_op, slowpath);
// RB is int, RC is float
def->f->getBasicBlockList().push_back(int_float_op);
def->builder->SetInsertPoint(int_float_op);
lhs = emit_load_reg_i(def, rb);
rhs = emit_load_reg_n(def, rc);
result = nullptr;
// Add and set RA
switch (op) {
case OP_ADD:
result = def->builder->CreateFAdd(def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
break;
case OP_SUB:
result = def->builder->CreateFSub(def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
break;
case OP_MUL:
result = def->builder->CreateFMul(def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
break;
case OP_DIV:
result = def->builder->CreateFDiv(def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
break;
default:
lua_assert(0);
}
emit_store_reg_n_withtype(def, result, ra);
// We are done
def->builder->CreateBr(done);
def->f->getBasicBlockList().push_back(slowpath);
def->builder->SetInsertPoint(slowpath);
switch (op) {
case OP_ADD:
CreateCall4(def->builder, def->raviV_op_addF,
def->L, ra, rb, rc);
break;
case OP_SUB:
CreateCall4(def->builder, def->raviV_op_subF,
def->L, ra, rb, rc);
break;
case OP_MUL:
CreateCall4(def->builder, def->raviV_op_mulF,
def->L, ra, rb, rc);
break;
case OP_DIV:
CreateCall4(def->builder, def->raviV_op_divF,
def->L, ra, rb, rc);
break;
default:
lua_assert(0);
}
def->builder->CreateBr(done);
def->f->getBasicBlockList().push_back(done);
def->builder->SetInsertPoint(done);
}
// OP_MOD
void RaviCodeGenerator::emit_MOD(RaviFunctionDef *def, int A, int B, int C,
int pc) {

@ -1869,7 +1869,8 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
}
}
if (doVerify && llvm::verifyFunction(*f->function(), &llvm::errs())) {
if (/* doVerify && */llvm::verifyFunction(*f->function(), &llvm::errs())) {
f->dump();
fprintf(stderr, "LLVM Code Verification failed\n");
abort();
}

@ -356,14 +356,15 @@ void RaviCodeGenerator::emit_common_GETTABLE_S_(RaviFunctionDef *def, int A,
def->builder->SetInsertPoint(testok);
// Get the value
llvm::Value *value1 = emit_table_get_value(def, node, offset);
llvm::Value *rc1 = emit_gep_register_or_constant(def, C);
def->builder->CreateBr(testend);
// Not found so call luaH_getstr
def->f->getBasicBlockList().push_back(testfail);
def->builder->SetInsertPoint(testfail);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
llvm::Value *rc2 = emit_gep_register_or_constant(def, C);
llvm::Value *value2 =
CreateCall2(def->builder, def->luaH_getstrF, t, emit_load_reg_s(def, rc));
CreateCall2(def->builder, def->luaH_getstrF, t, emit_load_reg_s(def, rc2));
def->builder->CreateBr(testend);
// merge
@ -372,7 +373,10 @@ void RaviCodeGenerator::emit_common_GETTABLE_S_(RaviFunctionDef *def, int A,
llvm::PHINode *phi = def->builder->CreatePHI(def->types->pTValueT, 2);
phi->addIncoming(value1, testok);
phi->addIncoming(value2, testfail);
emit_finish_GETTABLE(def, phi, t, ra, rb, rc);
llvm::PHINode *phi2 = def->builder->CreatePHI(def->types->pTValueT, 2);
phi2->addIncoming(rc1, testok);
phi2->addIncoming(rc2, testfail);
emit_finish_GETTABLE(def, phi, t, ra, rb, phi2);
}
// R(A) := R(B)[RK(C)]

Loading…
Cancel
Save