more libgccjit implemenation

pull/81/head
dibyendumajumdar 9 years ago
parent 2888f68d57
commit e97bd37c93

@ -182,11 +182,11 @@ target_link_libraries(lua ravi)
#add_executable(luac src/luac.c)
#target_link_libraries(luac ravis)
if (LLVM_JIT)
#VM test
add_executable(test_vm tests/test_vm.c)
target_link_libraries(test_vm ravi)
#VM test
add_executable(test_vm tests/test_vm.c)
target_link_libraries(test_vm ravi)
if (LLVM_JIT)
#LLVM playground
add_executable(test_llvm tests/test_llvm.cpp)
target_link_libraries(test_llvm ravi)
@ -202,7 +202,7 @@ endif()
add_executable(test_misc tests/test_misc.c)
if (LLVM_JIT)
add_test(TestVM test_vm)
add_test(TestLLVM test_llvm)
endif()
add_test(TestVM test_vm)
add_test(TestMisc test_misc)

@ -92,6 +92,7 @@ struct ravi_State {
struct ravi_gcc_types_t {
gcc_jit_type *C_boolT;
gcc_jit_type *C_doubleT;
gcc_jit_type *C_intptr_t;
gcc_jit_type *C_size_t;
@ -516,9 +517,14 @@ extern void ravi_emit_iFORLOOP(ravi_function_def_t *def, int A, int pc, ravi_bra
extern void ravi_emit_MOVE(ravi_function_def_t *def, int A, int B);
extern void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B);
extern void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B, int pc);
extern void ravi_emit_LOADFZ(ravi_function_def_t *def, int A);
extern void ravi_emit_LOADFZ(ravi_function_def_t *def, int A, int pc);
extern void ravi_emit_LOADIZ(ravi_function_def_t *def, int A, int pc);
extern void ravi_emit_LOADBOOL(ravi_function_def_t *def, int A, int B, int C,
int j, int pc);
extern void ravi_emit_ADDFN(ravi_function_def_t *def, int A, int B, int C);

@ -48,6 +48,7 @@ assert(ravi.compile(y))
assert(y() == 1234.0)
print('test 5 ok')
-- Test OP_EQ
x=function(a,b)
if a == b then
return "foo"
@ -62,6 +63,7 @@ assert(x(1,1) == "foo")
assert(x(1,2) == "bar")
print('test 6 ok')
-- Test OP_EQ
x=function(a,b)
if a ~= b then
return "foo"
@ -76,6 +78,93 @@ assert(x(1,1) == "bar")
assert(x(1,2) == "foo")
print('test 7 ok')
-- Test OP_LOADBOOL
function x() return not nil; end
ravi.dumplua(x)
assert(ravi.compile(x))
assert(x())
print('test 8 Ok')
-- Test OP_LOADBOOL
function x() return not 1; end
ravi.dumplua(x)
assert(ravi.compile(x))
assert(not x())
print('test 9 Ok')
-- test 8
x = function ()
if 1 == 2 then
return 5.0
end
return 1.0
end
assert(ravi.compile(x))
assert(x() == 1.0)
print("test 8 OK")
-- test 9
x = function (y)
if y == 1 then
return 1.0
elseif y == 5 then
return 2.0
else
return 3.0
end
end
assert(ravi.compile(x))
assert(x(5) == 2.0)
assert(x(4) == 3.0)
print("test 9 OK")
-- test 10
x = function (y,z)
if y == 1 then
if z == 1 then
return 99.0
else
return z
end
elseif y == 5 then
return 2.0
else
return 3.0
end
end
assert(ravi.compile(x))
assert(x(1,1) == 99.0)
assert(x(1,2) == 2)
assert(x(1,5.3) == 5.3)
assert(x(5) == 2.0)
assert(x(4) == 3.0)
print("test 10 OK")
-- test 16
function tryme(x,y)
if x < y then
return 1
else
return 0
end
end
--ravi.dumplua(tryme)
assert(ravi.compile(tryme))
assert(tryme(1,2) == 1)
assert(tryme(2,1) == 0)
print("test 16 OK")
-- test 17
function tryme(x,y)
return x < y
end
--ravi.dumplua(tryme)
assert(ravi.compile(tryme))
assert(tryme(1,2))
assert(not tryme(2,1))
print("test 17 OK")

@ -57,6 +57,7 @@ static bool can_compile(Proto *p) {
case OP_RAVI_FORPREP_I1:
case OP_MOVE:
case OP_LOADNIL:
case OP_RAVI_LOADIZ:
case OP_RAVI_LOADFZ:
case OP_RAVI_ADDFN:
case OP_CALL:
@ -66,12 +67,12 @@ static bool can_compile(Proto *p) {
case OP_LT:
case OP_LE:
case OP_GETTABUP:
break;
case OP_LOADKX:
case OP_LOADBOOL:
case OP_NOT:
case OP_TEST:
case OP_TESTSET:
break;
case OP_LOADKX:
case OP_FORPREP:
case OP_FORLOOP:
case OP_TFORCALL:
@ -102,7 +103,6 @@ static bool can_compile(Proto *p) {
case OP_RAVI_MOVEF:
case OP_RAVI_TOINT:
case OP_RAVI_TOFLT:
case OP_RAVI_LOADIZ:
case OP_RAVI_ADDIN:
case OP_RAVI_ADDFF:
case OP_RAVI_ADDFI:
@ -127,7 +127,9 @@ static bool can_compile(Proto *p) {
case OP_RAVI_GETTABLE_AI:
case OP_RAVI_GETTABLE_AF:
case OP_RAVI_SETTABLE_AI:
case OP_RAVI_SETTABLE_AII:
case OP_RAVI_SETTABLE_AF:
case OP_RAVI_SETTABLE_AFF:
case OP_RAVI_TOARRAYI:
case OP_RAVI_TOARRAYF:
case OP_RAVI_MOVEAI:
@ -156,8 +158,8 @@ static bool create_function(ravi_gcc_codegen_t *codegen,
// GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, 1);
// gcc_jit_context_set_bool_option(def->function_context,
// GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, 1);
gcc_jit_context_set_bool_option(def->function_context,
GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE, 1);
//gcc_jit_context_set_bool_option(def->function_context,
// GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE, 1);
gcc_jit_context_set_int_option(def->function_context,
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
@ -759,10 +761,18 @@ int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request,
} break;
case OP_LOADNIL: {
int B = GETARG_B(i);
ravi_emit_LOADNIL(&def, A, B);
ravi_emit_LOADNIL(&def, A, B, pc);
} break;
case OP_RAVI_LOADFZ: {
ravi_emit_LOADFZ(&def, A);
ravi_emit_LOADFZ(&def, A, pc);
} break;
case OP_RAVI_LOADIZ: {
ravi_emit_LOADIZ(&def, A, pc);
} break;
case OP_LOADBOOL: {
int B = GETARG_B(i);
int C = GETARG_C(i);
ravi_emit_LOADBOOL(&def, A, B, C, pc + 2, pc);
} break;
case OP_RAVI_ADDFN: {
int B = GETARG_B(i);

@ -97,7 +97,8 @@ gcc_jit_rvalue *ravi_emit_boolean_testfalse(ravi_function_def_t *def,
bool negate) {
// (isnil() || isbool() && b == 0)
gcc_jit_lvalue *var = gcc_jit_function_new_local(def->jit_function, NULL, def->ravi->types->C_intT,
// FIXME should this be bool or int?
gcc_jit_lvalue *var = gcc_jit_function_new_local(def->jit_function, NULL, def->ravi->types->C_boolT,
unique_name(def, "bvalue", 0));
gcc_jit_lvalue *type = ravi_emit_load_type(def, reg);

@ -24,7 +24,9 @@
#include <ravi_gccjit.h>
// R(A+1), ..., R(A+B) := nil
void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B) {
void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B, int pc) {
(void) pc;
ravi_function_call3_rvalue(
def, def->ravi->types->raviV_op_loadnilT,
gcc_jit_lvalue_as_rvalue(def->ci_val),
@ -35,7 +37,8 @@ void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B) {
}
// R(A) := tonumber(0)
void ravi_emit_LOADFZ(ravi_function_def_t *def, int A) {
void ravi_emit_LOADFZ(ravi_function_def_t *def, int A, int pc) {
(void) pc;
// Load pointer to base
ravi_emit_load_base(def);
@ -43,13 +46,31 @@ void ravi_emit_LOADFZ(ravi_function_def_t *def, int A) {
// ra
gcc_jit_rvalue *dest = ravi_emit_get_register(def, A);
// destvalue->n = 0.0
// destvalue->value_.n = 0.0
ravi_emit_store_reg_n_withtype(
def, gcc_jit_context_new_rvalue_from_double(
def->function_context, def->ravi->types->lua_NumberT, 0.0),
dest);
}
// R(A) := tointeger(0)
void ravi_emit_LOADIZ(ravi_function_def_t *def, int A, int pc) {
(void)pc;
// Load pointer to base
ravi_emit_load_base(def);
// ra
gcc_jit_rvalue *dest = ravi_emit_get_register(def, A);
// destvalue->value_.i =
ravi_emit_store_reg_i_withtype(
def, gcc_jit_context_new_rvalue_from_int(
def->function_context, def->ravi->types->lua_IntegerT, 0),
dest);
}
void ravi_emit_LOADK(ravi_function_def_t *def, int A, int Bx, int pc) {
(void)pc;
@ -86,3 +107,29 @@ void ravi_emit_MOVE(ravi_function_def_t *def, int A, int B) {
// *ra = *rb
ravi_emit_struct_assign(def, dest, src);
}
// R(A) := (Bool)B; if (C) pc++
void ravi_emit_LOADBOOL(ravi_function_def_t *def, int A, int B, int C,
int j, int pc) {
// setbvalue(ra, GETARG_B(i));
// if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
// Load pointer to base
ravi_emit_load_base(def);
// ra
gcc_jit_rvalue *dest = ravi_emit_get_register(def, A);
// dest->i = 0
ravi_emit_store_reg_b_withtype(def,
gcc_jit_context_new_rvalue_from_int(def->function_context, def->ravi->types->C_intT,
B),
dest);
if (C) {
// Skip next instruction if C
ravi_emit_branch(def, def->jmp_targets[j]->jmp);
gcc_jit_block *block =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "OP_LOADBOOL_", pc));
ravi_set_current_block(def, block);
}
}

@ -28,8 +28,6 @@ void ravi_emit_GETTABUP(ravi_function_def_t *def, int A, int B, int C, int pc) {
// int b = GETARG_B(i);
// Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
fprintf(stderr, "GETTABUP called");
(void)pc;
ravi_emit_load_base(def);
gcc_jit_rvalue *ra = ravi_emit_get_register(def, A);

@ -31,6 +31,7 @@ bool ravi_setup_lua_types(ravi_gcc_context_t *ravi) {
ravi_gcc_types_t *t = ravi->types;
t->C_boolT = gcc_jit_context_get_type(ravi->context, GCC_JIT_TYPE_BOOL);
t->C_doubleT = gcc_jit_context_get_type(ravi->context, GCC_JIT_TYPE_DOUBLE);
t->lua_NumberT = t->C_doubleT;
t->plua_NumberT = gcc_jit_type_get_pointer(t->lua_NumberT);

Loading…
Cancel
Save