Skip to content

Commit

Permalink
#245 #198 upstream fixes and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Jul 24, 2022
1 parent f665fba commit 15094f3
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 7 deletions.
27 changes: 26 additions & 1 deletion ravicomp/src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ static const char Lua_header[] =
"extern void raviV_settable_sskey(lua_State *L, const TValue *t, TValue *key, TValue *val);\n"
"extern void raviV_gettable_i(lua_State *L, const TValue *t, TValue *key, TValue *val);\n"
"extern void raviV_settable_i(lua_State *L, const TValue *t, TValue *key, TValue *val);\n"
"extern void raviV_op_settable_totop(lua_State *L, CallInfo *ci, TValue *ra, TValue *first_val, int start);\n"
#ifdef RAVI_DEFER_STATEMENT
"extern void raviV_op_defer(lua_State *L, TValue *ra);\n"
#endif
Expand Down Expand Up @@ -1462,20 +1463,39 @@ static int emit_op_load_table(Function *fn, Instruction *insn)
return 0;
}

static int emit_store_range_to_table(Function *fn, Instruction *insn) {
Pseudo *t = get_target(insn, 0);
Pseudo *k = get_target(insn, 1);
Pseudo *v = get_operand(insn, 0);
assert(v->type == PSEUDO_RANGE);
assert(k->type == PSEUDO_CONSTANT && k->constant->type == RAVI_TNUMINT);
raviX_buffer_add_string(&fn->body, "{\n");
raviX_buffer_add_string(&fn->body, " TValue *tab = ");
emit_reg_accessor(fn, t, 0);
raviX_buffer_add_string(&fn->body, ";\n TValue *src = ");
emit_reg_accessor(fn, v, 1);
raviX_buffer_add_fstring(&fn->body, ";\n raviV_op_settable_totop(L, ci, tab, src, %lld);\n ", (long long)k->constant->i);
raviX_buffer_add_string(&fn->body, "}\n");
return 0;
}

