reformat code

pull/81/head
Dibyendu Majumdar 9 years ago
parent d145211021
commit 4484d56971

@ -73,3 +73,5 @@ local sum, flips = fannkuch(n)
local t2 = os.clock()
io.write(sum, "\nPfannkuchen(", n, ") = ", flips, "\n")
io.write("elapsed time in secs ", (t2-t1), "\n")
-- ravi.dumplua(fannkuch)

@ -19,4 +19,4 @@ print(y)
assert(y == 1000000000.0)
print("time taken ", t2-t1);
--ravi.dumplua(x)

@ -163,4 +163,7 @@ print(matrix.getrow(a, y)[y])
--matrix.print(matrix.T(matrix.gen(2)))
--matrix.print(matrix.mul(matrix.gen(2), matrix.gen(2)))
--ravi.dumpllvmasm(matrix.mul)
--ravi.dumpllvmasm(matrix.mul)
-- ravi.dumplua(matrix.mul)

@ -37,13 +37,13 @@ void ravi_emit_ADDFN(ravi_function_def_t *def, int A, int B, int C) {
// rb->value_.n
gcc_jit_lvalue *lhs = ravi_emit_load_reg_n(def, rb);
gcc_jit_rvalue *result = gcc_jit_context_new_binary_op(def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_NumberT,
gcc_jit_lvalue_as_rvalue(lhs),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_NumberT,
C));
// result = rb->value_.n + (double)C
gcc_jit_rvalue *result = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_NumberT, gcc_jit_lvalue_as_rvalue(lhs),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_NumberT, C));
// ra->value_.n = result
// ra->tt_ = LUA_TNUMFLT
ravi_emit_store_reg_n_withtype(def, ra, result);
}

