Skip to content

Commit

Permalink
initial work on issue #100
Browse files Browse the repository at this point in the history
  • Loading branch information
Dibyendu Majumdar committed Sep 23, 2016
1 parent d5fb8a0 commit 9895f28
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
8 changes: 6 additions & 2 deletions include/lopcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,15 @@ OP_RAVI_TOTAB, /* A R(A) := to_table(R(A)) */
OP_RAVI_MOVETAB, /* A B R(A) := R(B), check R(B) is a table */
OP_RAVI_SETUPVALT,/* A B UpValue[B] := to_table(R(A)) */
OP_RAVI_SELF_S,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */


OP_RAVI_GETTABLE_SK, /* A B C R(A) := R(B)[RK(C)], string key */
OP_RAVI_SELF_SK, /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
OP_RAVI_SETTABLE_SK,/* A B C R(A)[RK(B)] := RK(C), string key */

} OpCode;


#define NUM_OPCODES (cast(int, OP_RAVI_SELF_S) + 1)
#define NUM_OPCODES (cast(int, OP_RAVI_SETTABLE_SK) + 1)

/*===========================================================================
Notes:
Expand Down
21 changes: 18 additions & 3 deletions src/lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
op = OP_RAVI_GETTABLE_S;
else if (e->ravi_type == RAVI_TTABLE && e->u.ind.key_type == RAVI_TNUMINT)
op = OP_RAVI_GETTABLE_I;
else if (e->u.ind.key_type == RAVI_TSTRING && isshortstr(fs, e->u.ind.idx))
op = OP_RAVI_GETTABLE_SK;
else
op = OP_GETTABLE;
if (e->ravi_type == RAVI_TARRAYFLT || e->ravi_type == RAVI_TARRAYINT)
Expand Down Expand Up @@ -959,10 +961,14 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
} else if (var->ravi_type == RAVI_TTABLE && var->u.ind.key_type == RAVI_TNUMINT) {
/* table with integer key */
op = OP_RAVI_SETTABLE_I;
} else if (var->ravi_type == RAVI_TTABLE && var->u.ind.key_type == RAVI_TSTRING) {
} else if (var->ravi_type == RAVI_TTABLE && var->u.ind.key_type == RAVI_TSTRING && isshortstr(fs, var->u.ind.idx)) {
/* table with string key */
op = OP_RAVI_SETTABLE_S;
}
else if (var->u.ind.key_type == RAVI_TSTRING && isshortstr(fs, var->u.ind.idx)) {
/* table with string key */
op = OP_RAVI_SETTABLE_SK;
}
}
int e = luaK_exp2RK(fs, ex);
luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
Expand All @@ -976,17 +982,26 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {

void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
int ereg;
int table_and_string = e->ravi_type == RAVI_TTABLE &&
/* Ravi extension:
If we only know the key is a string constant
we emit specialized bytecode OP_RAVI_SELF_SK
If we also know that the variable is a table
then we emit OP_RAVI_SELF_S
*/
int is_string_constant_key =
key->k == VK &&
key->ravi_type == RAVI_TSTRING &&
isshortstr(fs, RKASK(key->u.info));
int table_and_string =
e->ravi_type == RAVI_TTABLE &&
is_string_constant_key;
luaK_exp2anyreg(fs, e);
ereg = e->u.info; /* register where 'e' was placed */
freeexp(fs, e);
e->u.info = fs->freereg; /* base register for op_self */
e->k = VNONRELOC; /* self expression has a fixed register */
luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
luaK_codeABC(fs, table_and_string ? OP_RAVI_SELF_S : OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
luaK_codeABC(fs, table_and_string ? OP_RAVI_SELF_S : (is_string_constant_key ? OP_RAVI_SELF_SK : OP_SELF), e->u.info, ereg, luaK_exp2RK(fs, key));
freeexp(fs, key);
}

Expand Down
6 changes: 4 additions & 2 deletions src/ldebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
}
case OP_RAVI_GETTABLE_I:
case OP_RAVI_GETTABLE_S:
case OP_RAVI_GETTABLE_SK:
case OP_RAVI_GETTABLE_AI:
case OP_RAVI_GETTABLE_AF:
case OP_GETTABUP:
Expand All @@ -494,6 +495,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
}
break;
}
case OP_RAVI_SELF_SK:
case OP_RAVI_SELF_S:
case OP_SELF: {
int k = GETARG_C(i); /* key index */
Expand Down Expand Up @@ -525,10 +527,10 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
return "for iterator";
}
/* all other instructions can call only through metamethods */
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: case OP_RAVI_GETTABLE_SK: case OP_RAVI_SELF_SK:
tm = TM_INDEX;
break;
case OP_SETTABUP: case OP_SETTABLE:
case OP_SETTABUP: case OP_SETTABLE: case OP_RAVI_SETTABLE_SK:
tm = TM_NEWINDEX;
break;
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
Expand Down
15 changes: 12 additions & 3 deletions src/lopcodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"MOVETAB", /* A B R(A) := R(B), check R(B) is a table */
"SETUPVALT", /* A B UpValue[B] := to_table(R(A)) */
"SELF_S", /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
NULL
"GETTABLE_SK", /* A B C R(A) := R(B)[RK(C)], string key */
"SELF_SK", /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
"SETTABLE_SK", /* A B C R(A)[RK(B)] := RK(C), string key */
NULL
};


