fix bug in arith operators - need to set type

Dibyendu Majumdar 9 years ago
parent d6b2302aee
commit c94b382231

@ -171,6 +171,7 @@ struct LuaLLVMTypes {
llvm::FunctionType *luaG_runerrorT;
llvm::FunctionType *luaV_forlimitT;
llvm::FunctionType *luaV_tonumberT;
llvm::FunctionType *luaV_tointegerT;
llvm::FunctionType *luaV_op_loadnilT;
@ -352,6 +353,7 @@ struct RaviFunctionDef {
llvm::Constant *luaG_runerrorF;
llvm::Constant *luaV_forlimitF;
llvm::Constant *luaV_tonumberF;
llvm::Constant *luaV_tointegerF;
// Some cheats
llvm::Constant *luaV_op_loadnilF;
@ -567,6 +569,10 @@ public:
void emit_MOVE(RaviFunctionDef *def, llvm::Value *L_ci, llvm::Value *proto,
int A, int B);
void emit_MOVEI(RaviFunctionDef *def, llvm::Value *L_ci, llvm::Value *proto,
int A, int B);
// Emit code for OP_EQ, OP_LT and OP_LE
// The callee parameter should be luaV_equalobj, luaV_lessthan and
// luaV_lessequal

@ -8,7 +8,7 @@ local m, limit2 = 50, 4.0
local write, char = io.write, string.char
write("P4\n", width, " ", height, "\n")
t1 = os.time()
t1 = os.clock()
for y=0,height-1 do
local Ci = 2*y / height - 1
for xb=0,width-1,8 do
@ -37,5 +37,5 @@ for y=0,height-1 do
-- print(bits);
end
end
t2 = os.time()
t2 = os.clock()
print(t2-t1)

@ -11,6 +11,7 @@ void RaviCodeGenerator::emit_UNMF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *lhs = emit_load_reg_n(def, rb);
llvm::Value *result = def->builder->CreateFNeg(lhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// -int
@ -22,6 +23,7 @@ void RaviCodeGenerator::emit_UNMI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *lhs = emit_load_reg_i(def, rb);
llvm::Value *result = def->builder->CreateNeg(lhs, "", false, true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
// float+c
@ -36,6 +38,7 @@ void RaviCodeGenerator::emit_ADDFN(RaviFunctionDef *def, llvm::Value *L_ci,
def->builder->CreateSIToFP(llvm::ConstantInt::get(def->types->C_intT, C),
def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// float+float
@ -49,6 +52,7 @@ void RaviCodeGenerator::emit_ADDFF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_n(def, rc);
llvm::Value *result = def->builder->CreateFAdd(lhs, rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// float+int
@ -63,6 +67,7 @@ void RaviCodeGenerator::emit_ADDFI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFAdd(
lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// int+int
@ -77,6 +82,7 @@ void RaviCodeGenerator::emit_ADDII(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_i(def, rc);
llvm::Value *result = def->builder->CreateAdd(lhs, rhs, "", false, true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
// int+c
@ -90,6 +96,7 @@ void RaviCodeGenerator::emit_ADDIN(RaviFunctionDef *def, llvm::Value *L_ci,
lhs, llvm::ConstantInt::get(def->types->lua_IntegerT, C), "", false,
true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
void RaviCodeGenerator::emit_SUBFF(RaviFunctionDef *def, llvm::Value *L_ci,
@ -102,6 +109,7 @@ void RaviCodeGenerator::emit_SUBFF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_n(def, rc);
llvm::Value *result = def->builder->CreateFSub(lhs, rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_SUBFI(RaviFunctionDef *def, llvm::Value *L_ci,
@ -115,6 +123,7 @@ void RaviCodeGenerator::emit_SUBFI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFSub(
lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_SUBIF(RaviFunctionDef *def, llvm::Value *L_ci,
@ -128,6 +137,7 @@ void RaviCodeGenerator::emit_SUBIF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFSub(
def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_SUBII(RaviFunctionDef *def, llvm::Value *L_ci,
@ -140,6 +150,7 @@ void RaviCodeGenerator::emit_SUBII(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_i(def, rc);
llvm::Value *result = def->builder->CreateSub(lhs, rhs, "", false, true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
void RaviCodeGenerator::emit_SUBFN(RaviFunctionDef *def, llvm::Value *L_ci,
@ -153,6 +164,7 @@ void RaviCodeGenerator::emit_SUBFN(RaviFunctionDef *def, llvm::Value *L_ci,
def->builder->CreateSIToFP(llvm::ConstantInt::get(def->types->C_intT, C),
def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_SUBNF(RaviFunctionDef *def, llvm::Value *L_ci,
@ -166,6 +178,7 @@ void RaviCodeGenerator::emit_SUBNF(RaviFunctionDef *def, llvm::Value *L_ci,
def->types->lua_NumberT),
rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_SUBIN(RaviFunctionDef *def, llvm::Value *L_ci,
@ -178,6 +191,7 @@ void RaviCodeGenerator::emit_SUBIN(RaviFunctionDef *def, llvm::Value *L_ci,
lhs, llvm::ConstantInt::get(def->types->lua_IntegerT, C), "", false,
true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
void RaviCodeGenerator::emit_SUBNI(RaviFunctionDef *def, llvm::Value *L_ci,
@ -190,6 +204,7 @@ void RaviCodeGenerator::emit_SUBNI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::ConstantInt::get(def->types->lua_IntegerT, C), rhs, "", false,
true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
// float+c
@ -204,6 +219,7 @@ void RaviCodeGenerator::emit_MULFN(RaviFunctionDef *def, llvm::Value *L_ci,
def->builder->CreateSIToFP(llvm::ConstantInt::get(def->types->C_intT, C),
def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// float+float
@ -217,6 +233,7 @@ void RaviCodeGenerator::emit_MULFF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_n(def, rc);
llvm::Value *result = def->builder->CreateFMul(lhs, rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// float+int
@ -231,6 +248,7 @@ void RaviCodeGenerator::emit_MULFI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFMul(
lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
// int+int
@ -245,6 +263,7 @@ void RaviCodeGenerator::emit_MULII(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_i(def, rc);
llvm::Value *result = def->builder->CreateMul(lhs, rhs, "", false, true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
// int+c
@ -258,6 +277,7 @@ void RaviCodeGenerator::emit_MULIN(RaviFunctionDef *def, llvm::Value *L_ci,
lhs, llvm::ConstantInt::get(def->types->lua_IntegerT, C), "", false,
true);
emit_store_reg_i(def, result, ra);
emit_store_type(def, ra, LUA_TNUMINT);
}
void RaviCodeGenerator::emit_DIVFF(RaviFunctionDef *def, llvm::Value *L_ci,
@ -270,6 +290,7 @@ void RaviCodeGenerator::emit_DIVFF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Instruction *rhs = emit_load_reg_n(def, rc);
llvm::Value *result = def->builder->CreateFDiv(lhs, rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_DIVFI(RaviFunctionDef *def, llvm::Value *L_ci,
@ -283,6 +304,7 @@ void RaviCodeGenerator::emit_DIVFI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFDiv(
lhs, def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_DIVIF(RaviFunctionDef *def, llvm::Value *L_ci,
@ -296,6 +318,7 @@ void RaviCodeGenerator::emit_DIVIF(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *result = def->builder->CreateFDiv(
def->builder->CreateSIToFP(lhs, def->types->lua_NumberT), rhs);
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
void RaviCodeGenerator::emit_DIVII(RaviFunctionDef *def, llvm::Value *L_ci,
@ -310,5 +333,7 @@ void RaviCodeGenerator::emit_DIVII(RaviFunctionDef *def, llvm::Value *L_ci,
def->builder->CreateSIToFP(lhs, def->types->lua_NumberT),
def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
emit_store_reg_n(def, result, ra);
emit_store_type(def, ra, LUA_TNUMFLT);
}
}

@ -190,6 +190,7 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_FORPREP:
case OP_FORLOOP:
case OP_MOVE:
case OP_RAVI_MOVEI:
case OP_RAVI_LOADFZ:
case OP_RAVI_LOADIZ:
case OP_RAVI_ADDFN:
@ -283,6 +284,9 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
def->luaV_tonumberF = def->raviF->addExternFunction(
def->types->luaV_tonumberT, reinterpret_cast<void *>(&luaV_tonumber_),
"luaV_tonumber_");
def->luaV_tointegerF = def->raviF->addExternFunction(
def->types->luaV_tointegerT, reinterpret_cast<void *>(&luaV_tointeger_),
"luaV_tointeger_");
def->luaV_op_loadnilF = def->raviF->addExternFunction(
def->types->luaV_op_loadnilT, reinterpret_cast<void *>(&luaV_op_loadnil),
"luaV_op_loadnil");
@ -315,7 +319,7 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k + INDEXK(GETARG_C(i)))
void RaviCodeGenerator::link_block(RaviFunctionDef *def, int pc) {
if (def->jmp_targets[pc].jmp2) {
if (def->jmp_targets[pc].jmp2 && !def->builder->GetInsertBlock()->getTerminator()) {
// Handle special case for body of FORLOOP
auto b = def->builder->CreateLoad(def->jmp_targets[pc].forloop_branch);
auto idb = def->builder->CreateIndirectBr(b, 4);
@ -419,6 +423,10 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p) {
int B = GETARG_B(i);
emit_MOVE(&def, L_ci, proto, A, B);
} break;
case OP_RAVI_MOVEI: {
int B = GETARG_B(i);
emit_MOVEI(&def, L_ci, proto, A, B);
} break;
case OP_RETURN: {
int B = GETARG_B(i);
emit_RETURN(&def, L_ci, proto, A, B);

@ -103,6 +103,88 @@ void RaviCodeGenerator::emit_MOVE(RaviFunctionDef *def, llvm::Value *L_ci,
#endif
}
void RaviCodeGenerator::emit_MOVEI(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B) {
// case OP_RAVI_MOVEI: {
// TValue *rb = RB(i);
// lua_Integer j;
// if (tointeger(rb, &j)) {
// setivalue(ra, j);
// }
// else
// luaG_runerror(L, "integer expected");
// } break;
llvm::IRBuilder<> TmpB(def->entry, def->entry->begin());
llvm::Value *var =
TmpB.CreateAlloca(def->types->lua_IntegerT, nullptr, "i");
// Load pointer to base
llvm::Instruction *base_ptr = emit_load_base(def);
llvm::Value *dest = emit_gep_ra(def, base_ptr, A);
llvm::Value *src = emit_gep_ra(def, base_ptr, B);
llvm::Value *src_type = emit_load_type(def, src);
// Compare src->tt == LUA_TNUMINT
llvm::Value *cmp1 = def->builder->CreateICmpEQ(
src_type, def->types->kInt[LUA_TNUMINT], "is.integer");
llvm::BasicBlock *then1 = llvm::BasicBlock::Create(
def->jitState->context(), "if.integer", def->f);
llvm::BasicBlock *else1 =
llvm::BasicBlock::Create(def->jitState->context(), "if.not.integer");
llvm::BasicBlock *end1 =
llvm::BasicBlock::Create(def->jitState->context(), "done");
def->builder->CreateCondBr(cmp1, then1, else1);
def->builder->SetInsertPoint(then1);
// Already a int - move
llvm::Instruction *tmp = emit_load_reg_i(def, src);
llvm::Instruction *store = def->builder->CreateStore(
tmp, var, "i");
store->setMetadata(llvm::LLVMContext::MD_tbaa,
def->types->tbaa_longlongT);
def->builder->CreateBr(end1);
// we need to convert
def->f->getBasicBlockList().push_back(else1);
def->builder->SetInsertPoint(else1);
// Call luaV_tointeger_()
llvm::Value *var_isint = def->builder->CreateCall2(
def->luaV_tointegerF, src, var);
llvm::Value *tobool = def->builder->CreateICmpEQ(
var_isint, def->types->kInt[0], "int.conversion.failed");
// Did conversion fail?
llvm::BasicBlock *else2 = llvm::BasicBlock::Create(
def->jitState->context(), "if.conversion.failed", def->f);
def->builder->CreateCondBr(tobool, else2,
end1);
// Conversion failed, so raise error
def->builder->SetInsertPoint(else2);
llvm::Value *errmsg1 =
def->builder->CreateGlobalString("integer expected");
def->builder->CreateCall2(def->luaG_runerrorF, def->L,
emit_gep(def, "", errmsg1, 0, 0));
def->builder->CreateBr(end1);
// Conversion OK
def->f->getBasicBlockList().push_back(end1);
def->builder->SetInsertPoint(end1);
auto load_var = def->builder->CreateLoad(var);
load_var->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_longlongT);
emit_store_reg_i(def, load_var, dest);
emit_store_type(def, dest, LUA_TNUMINT);
}
void RaviCodeGenerator::emit_LOADK(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int Bx) {
// case OP_LOADK: {

@ -637,6 +637,11 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
elements.push_back(plua_NumberT);
luaV_tonumberT = llvm::FunctionType::get(C_intT, elements, false);
elements.clear();
elements.push_back(pTValueT);
elements.push_back(plua_IntegerT);
luaV_tointegerT = llvm::FunctionType::get(C_intT, elements, false);
elements.clear();
elements.push_back(pCallInfoT);
elements.push_back(C_intT);

@ -125,7 +125,7 @@ RaviJITFunctionImpl::~RaviJITFunctionImpl() {
void *RaviJITFunctionImpl::compile() {
// module_->dump();
//module_->dump();
// Create a function pass manager for this engine
llvm::FunctionPassManager *FPM = new llvm::FunctionPassManager(module_);

@ -43,6 +43,43 @@ static int test_luacompfile(const char *code)
return rc;
}
/* test supplied lua code compiles */
static int test_luafileexec1(const char *code, int expected)
{
int rc = 0;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L); /* open standard libraries */
if (luaL_loadfile(L, code) != 0) {
rc = 1;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
else {
ravi_dump_function(L);
time_t start = time(NULL);
if (lua_pcall(L, 0, 1, 0) != 0) {
rc = 1;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
}
else {
time_t end = time(NULL);
ravi_dump_stack(L, "after executing function");
printf("time taken = %f\n", difftime(end, start));
lua_Integer got = 0;
if (lua_isboolean(L, -1))
got = lua_toboolean(L, -1) ? 1 : 0;
else
got = lua_tointeger(L, -1);
if (got != expected) {
rc = 1;
}
}
}
lua_close(L);
return rc;
}
/* test supplied lua code compiles */
static int test_luacompexec1(const char *code, int expected)
@ -84,7 +121,7 @@ static int test_luacompexec1(const char *code, int expected)
int main(int argc, const char *argv[])
{
int failures = 0;
//failures += test_luacompfile("/github/ravi/ravi-tests/mandel1.ravi");
//failures += test_luafileexec1("\\github\\ravi\\ravi-tests\\mandel1.ravi", 0);
failures += test_luacompexec1("local function x(); local i, j:int; j=0; for i=1,1000000000 do; j = j+1; end; return j; end; local y = x(); print(y); return y", 1000000000);
failures += test_luacompexec1("local function x(); local j:double; for i=1,1000000000 do; j = j+1; end; return j; end; local y = x(); print(y); return y", 1000000000);

Loading…
Cancel
Save