@ -25,7 +25,8 @@
// Create a unique function name in the context
// of this generator
static const char *unique_function_name(ravi_function_def_t* def, ravi_gcc_codegen_t *cg) {
static const char *unique_function_name(ravi_function_def_t *def,
ravi_gcc_codegen_t *cg) {
snprintf(def->name, sizeof def->name, "ravif%d", cg->id++);
return def->name;
}
@ -151,14 +152,14 @@ static bool create_function(ravi_gcc_codegen_t *codegen,
fprintf(stderr, "error creating child context\n");
goto on_error;
}
//gcc_jit_context_set_bool_option(def->function_context,
// gcc_jit_context_set_bool_option(def->function_context,
// GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, 1);
//gcc_jit_context_set_bool_option(def->function_context,
// 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_int_option(def->function_context,
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
/* each function is given a unique name - as Lua functions are closures and do
* not really have names */
@ -181,7 +182,6 @@ static bool create_function(ravi_gcc_codegen_t *codegen,
def->lua_closure_val = gcc_jit_function_new_local(
def->jit_function, NULL, def->ravi->types->pLClosureT, "cl");
return true;
on_error:
@ -230,7 +230,8 @@ static void scan_jump_targets(ravi_function_def_t *def, Proto *p) {
// can generate branch instructions in the code
const Instruction *code = p->code;
int pc, n = p->sizecode;
def->jmp_targets = (ravi_branch_def_t **)calloc(n, sizeof(ravi_branch_def_t *));
def->jmp_targets =
(ravi_branch_def_t **)calloc(n, sizeof(ravi_branch_def_t *));
for (pc = 0; pc < n; pc++) {
Instruction i = code[pc];
OpCode op = GET_OPCODE(i);
@ -239,9 +240,10 @@ static void scan_jump_targets(ravi_function_def_t *def, Proto *p) {
int C = GETARG_C(i);
int j = pc + 2; // jump target
if (C && !def->jmp_targets[j]) {
def->jmp_targets[j] = (ravi_branch_def_t *)calloc(1, sizeof(ravi_branch_def_t));
def->jmp_targets[j]->jmp =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "loadbool", j));
def->jmp_targets[j] =
(ravi_branch_def_t *)calloc(1, sizeof(ravi_branch_def_t));
def->jmp_targets[j]->jmp = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "loadbool", j));
}
} break;
case OP_JMP:
@ -266,9 +268,10 @@ static void scan_jump_targets(ravi_function_def_t *def, Proto *p) {
int sbx = GETARG_sBx(i);
int j = sbx + pc + 1;
if (!def->jmp_targets[j]) {
def->jmp_targets[j] = (ravi_branch_def_t *)calloc(1, sizeof(ravi_branch_def_t));
def->jmp_targets[j]->jmp =
gcc_jit_function_new_block(def->jit_function, unique_name(def, targetname, j+1));
def->jmp_targets[j] =
(ravi_branch_def_t *)calloc(1, sizeof(ravi_branch_def_t));
def->jmp_targets[j]->jmp = gcc_jit_function_new_block(
def->jit_function, unique_name(def, targetname, j + 1));
}
} break;
default:
@ -277,31 +280,36 @@ static void scan_jump_targets(ravi_function_def_t *def, Proto *p) {
}
}
void ravi_emit_struct_assign(ravi_function_def_t *def, gcc_jit_rvalue* dest, gcc_jit_rvalue *src) {
// gcc_jit_block_add_assignment(def->current_block, NULL,
// gcc_jit_rvalue_dereference(dest, NULL),
// gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(src, NULL)));
void ravi_emit_struct_assign(ravi_function_def_t *def, gcc_jit_rvalue *dest,
gcc_jit_rvalue *src) {
// gcc_jit_block_add_assignment(def->current_block, NULL,
// gcc_jit_rvalue_dereference(dest, NULL),
// gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(src,
// NULL)));
gcc_jit_lvalue *dest_value = gcc_jit_rvalue_dereference_field(
dest, NULL, def->ravi->types->Value_value);
dest, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *dest_value_i = gcc_jit_lvalue_access_field(
dest_value, NULL, def->ravi->types->Value_value_i);
dest_value, NULL, def->ravi->types->Value_value_i);
gcc_jit_lvalue *src_value = gcc_jit_rvalue_dereference_field(
src, NULL, def->ravi->types->Value_value);
src, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *src_value_i = gcc_jit_lvalue_access_field(
src_value, NULL, def->ravi->types->Value_value_i);
src_value, NULL, def->ravi->types->Value_value_i);
gcc_jit_block_add_assignment(def->current_block, NULL,
dest_value_i,
gcc_jit_block_add_assignment(def->current_block, NULL, dest_value_i,
gcc_jit_lvalue_as_rvalue(src_value_i));
gcc_jit_lvalue *dest_tt = gcc_jit_rvalue_dereference_field(dest, NULL, def->ravi->types->Value_tt);
gcc_jit_lvalue *src_tt = gcc_jit_rvalue_dereference_field(src, NULL, def->ravi->types->Value_tt);
gcc_jit_lvalue *dest_tt =
gcc_jit_rvalue_dereference_field(dest, NULL, def->ravi->types->Value_tt);
gcc_jit_lvalue *src_tt =
gcc_jit_rvalue_dereference_field(src, NULL, def->ravi->types->Value_tt);
gcc_jit_block_add_assignment(def->current_block, NULL, dest_tt, gcc_jit_lvalue_as_rvalue(src_tt));
gcc_jit_block_add_assignment(def->current_block, NULL, dest_tt,
gcc_jit_lvalue_as_rvalue(src_tt));
}
/* Obtain reference to currently executing function (LClosure*) L->ci->func.value_.gc */
/* Obtain reference to currently executing function (LClosure*)
* L->ci->func.value_.gc */
static void emit_ci_func_value_gc_asLClosure(ravi_function_def_t *def,
gcc_jit_lvalue *ci) {
gcc_jit_lvalue *func = gcc_jit_rvalue_dereference_field(
@ -345,15 +353,16 @@ gcc_jit_rvalue *ravi_emit_get_register(ravi_function_def_t *def, int A) {
}
/* Get access to a constant identify by Bx */
gcc_jit_rvalue *ravi_emit_get_constant(ravi_function_def_t* def, int Bx) {
gcc_jit_rvalue *ravi_emit_get_constant(ravi_function_def_t *def, int Bx) {
gcc_jit_lvalue *kst = gcc_jit_context_new_array_access(
def->function_context, NULL, def->k,
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->C_intT, Bx));
def->function_context, NULL, def->k,
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->C_intT, Bx));
return gcc_jit_lvalue_get_address(kst, NULL);
}
gcc_jit_rvalue *ravi_emit_get_register_or_constant(ravi_function_def_t* def, int B) {
gcc_jit_rvalue *ravi_emit_get_register_or_constant(ravi_function_def_t *def,
int B) {
if (ISK(B))
return ravi_emit_get_constant(def, INDEXK(B));
else
@ -385,35 +394,52 @@ static void emit_getL_base_reference(ravi_function_def_t *def,
}
/* Get TValue->value_.i */
gcc_jit_lvalue *ravi_emit_load_reg_i(ravi_function_def_t *def, gcc_jit_rvalue *tv) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(tv, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *i = gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_i);
gcc_jit_lvalue *ravi_emit_load_reg_i(ravi_function_def_t *def,
gcc_jit_rvalue *tv) {
gcc_jit_lvalue *value =
gcc_jit_rvalue_dereference_field(tv, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *i =
gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_i);
return i;
}
/* Get TValue->value_.n */
gcc_jit_lvalue *ravi_emit_load_reg_n(ravi_function_def_t *def, gcc_jit_rvalue *tv) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(tv, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *n = gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_n);
gcc_jit_lvalue *ravi_emit_load_reg_n(ravi_function_def_t *def,
gcc_jit_rvalue *tv) {
gcc_jit_lvalue *value =
gcc_jit_rvalue_dereference_field(tv, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *n =
gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_n);
return n;
}
void ravi_emit_store_reg_i_withtype(ravi_function_def_t *def, gcc_jit_rvalue *reg, gcc_jit_rvalue *ivalue) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *i = gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_i);
void ravi_emit_store_reg_i_withtype(ravi_function_def_t *def,
gcc_jit_rvalue *reg,
gcc_jit_rvalue *ivalue) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(
reg, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *i =
gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_i);
gcc_jit_block_add_assignment(def->current_block, NULL, i, ivalue);
gcc_jit_rvalue *type = gcc_jit_context_new_rvalue_from_int(def->function_context, def->ravi->types->C_intT, LUA_TNUMINT);
gcc_jit_lvalue *tt = gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_tt);
gcc_jit_rvalue *type = gcc_jit_context_new_rvalue_from_int(
def->function_context, def->ravi->types->C_intT, LUA_TNUMINT);
gcc_jit_lvalue *tt =
gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_tt);
gcc_jit_block_add_assignment(def->current_block, NULL, tt, type);
}
void ravi_emit_store_reg_n_withtype(ravi_function_def_t *def, gcc_jit_rvalue *reg, gcc_jit_rvalue *nvalue) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *n = gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_n);
void ravi_emit_store_reg_n_withtype(ravi_function_def_t *def,
gcc_jit_rvalue *reg,
gcc_jit_rvalue *nvalue) {
gcc_jit_lvalue *value = gcc_jit_rvalue_dereference_field(
reg, NULL, def->ravi->types->Value_value);
gcc_jit_lvalue *n =
gcc_jit_lvalue_access_field(value, NULL, def->ravi->types->Value_value_n);
gcc_jit_block_add_assignment(def->current_block, NULL, n, nvalue);
gcc_jit_rvalue *type = gcc_jit_context_new_rvalue_from_int(def->function_context, def->ravi->types->C_intT, LUA_TNUMFLT);
gcc_jit_lvalue *tt = gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_tt);
gcc_jit_rvalue *type = gcc_jit_context_new_rvalue_from_int(
def->function_context, def->ravi->types->C_intT, LUA_TNUMFLT);
gcc_jit_lvalue *tt =
gcc_jit_rvalue_dereference_field(reg, NULL, def->ravi->types->Value_tt);
gcc_jit_block_add_assignment(def->current_block, NULL, tt, type);
}
@ -457,13 +483,11 @@ static void link_block(ravi_function_def_t *def, int pc) {
}
}
gcc_jit_rvalue *ravi_function_call5_rvalue(ravi_function_def_t *def,
gcc_jit_function *f,
gcc_jit_rvalue *arg1,
gcc_jit_rvalue *arg2,
gcc_jit_rvalue *arg3,
gcc_jit_rvalue *arg4,
gcc_jit_rvalue *arg5) {
gcc_jit_rvalue *
ravi_function_call5_rvalue(ravi_function_def_t *def, gcc_jit_function *f,
gcc_jit_rvalue *arg1, gcc_jit_rvalue *arg2,
gcc_jit_rvalue *arg3, gcc_jit_rvalue *arg4,
gcc_jit_rvalue *arg5) {
gcc_jit_rvalue *args[5];
args[0] = arg1;
args[1] = arg2;
@ -473,12 +497,10 @@ gcc_jit_rvalue *ravi_function_call5_rvalue(ravi_function_def_t *def,
return gcc_jit_context_new_call(def->function_context, NULL, f, 5, args);
}
gcc_jit_rvalue *ravi_function_call4_rvalue(ravi_function_def_t *def,
gcc_jit_function *f,
gcc_jit_rvalue *arg1,
gcc_jit_rvalue *arg2,
gcc_jit_rvalue *arg3,
gcc_jit_rvalue *arg4) {
gcc_jit_rvalue *
ravi_function_call4_rvalue(ravi_function_def_t *def, gcc_jit_function *f,
gcc_jit_rvalue *arg1, gcc_jit_rvalue *arg2,
gcc_jit_rvalue *arg3, gcc_jit_rvalue *arg4) {
gcc_jit_rvalue *args[4];
args[0] = arg1;
args[1] = arg2;
@ -487,7 +509,6 @@ gcc_jit_rvalue *ravi_function_call4_rvalue(ravi_function_def_t *def,
return gcc_jit_context_new_call(def->function_context, NULL, f, 4, args);
}
gcc_jit_rvalue *ravi_function_call3_rvalue(ravi_function_def_t *def,
gcc_jit_function *f,
gcc_jit_rvalue *arg1,
@ -515,7 +536,8 @@ void ravi_set_current_block(ravi_function_def_t *def, gcc_jit_block *block) {
def->current_block_terminated = false;
}
static void init_def(ravi_function_def_t *def, ravi_gcc_context_t *ravi, Proto *p) {
static void init_def(ravi_function_def_t *def, ravi_gcc_context_t *ravi,
Proto *p) {
def->ravi = ravi;
def->entry_block = NULL;
def->function_context = NULL;
@ -612,7 +634,7 @@ int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request,
int sbx = GETARG_sBx(i);
int j = sbx + pc + 1;
ravi_emit_iFORLOOP(&def, A, j, def.jmp_targets[pc],
op == OP_RAVI_FORLOOP_I1);
op == OP_RAVI_FORLOOP_I1);
} break;
case OP_MOVE: {
int B = GETARG_B(i);
@ -631,13 +653,13 @@ int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request,
ravi_emit_ADDFN(&def, A, B, C);
} break;
default:
break;
break;
}
}
gcc_jit_context_dump_to_file(def.function_context, "fdump.txt", 0);
//gcc_jit_context_dump_reproducer_to_file(def.function_context, "rdump.txt");
//gcc_jit_context_set_logfile (def.function_context, stderr, 0, 0);
// gcc_jit_context_dump_reproducer_to_file(def.function_context, "rdump.txt");
// gcc_jit_context_set_logfile (def.function_context, stderr, 0, 0);
if (gcc_jit_context_get_first_error(def.function_context)) {
fprintf(stderr, "aborting due to JIT error: %s\n",

@ -23,39 +23,39 @@
#include <ravi_gccjit.h>
void ravi_emit_iFORLOOP(ravi_function_def_t *def, int A, int pc, ravi_branch_def_t *b, int step_one) {
// lua_Integer step = ivalue(ra + 2);
// lua_Integer idx = ivalue(ra) + step; /* increment index */
// lua_Integer limit = ivalue(ra + 1);
// if (idx <= limit) {
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
// setivalue(ra, idx); /* update internal index... */
// setivalue(ra + 3, idx); /* ...and external index */
// }
void ravi_emit_iFORLOOP(ravi_function_def_t *def, int A, int pc,
ravi_branch_def_t *b, int step_one) {
// lua_Integer step = ivalue(ra + 2);
// lua_Integer idx = ivalue(ra) + step; /* increment index */
// lua_Integer limit = ivalue(ra + 1);
// if (idx <= limit) {
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
// setivalue(ra, idx); /* update internal index... */
// setivalue(ra + 3, idx); /* ...and external index */
// }
// We are in b->jmp as this is already the current block
lua_assert(def->current_block == b->jmp);
// Create the done block
gcc_jit_block *exit_block =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "FORLOOP_I1_exit", 0));
gcc_jit_block *exit_block = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "FORLOOP_I1_exit", 0));
gcc_jit_rvalue *new_idx;
if (!step_one) {
// lua_Integer step = ivalue(ra + 2);
new_idx = gcc_jit_context_new_binary_op(def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_IntegerT,
gcc_jit_lvalue_as_rvalue(b->iidx),
gcc_jit_lvalue_as_rvalue(b->istep));
}
else
new_idx = gcc_jit_context_new_binary_op(def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_IntegerT,
gcc_jit_lvalue_as_rvalue(b->iidx),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_IntegerT, 1));
new_idx = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_IntegerT, gcc_jit_lvalue_as_rvalue(b->iidx),
gcc_jit_lvalue_as_rvalue(b->istep));
} else
new_idx = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_PLUS,
def->ravi->types->lua_IntegerT, gcc_jit_lvalue_as_rvalue(b->iidx),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_IntegerT, 1));
// save new index
gcc_jit_block_add_assignment(def->current_block, NULL, b->iidx, new_idx);
@ -63,14 +63,15 @@ void ravi_emit_iFORLOOP(ravi_function_def_t *def, int A, int pc, ravi_branch_def
// lua_Integer limit = ivalue(ra + 1);
// idx > limit?
gcc_jit_rvalue *new_idx_gt_limit =
gcc_jit_context_new_comparison(def->function_context, NULL, GCC_JIT_COMPARISON_GT,
gcc_jit_lvalue_as_rvalue(b->iidx), gcc_jit_lvalue_as_rvalue(b->ilimit));
gcc_jit_rvalue *new_idx_gt_limit = gcc_jit_context_new_comparison(
def->function_context, NULL, GCC_JIT_COMPARISON_GT,
gcc_jit_lvalue_as_rvalue(b->iidx), gcc_jit_lvalue_as_rvalue(b->ilimit));
// If idx > limit we are done
gcc_jit_block *update_block =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "FORLOOP_I1_updatei", 0));
gcc_jit_block_end_with_conditional(def->current_block, NULL, new_idx_gt_limit, exit_block, update_block);
gcc_jit_block *update_block = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "FORLOOP_I1_updatei", 0));
gcc_jit_block_end_with_conditional(def->current_block, NULL, new_idx_gt_limit,
exit_block, update_block);
ravi_set_current_block(def, update_block);
@ -82,7 +83,7 @@ void ravi_emit_iFORLOOP(ravi_function_def_t *def, int A, int pc, ravi_branch_def
ravi_emit_store_reg_i_withtype(def, rvar, gcc_jit_lvalue_as_rvalue(b->iidx));
// ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
gcc_jit_block_end_with_jump(def->current_block, NULL, def->jmp_targets[pc]->jmp);
gcc_jit_block_end_with_jump(def->current_block, NULL,
def->jmp_targets[pc]->jmp);
ravi_set_current_block(def, exit_block);
}

