diff --git a/lopcodes.c b/lopcodes.c index 163eff60..74195d42 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.58 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.59 2017/06/29 15:38:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -124,10 +124,10 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORPREP */ ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_TFORLOOP */ ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ ,opmode(0, 1, OpArgU, OpArgR, iABC) /* OP_VARARG */ diff --git a/lopcodes.h b/lopcodes.h index fb989ac6..f35de02a 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.154 2017/05/08 16:08:01 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.155 2017/06/29 15:38:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -229,12 +229,12 @@ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) f->code[pc]; + int offset = dest - (pc + 1); + if (back) + offset = -offset; + if (offset > MAXARG_Bx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_Bx(*jmp, offset); +} + + static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { /* forbody -> DO block */ BlockCnt bl; @@ -1314,21 +1330,23 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { int prep, endfor; adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); + prep = isnum ? luaK_codeABx(fs, OP_FORPREP, base, 0) : luaK_jump(fs); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - if (isnum) /* numeric for? */ - endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); + if (isnum) { /* numeric for? */ + fixforjump(fs, prep, luaK_getlabel(fs), 0); + endfor = luaK_codeABx(fs, OP_FORLOOP, base, 0); + } else { /* generic for */ + luaK_patchtohere(fs, prep); luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); } - luaK_patchlist(fs, endfor, prep + 1); + fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } diff --git a/lvm.c b/lvm.c index 18799be7..be4793d1 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.289 2017/06/29 15:38:41 roberto Exp roberto $ +** $Id: lvm.c,v 2.290 2017/07/07 16:34:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1335,7 +1335,7 @@ void luaV_execute (lua_State *L) { lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */ lua_Integer limit = ivalue(s2v(ra + 1)); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ chgivalue(s2v(ra), idx); /* update internal index... */ setivalue(s2v(ra + 3), idx); /* ...and external index */ } @@ -1347,7 +1347,7 @@ void luaV_execute (lua_State *L) { idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ chgfltvalue(s2v(ra), idx); /* update internal index... */ setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } @@ -1381,7 +1381,7 @@ void luaV_execute (lua_State *L) { luaG_runerror(L, "'for' initial value must be a number"); setfltvalue(init, luai_numsub(L, ninit, nstep)); } - pc += GETARG_sBx(i); + pc += GETARG_Bx(i); vmbreak; } vmcase(OP_TFORCALL) { @@ -1401,7 +1401,7 @@ void luaV_execute (lua_State *L) { l_tforloop: if (!ttisnil(s2v(ra + 1))) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ } vmbreak; }