new opcodes OP_GTI/OP_GEI

This commit is contained in:
Roberto Ierusalimschy 2018-02-21 12:49:32 -03:00
parent c67603fafb
commit 212095a601
5 changed files with 50 additions and 45 deletions

30
lcode.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.155 2018/02/17 19:20:00 roberto Exp roberto $ ** $Id: lcode.c,v 2.156 2018/02/21 12:54:26 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -171,8 +171,8 @@ void luaK_ret (FuncState *fs, int first, int nret) {
** Code a "conditional jump", that is, a test or comparison opcode ** Code a "conditional jump", that is, a test or comparison opcode
** followed by a jump. Return jump position. ** followed by a jump. Return jump position.
*/ */
static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { static int condjump (FuncState *fs, OpCode op, int A, int B, int k) {
luaK_codeABCk(fs, op, A, B, C, k); luaK_codeABCk(fs, op, A, B, 0, k);
return luaK_jump(fs); return luaK_jump(fs);
} }
@ -979,13 +979,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) {
Instruction ie = getinstruction(fs, e); Instruction ie = getinstruction(fs, e);
if (GET_OPCODE(ie) == OP_NOT) { if (GET_OPCODE(ie) == OP_NOT) {
fs->pc--; /* remove previous OP_NOT */ fs->pc--; /* remove previous OP_NOT */
return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
} }
/* else go through */ /* else go through */
} }
discharge2anyreg(fs, e); discharge2anyreg(fs, e);
freeexp(fs, e); freeexp(fs, e);
return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
} }
@ -1338,16 +1338,12 @@ static void codeshift (FuncState *fs, OpCode op,
/* /*
** Emit code for order comparisons. ** Emit code for order comparisons.
** When the first operand is an integral value in the proper range, ** When the first operand A is an integral value in the proper range,
** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that ** change (A < B) to (B > A) and (A <= B) to (B >= A) so that
** it can use an immediate operand. In this case, C indicates this ** it can use an immediate operand.
** change, for cases that cannot assume a total order (NaN and
** metamethods).
*/ */
static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
int r1, r2; int r1, r2;
int cond = 1;
int C = 0;
lua_Integer im; lua_Integer im;
if (isSCnumber(e2, &im)) { if (isSCnumber(e2, &im)) {
/* use immediate operand */ /* use immediate operand */
@ -1356,19 +1352,17 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
op = cast(OpCode, (op - OP_LT) + OP_LTI); op = cast(OpCode, (op - OP_LT) + OP_LTI);
} }
else if (isSCnumber(e1, &im)) { else if (isSCnumber(e1, &im)) {
/* transform (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) */ /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
r1 = luaK_exp2anyreg(fs, e2); r1 = luaK_exp2anyreg(fs, e2);
r2 = cast_int(im); r2 = cast_int(im);
op = (op == OP_LT) ? OP_LEI : OP_LTI; op = (op == OP_LT) ? OP_GTI : OP_GEI;
cond = 0; /* negate original test */
C = 1; /* indication that it used the transformations */
} }
else { /* regular case, compare two registers */ else { /* regular case, compare two registers */
r1 = luaK_exp2anyreg(fs, e1); r1 = luaK_exp2anyreg(fs, e1);
r2 = luaK_exp2anyreg(fs, e2); r2 = luaK_exp2anyreg(fs, e2);
} }
freeexps(fs, e1, e2); freeexps(fs, e1, e2);
e1->u.info = condjump(fs, op, r1, r2, C, cond); e1->u.info = condjump(fs, op, r1, r2, 1);
e1->k = VJMP; e1->k = VJMP;
} }
@ -1399,7 +1393,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
r2 = luaK_exp2anyreg(fs, e2); r2 = luaK_exp2anyreg(fs, e2);
} }
freeexps(fs, e1, e2); freeexps(fs, e1, e2);
e1->u.info = condjump(fs, op, r1, r2, 0, (opr == OPR_EQ)); e1->u.info = condjump(fs, op, r1, r2, (opr == OPR_EQ));
e1->k = VJMP; e1->k = VJMP;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.77 2018/02/09 15:16:06 roberto Exp roberto $ ** $Id: lopcodes.c,v 1.78 2018/02/15 15:34:29 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -75,6 +75,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"EQI", "EQI",
"LTI", "LTI",
"LEI", "LEI",
"GTI",
"GEI",
"TEST", "TEST",
"TESTSET", "TESTSET",
"CALL", "CALL",
@ -156,6 +158,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */
,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */
,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */
,opmode(0, 0, 1, 0, iABC) /* OP_GTI */
,opmode(0, 0, 1, 0, iABC) /* OP_GEI */
,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */
,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */
,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.187 2018/02/09 15:16:06 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.188 2018/02/15 15:34:29 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -260,6 +260,8 @@ OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */
OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */ OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */
OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */ OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */
OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */ OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */
OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */
OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */
OP_TEST,/* A if (not R(A) == k) then pc++ */ OP_TEST,/* A if (not R(A) == k) then pc++ */
OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */
@ -317,11 +319,6 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
(*) For comparisons, k specifies what condition the test should accept (*) For comparisons, k specifies what condition the test should accept
(true or false). (true or false).
(*) For OP_LTI/OP_LEI, C indicates that the transformations
(A<B) => (!(B<=A)) or (A<=B) => (!(B<A)) were used to put the constant
operator on the right side. (Non-total orders with NaN or metamethods
use this indication to correct their behavior.)
(*) All 'skips' (pc++) assume that next instruction is a jump. (*) All 'skips' (pc++) assume that next instruction is a jump.
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the

