aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lcode.c44
-rw-r--r--lopcodes.h29
-rw-r--r--ltests.c4
3 files changed, 50 insertions, 27 deletions
diff --git a/lcode.c b/lcode.c
index 660e5270..162b732d 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.143 2017/12/13 18:32:09 roberto Exp roberto $ 2** $Id: lcode.c,v 2.144 2017/12/14 14:24:02 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*/
@@ -108,7 +108,7 @@ static void fixjump (FuncState *fs, int pc, int dest) {
108 Instruction *jmp = &fs->f->code[pc]; 108 Instruction *jmp = &fs->f->code[pc];
109 int offset = dest - (pc + 1); 109 int offset = dest - (pc + 1);
110 lua_assert(dest != NO_JUMP); 110 lua_assert(dest != NO_JUMP);
111 if (abs(offset) > MAXARG_sJ) 111 if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
112 luaX_syntaxerror(fs->ls, "control structure too long"); 112 luaX_syntaxerror(fs->ls, "control structure too long");
113 lua_assert(GET_OPCODE(*jmp) == OP_JMP); 113 lua_assert(GET_OPCODE(*jmp) == OP_JMP);
114 SETARG_sJ(*jmp, offset); 114 SETARG_sJ(*jmp, offset);
@@ -360,7 +360,7 @@ int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
360} 360}
361 361
362 362
363#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + MAXARG_sC),k) 363#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + OFFSET_sC),k)
364 364
365 365
366 366
@@ -378,7 +378,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
378** Format and emit an 'iAsBx' instruction. 378** Format and emit an 'iAsBx' instruction.
379*/ 379*/
380int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { 380int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
381 unsigned int b = bc + MAXARG_sBx; 381 unsigned int b = bc + OFFSET_sBx;
382 lua_assert(getOpMode(o) == iAsBx); 382 lua_assert(getOpMode(o) == iAsBx);
383 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); 383 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
384 return luaK_code(fs, CREATE_ABx(o, a, b)); 384 return luaK_code(fs, CREATE_ABx(o, a, b));
@@ -389,7 +389,7 @@ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
389** Format and emit an 'isJ' instruction. 389** Format and emit an 'isJ' instruction.
390*/ 390*/
391static int codesJ (FuncState *fs, OpCode o, int sj, int k) { 391static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
392 unsigned int j = sj + MAXARG_sJ; 392 unsigned int j = sj + OFFSET_sJ;
393 lua_assert(getOpMode(o) == isJ); 393 lua_assert(getOpMode(o) == isJ);
394 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); 394 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
395 return luaK_code(fs, CREATE_sJ(o, j, k)); 395 return luaK_code(fs, CREATE_sJ(o, j, k));
@@ -582,8 +582,26 @@ static int nilK (FuncState *fs) {
582} 582}
583 583
584 584
585/*
586** Check whether 'i' can be stored in an 'sC' operand.
587** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C)
588** but without risk of overflows in the addition.
589*/
590static int fitsC (lua_Integer i) {
591 return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC);
592}
593
594
595/*
596** Check whether 'i' can be stored in an 'sBx' operand.
597*/
598static int fitsBx (lua_Integer i) {
599 return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
600}
601
602
585void luaK_int (FuncState *fs, int reg, lua_Integer i) { 603void luaK_int (FuncState *fs, int reg, lua_Integer i) {
586 if (l_castS2U(i) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) 604 if (fitsBx(i))
587 luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); 605 luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
588 else 606 else
589 luaK_codek(fs, reg, luaK_intK(fs, i)); 607 luaK_codek(fs, reg, luaK_intK(fs, i));
@@ -593,8 +611,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
593static int floatI (lua_Number f, lua_Integer *fi) { 611static int floatI (lua_Number f, lua_Integer *fi) {
594 TValue v; 612 TValue v;
595 setfltvalue(&v, f); 613 setfltvalue(&v, f);
596 return (luaV_flttointeger(&v, fi, 0) && 614 return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi));
597 l_castS2U(*fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx));
598} 615}
599 616
600 617
@@ -1089,8 +1106,7 @@ static int isCint (expdesc *e) {
1089** proper range to fit in register sC 1106** proper range to fit in register sC
1090*/ 1107*/
1091static int isSCint (expdesc *e) { 1108static int isSCint (expdesc *e) {
1092 return (e->k == VKINT && !hasjumps(e) && 1109 return (e->k == VKINT && !hasjumps(e) && fitsC(e->u.ival));
1093 l_castS2U(e->u.ival + MAXARG_sC) <= l_castS2U(MAXARG_C));
1094} 1110}
1095 1111
1096 1112
@@ -1103,8 +1119,12 @@ static int isSCnumber (expdesc *e, lua_Integer *i) {
1103 *i = e->u.ival; 1119 *i = e->u.ival;
1104 else if (!(e->k == VKFLT && floatI(e->u.nval, i))) 1120 else if (!(e->k == VKFLT && floatI(e->u.nval, i)))
1105 return 0; /* not a number */ 1121 return 0; /* not a number */
1106 *i += MAXARG_sC; 1122 if (!hasjumps(e) && fitsC(*i)) {
1107 return (!hasjumps(e) && l_castS2U(*i) <= l_castS2U(MAXARG_C)); 1123 *i += OFFSET_sC;
1124 return 1;
1125 }
1126 else
1127 return 0;
1108} 1128}
1109 1129
1110 1130
diff --git a/lopcodes.h b/lopcodes.h
index 3338f717..c3269dff 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.177 2017/12/13 18:32:09 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.178 2017/12/15 18:35:22 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*/
@@ -62,13 +62,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
62** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) 62** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
63*/ 63*/
64#if SIZE_Bx < LUAI_BITSINT-1 64#if SIZE_Bx < LUAI_BITSINT-1
65#define MAXARG_Bx ((1<<SIZE_Bx)-1) 65#define MAXARG_Bx ((1<<SIZE_Bx)-1)
66#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
67#else 66#else
68#define MAXARG_Bx MAX_INT 67#define MAXARG_Bx MAX_INT
69#define MAXARG_sBx MAX_INT
70#endif 68#endif
71 69
70#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
71
72
72#if SIZE_Ax < LUAI_BITSINT-1 73#if SIZE_Ax < LUAI_BITSINT-1
73#define MAXARG_Ax ((1<<SIZE_Ax)-1) 74#define MAXARG_Ax ((1<<SIZE_Ax)-1)
74#else 75#else
@@ -76,16 +77,18 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
76#endif 77#endif
77 78
78#if SIZE_sJ < LUAI_BITSINT-1 79#if SIZE_sJ < LUAI_BITSINT-1
79#define MAXARG_sJ ((1 << (SIZE_sJ - 1)) - 1) 80#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
80#else 81#else
81#define MAXARG_sJ MAX_INT 82#define MAXARG_sJ MAX_INT
82#endif 83#endif
83 84
85#define OFFSET_sJ (MAXARG_sJ >> 1)
86
84 87
85#define MAXARG_A ((1<<SIZE_A)-1) 88#define MAXARG_A ((1<<SIZE_A)-1)
86#define MAXARG_B ((1<<SIZE_B)-1) 89#define MAXARG_B ((1<<SIZE_B)-1)
87#define MAXARG_C ((1<<SIZE_C)-1) 90#define MAXARG_C ((1<<SIZE_C)-1)
88#define MAXARG_sC (MAXARG_C >> 1) 91#define OFFSET_sC (MAXARG_C >> 1)
89#define MAXARG_Cx ((1<<(SIZE_C + 1))-1) 92#define MAXARG_Cx ((1<<(SIZE_C + 1))-1)
90 93
91 94
@@ -114,11 +117,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
114#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) 117#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
115 118
116#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) 119#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
117#define GETARG_sB(i) (GETARG_B(i) - MAXARG_sC) 120#define GETARG_sB(i) (GETARG_B(i) - OFFSET_sC)
118#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) 121#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
119 122
120#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) 123#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
121#define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC) 124#define GETARG_sC(i) (GETARG_C(i) - OFFSET_sC)
122#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) 125#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
123 126
124#define TESTARG_k(i) (cast(int, ((i) & (1u << POS_k)))) 127#define TESTARG_k(i) (cast(int, ((i) & (1u << POS_k))))
@@ -132,13 +135,13 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
132#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax) 135#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
133 136
134#define GETARG_sBx(i) \ 137#define GETARG_sBx(i) \
135 check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - MAXARG_sBx) 138 check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
136#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) 139#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+OFFSET_sBx))
137 140
138#define GETARG_sJ(i) \ 141#define GETARG_sJ(i) \
139 check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - MAXARG_sJ) 142 check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
140#define SETARG_sJ(i,j) \ 143#define SETARG_sJ(i,j) \
141 setarg(i, cast(unsigned int, (j)+MAXARG_sJ), POS_sJ, SIZE_sJ) 144 setarg(i, cast(unsigned int, (j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
142 145
143 146
144#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \ 147#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
diff --git a/ltests.c b/ltests.c
index 3e9c82a0..130f5d6b 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.235 2017/12/08 15:19:13 roberto Exp roberto $ 2** $Id: ltests.c,v 2.236 2017/12/11 18:55:31 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*/
@@ -665,7 +665,7 @@ static int get_limits (lua_State *L) {
665 setnameval(L, "BITS_INT", LUAI_BITSINT); 665 setnameval(L, "BITS_INT", LUAI_BITSINT);
666 setnameval(L, "MAXARG_Ax", MAXARG_Ax); 666 setnameval(L, "MAXARG_Ax", MAXARG_Ax);
667 setnameval(L, "MAXARG_Bx", MAXARG_Bx); 667 setnameval(L, "MAXARG_Bx", MAXARG_Bx);
668 setnameval(L, "MAXARG_sBx", MAXARG_sBx); 668 setnameval(L, "OFFSET_sBx", OFFSET_sBx);
669 setnameval(L, "BITS_INT", LUAI_BITSINT); 669 setnameval(L, "BITS_INT", LUAI_BITSINT);
670 setnameval(L, "LFPF", LFIELDS_PER_FLUSH); 670 setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
671 setnameval(L, "NUM_OPCODES", NUM_OPCODES); 671 setnameval(L, "NUM_OPCODES", NUM_OPCODES);