aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-13 16:32:09 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-13 16:32:09 -0200
commit86431a2f1c668844c665f9d09e246de906b511d8 (patch)
tree643d503667b624d4faeecd6bed6cc51f9084cb00
parent36cf8f3a3c44da00cc9255797153df3c02895379 (diff)
downloadlua-86431a2f1c668844c665f9d09e246de906b511d8.tar.gz
lua-86431a2f1c668844c665f9d09e246de906b511d8.tar.bz2
lua-86431a2f1c668844c665f9d09e246de906b511d8.zip
new opcodes BANDK/BORK/BXORK. (They do not use immediate operands
because, too often, masks in bitwise operations are integers larger than one byte.)
-rw-r--r--lcode.c52
-rw-r--r--ldebug.c5
-rw-r--r--lopcodes.c8
-rw-r--r--lopcodes.h6
-rw-r--r--ltm.c19
-rw-r--r--ltm.h4
-rw-r--r--lvm.c36
7 files changed, 107 insertions, 23 deletions
diff --git a/lcode.c b/lcode.c
index a09731a7..fc7de6e3 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 roberto Exp roberto $ 2** $Id: lcode.c,v 2.142 2017/12/04 17:41:30 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*/
@@ -1196,6 +1196,15 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1196} 1196}
1197 1197
1198 1198
1199static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1200 int pc, int line) {
1201 freeexps(fs, e1, e2);
1202 e1->u.info = pc;
1203 e1->k = VRELOCABLE; /* all those operations are relocatable */
1204 luaK_fixline(fs, line);
1205}
1206
1207
1199/* 1208/*
1200** Emit code for binary expressions that "produce values" 1209** Emit code for binary expressions that "produce values"
1201** (everything but logical operators 'and'/'or' and comparison 1210** (everything but logical operators 'and'/'or' and comparison
@@ -1209,10 +1218,8 @@ static void codebinexpval (FuncState *fs, OpCode op,
1209 expdesc *e1, expdesc *e2, int line) { 1218 expdesc *e1, expdesc *e2, int line) {
1210 int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ 1219 int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
1211 int v1 = luaK_exp2anyreg(fs, e1); 1220 int v1 = luaK_exp2anyreg(fs, e1);
1212 freeexps(fs, e1, e2); 1221 int pc = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
1213 e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ 1222 finishbinexpval(fs, e1, e2, pc, line);
1214 e1->k = VRELOCABLE; /* all those operations are relocatable */
1215 luaK_fixline(fs, line);
1216} 1223}
1217 1224
1218 1225
@@ -1223,10 +1230,8 @@ static void codebini (FuncState *fs, OpCode op,
1223 expdesc *e1, expdesc *e2, int k, int line) { 1230 expdesc *e1, expdesc *e2, int k, int line) {
1224 int v2 = cast_int(e2->u.ival); /* immediate operand */ 1231 int v2 = cast_int(e2->u.ival); /* immediate operand */
1225 int v1 = luaK_exp2anyreg(fs, e1); 1232 int v1 = luaK_exp2anyreg(fs, e1);
1226 freeexp(fs, e1); 1233 int pc = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */
1227 e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ 1234 finishbinexpval(fs, e1, e2, pc, line);
1228 e1->k = VRELOCABLE; /* all those operations are relocatable */
1229 luaK_fixline(fs, line);
1230} 1235}
1231 1236
1232 1237
@@ -1265,6 +1270,33 @@ static void codecommutative (FuncState *fs, OpCode op,
1265 1270
1266 1271
1267/* 1272/*
1273** Code bitwise operations; they are all associative, so the function
1274** tries to put an integer constant as the 2nd operand (a K operand).
1275*/
1276static void codebitwise (FuncState *fs, BinOpr opr,
1277 expdesc *e1, expdesc *e2, int line) {
1278 int inv = 0;
1279 int v1, v2, pc;
1280 OpCode op;
1281 if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
1282 swapexps(e1, e2); /* 'e2' will be the constant operand */
1283 inv = 1;
1284 }
1285 else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
1286 op = cast(OpCode, opr - OPR_BAND + OP_BAND);
1287 codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
1288 return;
1289 }
1290 v1 = luaK_exp2anyreg(fs, e1);
1291 v2 = e2->u.info; /* index in K array */
1292 op = cast(OpCode, opr - OPR_BAND + OP_BANDK);
1293 lua_assert(ttisinteger(&fs->f->k[v2]));
1294 pc = luaK_codeABCk(fs, op, 0, v1, v2, inv);
1295 finishbinexpval(fs, e1, e2, pc, line);
1296}
1297
1298
1299/*
1268** Code shift operators. If second operand is constant, use immediate 1300** Code shift operators. If second operand is constant, use immediate
1269** operand (negating it if shift is in the other direction). 1301** operand (negating it if shift is in the other direction).
1270*/ 1302*/
@@ -1468,7 +1500,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
1468 } 1500 }
1469 case OPR_BAND: case OPR_BOR: case OPR_BXOR: { 1501 case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1470 if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) 1502 if (!constfolding(fs, opr + LUA_OPADD, e1, e2))
1471 codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); 1503 codebitwise(fs, opr, e1, e2, line);
1472 break; 1504 break;
1473 } 1505 }
1474 case OPR_SHL: { 1506 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 @@
1/* 1/*
2** $Id: ldebug.c,v 2.146 2017/11/23 19:29:04 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.147 2017/12/07 15:44:10 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -589,7 +589,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
589 tm = TM_NEWINDEX; 589 tm = TM_NEWINDEX;
590 break; 590 break;
591 case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: 591 case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI:
592 case OP_POWI: case OP_DIVI: case OP_IDIVI: { 592 case OP_POWI: case OP_DIVI: case OP_IDIVI:
593 case OP_BANDK: case OP_BORK: case OP_BXORK: {
593 int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ 594 int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */
594 tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ 595 tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
595 break; 596 break;
diff --git a/lopcodes.c b/lopcodes.c
index 55b6050d..1d246384 100644
--- a/lopcodes.c
+++ b/lopcodes.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.c,v 1.71 2017/11/29 16:57:36 roberto Exp roberto $ 2** $Id: lopcodes.c,v 1.72 2017/12/04 17:41:30 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,9 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
44 "POWI", 44 "POWI",
45 "DIVI", 45 "DIVI",
46 "IDIVI", 46 "IDIVI",
47 "BANDK",
48 "BORK",
49 "BXORK",
47 "SHRI", 50 "SHRI",
48 "SHLI", 51 "SHLI",
49 "ADD", 52 "ADD",
@@ -119,6 +122,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
119 ,opmode(0, 1, iABC) /* OP_POWI */ 122 ,opmode(0, 1, iABC) /* OP_POWI */
120 ,opmode(0, 1, iABC) /* OP_DIVI */ 123 ,opmode(0, 1, iABC) /* OP_DIVI */
121 ,opmode(0, 1, iABC) /* OP_IDIVI */ 124 ,opmode(0, 1, iABC) /* OP_IDIVI */
125 ,opmode(0, 1, iABC) /* OP_BANDK */
126 ,opmode(0, 1, iABC) /* OP_BORK */
127 ,opmode(0, 1, iABC) /* OP_BXORK */
122 ,opmode(0, 1, iABC) /* OP_SHRI */ 128 ,opmode(0, 1, iABC) /* OP_SHRI */
123 ,opmode(0, 1, iABC) /* OP_SHLI */ 129 ,opmode(0, 1, iABC) /* OP_SHLI */
124 ,opmode(0, 1, iABC) /* OP_ADD */ 130 ,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 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.175 2017/11/30 13:16:43 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.176 2017/12/04 17:41:30 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,10 @@ 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_BANDK,/* A B C R(A) := R(B) & K(C):integer */
221OP_BORK,/* A B C R(A) := R(B) | K(C):integer */
222OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */
223
220OP_SHRI,/* A B C R(A) := R(B) >> C */ 224OP_SHRI,/* A B C R(A) := R(B) >> C */
221OP_SHLI,/* A B C R(A) := C << R(B) */ 225OP_SHLI,/* A B C R(A) := C << R(B) */
222 226
diff --git a/ltm.c b/ltm.c
index fe87553d..d30bef2f 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 2.50 2017/11/27 17:44:31 roberto Exp roberto $ 2** $Id: ltm.c,v 2.51 2017/11/30 15:37:16 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -166,15 +166,20 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
166} 166}
167 167
168 168
169void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
170 StkId res, int inv, TMS event) {
171 if (inv)
172 luaT_trybinTM(L, p2, p1, res, event);
173 else
174 luaT_trybinTM(L, p1, p2, res, event);
175}
176
177
169void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, 178void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
170 int inv, StkId res, TMS event) { 179 int inv, StkId res, TMS event) {
171 TValue aux; const TValue *p2; 180 TValue aux;
172 setivalue(&aux, i2); 181 setivalue(&aux, i2);
173 if (inv) { /* arguments were exchanged? */ 182 luaT_trybinassocTM(L, p1, &aux, res, inv, event);
174 p2 = p1; p1 = &aux; /* correct them */
175 }
176 else p2 = &aux;
177 luaT_trybinTM(L, p1, p2, res, event);
178} 183}
179 184
180 185
diff --git a/ltm.h b/ltm.h
index c8fed77c..34dbc82c 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 2.26 2017/09/27 18:59:08 roberto Exp roberto $ 2** $Id: ltm.h,v 2.27 2017/11/27 17:44:31 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -68,6 +68,8 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
68 const TValue *p1, const TValue *p2, StkId p3); 68 const TValue *p1, const TValue *p2, StkId p3);
69LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
70 StkId res, TMS event); 70 StkId res, TMS event);
71LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
72 const TValue *p2, StkId res, int inv, TMS event);
71LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, 73LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
72 int inv, StkId res, TMS event); 74 int inv, StkId res, TMS event);
73LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 75LUAI_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 @@
1/* 1/*
2** $Id: lvm.c,v 2.323 2017/11/30 13:29:18 roberto Exp roberto $ 2** $Id: lvm.c,v 2.324 2017/12/04 17:41:30 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,6 +687,7 @@ 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_BANDK: case OP_BORK: case OP_BXORK:
690 case OP_BAND: case OP_BOR: case OP_BXOR: 691 case OP_BAND: case OP_BOR: case OP_BXOR:
691 case OP_SHRI: case OP_SHL: case OP_SHR: 692 case OP_SHRI: case OP_SHL: case OP_SHR:
692 case OP_MOD: case OP_POW: 693 case OP_MOD: case OP_POW:
@@ -1182,6 +1183,39 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1182 Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); 1183 Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV));
1183 vmbreak; 1184 vmbreak;
1184 } 1185 }
1186 vmcase(OP_BANDK) {
1187 TValue *p1 = vRB(i);
1188 TValue *p2 = KC(i);
1189 lua_Integer i1;
1190 if (tointegerns(p1, &i1)) {
1191 setivalue(vra, intop(&, i1, ivalue(p2)));
1192 }
1193 else
1194 Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND));
1195 vmbreak;
1196 }
1197 vmcase(OP_BORK) {
1198 TValue *p1 = vRB(i);
1199 TValue *p2 = KC(i);
1200 lua_Integer i1;
1201 if (tointegerns(p1, &i1)) {
1202 setivalue(vra, intop(|, i1, ivalue(p2)));
1203 }
1204 else
1205 Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR));
1206 vmbreak;
1207 }
1208 vmcase(OP_BXORK) {
1209 TValue *p1 = vRB(i);
1210 TValue *p2 = KC(i);
1211 lua_Integer i1;
1212 if (tointegerns(p1, &i1)) {
1213 setivalue(vra, intop(^, i1, ivalue(p2)));
1214 }
1215 else
1216 Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR));
1217 vmbreak;
1218 }
1185 vmcase(OP_BAND) { 1219 vmcase(OP_BAND) {
1186 TValue *rb = vRB(i); 1220 TValue *rb = vRB(i);
1187 TValue *rc = vRC(i); 1221 TValue *rc = vRC(i);