Skip to content

Commit

Permalink
issue #169 Refactor the c2mir invocation so that we can reuse this wh…
Browse files Browse the repository at this point in the history
…en integrating with ravicomp library.
  • Loading branch information
dibyendumajumdar committed Aug 28, 2020
1 parent 16d59f6 commit cde0a39
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
11 changes: 11 additions & 0 deletions include/ravi_mirjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ struct ravi_State {
struct c2mir_options options; /* MIR options */
};

extern void mir_prepare(MIR_context_t ctx, int optlevel);
extern void mir_cleanup(MIR_context_t ctx);
extern MIR_item_t mir_find_function(MIR_module_t module, const char *func_name);
extern MIR_module_t mir_compile_C_module(
struct c2mir_options *options,
MIR_context_t ctx,
const char *inputbuffer, /* Code to be compiled */
const char *source_name, /* Name of the function, must be unique */
void *(Import_resolver_func)(const char *name)); /* Resolve external symbols */
extern void *mir_get_func(MIR_context_t ctx, MIR_module_t module, const char *func_name);

#ifdef __cplusplus
};
#endif
Expand Down
60 changes: 47 additions & 13 deletions src/ravi_mirjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int raviV_initjit(struct lua_State *L) {
jit->enabled_ = 1;
jit->min_code_size_ = 150;
jit->min_exec_count_ = 50;
jit->opt_level_ = 1;
jit->opt_level_ = 2;
// The parameter true means we will be dumping stuff as we compile
jit->jit = MIR_init();
G->ravi_state = jit;
Expand Down Expand Up @@ -281,7 +281,7 @@ int raviV_getjitenabled(lua_State *L) {
void raviV_setoptlevel(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
G->ravi_state->opt_level_ = value;
G->ravi_state->opt_level_ = value >= 0 && value <= 3 ? value : G->ravi_state->opt_level_;
}
int raviV_getoptlevel(lua_State *L) {
global_State *G = G(L);
Expand Down Expand Up @@ -364,7 +364,7 @@ static int t_getc (void *data) {
}

/* Searches within a Module for a function by name */
static MIR_item_t find_function(MIR_module_t module, const char *func_name) {
MIR_item_t mir_find_function(MIR_module_t module, const char *func_name) {
MIR_item_t func, main_func = NULL;
for (func = DLIST_HEAD (MIR_item_t, module->items); func != NULL;
func = DLIST_NEXT (MIR_item_t, func)) {
Expand All @@ -374,21 +374,22 @@ static MIR_item_t find_function(MIR_module_t module, const char *func_name) {
return main_func;
}

void *MIR_compile_C_module(
MIR_module_t mir_compile_C_module(
struct c2mir_options *options,
MIR_context_t ctx,
const char *inputbuffer, /* Code to be compiled */
const char *func_name, /* Name of the function, must be unique */
const char *source_name, /* Name of the function, must be unique */
void *(Import_resolver_func)(const char *name)) /* Resolve external symbols */
{
int ret_code = 0;
int (*fun_addr) (void *) = NULL;
char module_name[30];
struct ReadBuffer read_buffer = {.Current_char = 0, .Source_code = inputbuffer};
MIR_module_t module = NULL;
c2mir_init(ctx);
options->module_num++;
snprintf(module_name, sizeof module_name, "__mod_%d__", options->module_num);
options->message_file = stderr;
if (!c2mir_compile(ctx, options, t_getc, &read_buffer, func_name, NULL)) {
if (!c2mir_compile(ctx, options, t_getc, &read_buffer, module_name, NULL)) {
ret_code = 1;
}
else {
Expand All @@ -399,11 +400,6 @@ void *MIR_compile_C_module(
ret_code = 1;
}
if (ret_code == 0 && module) {
MIR_item_t main_func = find_function(module, func_name);
if (main_func == NULL) {
fprintf(stderr, "Error: Compiled function %s not found\n", func_name);
exit(1);
}
MIR_load_module (ctx, module);
MIR_gen_init (ctx);
MIR_gen_set_optimize_level(ctx, 2);
Expand All @@ -415,6 +411,44 @@ void *MIR_compile_C_module(
return fun_addr;
}

void *mir_get_func(MIR_context_t ctx, MIR_module_t module, const char *func_name) {
MIR_item_t main_func = mir_find_function(module, func_name);
if (main_func == NULL) {
fprintf(stderr, "Error: Compiled function %s not found\n", func_name);
exit(1);
}
return MIR_gen (ctx, main_func);
}

void mir_prepare(MIR_context_t ctx, int optlevel) {
c2mir_init(ctx);
MIR_gen_init (ctx);
MIR_gen_set_optimize_level(ctx, optlevel);
}

void mir_cleanup(MIR_context_t ctx) {
MIR_gen_finish (ctx);
c2mir_finish (ctx);
}

static void *compile_C_module(
struct c2mir_options *options,
MIR_context_t ctx,
const char *inputbuffer, /* Code to be compiled */
const char *func_name, /* Name of the function, must be unique */
void *(Import_resolver_func)(const char *name)) /* Resolve external symbols */
{
int (*fun_addr) (void *) = NULL;
mir_prepare(ctx, 2);
MIR_module_t module = mir_compile_C_module(options, ctx, inputbuffer, func_name, Import_resolver_func);
if (module) {
fun_addr = mir_get_func(ctx, module, func_name);
}
mir_cleanup(ctx);
return fun_addr;
}


// Compile a Lua function
// If JIT is turned off then compilation is skipped
// Compilation occurs if either auto compilation is ON (subject to some
Expand Down Expand Up @@ -469,7 +503,7 @@ int raviV_compile(struct lua_State *L, struct Proto *p, ravi_compile_options_t *
ravi_writestring(L, buf.buf, strlen(buf.buf));
ravi_writeline(L);
}
fp = MIR_compile_C_module(&G->ravi_state->options, G->ravi_state->jit, buf.buf, fname, import_resolver);
fp = compile_C_module(&G->ravi_state->options, G->ravi_state->jit, buf.buf, fname, import_resolver);
if (!fp) {
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
}
Expand Down

0 comments on commit cde0a39

Please sign in to comment.