@ -22,19 +22,21 @@
******************************************************************************/
#include <ravi_gccjit.h>
void ravi_emit_iFORPREP(ravi_function_def_t *def, int A, int pc,
int step_one) {
void ravi_emit_iFORPREP(ravi_function_def_t *def, int A, int pc, int step_one) {
ravi_branch_def_t *forloop_target = def->jmp_targets[pc];
assert(forloop_target);
forloop_target->ilimit =
gcc_jit_function_new_local(def->jit_function, NULL, def->ravi->types->lua_IntegerT, unique_name(def, "ilimit", 0));
forloop_target->ilimit = gcc_jit_function_new_local(
def->jit_function, NULL, def->ravi->types->lua_IntegerT,
unique_name(def, "ilimit", 0));
if (!step_one) {
forloop_target->istep =
gcc_jit_function_new_local(def->jit_function, NULL, def->ravi->types->lua_IntegerT, unique_name(def, "istep", 0));
forloop_target->istep = gcc_jit_function_new_local(
def->jit_function, NULL, def->ravi->types->lua_IntegerT,
unique_name(def, "istep", 0));
}
forloop_target->iidx =
gcc_jit_function_new_local(def->jit_function, NULL, def->ravi->types->lua_IntegerT, unique_name(def, "iidx", 0));
forloop_target->iidx = gcc_jit_function_new_local(
def->jit_function, NULL, def->ravi->types->lua_IntegerT,
unique_name(def, "iidx", 0));
// lua_Integer initv = ivalue(init);
// setivalue(init, initv - ivalue(pstep));
@ -57,32 +59,38 @@ void ravi_emit_iFORPREP(ravi_function_def_t *def, int A, int pc,
if (!step_one) {
// setivalue(init, initv - ivalue(pstep));
gcc_jit_lvalue *step_ivalue = ravi_emit_load_reg_i(def, pstep);
gcc_jit_rvalue *idx = gcc_jit_context_new_binary_op(def->function_context, NULL, GCC_JIT_BINARY_OP_MINUS,
def->ravi->types->lua_IntegerT,
gcc_jit_lvalue_as_rvalue(init_ivalue),
gcc_jit_lvalue_as_rvalue(step_ivalue));
gcc_jit_rvalue *idx = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_MINUS,
def->ravi->types->lua_IntegerT, gcc_jit_lvalue_as_rvalue(init_ivalue),
gcc_jit_lvalue_as_rvalue(step_ivalue));
// Save idx
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->iidx, idx);
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->iidx,
idx);
// Save step
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->istep, gcc_jit_lvalue_as_rvalue(step_ivalue));
gcc_jit_block_add_assignment(def->current_block, NULL,
forloop_target->istep,
gcc_jit_lvalue_as_rvalue(step_ivalue));
} else {
// setivalue(init, initv - ivalue(pstep));
gcc_jit_rvalue *idx = gcc_jit_context_new_binary_op(def->function_context, NULL, GCC_JIT_BINARY_OP_MINUS,
def->ravi->types->lua_IntegerT,
gcc_jit_lvalue_as_rvalue(init_ivalue),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_IntegerT, 1));
gcc_jit_rvalue *idx = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_MINUS,
def->ravi->types->lua_IntegerT, gcc_jit_lvalue_as_rvalue(init_ivalue),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->lua_IntegerT, 1));
// Save idx
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->iidx, idx);
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->iidx,
idx);
}
// Save limit
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->ilimit, gcc_jit_lvalue_as_rvalue(limit_ivalue));
gcc_jit_block_add_assignment(def->current_block, NULL, forloop_target->ilimit,
gcc_jit_lvalue_as_rvalue(limit_ivalue));
// We are done so jump to forloop
lua_assert(def->jmp_targets[pc]->jmp);
gcc_jit_block_end_with_jump(def->current_block, NULL, def->jmp_targets[pc]->jmp);
gcc_jit_block_end_with_jump(def->current_block, NULL,
def->jmp_targets[pc]->jmp);
def->current_block_terminated = true;
}

@ -25,9 +25,13 @@
// R(A+1), ..., R(A+B) := nil
void ravi_emit_LOADNIL(ravi_function_def_t *def, int A, int B) {
ravi_function_call3_rvalue(def, def->ravi->types->raviV_op_loadnilT, gcc_jit_lvalue_as_rvalue(def->ci_val),
gcc_jit_context_new_rvalue_from_int(def->function_context, def->ravi->types->C_intT, A),
gcc_jit_context_new_rvalue_from_int(def->function_context, def->ravi->types->C_intT, B));
ravi_function_call3_rvalue(
def, def->ravi->types->raviV_op_loadnilT,
gcc_jit_lvalue_as_rvalue(def->ci_val),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->C_intT, A),
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->C_intT, B));
}
// R(A) := tonumber(0)
@ -40,12 +44,12 @@ void ravi_emit_LOADFZ(ravi_function_def_t *def, int A) {
gcc_jit_rvalue *dest = ravi_emit_get_register(def, A);
// destvalue->n = 0.0
ravi_emit_store_reg_n_withtype(def, dest,
gcc_jit_context_new_rvalue_from_double(def->function_context,
def->ravi->types->lua_NumberT, 0.0));
ravi_emit_store_reg_n_withtype(
def, dest,
gcc_jit_context_new_rvalue_from_double(
def->function_context, def->ravi->types->lua_NumberT, 0.0));
}
void ravi_emit_LOADK(ravi_function_def_t *def, int A, int Bx, int pc) {
(void)pc;

@ -48,7 +48,8 @@ void ravi_emit_RETURN(ravi_function_def_t *def, int A, int B, int pc) {
// current block may already be terminated - so we have to insert
// a new block
if (def->current_block_terminated) {
def->current_block = gcc_jit_function_new_block(def->jit_function, unique_name(def, "OP_RETURN", pc));
def->current_block = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "OP_RETURN", pc));
def->current_block_terminated = false;
}
@ -74,10 +75,10 @@ void ravi_emit_RETURN(ravi_function_def_t *def, int A, int B, int pc) {
gcc_jit_context_new_rvalue_from_int(def->function_context,
def->ravi->types->C_intT, 0));
gcc_jit_block *then_block =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "OP_RETURN_if_sizep_gt_0", pc));
gcc_jit_block *else_block =
gcc_jit_function_new_block(def->jit_function, unique_name(def, "OP_RETURN_else_sizep_gt_0", pc));
gcc_jit_block *then_block = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "OP_RETURN_if_sizep_gt_0", pc));
gcc_jit_block *else_block = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "OP_RETURN_else_sizep_gt_0", pc));
gcc_jit_block_end_with_conditional(def->current_block, NULL, psize_gt_0,
then_block, else_block);