5
ltm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 2.61 2018/02/15 15:34:29 roberto Exp roberto $ ** $Id: ltm.c,v 2.62 2018/02/17 19:20:00 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -208,11 +208,10 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
setivalue(&aux, v2); setivalue(&aux, v2);
if (inv) { /* arguments were exchanged? */ if (inv) { /* arguments were exchanged? */
p2 = p1; p1 = &aux; /* correct them */ p2 = p1; p1 = &aux; /* correct them */
event = (event == TM_LE) ? TM_LT : TM_LE;
} }
else else
p2 = &aux; p2 = &aux;
return (luaT_callorderTM(L, p1, p2, event) != inv); return luaT_callorderTM(L, p1, p2, event);
} }

45
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.343 2018/02/21 12:54:26 roberto Exp roberto $ ** $Id: lvm.c,v 2.344 2018/02/21 13:47:03 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -731,18 +731,15 @@ void luaV_finishOp (lua_State *L) {
} }
case OP_LT: case OP_LE: case OP_LT: case OP_LE:
case OP_LTI: case OP_LEI: case OP_LTI: case OP_LEI:
case OP_GTI: case OP_GEI:
case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
int res = !l_isfalse(s2v(L->top - 1)); int res = !l_isfalse(s2v(L->top - 1));
L->top--; L->top--;
if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
lua_assert(op == OP_LE ||
(op == OP_LTI && GETARG_C(inst)) ||
(op == OP_LEI && !GETARG_C(inst)));
ci->callstatus ^= CIST_LEQ; /* clear mark */ ci->callstatus ^= CIST_LEQ; /* clear mark */
res = !res; /* negate result */ res = !res; /* negate result */
} }
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
if (GETARG_C(inst)) res = !res;
if (res != GETARG_k(inst)) /* condition failed? */ if (res != GETARG_k(inst)) /* condition failed? */
ci->u.l.savedpc++; /* skip jump instruction */ ci->u.l.savedpc++; /* skip jump instruction */
break; break;
@ -1473,26 +1470,40 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
int im = GETARG_sB(i); int im = GETARG_sB(i);
if (ttisinteger(vra)) if (ttisinteger(vra))
cond = (ivalue(vra) < im); cond = (ivalue(vra) < im);
else if (ttisfloat(vra)) { else if (ttisfloat(vra))
lua_Number f = fltvalue(vra); cond = luai_numlt(fltvalue(vra), cast_num(im));
cond = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im))
: GETARG_C(i); /* NaN */
}
else else
Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LT)); Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LT));
goto condjump; goto condjump;
} }
vmcase(OP_LEI) { vmcase(OP_LEI) {
int im = GETARG_sB(i); int im = GETARG_sB(i);
if (ttisinteger(vra)) if (ttisinteger(vra))
cond = (ivalue(vra) <= im); cond = (ivalue(vra) <= im);
else if (ttisfloat(vra)) { else if (ttisfloat(vra))
lua_Number f = fltvalue(vra); cond = luai_numle(fltvalue(vra), cast_num(im));
cond = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im))
: GETARG_C(i); /* NaN? */
}
else else
Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LE)); Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LE));
goto condjump;
}
vmcase(OP_GTI) {
int im = GETARG_sB(i);
if (ttisinteger(vra))
cond = (im < ivalue(vra));
else if (ttisfloat(vra))
cond = luai_numlt(cast_num(im), fltvalue(vra));
else
Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LT));
goto condjump;
}
vmcase(OP_GEI) {
int im = GETARG_sB(i);
if (ttisinteger(vra))
cond = (im <= ivalue(vra));
else if (ttisfloat(vra))
cond = luai_numle(cast_num(im), fltvalue(vra));
else
Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LE));
goto condjump; goto condjump;
} }
vmcase(OP_TEST) { vmcase(OP_TEST) {