Skip to content

Commit

Permalink
issue #217 define ravitype_t in terms of the new values
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Feb 21, 2021
1 parent 347ae98 commit 58980db
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 98 deletions.
62 changes: 49 additions & 13 deletions ravicomp/src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,58 @@ static const char Lua_header[] =
" io->value_ = iu->user_; settt_(io, iu->ttuv_); \\\n"
" checkliveness(L,io); }\n"
"typedef enum {\n"
" RAVI_TANY = 0,\n"
" RAVI_TNUMINT = 1,\n"
" RAVI_TNUMFLT,\n"
" RAVI_TARRAYINT,\n"
" RAVI_TARRAYFLT,\n"
" RAVI_TFUNCTION,\n"
" RAVI_TTABLE,\n"
" RAVI_TSTRING,\n"
" RAVI_TNIL,\n"
" RAVI_TBOOLEAN,\n"
" RAVI_TUSERDATA\n"
"RAVI_TI_NIL,\n"
"RAVI_TI_FALSE,\n"
"RAVI_TI_TRUE,\n"
"RAVI_TI_INTEGER,\n"
"RAVI_TI_FLOAT,\n"
"RAVI_TI_INTEGER_ARRAY,\n"
"RAVI_TI_FLOAT_ARRAY,\n"
"RAVI_TI_TABLE,\n"
"RAVI_TI_STRING,\n"
"RAVI_TI_FUNCTION,\n"
"RAVI_TI_USERDATA,\n"
"RAVI_TI_OTHER\n"
"} ravi_type_index;\n"
"typedef uint32_t ravi_type_map;\n"
"#define RAVI_TM_NIL (((ravi_type_map)1)<<RAVI_TI_NIL)\n"
"#define RAVI_TM_FALSE (((ravi_type_map)1)<<RAVI_TI_FALSE)\n"
"#define RAVI_TM_TRUE (((ravi_type_map)1)<<RAVI_TI_TRUE)\n"
"#define RAVI_TM_INTEGER (((ravi_type_map)1)<<RAVI_TI_INTEGER)\n"
"#define RAVI_TM_FLOAT (((ravi_type_map)1)<<RAVI_TI_FLOAT)\n"
"#define RAVI_TM_INTEGER_ARRAY (((ravi_type_map)1)<<RAVI_TI_INTEGER_ARRAY)\n"
"#define RAVI_TM_FLOAT_ARRAY (((ravi_type_map)1)<<RAVI_TI_FLOAT_ARRAY)\n"
"#define RAVI_TM_TABLE (((ravi_type_map)1)<<RAVI_TI_TABLE)\n"
"#define RAVI_TM_STRING (((ravi_type_map)1)<<RAVI_TI_STRING)\n"
"#define RAVI_TM_FUNCTION (((ravi_type_map)1)<<RAVI_TI_FUNCTION)\n"
"#define RAVI_TM_USERDATA (((ravi_type_map)1)<<RAVI_TI_USERDATA)\n"
"#define RAVI_TM_OTHER (((ravi_type_map)1)<<RAVI_TI_OTHER)\n"
"#define RAVI_TM_FALSISH (RAVI_TM_NIL | RAVI_TM_FALSE)\n"
"#define RAVI_TM_TRUISH (~RAVI_TM_FALSISH)\n"
"#define RAVI_TM_BOOLEAN (RAVI_TM_FALSE | RAVI_TM_TRUE)\n"
"#define RAVI_TM_NUMBER (RAVI_TM_INTEGER | RAVI_TM_FLOAT)\n"
"#define RAVI_TM_INDEXABLE (RAVI_TM_INTEGER_ARRAY | RAVI_TM_FLOAT_ARRAY | RAVI_TM_TABLE)\n"
"#define RAVI_TM_ANY (~0)\n"
"typedef enum {\n"
"RAVI_TNIL = RAVI_TM_NIL, /* NIL */\n"
"RAVI_TNUMINT = RAVI_TM_INTEGER, /* integer number */\n"
"RAVI_TNUMFLT = RAVI_TM_FLOAT, /* floating point number */\n"
"RAVI_TNUMBER = RAVI_TM_NUMBER,\n"
"RAVI_TARRAYINT = RAVI_TM_INTEGER_ARRAY, /* array of ints */\n"
"RAVI_TARRAYFLT = RAVI_TM_FLOAT_ARRAY, /* array of doubles */\n"
"RAVI_TTABLE = RAVI_TM_TABLE, /* Lua table */\n"
"RAVI_TSTRING = RAVI_TM_STRING | RAVI_TM_NIL, /* string */\n"
"RAVI_TFUNCTION = RAVI_TM_FUNCTION | RAVI_TM_NIL, /* Lua or C Function */\n"
"RAVI_TBOOLEAN = RAVI_TM_BOOLEAN | RAVI_TM_NIL, /* boolean */\n"
"RAVI_TTRUE = RAVI_TM_TRUE,\n"
"RAVI_TFALSE = RAVI_TM_FALSE,\n"
"RAVI_TUSERDATA = RAVI_TM_USERDATA | RAVI_TM_NIL, /* userdata or lightuserdata */\n"
"RAVI_TANY = RAVI_TM_ANY, /* Lua dynamic type */\n"
"} ravitype_t;\n"
"typedef struct Upvaldesc {\n"
" TString *name;\n"
" TString *usertype;\n"
" lu_byte ravi_type;\n"
" ravi_type_map ravi_type;\n"
" lu_byte instack;\n"
" lu_byte idx;\n"
"} Upvaldesc;\n"
Expand All @@ -337,7 +373,7 @@ static const char Lua_header[] =
" TString *usertype;\n"
" int startpc;\n"
" int endpc;\n"
" lu_byte ravi_type;\n"
" ravi_type_map ravi_type;\n"
"} LocVar;\n"
"typedef enum {\n"
" RAVI_JIT_NOT_COMPILED = 0,\n"
Expand Down
2 changes: 1 addition & 1 deletion src/ldebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
** not subvert the types of local variables
*/
StkId input = L->top - 1;
int compatible = ravi_checktype(L, input, type, usertype);
int compatible = raviV_checktype(L, input, type, usertype);
if (compatible) {
setobjs2s(L, pos, L->top - 1);
L->top--; /* pop value */
Expand Down
21 changes: 2 additions & 19 deletions src/ldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,6 @@ static void DumpUpvalues (const Proto *f, DumpState *D) {
}
}