Expand Down Expand Up @@ -288,8 +291,10 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgN, OpArgN, iABC) /* OP_RAVI_TOTAB A R(A) := check_table(R(A)) */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_RAVI_MOVETAB A B R(A) := R(B), check R(B) is a table */
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RAVI_SETUPVALT */
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */

,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_RAVI_SELF_S */
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_RAVI_GETTABLE_SK */
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_RAVI_SELF_SK */
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_RAVI_SETTABLE_SK */
};


Expand Down Expand Up @@ -456,11 +461,15 @@ static void PrintCode(const Proto* f)
case OP_RAVI_GETTABLE_AF:
case OP_RAVI_GETTABLE_AI:
case OP_SELF:
case OP_RAVI_GETTABLE_SK:
case OP_RAVI_SELF_S:
case OP_RAVI_SELF_SK:
if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
break;
case OP_SETTABLE:
case OP_RAVI_SETTABLE_I:
case OP_RAVI_SETTABLE_S:
case OP_RAVI_SETTABLE_SK:
case OP_RAVI_SETTABLE_AF:
case OP_RAVI_SETTABLE_AFF:
case OP_RAVI_SETTABLE_AI:
Expand Down
3 changes: 3 additions & 0 deletions src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ int luaV_execute (lua_State *L) {
GETTABLE_INLINE(L, upval, rc, ra);
Protect((void)0);
} break;
case OP_RAVI_GETTABLE_SK:
case OP_GETTABLE: {
StkId rb = RB(i); /* table */
TValue *rc = RKC(i); /* key */
Expand All @@ -956,6 +957,7 @@ int luaV_execute (lua_State *L) {
luaC_upvalbarrier(L, uv);
} break;

case OP_RAVI_SETTABLE_SK:
case OP_RAVI_SETTABLE_S:
case OP_RAVI_SETTABLE_I:
case OP_SETTABUP:
Expand All @@ -976,6 +978,7 @@ int luaV_execute (lua_State *L) {
luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
checkGC(L, ra + 1);
} break;
case OP_RAVI_SELF_SK:
case OP_SELF: {
const TValue *aux;
StkId rb = RB(i);
Expand Down
9 changes: 8 additions & 1 deletion src/ravi_llvmcodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,16 +929,20 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_SHL:
case OP_RAVI_GETTABLE_I:
case OP_RAVI_GETTABLE_S:
case OP_RAVI_GETTABLE_SK:
case OP_RAVI_SETTABLE_I:
case OP_RAVI_SETTABLE_S:
case OP_RAVI_SETTABLE_SK:
case OP_RAVI_TOTAB:
case OP_RAVI_MOVETAB:
case OP_RAVI_SETUPVALI:
case OP_RAVI_SETUPVALF:
case OP_RAVI_SETUPVALAI:
case OP_RAVI_SETUPVALAF:
case OP_RAVI_SETUPVALT:
case OP_RAVI_SELF_S: break;
case OP_RAVI_SELF_S:
case OP_RAVI_SELF_SK:
break;
default: return false;
}
}
Expand Down Expand Up @@ -1473,6 +1477,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
int C = GETARG_C(i);
emit_SETLIST(def, A, B, C, pc);
} break;
case OP_RAVI_SELF_SK:
case OP_SELF: {
int B = GETARG_B(i);
int C = GETARG_C(i);
Expand Down Expand Up @@ -1633,6 +1638,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
int C = GETARG_C(i);
emit_SETTABLE_I(def, A, B, C, pc);
} break; */
case OP_RAVI_SETTABLE_SK:
case OP_RAVI_SETTABLE_S:
case OP_SETTABLE: {
int B = GETARG_B(i);
Expand Down Expand Up @@ -1662,6 +1668,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
int C = GETARG_C(i);
emit_GETTABLE_I(def, A, B, C, pc);
} break;
case OP_RAVI_GETTABLE_SK:
case OP_GETTABLE: {
int B = GETARG_B(i);
int C = GETARG_C(i);
Expand Down

0 comments on commit 9895f28

Please sign in to comment.