aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-04 15:41:30 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-04 15:41:30 -0200
commitc7ee7fe026aa2247377bdd7915089a8e0074de1a (patch)
tree6c76f673032ede69516fc1c19c98e0d9a26d778d
parent421e459684f7fdf483454d99c048b47783cb0611 (diff)
downloadlua-c7ee7fe026aa2247377bdd7915089a8e0074de1a.tar.gz
lua-c7ee7fe026aa2247377bdd7915089a8e0074de1a.tar.bz2
lua-c7ee7fe026aa2247377bdd7915089a8e0074de1a.zip
new opcodes OP_SHLI/OP_SHRI
-rw-r--r--lcode.c69
-rw-r--r--lopcodes.c6
-rw-r--r--lopcodes.h5
-rw-r--r--lvm.c32
4 files changed, 94 insertions, 18 deletions
diff --git a/lcode.c b/lcode.c
index 56fd8522..a09731a7 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.140 2017/11/30 13:29:18 roberto Exp roberto $ 2** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -1217,6 +1217,20 @@ static void codebinexpval (FuncState *fs, OpCode op,
1217 1217
1218 1218
1219/* 1219/*
1220** Code binary operators ('+', '-', ...) with immediate operands.
1221*/
1222static void codebini (FuncState *fs, OpCode op,
1223 expdesc *e1, expdesc *e2, int k, int line) {
1224 int v2 = cast_int(e2->u.ival); /* immediate operand */
1225 int v1 = luaK_exp2anyreg(fs, e1);
1226 freeexp(fs, e1);
1227 e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */
1228 e1->k = VRELOCABLE; /* all those operations are relocatable */
1229 luaK_fixline(fs, line);
1230}
1231
1232
1233/*
1220** Code arithmetic operators ('+', '-', ...). If second operand is a 1234** Code arithmetic operators ('+', '-', ...). If second operand is a
1221** constant in the proper range, use variant opcodes with immediate 1235** constant in the proper range, use variant opcodes with immediate
1222** operands. 1236** operands.
@@ -1225,15 +1239,8 @@ static void codearith (FuncState *fs, OpCode op,
1225 expdesc *e1, expdesc *e2, int flip, int line) { 1239 expdesc *e1, expdesc *e2, int flip, int line) {
1226 if (!isSCint(e2)) 1240 if (!isSCint(e2))
1227 codebinexpval(fs, op, e1, e2, line); /* use standard operators */ 1241 codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1228 else { /* use immediate operators */ 1242 else /* use immediate operators */
1229 int v2 = cast_int(e2->u.ival); /* immediate operand */ 1243 codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line);
1230 int v1 = luaK_exp2anyreg(fs, e1);
1231 op = cast(OpCode, op - OP_ADD + OP_ADDI);
1232 freeexp(fs, e1);
1233 e1->u.info = codeABsC(fs, op, 0, v1, v2, flip); /* generate opcode */
1234 e1->k = VRELOCABLE; /* all those operations are relocatable */
1235 luaK_fixline(fs, line);
1236 }
1237} 1244}
1238 1245
1239 1246
@@ -1258,11 +1265,30 @@ static void codecommutative (FuncState *fs, OpCode op,
1258 1265
1259 1266
1260/* 1267/*
1268** Code shift operators. If second operand is constant, use immediate
1269** operand (negating it if shift is in the other direction).
1270*/
1271static void codeshift (FuncState *fs, OpCode op,
1272 expdesc *e1, expdesc *e2, int line) {
1273 if (isSCint(e2)) {
1274 int changedir = 0;
1275 if (op == OP_SHL) {
1276 changedir = 1;
1277 e2->u.ival = -(e2->u.ival);
1278 }
1279 codebini(fs, OP_SHRI, e1, e2, changedir, line);
1280 }
1281 else
1282 codebinexpval(fs, op, e1, e2, line);
1283}
1284
1285
1286/*
1261** Emit code for order comparisons. 1287** Emit code for order comparisons.
1262** When the first operand is an integral value in the proper range, 1288** When the first operand is an integral value in the proper range,
1263** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that 1289** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that
1264** it can use an immediate operand. In this case, C indicates this 1290** it can use an immediate operand. In this case, C indicates this
1265** change, for cases that cannot assume a total order (NaN and 1291** change, for cases that cannot assume a total order (NaN and
1266** metamethods). 1292** metamethods).
1267*/ 1293*/
1268static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { 1294static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
@@ -1440,12 +1466,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
1440 codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); 1466 codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line);
1441 break; 1467 break;
1442 } 1468 }
1443 case OPR_BAND: case OPR_BOR: case OPR_BXOR: 1469 case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1444 case OPR_SHL: case OPR_SHR: {
1445 if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) 1470 if (!constfolding(fs, opr + LUA_OPADD, e1, e2))
1446 codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); 1471 codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line);
1447 break; 1472 break;
1448 } 1473 }
1474 case OPR_SHL: {
1475 if (!constfolding(fs, LUA_OPSHL, e1, e2)) {
1476 if (isSCint(e1)) {
1477 swapexps(e1, e2);
1478 codebini(fs, OP_SHLI, e1, e2, 1, line);
1479 }
1480 else
1481 codeshift(fs, OP_SHL, e1, e2, line);
1482 }
1483 break;
1484 }
1485 case OPR_SHR: {
1486 if (!constfolding(fs, LUA_OPSHR, e1, e2))
1487 codeshift(fs, OP_SHR, e1, e2, line);
1488 break;
1489 }
1449 case OPR_EQ: case OPR_NE: { 1490 case OPR_EQ: case OPR_NE: {
1450 codeeq(fs, opr, e1, e2); 1491 codeeq(fs, opr, e1, e2);
1451 break; 1492 break;
@@ -1483,7 +1524,7 @@ void luaK_fixline (FuncState *fs, int line) {
1483 } 1524 }
1484 else { 1525 else {
1485 fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ 1526 fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */
1486 savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ 1527 savelineinfo(fs, f, fs->pc - 1, line); /* redo it */
1487 } 1528 }
1488} 1529}
1489 1530
diff --git a/lopcodes.c b/lopcodes.c
index 1b081639..55b6050d 100644
--- a/lopcodes.c
+++ b/lopcodes.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.c,v 1.70 2017/11/27 17:44:31 roberto Exp roberto $ 2** $Id: lopcodes.c,v 1.71 2017/11/29 16:57:36 roberto Exp roberto $
3** Opcodes for Lua virtual machine 3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -44,6 +44,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
44 "POWI", 44 "POWI",
45 "DIVI", 45 "DIVI",
46 "IDIVI", 46 "IDIVI",
47 "SHRI",
48 "SHLI",
47 "ADD", 49 "ADD",
48 "SUB", 50 "SUB",
49 "MUL", 51 "MUL",
@@ -117,6 +119,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
117 ,opmode(0, 1, iABC) /* OP_POWI */ 119 ,opmode(0, 1, iABC) /* OP_POWI */
118 ,opmode(0, 1, iABC) /* OP_DIVI */ 120 ,opmode(0, 1, iABC) /* OP_DIVI */
119 ,opmode(0, 1, iABC) /* OP_IDIVI */ 121 ,opmode(0, 1, iABC) /* OP_IDIVI */
122 ,opmode(0, 1, iABC) /* OP_SHRI */
123 ,opmode(0, 1, iABC) /* OP_SHLI */
120 ,opmode(0, 1, iABC) /* OP_ADD */ 124 ,opmode(0, 1, iABC) /* OP_ADD */
121 ,opmode(0, 1, iABC) /* OP_SUB */ 125 ,opmode(0, 1, iABC) /* OP_SUB */
122 ,opmode(0, 1, iABC) /* OP_MUL */ 126 ,opmode(0, 1, iABC) /* OP_MUL */
diff --git a/lopcodes.h b/lopcodes.h
index 0d697c55..7468b9e3 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.174 2017/11/30 12:03:00 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.175 2017/11/30 13:16:43 roberto Exp roberto $
3** Opcodes for Lua virtual machine 3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -217,6 +217,9 @@ OP_POWI,/* A B sC R(A) := R(B) ^ C */
217OP_DIVI,/* A B sC R(A) := R(B) / C */ 217OP_DIVI,/* A B sC R(A) := R(B) / C */
218OP_IDIVI,/* A B sC R(A) := R(B) // C */ 218OP_IDIVI,/* A B sC R(A) := R(B) // C */
219 219
220OP_SHRI,/* A B C R(A) := R(B) >> C */
221OP_SHLI,/* A B C R(A) := C << R(B) */
222
220OP_ADD,/* A B C R(A) := R(B) + R(C) */ 223OP_ADD,/* A B C R(A) := R(B) + R(C) */
221OP_SUB,/* A B C R(A) := R(B) - R(C) */ 224OP_SUB,/* A B C R(A) := R(B) - R(C) */
222OP_MUL,/* A B C R(A) := R(B) * R(C) */ 225OP_MUL,/* A B C R(A) := R(B) * R(C) */
diff --git a/lvm.c b/lvm.c
index 0ba78b3d..9cbba4a9 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.322 2017/11/29 16:57:36 roberto Exp roberto $ 2** $Id: lvm.c,v 2.323 2017/11/30 13:29:18 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -687,7 +687,8 @@ void luaV_finishOp (lua_State *L) {
687 case OP_MODI: case OP_POWI: 687 case OP_MODI: case OP_POWI:
688 case OP_ADD: case OP_SUB: 688 case OP_ADD: case OP_SUB:
689 case OP_MUL: case OP_DIV: case OP_IDIV: 689 case OP_MUL: case OP_DIV: case OP_IDIV:
690 case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: 690 case OP_BAND: case OP_BOR: case OP_BXOR:
691 case OP_SHRI: case OP_SHL: case OP_SHR:
691 case OP_MOD: case OP_POW: 692 case OP_MOD: case OP_POW:
692 case OP_UNM: case OP_BNOT: case OP_LEN: 693 case OP_UNM: case OP_BNOT: case OP_LEN:
693 case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: 694 case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
@@ -1214,6 +1215,33 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1214 Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); 1215 Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR));
1215 vmbreak; 1216 vmbreak;
1216 } 1217 }
1218 vmcase(OP_SHRI) {
1219 TValue *rb = vRB(i);
1220 int ic = GETARG_sC(i);
1221 lua_Integer ib;
1222 if (tointegerns(rb, &ib)) {
1223 setivalue(vra, luaV_shiftl(ib, -ic));
1224 }
1225 else {
1226 TMS ev = TM_SHR;
1227 if (TESTARG_k(i)) {
1228 ic = -ic; ev = TM_SHL;
1229 }
1230 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev));
1231 }
1232 vmbreak;
1233 }
1234 vmcase(OP_SHLI) {
1235 TValue *rb = vRB(i);
1236 int ic = GETARG_sC(i);
1237 lua_Integer ib;
1238 if (tointegerns(rb, &ib)) {
1239 setivalue(vra, luaV_shiftl(ic, ib));
1240 }
1241 else
1242 Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL));
1243 vmbreak;
1244 }
1217 vmcase(OP_SHL) { 1245 vmcase(OP_SHL) {
1218 TValue *rb = vRB(i); 1246 TValue *rb = vRB(i);
1219 TValue *rc = vRC(i); 1247 TValue *rc = vRC(i);