From 093c16b67b263e334600ed306310b9015f65fa8b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Nov 2017 15:44:31 -0200 Subject: [PATCH] new opcodes 'OP_LTI' and 'OP_LEI' --- lopcodes.c | 6 +++++- lopcodes.h | 4 +++- ltm.c | 16 +++++++++++++++- ltm.h | 4 +++- lvm.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 05700950..122157eb 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.68 2017/11/16 12:59:14 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.69 2017/11/22 18:41:20 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -68,6 +68,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "LE", "EQK", "EQI", + "LTI", + "LEI", "TEST", "TESTSET", "CALL", @@ -137,6 +139,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 0, iABC) /* OP_LE */ ,opmode(1, 0, iABC) /* OP_EQK */ ,opmode(1, 0, iABC) /* OP_EQI */ + ,opmode(1, 0, iABC) /* OP_LTI */ + ,opmode(1, 0, iABC) /* OP_LEI */ ,opmode(1, 0, iABC) /* OP_TEST */ ,opmode(1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 1, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index a9645620..5b955ce8 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.169 2017/11/22 18:41:20 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.170 2017/11/22 19:15:44 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -242,6 +242,8 @@ OP_LE,/* A B C if ((R(A) <= R(C)) ~= B) then pc++ */ OP_EQK,/* A B C if ((R(A) == K(C)) ~= B) then pc++ */ OP_EQI,/* A B C if ((R(A) == C) ~= B) then pc++ */ +OP_LTI,/* A B C if ((R(A) < C) ~= B) then pc++ */ +OP_LEI,/* A B C if ((R(A) <= C) ~= B) then pc++ */ OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/ltm.c b/ltm.c index 873c2be4..5ee123f5 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.48 2017/11/08 14:50:23 roberto Exp $ +** $Id: ltm.c,v 2.49 2017/11/23 19:18:10 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -196,6 +196,20 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, } +int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int inv, TMS event) { + TValue aux; const TValue *p2; + setivalue(&aux, v2); + if (inv) { /* arguments were exchanged? */ + p2 = p1; p1 = &aux; /* correct them */ + event = (event == TM_LE) ? TM_LT : TM_LE; + } + else + p2 = &aux; + return (luaT_callorderTM(L, p1, p2, event) != inv); +} + + void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { int i; Table *vtab; diff --git a/ltm.h b/ltm.h index 31454a5c..c8fed77c 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.25 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ltm.h,v 2.26 2017/09/27 18:59:08 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -72,6 +72,8 @@ LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); +LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int inv, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual); LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, diff --git a/lvm.c b/lvm.c index 5d9440a6..364c64dd 100644 --- a/lvm.c +++ b/lvm.c @@ -1,9 +1,5 @@ /* -<<<<<<< lvm.c -** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ -======= -** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ ->>>>>>> 2.315 +** $Id: lvm.c,v 2.317 2017/11/23 19:18:10 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -699,16 +695,21 @@ void luaV_finishOp (lua_State *L) { setobjs2s(L, base + GETARG_A(inst), --L->top); break; } - case OP_LE: case OP_LT: case OP_EQ: { + case OP_LT: case OP_LE: + case OP_LTI: case OP_LEI: + case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ int res = !l_isfalse(s2v(L->top - 1)); L->top--; if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - lua_assert(op == OP_LE); + lua_assert(op == OP_LE || + (op == OP_LEI && !(GETARG_B(inst) & 2)) || + (op == OP_LTI && GETARG_B(inst) & 2)); ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_B(inst)) /* condition failed? */ + if (GETARG_B(inst) & 2) res = !res; + if (res != (GETARG_B(inst) & 1)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } @@ -1383,6 +1384,42 @@ void luaV_execute (lua_State *L) { donextjump(ci); vmbreak; } + vmcase(OP_LTI) { + int res; + int ic = GETARG_sC(i); + if (ttisinteger(s2v(ra))) + res = (ivalue(s2v(ra)) < ic); + else if (ttisfloat(s2v(ra))) { + lua_Number f = fltvalue(s2v(ra)); + res = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(ic)) + : GETARG_B(i) >> 1; /* NaN? */ + } + else + Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LT)); + if (res != (GETARG_B(i) & 1)) + pc++; + else + donextjump(ci); + vmbreak; + } + vmcase(OP_LEI) { + int res; + int ic = GETARG_sC(i); + if (ttisinteger(s2v(ra))) + res = (ivalue(s2v(ra)) <= ic); + else if (ttisfloat(s2v(ra))) { + lua_Number f = fltvalue(s2v(ra)); + res = (!luai_numisnan(f)) ? luai_numle(f, cast_num(ic)) + : GETARG_B(i) >> 1; /* NaN? */ + } + else + Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LE)); + if (res != (GETARG_B(i) & 1)) + pc++; + else + donextjump(ci); + vmbreak; + } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++;