diff options
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 44 |
1 files changed, 32 insertions, 12 deletions
| @@ -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 | */ |
| 380 | int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { | 380 | int 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 | */ |
| 391 | static int codesJ (FuncState *fs, OpCode o, int sj, int k) { | 391 | static 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 | */ | ||
| 590 | static 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 | */ | ||
| 598 | static int fitsBx (lua_Integer i) { | ||
| 599 | return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); | ||
| 600 | } | ||
| 601 | |||
| 602 | |||
| 585 | void luaK_int (FuncState *fs, int reg, lua_Integer i) { | 603 | void 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) { | |||
| 593 | static int floatI (lua_Number f, lua_Integer *fi) { | 611 | static 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 | */ |
| 1091 | static int isSCint (expdesc *e) { | 1108 | static 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 | ||