@ -170,6 +170,7 @@ llvm::Instruction *RaviCodeGenerator::emit_load_reg_n(RaviFunctionDef *def,
lhs->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_pdoubleT);
#else
llvm::Value *rb_n = def->builder->CreateBitCast(rb, def->types->plua_NumberT);
//llvm::Value *rb_n = emit_gep(def, "value.value_.n", rb, 0, 0, 0);
llvm::Instruction *lhs = def->builder->CreateLoad(rb_n);
lhs->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
#endif
@ -237,6 +238,7 @@ void RaviCodeGenerator::emit_store_reg_n(RaviFunctionDef *def,
#else
llvm::Value *ra_n =
def->builder->CreateBitCast(dest_ptr, def->types->plua_NumberT);
//llvm::Value *ra_n = emit_gep(def, "value.value_.n", dest_ptr, 0, 0, 0);
llvm::Instruction *store = def->builder->CreateStore(result, ra_n);
store->setMetadata(llvm::LLVMContext::MD_tbaa, def->types->tbaa_TValue_nT);
#endif

@ -250,7 +250,7 @@ void RaviJITFunctionImpl::runpasses(bool dumpAsm) {
module_->setDataLayout(target_layout);
FPM->add(new llvm::DataLayoutPass(*engine_->getDataLayout()));
#elif LLVM_VERSION_MINOR == 7
// Apparently no need to set DataLayout
// Apparently no need to set DataLayout
#else
#error Unsupported LLVM version
#endif
@ -363,7 +363,6 @@ std::unique_ptr<RaviJITState> RaviJITStateFactory::newJITState() {
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -406,7 +405,8 @@ void raviV_close(struct lua_State *L) {
// Compile a Lua function
// If JIT is turned off then compilation is skipped
// Compilation occurs if either auto compilation is ON (subject to some thresholds)
// Compilation occurs if either auto compilation is ON (subject to some
// thresholds)
// or if a manual compilation request was made
// Returns true if compilation was successful
int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request,
@ -423,10 +423,15 @@ int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request,
if (!doCompile && G->ravi_state->jit->is_auto()) {
if (p->ravi_jit.jit_flags != 0) /* function has fornum loop, so compile */
doCompile = true;
else if (p->sizecode > G->ravi_state->jit->get_mincodesize()) /* function is long so compile */
else if (p->sizecode >
G->ravi_state->jit
->get_mincodesize()) /* function is long so compile */
doCompile = true;
else {
if (p->ravi_jit.execution_count < G->ravi_state->jit->get_minexeccount()) /* function has been executed many times so compile */
if (p->ravi_jit.execution_count <
G->ravi_state
->jit->get_minexeccount()) /* function has been executed many
times so compile */
p->ravi_jit.execution_count++;
else
doCompile = true;
@ -549,7 +554,6 @@ int raviV_sizelevel(lua_State *L) {
return 1;
}
#else
// TODO we probably do not need all the headers
@ -567,7 +571,6 @@ int raviV_sizelevel(lua_State *L) {
#include "ravi_gccjit.h"
// Initialize the JIT State and attach it to the
// Global Lua State
// If a JIT State already exists then this function
@ -578,8 +581,7 @@ int raviV_initjit(struct lua_State *L) {
return -1;
ravi_State *jit = (ravi_State *)calloc(1, sizeof(ravi_State));
jit->jit = ravi_jit_new_context();
jit->code_generator =
ravi_jit_new_codegen(jit->jit);
jit->code_generator = ravi_jit_new_codegen(jit->jit);
G->ravi_state = jit;
return 0;
}
@ -594,39 +596,26 @@ void raviV_close(struct lua_State *L) {
free(G->ravi_state);
}
// Dump the LLVM IR
void raviV_dumpllvmir(struct lua_State *L, struct Proto *p) {
}
void raviV_dumpllvmir(struct lua_State *L, struct Proto *p) {}
// Dump the LLVM ASM
void raviV_dumpllvmasm(struct lua_State *L, struct Proto *p) {
}
void raviV_dumpllvmasm(struct lua_State *L, struct Proto *p) {}
// Turn on/off auto JIT compilation
int raviV_auto(lua_State *L) {
return 0;
}
int raviV_auto(lua_State *L) { return 0; }
// Turn on/off the JIT compiler
int raviV_jitenable(lua_State *L) {
return 0;
}
int raviV_jitenable(lua_State *L) { return 0; }
// Set LLVM optimization level
int raviV_optlevel(lua_State *L) {
return 0;
}
int raviV_optlevel(lua_State *L) { return 0; }
// Set LLVM code size level
int raviV_sizelevel(lua_State *L) {
return 0;
}
int raviV_sizelevel(lua_State *L) { return 0; }
#endif
// Test if the given function is compiled
static int ravi_is_compiled(lua_State *L) {
int n = lua_gettop(L);
@ -641,7 +630,7 @@ static int ravi_is_compiled(lua_State *L) {
// Try to JIT compile the given function
// Optional boolean (second) parameter specifies whether
// to dump the code generation
// to dump the code generation
static int ravi_compile(lua_State *L) {
int n = lua_gettop(L);
luaL_argcheck(L, n >= 1, 1, "1 or 2 arguments expected");
@ -649,7 +638,7 @@ static int ravi_compile(lua_State *L) {
"argument must be a Lua function");
void *p = (void *)lua_topointer(L, 1);
LClosure *l = reinterpret_cast<LClosure *>(p);
int manualRequest = 1;
int manualRequest = 1;
// Is there a second boolean parameter requesting
// dump of code generation?
int dumpAsm = (n == 2) ? lua_toboolean(L, 2) : 0;
@ -695,24 +684,16 @@ static int ravi_dump_llvmasm(lua_State *L) {
}
// Turn on/off auto JIT compilation
static int ravi_auto(lua_State *L) {
return raviV_auto(L);
}
static int ravi_auto(lua_State *L) { return raviV_auto(L); }
// Turn on/off the JIT compiler
static int ravi_jitenable(lua_State *L) {
return raviV_jitenable(L);
}
static int ravi_jitenable(lua_State *L) { return raviV_jitenable(L); }
// Set LLVM optimization level
static int ravi_optlevel(lua_State *L) {
return raviV_optlevel(L);
}
static int ravi_optlevel(lua_State *L) { return raviV_optlevel(L); }
// Set LLVM code size level
static int ravi_sizelevel(lua_State *L) {
return raviV_sizelevel(L);
}
static int ravi_sizelevel(lua_State *L) { return raviV_sizelevel(L); }
static const luaL_Reg ravilib[] = {{"iscompiled", ravi_is_compiled},
{"compile", ravi_compile},

Loading…
Cancel
Save