static lu_byte ravi_type_map_to_old_type(ravi_type_map type_map) {
switch (type_map) {
case RAVI_TM_ANY: return 0;
case RAVI_TM_INTEGER: return 1;
case RAVI_TM_FLOAT: return 2;
case RAVI_TM_INTEGER_ARRAY: return 3;
case RAVI_TM_FLOAT_ARRAY: return 4;
case RAVI_TM_FUNCTION | RAVI_TM_NIL: return 5;
case RAVI_TM_TABLE: return 6;
case RAVI_TM_STRING | RAVI_TM_NIL: return 7;
case RAVI_TM_NIL: return 8;
case RAVI_TM_BOOLEAN | RAVI_TM_NIL: return 9;
case RAVI_TM_USERDATA | RAVI_TM_NIL: return 10;
default: return 0;
}
}

static void DumpDebug (const Proto *f, DumpState *D) {
int i, n;
n = (D->strip) ? 0 : f->sizelineinfo;
Expand All @@ -179,15 +162,15 @@ static void DumpDebug (const Proto *f, DumpState *D) {
DumpString((D->strip) ? NULL : f->locvars[i].varname, D);
DumpInt(f->locvars[i].startpc, D);
DumpInt(f->locvars[i].endpc, D);
DumpByte(ravi_type_map_to_old_type(f->locvars[i].ravi_type_map), D);
DumpInt(f->locvars[i].ravi_type_map, D);
DumpString(f->locvars[i].usertype, D);
}
/* n = (D->strip) ? 0 : f->sizeupvalues; */
n = f->sizeupvalues;
DumpInt(n, D);
for (i = 0; i < n; i++) {
DumpString((D->strip) ? NULL : f->upvalues[i].name, D);
DumpByte(ravi_type_map_to_old_type(f->upvalues[i].ravi_type_map), D);
DumpInt(f->upvalues[i].ravi_type_map, D);
DumpString(f->upvalues[i].usertype, D);
}
}
Expand Down
18 changes: 0 additions & 18 deletions src/lobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@
LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};


