diff --git a/lcode.c b/lcode.c index a09731a7..fc7de6e3 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 roberto Exp roberto $ +** $Id: lcode.c,v 2.142 2017/12/04 17:41:30 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1196,6 +1196,15 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { } +static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, + int pc, int line) { + freeexps(fs, e1, e2); + e1->u.info = pc; + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); +} + + /* ** Emit code for binary expressions that "produce values" ** (everything but logical operators 'and'/'or' and comparison @@ -1209,10 +1218,8 @@ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ int v1 = luaK_exp2anyreg(fs, e1); - freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); + int pc = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ + finishbinexpval(fs, e1, e2, pc, line); } @@ -1223,10 +1230,8 @@ static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int k, int line) { int v2 = cast_int(e2->u.ival); /* immediate operand */ int v1 = luaK_exp2anyreg(fs, e1); - freeexp(fs, e1); - e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); + int pc = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ + finishbinexpval(fs, e1, e2, pc, line); } @@ -1264,6 +1269,33 @@ static void codecommutative (FuncState *fs, OpCode op, } +/* +** Code bitwise operations; they are all associative, so the function +** tries to put an integer constant as the 2nd operand (a K operand). +*/ +static void codebitwise (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int line) { + int inv = 0; + int v1, v2, pc; + OpCode op; + if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + swapexps(e1, e2); /* 'e2' will be the constant operand */ + inv = 1; + } + else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ + op = cast(OpCode, opr - OPR_BAND + OP_BAND); + codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ + return; + } + v1 = luaK_exp2anyreg(fs, e1); + v2 = e2->u.info; /* index in K array */ + op = cast(OpCode, opr - OPR_BAND + OP_BANDK); + lua_assert(ttisinteger(&fs->f->k[v2])); + pc = luaK_codeABCk(fs, op, 0, v1, v2, inv); + finishbinexpval(fs, e1, e2, pc, line); +} + + /* ** Code shift operators. If second operand is constant, use immediate ** operand (negating it if shift is in the other direction). @@ -1468,7 +1500,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr, } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); + codebitwise(fs, opr, e1, e2, line); break; } case OPR_SHL: { diff --git a/ldebug.c b/ldebug.c index 160b38fc..93f2d53d 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.146 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: ldebug.c,v 2.147 2017/12/07 15:44:10 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -589,7 +589,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, tm = TM_NEWINDEX; break; case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: - case OP_POWI: case OP_DIVI: case OP_IDIVI: { + case OP_POWI: case OP_DIVI: case OP_IDIVI: + case OP_BANDK: case OP_BORK: case OP_BXORK: { int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; diff --git a/lopcodes.c b/lopcodes.c index 55b6050d..1d246384 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.71 2017/11/29 16:57:36 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.72 2017/12/04 17:41:30 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -44,6 +44,9 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "POWI", "DIVI", "IDIVI", + "BANDK", + "BORK", + "BXORK", "SHRI", "SHLI", "ADD", @@ -119,6 +122,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_POWI */ ,opmode(0, 1, iABC) /* OP_DIVI */ ,opmode(0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 1, iABC) /* OP_BORK */ + ,opmode(0, 1, iABC) /* OP_BXORK */ ,opmode(0, 1, iABC) /* OP_SHRI */ ,opmode(0, 1, iABC) /* OP_SHLI */ ,opmode(0, 1, iABC) /* OP_ADD */ diff --git a/lopcodes.h b/lopcodes.h index 7468b9e3..f3c3e055 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.175 2017/11/30 13:16:43 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.176 2017/12/04 17:41:30 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -217,6 +217,10 @@ OP_POWI,/* A B sC R(A) := R(B) ^ C */ OP_DIVI,/* A B sC R(A) := R(B) / C */ OP_IDIVI,/* A B sC R(A) := R(B) // C */ +OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ +OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ +OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ + OP_SHRI,/* A B C R(A) := R(B) >> C */ OP_SHLI,/* A B C R(A) := C << R(B) */ diff --git a/ltm.c b/ltm.c index fe87553d..d30bef2f 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.50 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: ltm.c,v 2.51 2017/11/30 15:37:16 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -166,15 +166,20 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, int inv, TMS event) { + if (inv) + luaT_trybinTM(L, p2, p1, res, event); + else + luaT_trybinTM(L, p1, p2, res, event); +} + + void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event) { - TValue aux; const TValue *p2; + TValue aux; setivalue(&aux, i2); - if (inv) { /* arguments were exchanged? */ - p2 = p1; p1 = &aux; /* correct them */ - } - else p2 = &aux; - luaT_trybinTM(L, p1, p2, res, event); + luaT_trybinassocTM(L, p1, &aux, res, inv, event); } diff --git a/ltm.h b/ltm.h index c8fed77c..34dbc82c 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.26 2017/09/27 18:59:08 roberto Exp roberto $ +** $Id: ltm.h,v 2.27 2017/11/27 17:44:31 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -68,6 +68,8 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, + const TValue *p2, StkId res, int inv, TMS event); 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, diff --git a/lvm.c b/lvm.c index 9cbba4a9..90fd1060 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.323 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lvm.c,v 2.324 2017/12/04 17:41:30 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -687,6 +687,7 @@ void luaV_finishOp (lua_State *L) { case OP_MODI: case OP_POWI: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: + case OP_BANDK: case OP_BORK: case OP_BXORK: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHRI: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: @@ -1182,6 +1183,39 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); vmbreak; } + vmcase(OP_BANDK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(&, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND)); + vmbreak; + } + vmcase(OP_BORK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(|, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR)); + vmbreak; + } + vmcase(OP_BXORK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(^, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR)); + vmbreak; + } vmcase(OP_BAND) { TValue *rb = vRB(i); TValue *rc = vRC(i);