From 33e3774f447cbcfa4fe43b8b47d0306e52937428 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 9 Jan 2018 09:24:12 -0200
Subject: keep more opcode arguments byte-aligned

---
 lcode.c    | 12 ++++++------
 lopcodes.h | 36 ++++++++++++++++++++----------------
 ltests.c   |  4 ++--
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/lcode.c b/lcode.c
index 1eae475a..ae024da8 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lcode.c,v 2.147 2017/12/22 14:16:46 roberto Exp roberto $
+** $Id: lcode.c,v 2.149 2018/01/09 11:21:41 roberto Exp $
 ** Code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -262,16 +262,16 @@ void luaK_patchtohere (FuncState *fs, int list) {
 /*
 ** Correct a jump list to jump to 'target'. If 'hasclose' is true,
 ** 'target' contains an OP_CLOSE instruction (see first assert).
-** Only jumps with the 'k' arg true need that close; other jumps
+** Only the jumps with ('m' == true) need that close; other jumps
 ** avoid it jumping to the next instruction.
 */
 void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) {
   lua_assert(!hasclose || GET_OPCODE(fs->f->code[target]) == OP_CLOSE);
   while (list != NO_JUMP) {
     int next = getjump(fs, list);
-    lua_assert(!GETARG_k(fs->f->code[list]) || hasclose);
+    lua_assert(!GETARG_m(fs->f->code[list]) || hasclose);
     patchtestreg(fs, list, NO_REG);  /* do not generate values */
-    if (!hasclose || GETARG_k(fs->f->code[list]))
+    if (!hasclose || GETARG_m(fs->f->code[list]))
       fixjump(fs, list, target);
     else  /* there is a CLOSE instruction but jump does not need it */
       fixjump(fs, list, target + 1);  /* avoid CLOSE instruction */
@@ -281,14 +281,14 @@ void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) {
 
 
 /*
-** Mark (using the 'k' arg) all jumps in 'list' to close upvalues. Mark
+** Mark (using the 'm' arg) all jumps in 'list' to close upvalues. Mark
 ** will instruct 'luaK_patchgoto' to make these jumps go to OP_CLOSE
 ** instructions.
 */
 void luaK_patchclose (FuncState *fs, int list) {
   for (; list != NO_JUMP; list = getjump(fs, list)) {
     lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP);
-    SETARG_k(fs->f->code[list], 1);
+    SETARG_m(fs->f->code[list], 1);
   }
 }
 
diff --git a/lopcodes.h b/lopcodes.h
index eae0dfaf..22de7a71 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lopcodes.h,v 1.180 2017/12/18 17:49:31 roberto Exp roberto $
+** $Id: lopcodes.h,v 1.182 2018/01/09 11:21:41 roberto Exp $
 ** Opcodes for Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -17,11 +17,11 @@
 
         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
         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
-iABC    |k|     C(8)    | |     B(8)    | |     A(8)    | |   Op(7)   |
-iABx    |            Bx(17)             | |     A(8)    | |   Op(7)   |
-iAsBx   |           sBx (signed)(17)    | |     A(8)    | |   Op(7)   |
-iAx     |                       Ax(25)                  | |   Op(7)   |
-iksJ    |k|                     sJ(24)                  | |   Op(7)   |
+iABC         C(8)     |      B(8)     |k|     A(8)      |   Op(7)     |
+iABx               Bx(17)               |     A(8)      |   Op(7)     |
+iAsB              sBx (signed)(17)      |     A(8)      |   Op(7)     |
+iAx                           Ax(25)                    |   Op(7)     |
+isJ                          sJ(24)                   |m|   Op(7)     |
 
   A signed argument is represented in excess K: the represented value is
   the written unsigned value minus K, where K is half the maximum for the
@@ -36,25 +36,27 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 ** size and position of opcode arguments.
 */
 #define SIZE_C		8
-#define SIZE_Cx		(SIZE_C + 1)
 #define SIZE_B		8
-#define SIZE_Bx		(SIZE_Cx + SIZE_B)
+#define SIZE_Bx		(SIZE_C + SIZE_B + 1)
 #define SIZE_A		8
-#define SIZE_Ax		(SIZE_Cx + SIZE_B + SIZE_A)
-#define SIZE_sJ		(SIZE_C + SIZE_B + SIZE_A)
-
+#define SIZE_Ax		(SIZE_Bx + SIZE_A)
+#define SIZE_sJ		(SIZE_Bx + SIZE_A - 1)
 
 #define SIZE_OP		7
 
 #define POS_OP		0
+
 #define POS_A		(POS_OP + SIZE_OP)
-#define POS_B		(POS_A + SIZE_A)
+#define POS_k		(POS_A + SIZE_A)
+#define POS_B		(POS_k + 1)
 #define POS_C		(POS_B + SIZE_B)
-#define POS_k		(POS_C + SIZE_C)
-#define POS_Bx		POS_B
+
+#define POS_Bx		POS_k
+
 #define POS_Ax		POS_A
-#define POS_sJ		POS_A
 
+#define POS_m		POS_A
+#define POS_sJ		(POS_A + 1)
 
 /*
 ** limits for opcode arguments.
@@ -125,7 +127,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 #define SETARG_C(i,v)	setarg(i, v, POS_C, SIZE_C)
 
 #define TESTARG_k(i)	(cast(int, ((i) & (1u << POS_k))))
-#define GETARG_k(i)	getarg(i, POS_k, 1)
+#define GETARG_k(i)	check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
 #define SETARG_k(i,v)	setarg(i, v, POS_k, 1)
 
 #define GETARG_Bx(i)	check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
@@ -142,6 +144,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 	check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
 #define SETARG_sJ(i,j) \
 	setarg(i, cast(unsigned int, (j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
+#define GETARG_m(i)	check_exp(checkopm(i, isJ), getarg(i, POS_m, 1))
+#define SETARG_m(i,m)	setarg(i, m, POS_m, 1)
 
 
 #define CREATE_ABCk(o,a,b,c,k)	((cast(Instruction, o)<<POS_OP) \
diff --git a/ltests.c b/ltests.c
index 513c846b..569c1896 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 2.237 2017/12/15 18:53:48 roberto Exp roberto $
+** $Id: ltests.c,v 2.239 2018/01/09 11:21:41 roberto Exp $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -557,7 +557,7 @@ static char *buildop (Proto *p, int pc, char *buff) {
       break;
     case isJ:
       sprintf(buff+strlen(buff), "%-12s%4d (%1d)", name, GETARG_sJ(i),
-                                                         !!GETARG_k(i));
+                                                         !!GETARG_m(i));
       break;
   }
   return buff;
-- 
cgit v1.2.3-55-g6feb