int ravi_checktype(lua_State *L, StkId input, ravi_type_map type, TString* usertype) {
if (type == RAVI_TM_ANY) return 1;
if (type & RAVI_TM_NIL && ttisnil(input)) return 1;
if (type & RAVI_TM_FALSE && ttisboolean(input) && l_isfalse(input)) return 1;
if (type & RAVI_TM_TRUE && ttisboolean(input) && !l_isfalse(input)) return 1;
if (type & RAVI_TM_INTEGER && ttisinteger(input)) return 1;
if (type & RAVI_TM_FLOAT && ttisfloat(input)) return 1;
if (type & RAVI_TM_INTEGER_ARRAY && ttisiarray(input)) return 1;
if (type & RAVI_TM_FLOAT_ARRAY && ttisfarray(input)) return 1;
if (type & RAVI_TM_TABLE && ttisLtable(input)) return 1;
if (type & RAVI_TM_STRING && ttisstring(input)) return 1;
if (type & RAVI_TM_FUNCTION && ttisclosure(input)) return 1;
if (type & RAVI_TM_USERDATA) {
if (raviV_check_usertype(L, usertype, input)) return 1;
}
return 0;
}

/*
** converts an integer to a "floating point byte", represented as
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
Expand Down
17 changes: 17 additions & 0 deletions src/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ typedef uint32_t ravi_type_map;
#define RAVI_TM_INDEXABLE (RAVI_TM_INTEGER_ARRAY | RAVI_TM_FLOAT_ARRAY | RAVI_TM_TABLE)
#define RAVI_TM_ANY (~0)

typedef enum {
RAVI_TNIL = RAVI_TM_NIL, /* NIL */
RAVI_TNUMINT = RAVI_TM_INTEGER, /* integer number */
RAVI_TNUMFLT = RAVI_TM_FLOAT, /* floating point number */
RAVI_TNUMBER = RAVI_TM_NUMBER,
RAVI_TARRAYINT = RAVI_TM_INTEGER_ARRAY, /* array of ints */
RAVI_TARRAYFLT = RAVI_TM_FLOAT_ARRAY, /* array of doubles */
RAVI_TTABLE = RAVI_TM_TABLE, /* Lua table */
RAVI_TSTRING = RAVI_TM_STRING | RAVI_TM_NIL, /* string */
RAVI_TFUNCTION = RAVI_TM_FUNCTION | RAVI_TM_NIL, /* Lua or C Function */
RAVI_TBOOLEAN = RAVI_TM_BOOLEAN | RAVI_TM_NIL, /* boolean */
RAVI_TTRUE = RAVI_TM_TRUE,
RAVI_TFALSE = RAVI_TM_FALSE,
RAVI_TUSERDATA = RAVI_TM_USERDATA | RAVI_TM_NIL, /* userdata or lightuserdata */
RAVI_TANY = RAVI_TM_ANY, /* Lua dynamic type */
} ravitype_t;

