issue #110 option to inline some arithmetic ops

pull/167/head
Dibyendu Majumdar 6 years ago
parent 5d5d036fdc
commit 3def57baf8

@ -103,10 +103,13 @@ enum ravi_codegen_type {
struct ravi_compile_options_t {
/* Is this a manual request? */
int manual_request;
unsigned int manual_request : 1;
/* Should range check be omitted when compiling array access */
int omit_array_get_range_check;
unsigned int omit_array_get_range_check : 1;
/* Should we generate inline code for Lua's arithmetc ops */
unsigned int inline_lua_arithmetic_operators : 1;
enum ravi_codegen_type codegen_type;
};

@ -61,6 +61,12 @@ static const char Lua_header[] = ""
"#define LUA_TFUNCTION 6\n"
"#define LUA_TUSERDATA 7\n"
"#define LUA_TTHREAD 8\n"
"typedef enum {TM_INDEX,TM_NEWINDEX,TM_GC,\n"
" TM_MODE,TM_LEN,TM_EQ,TM_ADD,TM_SUB,TM_MUL,\n"
" TM_MOD,TM_POW,TM_DIV,TM_IDIV,TM_BAND,TM_BOR,\n"
" TM_BXOR,TM_SHL,TM_SHR,TM_UNM,TM_BNOT,TM_LT,\n"
" TM_LE,TM_CONCAT,TM_CALL,TM_N\n"
"} TMS;\n"
"typedef double lua_Number;\n"
"typedef int64_t lua_Integer;\n"
"typedef uint64_t lua_Unsigned;\n"
@ -564,6 +570,7 @@ static const char Lua_header[] = ""
"extern void raviH_set_int(lua_State *L, Table *t, lua_Unsigned key, lua_Integer value);\n"
"extern void raviH_set_float(lua_State *L, Table *t, lua_Unsigned key, lua_Number value);\n"
"extern int raviV_check_usertype(lua_State *L, TString *name, const TValue *o);\n"
"extern void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, TValue *res, TMS event);\n"
"#define R(i) (base + i)\n"
"#define K(i) (k + i)\n"
;
@ -890,7 +897,7 @@ static void emit_settable_af(struct function *fn, int A, int B, int C, bool know
/* Handle OP_ADD, OP_SUB, OP_MUL */
static void emit_op_arithslow(struct function *fn, int A, int B, int C, OpCode op, int pc, const char *opchar,
const char *numop, const char *tm) {
const char *tm) {
(void)pc;
(void)op;
emit_reg(fn, "ra", A);
@ -1955,9 +1962,39 @@ bool raviJ_codegen(struct lua_State *L, struct Proto *p,
emit_UNM(&fn, A, B, pc);
} break;
#endif
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_ADD: {
int B = GETARG_B(i);
int C = GETARG_C(i);
if (options->inline_lua_arithmetic_operators) {
emit_op_arithslow(&fn, A, B, C, op, pc, "+",
"TM_ADD");
}
else {
emit_binary_op(&fn, A, B, C, op, pc);
}
} break;
case OP_SUB: {
int B = GETARG_B(i);
int C = GETARG_C(i);
if (options->inline_lua_arithmetic_operators) {
emit_op_arithslow(&fn, A, B, C, op, pc, "-",
"TM_SUB");
}
else {
emit_binary_op(&fn, A, B, C, op, pc);
}
} break;
case OP_MUL: {
int B = GETARG_B(i);
int C = GETARG_C(i);
if (options->inline_lua_arithmetic_operators) {
emit_op_arithslow(&fn, A, B, C, op, pc, "*",
"TM_MUL");
}
else {
emit_binary_op(&fn, A, B, C, op, pc);
}
} break;
case OP_MOD:
case OP_POW:
case OP_DIV:

@ -186,7 +186,8 @@ int raviV_initjit(struct lua_State *L) {
register_builtin_arg4(jit->jit, "raviH_set_float", raviH_set_float, JIT_NoType, JIT_Address, JIT_Address, JIT_Int64, JIT_Double);
//int raviV_check_usertype(lua_State *L, TString *name, const TValue *o);
register_builtin_arg3(jit->jit, "raviV_check_usertype", raviV_check_usertype, JIT_Int32, JIT_Address, JIT_Address, JIT_Address);
// void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, TValue *res, TMS event);
register_builtin_arg5(jit->jit, "luaT_trybinTM", luaT_trybinTM, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
G->ravi_state = jit;
return 0;
}

@ -91,13 +91,16 @@ static int ravi_compile_n(lua_State *L) {
LClosure *l = reinterpret_cast<LClosure *>(p);
functions[n++] = l->p;
}
ravi_compile_options_t options = {0, 0, RAVI_CODEGEN_NONE};
ravi_compile_options_t options = {0, 0, 0, RAVI_CODEGEN_NONE};
options.manual_request = 1;
if (lua_istable(L, 2)) {
lua_Integer omit_arrayget_rangecheck;
lua_Integer ival;
l_table_get_integer(L, 2, "omitArrayGetRangeCheck",
&omit_arrayget_rangecheck, 0);
options.omit_array_get_range_check = (int)omit_arrayget_rangecheck;
&ival, 0);
options.omit_array_get_range_check = (int)ival ? 1 : 0;
l_table_get_integer(L, 2, "inlineLuaArithmeticOperators",
&ival, 0);
options.inline_lua_arithmetic_operators = (int)ival ? 1 : 0;
}
int result = 0;
if (n > 0) { result = raviV_compile_n(L, functions, n, &options); }

Loading…
Cancel
Save