aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-10-04 12:49:24 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-10-04 12:49:24 -0300
commit8fbe9e3470295e3b70273fa32ef56459cc0b5201 (patch)
tree21fcdfc75c622f5b015a1fcd131eef1b7701cc5d
parent9ed9f40f1e3674f9ed7ffbe73c0c9718782fe6cd (diff)
downloadlua-8fbe9e3470295e3b70273fa32ef56459cc0b5201.tar.gz
lua-8fbe9e3470295e3b70273fa32ef56459cc0b5201.tar.bz2
lua-8fbe9e3470295e3b70273fa32ef56459cc0b5201.zip
new opcodes with immediate integer operand for all arithmetic operations
-rw-r--r--lcode.c71
-rw-r--r--ldebug.c13
-rw-r--r--lopcodes.c14
-rw-r--r--lopcodes.h9
-rw-r--r--lvm.c91
5 files changed, 170 insertions, 28 deletions
diff --git a/lcode.c b/lcode.c
index 82bcdcbb..786fd342 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.127 2017/10/01 19:13:43 roberto Exp roberto $ 2** $Id: lcode.c,v 2.128 2017/10/02 22:50:57 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*/
@@ -1171,22 +1171,8 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1171*/ 1171*/
1172static void codebinexpval (FuncState *fs, OpCode op, 1172static void codebinexpval (FuncState *fs, OpCode op,
1173 expdesc *e1, expdesc *e2, int line) { 1173 expdesc *e1, expdesc *e2, int line) {
1174 int v1, v2; 1174 int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
1175 if (op == OP_ADD && (isKint(e1) || isKint(e2))) { 1175 int v1 = luaK_exp2anyreg(fs, e1);
1176 if (isKint(e2)) {
1177 v2 = cast_int(e2->u.ival);
1178 v1 = luaK_exp2anyreg(fs, e1);
1179 }
1180 else { /* exchange operands to make 2nd one a constant */
1181 v2 = cast_int(e1->u.ival) | BITRK; /* K bit signal the exchange */
1182 v1 = luaK_exp2anyreg(fs, e2);
1183 }
1184 op = OP_ADDI;
1185 }
1186 else {
1187 v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
1188 v1 = luaK_exp2anyreg(fs, e1);
1189 }
1190 freeexps(fs, e1, e2); 1176 freeexps(fs, e1, e2);
1191 e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ 1177 e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
1192 e1->k = VRELOCABLE; /* all those operations are relocatable */ 1178 e1->k = VRELOCABLE; /* all those operations are relocatable */
@@ -1195,6 +1181,44 @@ static void codebinexpval (FuncState *fs, OpCode op,
1195 1181
1196 1182
1197/* 1183/*
1184** Code arithmetic operators ('+', '-', ...). If second operand is a
1185** constant in the proper range, use variant opcodes with immediate
1186** operands.
1187*/
1188static void codearith (FuncState *fs, OpCode op,
1189 expdesc *e1, expdesc *e2, int flip, int line) {
1190 if (!isKint(e2))
1191 codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1192 else { /* use immediate operators */
1193 int v2 = cast_int(e2->u.ival); /* immediate operand */
1194 int v1 = luaK_exp2anyreg(fs, e1);
1195 if (flip)
1196 v2 |= BITRK; /* signal that operands were flipped */
1197 op = cast(OpCode, op - OP_ADD + OP_ADDI);
1198 freeexp(fs, e1);
1199 e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
1200 e1->k = VRELOCABLE; /* all those operations are relocatable */
1201 luaK_fixline(fs, line);
1202 }
1203}
1204
1205
1206/*
1207** Code commutative operators ('+', '*'). If first operand is a
1208** constant, change order of operands to use immediate operator.
1209*/
1210static void codecommutative (FuncState *fs, OpCode op,
1211 expdesc *e1, expdesc *e2, int line) {
1212 int flip = 0;
1213 if (isKint(e1)) {
1214 expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
1215 flip = 1;
1216 }
1217 codearith(fs, op, e1, e2, flip, line);
1218}
1219
1220
1221/*
1198** Emit code for comparisons. 1222** Emit code for comparisons.
1199** 'e1' was already put in register by 'luaK_infix'. 1223** 'e1' was already put in register by 'luaK_infix'.
1200*/ 1224*/
@@ -1318,8 +1342,17 @@ void luaK_posfix (FuncState *fs, BinOpr op,
1318 } 1342 }
1319 break; 1343 break;
1320 } 1344 }
1321 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: 1345 case OPR_ADD: case OPR_MUL: {
1322 case OPR_IDIV: case OPR_MOD: case OPR_POW: 1346 if (!constfolding(fs, op + LUA_OPADD, e1, e2))
1347 codecommutative(fs, cast(OpCode, op + OP_ADD), e1, e2, line);
1348 break;
1349 }
1350 case OPR_SUB: case OPR_DIV:
1351 case OPR_IDIV: case OPR_MOD: case OPR_POW: {
1352 if (!constfolding(fs, op + LUA_OPADD, e1, e2))
1353 codearith(fs, cast(OpCode, op + OP_ADD), e1, e2, 0, line);
1354 break;
1355 }
1323 case OPR_BAND: case OPR_BOR: case OPR_BXOR: 1356 case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1324 case OPR_SHL: case OPR_SHR: { 1357 case OPR_SHL: case OPR_SHR: {
1325 if (!constfolding(fs, op + LUA_OPADD, e1, e2)) 1358 if (!constfolding(fs, op + LUA_OPADD, e1, e2))
diff --git a/ldebug.c b/ldebug.c
index fbb3839d..67b9496a 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.129 2017/07/07 16:34:32 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.130 2017/07/10 17:35:12 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*/
@@ -592,14 +592,17 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
592 case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: 592 case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
593 tm = TM_NEWINDEX; 593 tm = TM_NEWINDEX;
594 break; 594 break;
595 case OP_ADDI: 595 case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI:
596 tm = TM_ADD; 596 case OP_POWI: case OP_DIVI: case OP_IDIVI: {
597 int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */
598 tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
597 break; 599 break;
600 }
598 case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: 601 case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
599 case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: 602 case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
600 case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { 603 case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
601 int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */ 604 int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */
602 tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */ 605 tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
603 break; 606 break;
604 } 607 }
605 case OP_UNM: tm = TM_UNM; break; 608 case OP_UNM: tm = TM_UNM; break;
diff --git a/lopcodes.c b/lopcodes.c
index 5051f2a8..42ce73d9 100644
--- a/lopcodes.c
+++ b/lopcodes.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.c,v 1.64 2017/09/26 18:14:45 roberto Exp roberto $ 2** $Id: lopcodes.c,v 1.65 2017/09/28 16:53:29 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*/
@@ -38,6 +38,12 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
38 "NEWTABLE", 38 "NEWTABLE",
39 "SELF", 39 "SELF",
40 "ADDI", 40 "ADDI",
41 "SUBI",
42 "MULI",
43 "MODI",
44 "POWI",
45 "DIVI",
46 "IDIVI",
41 "ADD", 47 "ADD",
42 "SUB", 48 "SUB",
43 "MUL", 49 "MUL",
@@ -99,6 +105,12 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
99 ,opmode(0, 1, iABC) /* OP_NEWTABLE */ 105 ,opmode(0, 1, iABC) /* OP_NEWTABLE */
100 ,opmode(0, 1, iABC) /* OP_SELF */ 106 ,opmode(0, 1, iABC) /* OP_SELF */
101 ,opmode(0, 1, iABC) /* OP_ADDI */ 107 ,opmode(0, 1, iABC) /* OP_ADDI */
108 ,opmode(0, 1, iABC) /* OP_SUBI */
109 ,opmode(0, 1, iABC) /* OP_MULI */
110 ,opmode(0, 1, iABC) /* OP_MODI */
111 ,opmode(0, 1, iABC) /* OP_POWI */
112 ,opmode(0, 1, iABC) /* OP_DIVI */
113 ,opmode(0, 1, iABC) /* OP_IDIVI */
102 ,opmode(0, 1, iABC) /* OP_ADD */ 114 ,opmode(0, 1, iABC) /* OP_ADD */
103 ,opmode(0, 1, iABC) /* OP_SUB */ 115 ,opmode(0, 1, iABC) /* OP_SUB */
104 ,opmode(0, 1, iABC) /* OP_MUL */ 116 ,opmode(0, 1, iABC) /* OP_MUL */
diff --git a/lopcodes.h b/lopcodes.h
index 5c0bb836..39f36aa7 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.163 2017/10/01 19:13:43 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.164 2017/10/02 22:51:32 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*/
@@ -201,6 +201,13 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
201OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ 201OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */
202 202
203OP_ADDI,/* A B C R(A) := R(B) + C */ 203OP_ADDI,/* A B C R(A) := R(B) + C */
204OP_SUBI,/* A B C R(A) := R(B) - C */
205OP_MULI,/* A B C R(A) := R(B) * C */
206OP_MODI,/* A B C R(A) := R(B) % C */
207OP_POWI,/* A B C R(A) := R(B) ^ C */
208OP_DIVI,/* A B C R(A) := R(B) / C */
209OP_IDIVI,/* A B C R(A) := R(B) // C */
210
204OP_ADD,/* A B C R(A) := R(B) + R(C) */ 211OP_ADD,/* A B C R(A) := R(B) + R(C) */
205OP_SUB,/* A B C R(A) := R(B) - R(C) */ 212OP_SUB,/* A B C R(A) := R(B) - R(C) */
206OP_MUL,/* A B C R(A) := R(B) * R(C) */ 213OP_MUL,/* A B C R(A) := R(B) * R(C) */
diff --git a/lvm.c b/lvm.c
index b534f0a0..9c2b1345 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.296 2017/09/28 16:53:29 roberto Exp roberto $ 2** $Id: lvm.c,v 2.297 2017/10/01 19:13:43 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*/
@@ -663,7 +663,10 @@ void luaV_finishOp (lua_State *L) {
663 Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ 663 Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
664 OpCode op = GET_OPCODE(inst); 664 OpCode op = GET_OPCODE(inst);
665 switch (op) { /* finish its execution */ 665 switch (op) { /* finish its execution */
666 case OP_ADDI: case OP_ADD: case OP_SUB: 666 case OP_ADDI: case OP_SUBI:
667 case OP_MULI: case OP_DIVI: case OP_IDIVI:
668 case OP_MODI: case OP_POWI:
669 case OP_ADD: case OP_SUB:
667 case OP_MUL: case OP_DIV: case OP_IDIV: 670 case OP_MUL: case OP_DIV: case OP_IDIV:
668 case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: 671 case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
669 case OP_MOD: case OP_POW: 672 case OP_MOD: case OP_POW:
@@ -1001,6 +1004,90 @@ void luaV_execute (lua_State *L) {
1001 Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_ADD)); 1004 Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_ADD));
1002 vmbreak; 1005 vmbreak;
1003 } 1006 }
1007 vmcase(OP_SUBI) {
1008 TValue *rb = vRB(i);
1009 int ic = GETARG_Cr(i);
1010 lua_Number nb;
1011 if (ttisinteger(rb)) {
1012 setivalue(s2v(ra), intop(-, ivalue(rb), ic));
1013 }
1014 else if (tonumberns(rb, nb)) {
1015 setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic)));
1016 }
1017 else
1018 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB));
1019 vmbreak;
1020 }
1021 vmcase(OP_MULI) {
1022 TValue *rb = vRB(i);
1023 int ic = GETARG_Cr(i);
1024 lua_Number nb;
1025 if (ttisinteger(rb)) {
1026 setivalue(s2v(ra), intop(*, ivalue(rb), ic));
1027 }
1028 else if (tonumberns(rb, nb)) {
1029 setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic)));
1030 }
1031 else
1032 Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_MUL));
1033 vmbreak;
1034 }
1035 vmcase(OP_MODI) {
1036 TValue *rb = vRB(i);
1037 int ic = GETARG_Cr(i);
1038 lua_Number nb;
1039 if (ttisinteger(rb)) {
1040 setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic));
1041 }
1042 else if (tonumberns(rb, nb)) {
1043 lua_Number m;
1044 lua_Number nc = cast_num(ic);
1045 luai_nummod(L, nb, nc, m);
1046 setfltvalue(s2v(ra), m);
1047 }
1048 else
1049 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD));
1050 vmbreak;
1051 }
1052 vmcase(OP_POWI) {
1053 TValue *rb = vRB(i);
1054 int ic = GETARG_Cr(i);
1055 lua_Number nb;
1056 if (tonumberns(rb, nb)) {
1057 lua_Number nc = cast_num(ic);
1058 setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
1059 }
1060 else
1061 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW));
1062 vmbreak;
1063 }
1064 vmcase(OP_DIVI) {
1065 TValue *rb = vRB(i);
1066 int ic = GETARG_Cr(i);
1067 lua_Number nb;
1068 if (tonumberns(rb, nb)) {
1069 lua_Number nc = cast_num(ic);
1070 setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
1071 }
1072 else
1073 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV));
1074 vmbreak;
1075 }
1076 vmcase(OP_IDIVI) {
1077 TValue *rb = vRB(i);
1078 int ic = GETARG_Cr(i);
1079 lua_Number nb;
1080 if (ttisinteger(rb)) {
1081 setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic));
1082 }
1083 else if (tonumberns(rb, nb)) {
1084 lua_Number nc = cast_num(ic);
1085 setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
1086 }
1087 else
1088 Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV));
1089 vmbreak;
1090 }
1004 vmcase(OP_ADD) { 1091 vmcase(OP_ADD) {
1005 TValue *rb = vRB(i); 1092 TValue *rb = vRB(i);
1006 TValue *rc = vRC(i); 1093 TValue *rc = vRC(i);