mirror of
https://github.com/lua/lua
synced 2025-02-11 18:54:00 +03:00
jumps in 'for' loops don't need to be signed
This commit is contained in:
parent
f185c0132e
commit
ac65bab25f
@ -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 */
|
||||
|
10
lopcodes.h
10
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) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
|
||||
OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
|
||||
OP_FORLOOP,/* A Bx R(A)+=R(A+2);
|
||||
if R(A) <?= R(A+1) then { pc-=Bx; R(A+3)=R(A) }*/
|
||||
OP_FORPREP,/* A Bx R(A)-=R(A+2); pc+=Bx */
|
||||
|
||||
OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
|
||||
OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
|
||||
OP_TFORLOOP,/* A Bx if R(A+1) ~= nil then { R(A)=R(A+1); pc -= Bx }*/
|
||||
|
||||
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
||||
|
||||
|
30
lparser.c
30
lparser.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.c,v 2.162 2017/06/29 15:38:41 roberto Exp roberto $
|
||||
** $Id: lparser.c,v 2.163 2017/08/12 13:12:21 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -1307,6 +1307,22 @@ static int exp1 (LexState *ls) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Fix for instruction at position 'pc' to jump to 'dest'.
|
||||
** (Jump addresses are relative in Lua). 'back' true means
|
||||
** a back jump.
|
||||
*/
|
||||
static void fixforjump (FuncState *fs, int pc, int dest, int back) {
|
||||
Instruction *jmp = &fs->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);
|
||||
}
|
||||
|
||||
|
10
lvm.c
10
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user