|
|
|
@ -2,6 +2,8 @@
|
|
|
|
|
|
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
|
|
#include "ravi_mirjit.h"
|
|
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
#include "lapi.h"
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
@ -9,23 +11,28 @@
|
|
|
|
|
#include "lmem.h"
|
|
|
|
|
#include "lstring.h"
|
|
|
|
|
#include "ltable.h"
|
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct CompilerContext {
|
|
|
|
|
lua_State* L;
|
|
|
|
|
ravi_State* jit;
|
|
|
|
|
Table* h; /* to avoid collection/reuse strings */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Create a new proto and insert it into parent's list of protos */
|
|
|
|
|
static Proto* lua_newProto(void* context, Proto* parent) {
|
|
|
|
|
struct CompilerContext* ccontext = (struct CompilerContext*)context;
|
|
|
|
|
lua_State* L = ccontext->L;
|
|
|
|
|
Proto* p = luaF_newproto(L);
|
|
|
|
|
if (parent) {
|
|
|
|
|
assert(parent);
|
|
|
|
|
/* FIXME make this more efficient */
|
|
|
|
|
int old_size = parent->sizep;
|
|
|
|
|
int new_size = parent->sizep + 1;
|
|
|
|
|
luaM_growvector(L, parent->p, old_size, new_size, Proto*, MAXARG_Bx, "functions");
|
|
|
|
|
parent->p[parent->sizep++] = p;
|
|
|
|
|
// luaC_objbarrier(L, f, clp);
|
|
|
|
|
}
|
|
|
|
|
luaC_objbarrier(L, parent, p);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -63,8 +70,7 @@ static int addk(lua_State *L, Proto* f, Table* h, TValue* key, TValue* v) {
|
|
|
|
|
if (ttisinteger(idx)) { /* is there an index there? */
|
|
|
|
|
k = cast_int(ivalue(idx));
|
|
|
|
|
/* correct value? (warning: must distinguish floats from integers!) */
|
|
|
|
|
if (k < f->sizek && ttype(&f->k[k]) == ttype(v) &&
|
|
|
|
|
luaV_rawequalobj(&f->k[k], v))
|
|
|
|
|
if (k < f->sizek && ttype(&f->k[k]) == ttype(v) && luaV_rawequalobj(&f->k[k], v))
|
|
|
|
|
return k; /* reuse index */
|
|
|
|
|
}
|
|
|
|
|
/* constant not found; create a new entry */
|
|
|
|
@ -74,14 +80,14 @@ static int addk(lua_State *L, Proto* f, Table* h, TValue* key, TValue* v) {
|
|
|
|
|
table has no metatable, so it does not need to invalidate cache */
|
|
|
|
|
setivalue(idx, k);
|
|
|
|
|
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
|
|
|
|
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
|
|
|
|
while (oldsize < f->sizek)
|
|
|
|
|
setnilvalue(&f->k[oldsize++]);
|
|
|
|
|
setobj(L, &f->k[k], v);
|
|
|
|
|
f->sizek++;
|
|
|
|
|
luaC_barrier(L, f, v);
|
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Add a string to list of constants and return its index.
|
|
|
|
|
*/
|
|
|
|
@ -129,7 +135,7 @@ static void lua_setProtoFunction(void* context, Proto* p, lua_CFunction func) {
|
|
|
|
|
|
|
|
|
|
static int load_and_compile(lua_State* L) {
|
|
|
|
|
const char* s = luaL_checkstring(L, 1);
|
|
|
|
|
struct CompilerContext ccontext = { .L = L };
|
|
|
|
|
struct CompilerContext ccontext = {.L = L, .jit = G(L)->ravi_state};
|
|
|
|
|
|
|
|
|
|
LClosure* cl = luaF_newLclosure(L, 1); /* create main closure */
|
|
|
|
|
setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
|
|
|
|
@ -140,27 +146,27 @@ static int load_and_compile(lua_State* L) {
|
|
|
|
|
Proto* main_proto = cl->p = luaF_newproto(L);
|
|
|
|
|
luaC_objbarrier(L, cl, cl->p);
|
|
|
|
|
|
|
|
|
|
struct Ravi_CompilerInterface ravicomp_interface = {
|
|
|
|
|
.source = s,
|
|
|
|
|
struct Ravi_CompilerInterface ravicomp_interface = {.source = s,
|
|
|
|
|
.source_len = strlen(s),
|
|
|
|
|
.source_name = "input",
|
|
|
|
|
.main_proto = main_proto,
|
|
|
|
|
.context = &ccontext,
|
|
|
|
|
.lua_newProto = lua_newProto,
|
|
|
|
|
.lua_newStringConstant = lua_newStringConstant,
|
|
|
|
|
.main_proto = main_proto,
|
|
|
|
|
.context = &ccontext
|
|
|
|
|
};
|
|
|
|
|
.init_C_compiler = init_C_compiler,
|
|
|
|
|
.compile_C = compile_C,
|
|
|
|
|
.finish_C_compiler = finish_C_compiler,
|
|
|
|
|
.get_compiled_function = get_compiled_function,
|
|
|
|
|
.lua_setProtoFunction = lua_setProtoFunction};
|
|
|
|
|
|
|
|
|
|
int rc = raviX_compile(&ravicomp_interface);
|
|
|
|
|
L->top--; /* remove table */
|
|
|
|
|
|
|
|
|
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
|
|
|
|
luaF_initupvals(L, cl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const luaL_Reg ravilib[] = {
|
|
|
|
|
{NULL, NULL} };
|
|
|
|
|
static const luaL_Reg ravilib[] = {{NULL, NULL}};
|
|
|
|
|
|
|
|
|
|
int(raviopen_compiler)(lua_State* L) {
|
|
|
|
|
luaL_newlib(L, ravilib);
|
|
|
|
|