/* Emit code for a variety of store table operations */
static int emit_op_store_table(Function *fn, Instruction *insn)
{
// FIXME what happens if key and value are both constants
// Our pseudo reg will break I think
Pseudo *src = get_operand(insn, 0);
const char *fname = "luaV_settable";
if (insn->opcode == op_tput_ikey) {
if (src->type == PSEUDO_RANGE) {
return emit_store_range_to_table(fn, insn);
}
fname = "raviV_settable_i";
} else if (insn->opcode == op_tput_skey) {
fname = "raviV_settable_sskey";
}
Pseudo *env = get_target(insn, 0);
Pseudo *varname = get_target(insn, 1);
Pseudo *src = get_operand(insn, 0);
if (varname->type == PSEUDO_CONSTANT && varname->constant->type == RAVI_TSTRING) {
if (varname->constant->s->len < 40) {
fname = "raviV_settable_sskey";
Expand Down Expand Up @@ -1920,6 +1940,11 @@ static int emit_op_arrayput_val(Function *fn, Instruction *insn)
Pseudo *arr = get_target(insn, 0);
Pseudo *key = get_target(insn, 1);
Pseudo *src = get_operand(insn, 0);

if (src->type == PSEUDO_RANGE) {
return emit_store_range_to_table(fn, insn);
}

raviX_buffer_add_string(&fn->body, "{\n");
raviX_buffer_add_string(&fn->body, " RaviArray *arr = arrvalue(");
emit_reg_accessor(fn, arr, 0);
Expand Down
2 changes: 1 addition & 1 deletion ravicomp/src/linearizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ static Pseudo *linearize_expression(Proc *proc, AstNode *expr)
check_pseudo_is_top(proc, result);
if (result->type == PSEUDO_RANGE && expr->common_expr.truncate_results) {
// Need to truncate the results to 1
return raviX_allocate_range_select_pseudo(proc, result, 0);
return convert_range_to_temp(result);
}
return result;
}
Expand Down
54 changes: 54 additions & 0 deletions src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2860,6 +2860,60 @@ void raviV_op_setlist(lua_State *L, CallInfo *ci, TValue *ra, int b, int c) {
L->top = ci->top; /* correct top (in case of previous open call) */
}

void raviV_op_settable_totop(lua_State *L, CallInfo *ci, TValue *ra, TValue *first_val, int start) {
unsigned int last;
int n = cast_int(L->top - first_val);
last = start + n - 1;
if (ttisLtable(ra)) {
Table *h = hvalue(ra);
if (last > h->sizearray) /* needs more space? */
luaH_resizearray(L, h, last); /* pre-allocate it at once */
for (; n > 0; n--) {
TValue *val = first_val + n - 1;
luaH_setint(L, h, last--, val);
luaC_barrierback(L, obj2gco(h), val);
}
}
else {
RaviArray *h = arrvalue(ra);
int i = start;
for (; i <= (int)last; i++) {
TValue *val = first_val + i - 1;
unsigned int u = (unsigned int)(i);
if ((h->flags & RAVI_ARRAY_ISFLOAT) == 0) {
if (ttisinteger(val)) {
raviH_set_int_inline(L, h, u, ivalue(val));
}
else {
lua_Integer i = 0;
if (tointegerns(val, &i)) {
raviH_set_int_inline(L, h, u, i);
}
else
luaG_runerror(L, "value cannot be converted to integer");
}
}
else {
if (ttisfloat(val)) {
raviH_set_float_inline(L, h, u, fltvalue(val));
}
else if (ttisinteger(val)) {
raviH_set_float_inline(L, h, u, (lua_Number)(ivalue(val)));
}
else {
lua_Number d = 0.0;
if (tonumberns(val, d)) {
raviH_set_float_inline(L, h, u, d);
}
else
luaG_runerror(L, "value cannot be converted to number");
}
}
}
}
//L->top = ci->top; /* correct top (in case of previous open call) */
}

void raviV_op_concat(lua_State *L, CallInfo *ci, int a, int b, int c) {
StkId rb, ra;
StkId base = ci->u.l.base;
Expand Down
1 change: 1 addition & 0 deletions src/lvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ LUAI_FUNC void raviV_gettable_sskey(lua_State *L, const TValue *t, TValue *key,
LUAI_FUNC void raviV_settable_sskey(lua_State *L, const TValue *t, TValue *key, StkId val);
LUAI_FUNC void raviV_gettable_i(lua_State *L, const TValue *t, TValue *key, StkId val);
LUAI_FUNC void raviV_settable_i(lua_State *L, const TValue *t, TValue *key, StkId val);
LUAI_FUNC void raviV_op_settable_totop(lua_State *L, CallInfo *ci, TValue *ra, TValue *first_val, int start);
LUAI_FUNC void raviV_op_totype(lua_State *L, TValue *ra, TValue *rb);
LUAI_FUNC int raviV_checktype(lua_State *L, TValue *input, ravi_type_map type, TString *usertype);
LUAI_FUNC int raviV_check_usertype(lua_State *L, TString *name, const TValue *o);
Expand Down
1 change: 1 addition & 0 deletions src/ravi_mirjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static LuaFunc Lua_functions[] = {
{ "raviV_settable_sskey", raviV_settable_sskey },
{ "raviV_gettable_i", raviV_gettable_i },
{ "raviV_settable_i", raviV_settable_i },
{ "raviV_op_settable_totop", raviV_op_settable_totop },
#ifdef RAVI_DEFER_STATEMENT
{ "raviV_op_defer", raviV_op_defer },
#endif
Expand Down
9 changes: 4 additions & 5 deletions tests/comptests/inputs/68_calls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ a,b,c,d = ret2(f()), ret2(f())
assert(a==1 and b==1 and c==2 and d==nil)
a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
assert(a==1 and b==1 and c==2 and d==nil)
-- a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
-- assert(a==1 and b==1 and c==nil and d==nil)
--
-- a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
-- assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
assert(a==1 and b==1 and c==nil and d==nil)
a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")

print '68 Ok'
6 changes: 6 additions & 0 deletions tests/comptests/inputs/72_table_construct.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local function f() return 1,2,3 end
local t = { f() }
assert( t[1] == 1 and t[2] == 2 and t[3] == 3 )
assert( #t == 3 )

print '72 Ok'
6 changes: 6 additions & 0 deletions tests/comptests/inputs/73_table_construct.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local function f() return 1,2,3 end
local t: integer[] = { f() }
assert( t[1] == 1 and t[2] == 2 and t[3] == 3 )
assert( #t == 3 )

print '73 Ok'
2 changes: 2 additions & 0 deletions tests/comptests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ runfile("inputs/68_calls.lua")
runfile("inputs/69_binexpr.lua")
runfile("inputs/70_gaussian2.lua")
runfile("inputs/71_indexedload.lua")
runfile("inputs/72_table_construct.lua")
--runfile("inputs/73_table_construct.lua")


print 'SUCCESS'
Expand Down

0 comments on commit 15094f3

Please sign in to comment.