aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-10-04 18:56:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-10-04 18:56:32 -0300
commita1ef58b3a5986293ed0b7acef50073d94c7f932f (patch)
treea2bb5ad195001c03caeec9b536a1f8d97acba693
parent283e7455ffe32235eaf790ebd3c40c7970b7a833 (diff)
downloadlua-a1ef58b3a5986293ed0b7acef50073d94c7f932f.tar.gz
lua-a1ef58b3a5986293ed0b7acef50073d94c7f932f.tar.bz2
lua-a1ef58b3a5986293ed0b7acef50073d94c7f932f.zip
eplicit 1-bit opcode operand 'k'
-rw-r--r--lcode.c74
-rw-r--r--lcode.h8
-rw-r--r--ldebug.c17
-rw-r--r--lopcodes.h71
-rw-r--r--ltests.c7
-rw-r--r--lvm.c22
6 files changed, 103 insertions, 96 deletions
diff --git a/lcode.c b/lcode.c
index 786fd342..f94afb0b 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.128 2017/10/02 22:50:57 roberto Exp roberto $ 2** $Id: lcode.c,v 2.129 2017/10/04 15:49:24 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*/
@@ -202,7 +202,7 @@ static int patchtestreg (FuncState *fs, int node, int reg) {
202 else { 202 else {
203 /* no register to put value or register already has the value; 203 /* no register to put value or register already has the value;
204 change instruction to simple test */ 204 change instruction to simple test */
205 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); 205 *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i), 0);
206 } 206 }
207 return 1; 207 return 1;
208} 208}
@@ -365,13 +365,18 @@ static int luaK_code (FuncState *fs, Instruction i) {
365** Format and emit an 'iABC' instruction. (Assertions check consistency 365** Format and emit an 'iABC' instruction. (Assertions check consistency
366** of parameters versus opcode.) 366** of parameters versus opcode.)
367*/ 367*/
368int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { 368int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
369 lua_assert(getOpMode(o) == iABC); 369 lua_assert(getOpMode(o) == iABC);
370 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); 370 lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
371 return luaK_code(fs, CREATE_ABC(o, a, b, c)); 371 c <= MAXARG_C && (k & ~1) == 0);
372 return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
372} 373}
373 374
374 375
376#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + MAXARG_sC),k)
377
378
379
375/* 380/*
376** Format and emit an 'iABx' instruction. 381** Format and emit an 'iABx' instruction.
377*/ 382*/
@@ -448,7 +453,7 @@ void luaK_reserveregs (FuncState *fs, int n) {
448) 453)
449*/ 454*/
450static void freereg (FuncState *fs, int reg) { 455static void freereg (FuncState *fs, int reg) {
451 if (!ISK(reg) && reg >= fs->nactvar) { 456 if (reg >= fs->nactvar) {
452 fs->freereg--; 457 fs->freereg--;
453 lua_assert(reg == fs->freereg); 458 lua_assert(reg == fs->freereg);
454 } 459 }
@@ -853,7 +858,7 @@ void luaK_exp2val (FuncState *fs, expdesc *e) {
853** Ensures final expression result is in a valid R/K index 858** Ensures final expression result is in a valid R/K index
854** (that is, it is either in a register or in 'k' with an index 859** (that is, it is either in a register or in 'k' with an index
855** in the range of R/K indices). 860** in the range of R/K indices).
856** Returns R/K index. 861** Returns 1 if expression is K, 0 otherwise.
857*/ 862*/
858int luaK_exp2RK (FuncState *fs, expdesc *e) { 863int luaK_exp2RK (FuncState *fs, expdesc *e) {
859 luaK_exp2val(fs, e); 864 luaK_exp2val(fs, e);
@@ -867,12 +872,20 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
867 vk: 872 vk:
868 e->k = VK; 873 e->k = VK;
869 if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ 874 if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
870 return RKASK(e->u.info); 875 return 1;
871 else break; 876 else break;
872 default: break; 877 default: break;
873 } 878 }
874 /* not a constant in the right range: put it in a register */ 879 /* not a constant in the right range: put it in a register */
875 return luaK_exp2anyreg(fs, e); 880 luaK_exp2anyreg(fs, e);
881 return 0;
882}
883
884
885static void codeABRK (FuncState *fs, OpCode o, int a, int b,
886 expdesc *ec) {
887 int k = luaK_exp2RK(fs, ec);
888 luaK_codeABCk(fs, o, a, b, ec->u.info, k);
876} 889}
877 890
878 891
@@ -892,23 +905,19 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
892 break; 905 break;
893 } 906 }
894 case VINDEXUP: { 907 case VINDEXUP: {
895 int e = luaK_exp2RK(fs, ex); 908 codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
896 luaK_codeABC(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, e);
897 break; 909 break;
898 } 910 }
899 case VINDEXI: { 911 case VINDEXI: {
900 int e = luaK_exp2RK(fs, ex); 912 codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
901 luaK_codeABC(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, e);
902 break; 913 break;
903 } 914 }
904 case VINDEXSTR: { 915 case VINDEXSTR: {
905 int e = luaK_exp2RK(fs, ex); 916 codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
906 luaK_codeABC(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, e);
907 break; 917 break;
908 } 918 }
909 case VINDEXED: { 919 case VINDEXED: {
910 int e = luaK_exp2RK(fs, ex); 920 codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
911 luaK_codeABC(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, e);
912 break; 921 break;
913 } 922 }
914 default: lua_assert(0); /* invalid var kind to store */ 923 default: lua_assert(0); /* invalid var kind to store */
@@ -928,7 +937,7 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
928 e->u.info = fs->freereg; /* base register for op_self */ 937 e->u.info = fs->freereg; /* base register for op_self */
929 e->k = VNONRELOC; /* self expression has a fixed register */ 938 e->k = VNONRELOC; /* self expression has a fixed register */
930 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ 939 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
931 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); 940 codeABRK(fs, OP_SELF, e->u.info, ereg, key);
932 freeexp(fs, key); 941 freeexp(fs, key);
933} 942}
934 943
@@ -1064,11 +1073,21 @@ static int isKstr (FuncState *fs, expdesc *e) {
1064 1073
1065/* 1074/*
1066** Check whether expression 'e' is a literal integer in 1075** Check whether expression 'e' is a literal integer in
1067** proper range 1076** proper range to fit in register C
1077*/
1078static int isCint (expdesc *e) {
1079 return (e->k == VKINT && !hasjumps(e) &&
1080 l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1081}
1082
1083
1084/*
1085** Check whether expression 'e' is a literal integer in
1086** proper range to fit in register sC
1068*/ 1087*/
1069static int isKint (expdesc *e) { 1088static int isSCint (expdesc *e) {
1070 return (e->k == VKINT && !hasjumps(e) && 1089 return (e->k == VKINT && !hasjumps(e) &&
1071 l_castS2U(e->u.ival) <= l_castS2U(MAXARG_Cr)); 1090 l_castS2U(e->u.ival + MAXARG_sC) <= l_castS2U(MAXARG_C));
1072} 1091}
1073 1092
1074 1093
@@ -1091,7 +1110,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1091 t->u.ind.idx = k->u.info; /* literal string */ 1110 t->u.ind.idx = k->u.info; /* literal string */
1092 t->k = VINDEXSTR; 1111 t->k = VINDEXSTR;
1093 } 1112 }
1094 else if (isKint(k)) { 1113 else if (isCint(k)) {
1095 t->u.ind.idx = k->u.ival; /* integer constant */ 1114 t->u.ind.idx = k->u.ival; /* integer constant */
1096 t->k = VINDEXI; 1115 t->k = VINDEXI;
1097 } 1116 }
@@ -1187,16 +1206,14 @@ static void codebinexpval (FuncState *fs, OpCode op,
1187*/ 1206*/
1188static void codearith (FuncState *fs, OpCode op, 1207static void codearith (FuncState *fs, OpCode op,
1189 expdesc *e1, expdesc *e2, int flip, int line) { 1208 expdesc *e1, expdesc *e2, int flip, int line) {
1190 if (!isKint(e2)) 1209 if (!isSCint(e2))
1191 codebinexpval(fs, op, e1, e2, line); /* use standard operators */ 1210 codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1192 else { /* use immediate operators */ 1211 else { /* use immediate operators */
1193 int v2 = cast_int(e2->u.ival); /* immediate operand */ 1212 int v2 = cast_int(e2->u.ival); /* immediate operand */
1194 int v1 = luaK_exp2anyreg(fs, e1); 1213 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); 1214 op = cast(OpCode, op - OP_ADD + OP_ADDI);
1198 freeexp(fs, e1); 1215 freeexp(fs, e1);
1199 e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ 1216 e1->u.info = codeABsC(fs, op, 0, v1, v2, flip); /* generate opcode */
1200 e1->k = VRELOCABLE; /* all those operations are relocatable */ 1217 e1->k = VRELOCABLE; /* all those operations are relocatable */
1201 luaK_fixline(fs, line); 1218 luaK_fixline(fs, line);
1202 } 1219 }
@@ -1210,7 +1227,7 @@ static void codearith (FuncState *fs, OpCode op,
1210static void codecommutative (FuncState *fs, OpCode op, 1227static void codecommutative (FuncState *fs, OpCode op,
1211 expdesc *e1, expdesc *e2, int line) { 1228 expdesc *e1, expdesc *e2, int line) {
1212 int flip = 0; 1229 int flip = 0;
1213 if (isKint(e1)) { 1230 if (isSCint(e1)) {
1214 expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ 1231 expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
1215 flip = 1; 1232 flip = 1;
1216 } 1233 }
@@ -1223,8 +1240,7 @@ static void codecommutative (FuncState *fs, OpCode op,
1223** 'e1' was already put in register by 'luaK_infix'. 1240** 'e1' was already put in register by 'luaK_infix'.
1224*/ 1241*/
1225static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { 1242static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1226 int rk1 = (e1->k == VK) ? RKASK(e1->u.info) 1243 int rk1 = check_exp(e1->k == VNONRELOC, e1->u.info);
1227 : check_exp(e1->k == VNONRELOC, e1->u.info);
1228 int rk2 = luaK_exp2anyreg(fs, e2); 1244 int rk2 = luaK_exp2anyreg(fs, e2);
1229 freeexps(fs, e1, e2); 1245 freeexps(fs, e1, e2);
1230 switch (opr) { 1246 switch (opr) {
diff --git a/lcode.h b/lcode.h
index e7f048fd..1c3229d9 100644
--- a/lcode.h
+++ b/lcode.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.h,v 1.66 2017/09/13 19:50:08 roberto Exp roberto $ 2** $Id: lcode.h,v 1.67 2017/09/28 16:53:29 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*/
@@ -37,6 +37,9 @@ typedef enum BinOpr {
37} BinOpr; 37} BinOpr;
38 38
39 39
40#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
41
42
40typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 43typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
41 44
42 45
@@ -50,7 +53,8 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
50 53
51LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 54LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
52LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); 55LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx);
53LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 56LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
57 int B, int C, int k);
54LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 58LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
55LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 59LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
56LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 60LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
diff --git a/ldebug.c b/ldebug.c
index 67b9496a..41eaf68c 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.130 2017/07/10 17:35:12 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.131 2017/10/04 15:49:24 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*/
@@ -402,7 +402,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
402** Find a "name" for the constant 'c'. 402** Find a "name" for the constant 'c'.
403*/ 403*/
404static void kname (Proto *p, int c, const char **name) { 404static void kname (Proto *p, int c, const char **name) {
405 TValue *kvalue = &p->k[INDEXK(c)]; 405 TValue *kvalue = &p->k[c];
406 *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; 406 *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?";
407} 407}
408 408
@@ -418,17 +418,17 @@ static void rname (Proto *p, int pc, int c, const char **name) {
418 418
419 419
420/* 420/*
421** Find a "name" for the R/K index 'c'. 421** Find a "name" for a 'C' value in an RK instruction.
422*/ 422*/
423static void rkname (Proto *p, int pc, int c, const char **name) { 423static void rkname (Proto *p, int pc, Instruction i, const char **name) {
424 if (ISK(c)) /* is 'c' a constant? */ 424 int c = GETARG_C(i); /* key index */
425 kname(p, INDEXK(c), name); 425 if (GETARG_k(i)) /* is 'c' a constant? */
426 kname(p, c, name);
426 else /* 'c' is a register */ 427 else /* 'c' is a register */
427 rname(p, pc, c, name); 428 rname(p, pc, c, name);
428} 429}
429 430
430 431
431
432static int filterpc (int pc, int jmptarget) { 432static int filterpc (int pc, int jmptarget) {
433 if (pc < jmptarget) /* is code conditional (inside a jump)? */ 433 if (pc < jmptarget) /* is code conditional (inside a jump)? */
434 return -1; /* cannot know who sets that register */ 434 return -1; /* cannot know who sets that register */
@@ -549,8 +549,7 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) {
549 break; 549 break;
550 } 550 }
551 case OP_SELF: { 551 case OP_SELF: {
552 int k = GETARG_C(i); /* key index */ 552 rkname(p, pc, i, name);
553 rkname(p, pc, k, name);
554 return "method"; 553 return "method";
555 } 554 }
556 default: break; /* go through to return NULL */ 555 default: break; /* go through to return NULL */
diff --git a/lopcodes.h b/lopcodes.h
index 39f36aa7..de0c5cc1 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.164 2017/10/02 22:51:32 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.165 2017/10/04 15:49:24 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*/
@@ -17,7 +17,7 @@
17 17
18 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 18 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
19 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 19 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
20iABC | C(9) | | B(8) | | A(8) | | Op(7) | 20iABC |k| C(8) | | B(8) | | A(8) | | Op(7) |
21iABx | Bx(17) | | A(8) | | Op(7) | 21iABx | Bx(17) | | A(8) | | Op(7) |
22iAsBx | sBx (signed)(17) | | A(8) | | Op(7) | 22iAsBx | sBx (signed)(17) | | A(8) | | Op(7) |
23iAx | Ax(25) | | Op(7) | 23iAx | Ax(25) | | Op(7) |
@@ -34,19 +34,21 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
34/* 34/*
35** size and position of opcode arguments. 35** size and position of opcode arguments.
36*/ 36*/
37#define SIZE_C 9 37#define SIZE_C 8
38#define SIZE_Cx (SIZE_C + 1)
38#define SIZE_B 8 39#define SIZE_B 8
39#define SIZE_Bx (SIZE_C + SIZE_B) 40#define SIZE_Bx (SIZE_Cx + SIZE_B)
40#define SIZE_A 8 41#define SIZE_A 8
41#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) 42#define SIZE_Ax (SIZE_Cx + SIZE_B + SIZE_A)
42 43
43#define SIZE_OP 7 44#define SIZE_OP 7
44 45
45#define POS_OP 0 46#define POS_OP 0
46#define POS_A (POS_OP + SIZE_OP) 47#define POS_A (POS_OP + SIZE_OP)
47#define POS_C (POS_A + SIZE_A) 48#define POS_B (POS_A + SIZE_A)
48#define POS_B (POS_C + SIZE_C) 49#define POS_C (POS_B + SIZE_B)
49#define POS_Bx POS_C 50#define POS_k (POS_C + SIZE_C)
51#define POS_Bx POS_B
50#define POS_Ax POS_A 52#define POS_Ax POS_A
51 53
52 54
@@ -70,10 +72,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
70#endif 72#endif
71 73
72 74
73#define MAXARG_A ((1<<SIZE_A)-1) 75#define MAXARG_A ((1<<SIZE_A)-1)
74#define MAXARG_B ((1<<SIZE_B)-1) 76#define MAXARG_B ((1<<SIZE_B)-1)
75#define MAXARG_C ((1<<SIZE_C)-1) 77#define MAXARG_C ((1<<SIZE_C)-1)
76#define MAXARG_Cr ((1<<(SIZE_C - 1))-1) 78#define MAXARG_sC (MAXARG_C >> 1)
79#define MAXARG_Cx ((1<<(SIZE_C + 1))-1)
77 80
78 81
79/* creates a mask with 'n' 1 bits at position 'p' */ 82/* creates a mask with 'n' 1 bits at position 'p' */
@@ -104,11 +107,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
104#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) 107#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
105 108
106#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) 109#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
110#define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC)
107#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) 111#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
108 112
109#define GETARG_Cr(i) \ 113#define GETARG_k(i) (cast(int, ((i) & (1 << POS_k))))
110 check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C - 1))
111#define GETARG_Ck(i) getarg(i, (POS_C + SIZE_C - 1), 1)
112 114
113#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx)) 115#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
114#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx) 116#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
@@ -121,10 +123,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
121#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) 123#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
122 124
123 125
124#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ 126#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
125 | (cast(Instruction, a)<<POS_A) \ 127 | (cast(Instruction, a)<<POS_A) \
126 | (cast(Instruction, b)<<POS_B) \ 128 | (cast(Instruction, b)<<POS_B) \
127 | (cast(Instruction, c)<<POS_C)) 129 | (cast(Instruction, c)<<POS_C)) \
130 | (cast(Instruction, k)<<POS_k)
128 131
129#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ 132#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
130 | (cast(Instruction, a)<<POS_A) \ 133 | (cast(Instruction, a)<<POS_A) \
@@ -134,26 +137,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
134 | (cast(Instruction, a)<<POS_Ax)) 137 | (cast(Instruction, a)<<POS_Ax))
135 138
136 139
137/*
138** Macros to operate RK indices
139*/
140
141/* this bit 1 means constant (0 means register) */
142#define BITRK (1 << (SIZE_C - 1))
143
144/* test whether value is a constant */
145#define ISK(x) ((x) & BITRK)
146
147/* gets the index of the constant */
148#define INDEXK(r) ((int)(r) & ~BITRK)
149
150#if !defined(MAXINDEXRK) /* (for debugging only) */ 140#if !defined(MAXINDEXRK) /* (for debugging only) */
151#define MAXINDEXRK (BITRK - 1) 141#define MAXINDEXRK MAXARG_B
152#endif 142#endif
153 143
154/* code a constant index as a RK value */
155#define RKASK(x) ((x) | BITRK)
156
157 144
158/* 145/*
159** invalid register that fits in 8 bits 146** invalid register that fits in 8 bits
@@ -164,7 +151,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
164/* 151/*
165** R(x) - register 152** R(x) - register
166** K(x) - constant (in constant table) 153** K(x) - constant (in constant table)
167** RK(x) == if ISK(x) then K(INDEXK(x)) else R(x) 154** RK(x) == if k(i) then K(x) else R(x)
168*/ 155*/
169 156
170 157
@@ -200,13 +187,13 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
200 187
201OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ 188OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */
202 189
203OP_ADDI,/* A B C R(A) := R(B) + C */ 190OP_ADDI,/* A B sC R(A) := R(B) + C */
204OP_SUBI,/* A B C R(A) := R(B) - C */ 191OP_SUBI,/* A B sC R(A) := R(B) - C */
205OP_MULI,/* A B C R(A) := R(B) * C */ 192OP_MULI,/* A B sC R(A) := R(B) * C */
206OP_MODI,/* A B C R(A) := R(B) % C */ 193OP_MODI,/* A B sC R(A) := R(B) % C */
207OP_POWI,/* A B C R(A) := R(B) ^ C */ 194OP_POWI,/* A B sC R(A) := R(B) ^ C */
208OP_DIVI,/* A B C R(A) := R(B) / C */ 195OP_DIVI,/* A B sC R(A) := R(B) / C */
209OP_IDIVI,/* A B C R(A) := R(B) // C */ 196OP_IDIVI,/* A B sC R(A) := R(B) // C */
210 197
211OP_ADD,/* A B C R(A) := R(B) + R(C) */ 198OP_ADD,/* A B C R(A) := R(B) + R(C) */
212OP_SUB,/* A B C R(A) := R(B) - R(C) */ 199OP_SUB,/* A B C R(A) := R(B) - R(C) */
diff --git a/ltests.c b/ltests.c
index b4518604..7420fe3b 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.223 2017/06/29 15:06:44 roberto Exp roberto $ 2** $Id: ltests.c,v 2.224 2017/10/01 19:17:51 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -541,8 +541,9 @@ static char *buildop (Proto *p, int pc, char *buff) {
541 sprintf(buff, "(%4d) %4d - ", line, pc); 541 sprintf(buff, "(%4d) %4d - ", line, pc);
542 switch (getOpMode(o)) { 542 switch (getOpMode(o)) {
543 case iABC: 543 case iABC:
544 sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name, 544 sprintf(buff+strlen(buff), "%-12s%4d %4d %4d%s", name,
545 GETARG_A(i), GETARG_B(i), GETARG_C(i)); 545 GETARG_A(i), GETARG_B(i), GETARG_C(i),
546 GETARG_k(i) ? " (k)" : "");
546 break; 547 break;
547 case iABx: 548 case iABx:
548 sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); 549 sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
diff --git a/lvm.c b/lvm.c
index 9c2b1345..b3e32047 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.297 2017/10/01 19:13:43 roberto Exp roberto $ 2** $Id: lvm.c,v 2.298 2017/10/04 15:49:24 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*/
@@ -742,7 +742,7 @@ void luaV_finishOp (lua_State *L) {
742#define RC(i) (base+GETARG_C(i)) 742#define RC(i) (base+GETARG_C(i))
743#define vRC(i) s2v(RC(i)) 743#define vRC(i) s2v(RC(i))
744#define KC(i) (k+GETARG_C(i)) 744#define KC(i) (k+GETARG_C(i))
745#define RKC(i) ((GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i))) 745#define RKC(i) ((GETARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
746 746
747 747
748 748
@@ -992,7 +992,7 @@ void luaV_execute (lua_State *L) {
992 } 992 }
993 vmcase(OP_ADDI) { 993 vmcase(OP_ADDI) {
994 TValue *rb = vRB(i); 994 TValue *rb = vRB(i);
995 int ic = GETARG_Cr(i); 995 int ic = GETARG_sC(i);
996 lua_Number nb; 996 lua_Number nb;
997 if (ttisinteger(rb)) { 997 if (ttisinteger(rb)) {
998 setivalue(s2v(ra), intop(+, ivalue(rb), ic)); 998 setivalue(s2v(ra), intop(+, ivalue(rb), ic));
@@ -1001,12 +1001,12 @@ void luaV_execute (lua_State *L) {
1001 setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); 1001 setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic)));
1002 } 1002 }
1003 else 1003 else
1004 Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_ADD)); 1004 Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD));
1005 vmbreak; 1005 vmbreak;
1006 } 1006 }
1007 vmcase(OP_SUBI) { 1007 vmcase(OP_SUBI) {
1008 TValue *rb = vRB(i); 1008 TValue *rb = vRB(i);
1009 int ic = GETARG_Cr(i); 1009 int ic = GETARG_sC(i);
1010 lua_Number nb; 1010 lua_Number nb;
1011 if (ttisinteger(rb)) { 1011 if (ttisinteger(rb)) {
1012 setivalue(s2v(ra), intop(-, ivalue(rb), ic)); 1012 setivalue(s2v(ra), intop(-, ivalue(rb), ic));
@@ -1020,7 +1020,7 @@ void luaV_execute (lua_State *L) {
1020 } 1020 }
1021 vmcase(OP_MULI) { 1021 vmcase(OP_MULI) {
1022 TValue *rb = vRB(i); 1022 TValue *rb = vRB(i);
1023 int ic = GETARG_Cr(i); 1023 int ic = GETARG_sC(i);
1024 lua_Number nb; 1024 lua_Number nb;
1025 if (ttisinteger(rb)) { 1025 if (ttisinteger(rb)) {
1026 setivalue(s2v(ra), intop(*, ivalue(rb), ic)); 1026 setivalue(s2v(ra), intop(*, ivalue(rb), ic));
@@ -1029,12 +1029,12 @@ void luaV_execute (lua_State *L) {
1029 setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic))); 1029 setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic)));
1030 } 1030 }
1031 else 1031 else
1032 Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_MUL)); 1032 Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL));
1033 vmbreak; 1033 vmbreak;
1034 } 1034 }
1035 vmcase(OP_MODI) { 1035 vmcase(OP_MODI) {
1036 TValue *rb = vRB(i); 1036 TValue *rb = vRB(i);
1037 int ic = GETARG_Cr(i); 1037 int ic = GETARG_sC(i);
1038 lua_Number nb; 1038 lua_Number nb;
1039 if (ttisinteger(rb)) { 1039 if (ttisinteger(rb)) {
1040 setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); 1040 setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic));
@@ -1051,7 +1051,7 @@ void luaV_execute (lua_State *L) {
1051 } 1051 }
1052 vmcase(OP_POWI) { 1052 vmcase(OP_POWI) {
1053 TValue *rb = vRB(i); 1053 TValue *rb = vRB(i);
1054 int ic = GETARG_Cr(i); 1054 int ic = GETARG_sC(i);
1055 lua_Number nb; 1055 lua_Number nb;
1056 if (tonumberns(rb, nb)) { 1056 if (tonumberns(rb, nb)) {
1057 lua_Number nc = cast_num(ic); 1057 lua_Number nc = cast_num(ic);
@@ -1063,7 +1063,7 @@ void luaV_execute (lua_State *L) {
1063 } 1063 }
1064 vmcase(OP_DIVI) { 1064 vmcase(OP_DIVI) {
1065 TValue *rb = vRB(i); 1065 TValue *rb = vRB(i);
1066 int ic = GETARG_Cr(i); 1066 int ic = GETARG_sC(i);
1067 lua_Number nb; 1067 lua_Number nb;
1068 if (tonumberns(rb, nb)) { 1068 if (tonumberns(rb, nb)) {
1069 lua_Number nc = cast_num(ic); 1069 lua_Number nc = cast_num(ic);
@@ -1075,7 +1075,7 @@ void luaV_execute (lua_State *L) {
1075 } 1075 }
1076 vmcase(OP_IDIVI) { 1076 vmcase(OP_IDIVI) {
1077 TValue *rb = vRB(i); 1077 TValue *rb = vRB(i);
1078 int ic = GETARG_Cr(i); 1078 int ic = GETARG_sC(i);
1079 lua_Number nb; 1079 lua_Number nb;
1080 if (ttisinteger(rb)) { 1080 if (ttisinteger(rb)) {
1081 setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic)); 1081 setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic));