issue #198 Refactored api that puts all of the codegen at the compiler end, making the way for an AOT solution

pull/200/head
Dibyendu Majumdar 3 years ago
parent bb218051ba
commit 8d6e403f30

@ -18,7 +18,6 @@
struct CompilerContext {
lua_State* L;
ravi_State* jit;
Table* h; /* to avoid collection/reuse strings */
};
static void debug_message(void* context, const char* filename, long long line, const char* message) {
@ -37,225 +36,52 @@ void error_message(void* context, const char* message) {
ravi_writeline(ccontext->L);
}
/* 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);
assert(parent);
/* FIXME make this more efficient */
int old_size = parent->sizep;
int new_size = parent->sizep + 1;
luaM_reallocvector(L, parent->p, old_size, new_size, Proto*);
parent->p[old_size] = p;
parent->sizep++;
lua_assert(parent->sizep == new_size);
luaC_objbarrier(L, parent, p);
return p;
}
/*
* Based off the Lua lexer code. Strings are anchored in a table initially - eventually
* ending up either in a Proto constant table or some other structure related to
* protos.
*/
TString* intern_string(lua_State* L, Table* h, const char* str, size_t l) {
TValue* o; /* entry for 'str' */
TString* ts = luaS_newlstr(L, str, l); /* create new string */
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
o = luaH_set(L, h, L->top - 1);
if (ttisnil(o)) { /* not in use yet? */
/* boolean value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */
setbvalue(o, 1); /* t[string] = true */
luaC_checkGC(L);
}
else { /* string already present */
ts = tsvalue(keyfromval(o)); /* re-use value previously stored */
}
L->top--; /* remove string from stack */
return ts;
}
/*
** Add constant 'v' to prototype's list of constants (field 'k').
** Use parser's table to cache position of constants in constant list
** and try to reuse constants.
*/
static int add_konstant(lua_State* L, Proto* f, Table* h, TValue* key, TValue* v) {
TValue* idx = luaH_set(L, h, key); /* The k index is cached against the key */
int k, oldsize, newsize;
if (ttisinteger(idx)) { /* is there an integer value index there? */
k = cast_int(ivalue(idx));
/* correct value? (warning: must distinguish floats from integers!) */
if (k >= 0 && 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 in Proto->k */
oldsize = k = f->sizek;
newsize = oldsize + 1;
/* numerical value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */
setivalue(idx, k);
// FIXME make the allocation more efficient
luaM_reallocvector(L, f->k, oldsize, newsize, TValue);
setobj(L, &f->k[k], v); /* record the position k in the table against key */
f->sizek++;
lua_assert(f->sizek == newsize);
luaC_barrier(L, f, v);
return k;
}
/*
** Add a string to list of constants and return its index.
*/
static int add_string_konstant(lua_State* L, Proto* p, Table* h, TString* s) {
TValue o;
setsvalue(L, &o, s);
return add_konstant(L, p, h, &o, &o); /* use string itself as key */
}
/* Create a Lua TString object from a string. Save it so that we can avoid creating same
* string again.
*/
static inline TString* create_luaString(lua_State* L, Table* h, struct string_object* s) {
if (s->userdata == NULL) {
/* Create and save it */
s->userdata = intern_string(L, h, s->str, s->len);
}
return (TString*)s->userdata;
}
/* Add a string constant to Proto and return its index */
static int lua_newStringConstant(void* context, Proto* proto, struct string_object* s) {
struct CompilerContext* ccontext = (struct CompilerContext*)context;
lua_State* L = ccontext->L;
Table* h = ccontext->h;
TString* ts = create_luaString(L, h, s);
return add_string_konstant(L, proto, h, ts);
}
/* Add an upvalue. If the upvalue refers to a local variable in parent proto then idx should contain
* the register for the local variable and instack should be true, else idx should have the index of
* upvalue in parent proto and instack should be false.
*/
static int lua_addUpValue(void* context, Proto* f, struct string_object* name, unsigned idx, int instack, unsigned tc,
struct string_object* usertype) {
ravitype_t typecode = (ravitype_t)tc;
struct CompilerContext* ccontext = (struct CompilerContext*)context;
lua_State* L = ccontext->L;
Table* h = ccontext->h;
int oldsize = f->sizeupvalues;
int newsize = oldsize + 1;
int pos = oldsize;
// checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
// FIXME optimize the allocation
luaM_reallocvector(L, f->upvalues, oldsize, newsize, Upvaldesc);
f->sizeupvalues++;
lua_assert(f->sizeupvalues == newsize);
f->upvalues[pos].instack = cast_byte(instack); /* is the upvalue in parent function's local stack ? */
f->upvalues[pos].idx = cast_byte(idx); /* If instack then parent's local register else parent's upvalue index */
TString* tsname = create_luaString(L, h, name); /* name of the variable */
f->upvalues[pos].name = tsname;
f->upvalues[pos].ravi_type = typecode;
if (usertype != NULL) {
/* User type string goes into the proto's constant table */
int kpos = lua_newStringConstant(context, f, usertype);
f->upvalues[pos].usertype = tsvalue(&f->k[kpos]);
}
else {
f->upvalues[pos].usertype = NULL;
}
luaC_objbarrier(L, f, tsname);
return pos;
}
static void init_C_compiler(void* context) {
struct CompilerContext* ccontext = (struct CompilerContext*)context;
#ifdef USE_MIRJIT
mir_prepare(ccontext->jit->jit, 2);
#endif
}
static void* compile_C(void* context, const char* C_src, unsigned len) {
struct CompilerContext* ccontext = (struct CompilerContext*)context;
fprintf(stdout, "%s\n", C_src);
#ifdef USE_MIRJIT
return mir_compile_C_module(&ccontext->jit->options, ccontext->jit->jit, C_src, "input");
#else
return NULL;
#endif
}
static void finish_C_compiler(void* context) {
struct CompilerContext* ccontext = (struct CompilerContext*)context;
#ifdef USE_MIRJIT
mir_cleanup(ccontext->jit->jit);
#endif
}
static lua_CFunction get_compiled_function(void* context, void* module, const char* name) {
struct CompilerContext* ccontext = (struct CompilerContext*)context;
#if USE_MIRJIT
MIR_module_t M = (MIR_module_t)module;
return (lua_CFunction)mir_get_func(ccontext->jit->jit, M, name);
#else
return NULL;
#endif
}
static void lua_setProtoFunction(void* context, Proto* p, lua_CFunction func) {
p->ravi_jit.jit_function = func;
p->ravi_jit.jit_status = RAVI_JIT_COMPILED;
}
static void lua_setVarArg(void* context, Proto* p) { p->is_vararg = 1; }
static void lua_setNumParams(void* context, Proto* p, unsigned num_params) { p->numparams = cast_byte(num_params); }
static void lua_setMaxStackSize(void *context, Proto *p, unsigned max_stack_size) {
p->maxstacksize = cast_byte(max_stack_size);
}
static int load_and_compile(lua_State* L) {
const char* s = luaL_checkstring(L, 1);
struct CompilerContext ccontext = {.L = L, .jit = G(L)->ravi_state};
LClosure* cl = luaF_newLclosure(L, 1); /* create main closure with 1 up-value for _ENV */
setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
luaD_inctop(L);
ccontext.h = luaH_new(L); /* create table for string constants */
sethvalue(L, L->top, ccontext.h); /* anchor it */
luaD_inctop(L);
Proto* main_proto = cl->p = luaF_newproto(L);
luaC_objbarrier(L, cl, cl->p);
struct Ravi_CompilerInterface ravicomp_interface = {.source = s,
.source_len = strlen(s),
.source_name = "input",
.main_proto = main_proto,
.generated_code = NULL,
.context = &ccontext,
.lua_newProto = lua_newProto,
.lua_newStringConstant = lua_newStringConstant,
.lua_addUpValue = lua_addUpValue,
.lua_setVarArg = lua_setVarArg,
.lua_setProtoFunction = lua_setProtoFunction,
.lua_setNumParams = lua_setNumParams,
.lua_setMaxStackSize = lua_setMaxStackSize,
.init_C_compiler = init_C_compiler,
.compile_C = compile_C,
.finish_C_compiler = finish_C_compiler,
.get_compiled_function = get_compiled_function,
.debug_message = debug_message,
.error_message = error_message};
int rc = raviX_compile(&ravicomp_interface);
L->top--; /* remove table for string constants */
if (rc == 0) {
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luaF_initupvals(L, cl);
if (cl->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, cl->upvals[0]->v, gt);
luaC_upvalbarrier(L, cl->upvals[0], gt);
#ifdef USE_MIRJIT
LClosure* (*load_in_lua)(lua_State * L) = NULL;
fprintf(stdout, "%s\n", ravicomp_interface.generated_code);
mir_prepare(ccontext.jit->jit, 2);
MIR_module_t M =
mir_compile_C_module(&ccontext.jit->options, ccontext.jit->jit, ravicomp_interface.generated_code, "input");
if (M != NULL) {
load_in_lua = mir_get_func(ccontext.jit->jit, M, "load_in_lua");
}
mir_cleanup(ccontext.jit->jit);
if (load_in_lua) {
LClosure* cl = load_in_lua(L);
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luaF_initupvals(L, cl);
if (cl->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
Table* reg = hvalue(&G(L)->l_registry);
const TValue* gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, cl->upvals[0]->v, gt);
luaC_upvalbarrier(L, cl->upvals[0], gt);
}
}
else {
return 0;
}
return 1;
#else
return 0;
#endif
}
else {
lua_error(L);

@ -174,6 +174,11 @@ static LuaFunc Lua_functions[] = {
{ "lua_setmetatable", lua_setmetatable },
{ "lua_setuservalue", lua_setuservalue },
{ "luaD_growstack", luaD_growstack },
{ "luaF_newLclosure", luaF_newLclosure },
{ "luaS_newlstr", luaS_newlstr },
{ "luaF_newproto", luaF_newproto },
{ "luaD_inctop", luaD_inctop },
{ "luaM_realloc_", luaM_realloc_ },
{ NULL, NULL }
};

@ -8,3 +8,4 @@ assert(c == 5.6)
assert(d == true)
assert(x == 4.3)
assert(e == nil)
print 'Ok'

@ -5,4 +5,5 @@ local a,b,c,d = f()
assert(z == 62)
assert(a == 4.2)
assert(b == true)
assert(c == 'hi')
assert(c == 'hi')
print 'Ok'

@ -1,3 +1,4 @@
f = compiler.load("local a,b = 1,2 return a or b");
assert(f and type(f) == 'function')
assert(f() == 1)
assert(f() == 1)
print 'Ok'

@ -1,3 +1,4 @@
f = compiler.load("local a,b = 1,2 return a and b");
assert(f and type(f) == 'function')
assert(f() == 2)
assert(f() == 2)
print 'Ok'

@ -1,3 +1,4 @@
f = compiler.load("local a = 2 return 1 or a");
assert(f and type(f) == 'function')
assert(f() == 1)
assert(f() == 1)
print 'Ok'

@ -1,3 +1,4 @@
f = compiler.load("local a = 2 return 1 and a");
assert(f and type(f) == 'function')
assert(f() == 2)
assert(f() == 2)
print 'Ok'

@ -1,4 +1,5 @@
f = compiler.load('x=1')
assert(f and type(f) == 'function')
f()
assert(x==1)
assert(x==1)
print 'Ok'

@ -4,4 +4,5 @@ f = compiler.load('return x,y')
assert(f and type(f) == 'function')
local a,b = f()
assert(a == 1)
assert(b == 2)
assert(b == 2)
print 'Ok'

@ -4,4 +4,5 @@ f = compiler.load('x,y = y,x')
assert(f and type(f) == 'function')
f()
assert(x == 2)
assert(y == 4)
assert(y == 4)
print 'Ok'

@ -2,4 +2,5 @@ function g() return 4.2 end
f = compiler.load('return g()')
assert(f and type(f) == 'function')
local a = f()
assert(a == 4.2)
assert(a == 4.2)
print 'Ok'

@ -3,4 +3,5 @@ f = compiler.load('return g()')
assert(f and type(f) == 'function')
local a, b = f()
assert(a == 3)
assert(b == 4.2)
assert(b == 4.2)
print 'Ok'

@ -3,4 +3,5 @@ f = compiler.load('return g(), 1')
assert(f and type(f) == 'function')
local a, b = f()
assert(a == 3)
assert(b == 1)
assert(b == 1)
print 'Ok'

@ -4,4 +4,5 @@ assert(f and type(f) == 'function')
local a, b, c = f()
assert(a == 0)
assert(b == 3)
assert(c == 1)
assert(c == 1)
print 'Ok'

@ -8,4 +8,5 @@ assert(f and type(f) == 'function')
local a, b, c = f()
assert(a == 0)
assert(b == 3)
assert(c == 1)
assert(c == 1)
print 'Ok'

@ -18,4 +18,5 @@ assert(s == 18)
assert(arr[0] == 10)
assert(arr[4] == 2)
assert(arr[11] == 6)
assert(#arr == 11)
assert(#arr == 11)
print 'Ok'

@ -28,3 +28,4 @@ local t2 = os.clock()
print("time taken ", t2-t1)
print(count)
assert(count == 1899)
print 'Ok'

@ -19,3 +19,4 @@ assert(x[2] == 2.2)
assert(x[11] == 11.11)
z(x, 12, 12)
assert(x[12] == 12.0)
print 'Ok'

@ -36,3 +36,4 @@ local t2 = os.clock()
print("time taken ", t2-t1)
print(count)
assert(count == 1899)
print 'Ok'

@ -3,4 +3,5 @@ f = compiler.load("x.a = 'Dibyendu' x[1] = 2")
assert(f and type(f) == 'function')
f()
assert(x.a == 'Dibyendu')
assert(x[1] == 2)
assert(x[1] == 2)
print 'Ok'

@ -4,4 +4,5 @@ assert(math.abs(adder() - 3.3) < 1e-15)
mult = compiler.load('local a: number, b: number = 1.1, 2.2 return a*b')
assert(mult and type(mult) == 'function')
assert(math.abs(mult() - 2.42) < 1e-15)
assert(math.abs(mult() - 2.42) < 1e-15)
print 'Ok'

@ -27,3 +27,4 @@ assert(l and type(l) == 'function')
b = l()
assert(b == 4)
assert('integer' == math.type(b))
print 'Ok'

@ -51,4 +51,5 @@ local t1 = os.clock()
local a = matrix.mul(matrix.gen(n), matrix.gen(n))
local t2 = os.clock()
print("time taken ", t2-t1)
print(a[n/2+1][n//2+1]);
print(a[n/2+1][n//2+1])
print 'Ok'

@ -13,3 +13,4 @@ assert(f() == true)
f = compiler.load('return b >= a')
assert(f and type(f) == 'function')
assert(f() == true)
print 'Ok'

@ -44,4 +44,5 @@ assert(doit and type(doit) == 'function')
assert(math.abs(doit(11.1, 'add') - 16.1) < 1e-15)
assert(math.abs(doit(1.1, 'mul') - 5.5) < 1e-15)
assert(math.abs(doit(5.5, 'sub') + 0.5) < 1e-15)
assert(math.abs(doit(2.5, 'div') - 2.0) < 1e-15)
assert(math.abs(doit(2.5, 'div') - 2.0) < 1e-15)
print 'Ok'
Loading…
Cancel
Save