/*
** Tagged Values. This is the basic representation of values in Lua,
** an actual value plus a tag with its type.
Expand Down
22 changes: 2 additions & 20 deletions src/lundump.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,6 @@ static void LoadUpvalues (LoadState *S, Proto *f) {
}
}

static ravi_type_map ravi_old_type_to_type_map(lu_byte old) {
switch (old) {
case 0: return RAVI_TM_ANY;
case 1: return RAVI_TM_INTEGER;
case 2: return RAVI_TM_FLOAT;
case 3: return RAVI_TM_INTEGER_ARRAY;
case 4: return RAVI_TM_FLOAT_ARRAY;
case 5: return RAVI_TM_FUNCTION | RAVI_TM_NIL;
case 6: return RAVI_TM_TABLE;
case 7: return RAVI_TM_STRING | RAVI_TM_NIL;
case 8: return RAVI_TM_NIL;
case 9: return RAVI_TM_BOOLEAN | RAVI_TM_NIL;
case 10: return RAVI_TM_USERDATA | RAVI_TM_NIL;
default: return RAVI_TM_ANY;
}
}


static void LoadDebug (LoadState *S, Proto *f) {
int i, n;
n = LoadInt(S);
Expand All @@ -218,13 +200,13 @@ static void LoadDebug (LoadState *S, Proto *f) {
f->locvars[i].varname = LoadString(S);
f->locvars[i].startpc = LoadInt(S);
f->locvars[i].endpc = LoadInt(S);
f->locvars[i].ravi_type_map = ravi_old_type_to_type_map(LoadByte(S));
f->locvars[i].ravi_type_map = LoadInt(S);
f->locvars[i].usertype = LoadString(S);
}
n = LoadInt(S);
for (i = 0; i < n; i++) {
f->upvalues[i].name = LoadString(S);
f->upvalues[i].ravi_type_map = ravi_old_type_to_type_map(LoadByte(S));
f->upvalues[i].ravi_type_map = LoadInt(S);
f->upvalues[i].usertype = LoadString(S);
}
}
Expand Down
39 changes: 15 additions & 24 deletions src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1204,32 +1204,23 @@ void luaV_finishOp (lua_State *L) {
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
Protect(luaV_finishset(L,t,k,v,slot)); }

int raviV_checktype(lua_State *L, TValue *input, ravitype_t type, TString *usertype) {
if (type == RAVI_TANY)
return 1;
if (type == RAVI_TNIL && ttisnil(input))
return 1;
if (type == RAVI_TBOOLEAN && ttisboolean(input))
return 1;
if (type == RAVI_TNUMINT && ttisinteger(input))
return 1;
if (type == RAVI_TNUMFLT && ttisfloat(input))
return 1;
if (type == RAVI_TARRAYINT && ttisiarray(input))
return 1;
if (type == RAVI_TARRAYFLT && ttisfarray(input))
return 1;
if (type == RAVI_TTABLE && ttisLtable(input))
return 1;
if (type == RAVI_TSTRING && ttisstring(input))
return 1;
if (type == RAVI_TFUNCTION && ttisclosure(input))
return 1;
if (type == RAVI_TUSERDATA) {
if (raviV_check_usertype(L, usertype, input))
return 1;
int raviV_checktype(lua_State *L, TValue *input, ravi_type_map type, TString *usertype) {
if (type == RAVI_TM_ANY) return 1;
if (type & RAVI_TM_NIL && ttisnil(input)) return 1;
if (type & RAVI_TM_FALSE && ttisboolean(input) && l_isfalse(input)) return 1;
if (type & RAVI_TM_TRUE && ttisboolean(input) && !l_isfalse(input)) return 1;
if (type & RAVI_TM_INTEGER && ttisinteger(input)) return 1;
if (type & RAVI_TM_FLOAT && ttisfloat(input)) return 1;
if (type & RAVI_TM_INTEGER_ARRAY && ttisiarray(input)) return 1;
if (type & RAVI_TM_FLOAT_ARRAY && ttisfarray(input)) return 1;
if (type & RAVI_TM_TABLE && ttisLtable(input)) return 1;
if (type & RAVI_TM_STRING && ttisstring(input)) return 1;
if (type & RAVI_TM_FUNCTION && ttisclosure(input)) return 1;
if (type & RAVI_TM_USERDATA) {
if (raviV_check_usertype(L, usertype, input)) return 1;
}
return 0;

}

int raviV_check_usertype(lua_State *L, TString *name, const TValue *o)
Expand Down
2 changes: 1 addition & 1 deletion src/lvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ LUAI_FUNC void raviV_settable_sskey(lua_State *L, const TValue *t, TValue *key,
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_totype(lua_State *L, TValue *ra, TValue *rb);
LUAI_FUNC int raviV_checktype(lua_State *L, TValue *input, ravitype_t type, TString *usertype);
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);
#ifdef RAVI_DEFER_STATEMENT
LUAI_FUNC void raviV_op_defer(lua_State *L, TValue *ra);
Expand Down
53 changes: 51 additions & 2 deletions src/ravi_jitshared.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,59 @@ static const char Lua_header[] =
" { TValue *io=(o); const Udata *iu = (u); \\\n"
" io->value_ = iu->user_; settt_(io, iu->ttuv_); \\\n"
" checkliveness(L,io); }\n"
"typedef enum {\n"
"RAVI_TI_NIL,\n"
"RAVI_TI_FALSE,\n"
"RAVI_TI_TRUE,\n"
"RAVI_TI_INTEGER,\n"
"RAVI_TI_FLOAT,\n"
"RAVI_TI_INTEGER_ARRAY,\n"
"RAVI_TI_FLOAT_ARRAY,\n"
"RAVI_TI_TABLE,\n"
"RAVI_TI_STRING,\n"
"RAVI_TI_FUNCTION,\n"
"RAVI_TI_USERDATA,\n"
"RAVI_TI_OTHER\n"
"} ravi_type_index;\n"
"typedef uint32_t ravi_type_map;\n"
"#define RAVI_TM_NIL (((ravi_type_map)1)<<RAVI_TI_NIL)\n"
"#define RAVI_TM_FALSE (((ravi_type_map)1)<<RAVI_TI_FALSE)\n"
"#define RAVI_TM_TRUE (((ravi_type_map)1)<<RAVI_TI_TRUE)\n"
"#define RAVI_TM_INTEGER (((ravi_type_map)1)<<RAVI_TI_INTEGER)\n"
"#define RAVI_TM_FLOAT (((ravi_type_map)1)<<RAVI_TI_FLOAT)\n"
"#define RAVI_TM_INTEGER_ARRAY (((ravi_type_map)1)<<RAVI_TI_INTEGER_ARRAY)\n"
"#define RAVI_TM_FLOAT_ARRAY (((ravi_type_map)1)<<RAVI_TI_FLOAT_ARRAY)\n"
"#define RAVI_TM_TABLE (((ravi_type_map)1)<<RAVI_TI_TABLE)\n"
"#define RAVI_TM_STRING (((ravi_type_map)1)<<RAVI_TI_STRING)\n"
"#define RAVI_TM_FUNCTION (((ravi_type_map)1)<<RAVI_TI_FUNCTION)\n"
"#define RAVI_TM_USERDATA (((ravi_type_map)1)<<RAVI_TI_USERDATA)\n"
"#define RAVI_TM_OTHER (((ravi_type_map)1)<<RAVI_TI_OTHER)\n"
"#define RAVI_TM_FALSISH (RAVI_TM_NIL | RAVI_TM_FALSE)\n"
"#define RAVI_TM_TRUISH (~RAVI_TM_FALSISH)\n"
"#define RAVI_TM_BOOLEAN (RAVI_TM_FALSE | RAVI_TM_TRUE)\n"
"#define RAVI_TM_NUMBER (RAVI_TM_INTEGER | RAVI_TM_FLOAT)\n"
"#define RAVI_TM_INDEXABLE (RAVI_TM_INTEGER_ARRAY | RAVI_TM_FLOAT_ARRAY | RAVI_TM_TABLE)\n"
"#define RAVI_TM_ANY (~0)\n"
"typedef enum {\n"
"RAVI_TNIL = RAVI_TM_NIL, /* NIL */\n"
"RAVI_TNUMINT = RAVI_TM_INTEGER, /* integer number */\n"
"RAVI_TNUMFLT = RAVI_TM_FLOAT, /* floating point number */\n"
"RAVI_TNUMBER = RAVI_TM_NUMBER,\n"
"RAVI_TARRAYINT = RAVI_TM_INTEGER_ARRAY, /* array of ints */\n"
"RAVI_TARRAYFLT = RAVI_TM_FLOAT_ARRAY, /* array of doubles */\n"
"RAVI_TTABLE = RAVI_TM_TABLE, /* Lua table */\n"
"RAVI_TSTRING = RAVI_TM_STRING | RAVI_TM_NIL, /* string */\n"
"RAVI_TFUNCTION = RAVI_TM_FUNCTION | RAVI_TM_NIL, /* Lua or C Function */\n"
"RAVI_TBOOLEAN = RAVI_TM_BOOLEAN | RAVI_TM_NIL, /* boolean */\n"
"RAVI_TTRUE = RAVI_TM_TRUE,\n"
"RAVI_TFALSE = RAVI_TM_FALSE,\n"
"RAVI_TUSERDATA = RAVI_TM_USERDATA | RAVI_TM_NIL, /* userdata or lightuserdata */\n"
"RAVI_TANY = RAVI_TM_ANY, /* Lua dynamic type */\n"
"} ravitype_t;\n"
"typedef struct Upvaldesc {\n"
" TString *name;\n"
" TString *usertype;\n"
" lu_byte ravi_type;\n"
" ravi_type_map ravi_type;\n"
" lu_byte instack;\n"
" lu_byte idx;\n"
"} Upvaldesc;\n"
Expand All @@ -311,7 +360,7 @@ static const char Lua_header[] =
" TString *usertype;\n"
" int startpc;\n"
" int endpc;\n"
" lu_byte ravi_type;\n"
" ravi_type_map ravi_type;\n"
"} LocVar;\n"
"typedef enum {\n"
" RAVI_JIT_NOT_COMPILED = 0,\n"
Expand Down

0 comments on commit 58980db

Please sign in to comment.