Browse Source

new scheme to close upvalues in 'break'; jump instructions may

do the close, avoiding the need for a OP_CLOSE instruction
pull/9/head
Roberto Ierusalimschy 14 years ago
parent
commit
dd547c55c8
  1. 15
      lcode.c
  2. 3
      lcode.h
  3. 7
      lopcodes.h
  4. 8
      lparser.c
  5. 29
      lvm.c

15
lcode.c

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.49 2010/07/07 16:27:29 roberto Exp roberto $
** $Id: lcode.c,v 2.50 2011/01/31 14:28:41 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -171,6 +171,19 @@ void luaK_patchlist (FuncState *fs, int list, int target) {
}
LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
level++; /* argument is +1 to reserve 0 as non-op */
while (list != NO_JUMP) {
int next = getjump(fs, list);
lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
(GETARG_A(fs->f->code[list]) == 0 ||
GETARG_A(fs->f->code[list]) >= level));
SETARG_A(fs->f->code[list], level);
list = next;
}
}
void luaK_patchtohere (FuncState *fs, int list) {
luaK_getlabel(fs);
luaK_concat(fs, &fs->jpc, list);

3
lcode.h

@ -1,5 +1,5 @@
/*
** $Id: lcode.h,v 1.54 2010/04/17 12:46:32 roberto Exp roberto $
** $Id: lcode.h,v 1.55 2010/07/02 20:42:40 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -71,6 +71,7 @@ LUAI_FUNC int luaK_jump (FuncState *fs);
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel (FuncState *fs);
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);

7
lopcodes.h

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.136 2010/10/13 16:45:54 roberto Exp roberto $
** $Id: lopcodes.h,v 1.137 2010/10/25 12:24:55 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -195,8 +195,7 @@ OP_LEN,/* A B R(A) := length of R(B) */
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
OP_JMP,/* sBx pc+=sBx */
OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
@ -217,7 +216,7 @@ OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
OP_CLOSE,/* A close all upvalues >= R(A) */
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */

8
lparser.c

@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 2.94 2010/12/17 12:03:41 roberto Exp roberto $
** $Id: lparser.c,v 2.95 2011/01/26 16:30:02 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -245,7 +245,7 @@ static int searchvar (FuncState *fs, TString *n) {
/*
Mark block where variable at given level was defined
(to emit OP_CLOSE later).
(to emit close instructions later).
*/
static void markupval (FuncState *fs, int level) {
BlockCnt *bl = fs->bl;
@ -1053,9 +1053,9 @@ static void breakstat (LexState *ls) {
}
if (!bl)
luaX_syntaxerror(ls, "no loop to break");
if (upval)
luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
if (upval)
luaK_patchclose(fs, bl->breaklist, bl->nactvar);
}

29
lvm.c

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.126 2010/12/06 21:08:36 roberto Exp roberto $
** $Id: lvm.c,v 2.127 2010/12/17 12:05:37 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -466,7 +466,14 @@ void luaV_finishOp (lua_State *L) {
(k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
#define dojump(i) (ci->u.l.savedpc += (i))
/* execute a jump instruction */
#define dojump(ci,i) \
{ int a = GETARG_A(i); \
if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
ci->u.l.savedpc += GETARG_sBx(i); }
/* for test instructions, execute the jump instruction that follows it */
#define donextjump(ci) dojump(ci, *ci->u.l.savedpc)
#define Protect(x) { {x;}; base = ci->u.l.base; }
@ -620,41 +627,41 @@ void luaV_execute (lua_State *L) {
L->top = ci->top; /* restore top */
)
vmcase(OP_JMP,
dojump(GETARG_sBx(i));
dojump(ci, i);
)
vmcase(OP_EQ,
TValue *rb = RKB(i);
TValue *rc = RKC(i);
Protect(
if (equalobj(L, rb, rc) == GETARG_A(i))
dojump(GETARG_sBx(*ci->u.l.savedpc));
donextjump(ci);
)
ci->u.l.savedpc++;
)
vmcase(OP_LT,
Protect(
if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
dojump(GETARG_sBx(*ci->u.l.savedpc));
donextjump(ci);
)
ci->u.l.savedpc++;
)
vmcase(OP_LE,
Protect(
if (luaV_lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
dojump(GETARG_sBx(*ci->u.l.savedpc));
donextjump(ci);
)
ci->u.l.savedpc++;
)
vmcase(OP_TEST,
if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
dojump(GETARG_sBx(*ci->u.l.savedpc));
donextjump(ci);
ci->u.l.savedpc++;
)
vmcase(OP_TESTSET,
TValue *rb = RB(i);
if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
setobjs2s(L, ra, rb);
dojump(GETARG_sBx(*ci->u.l.savedpc));
donextjump(ci);
}
ci->u.l.savedpc++;
)
@ -722,7 +729,7 @@ void luaV_execute (lua_State *L) {
lua_Number limit = nvalue(ra+1);
if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
: luai_numle(L, limit, idx)) {
dojump(GETARG_sBx(i)); /* jump back */
ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
setnvalue(ra, idx); /* update internal index... */
setnvalue(ra+3, idx); /* ...and external index */
}
@ -738,7 +745,7 @@ void luaV_execute (lua_State *L) {
else if (!tonumber(pstep, ra+2))
luaG_runerror(L, LUA_QL("for") " step must be a number");
setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
dojump(GETARG_sBx(i));
ci->u.l.savedpc += GETARG_sBx(i);
)
vmcase(OP_TFORCALL,
StkId cb = ra + 3; /* call base */
@ -757,7 +764,7 @@ void luaV_execute (lua_State *L) {
l_tforloop:
if (!ttisnil(ra + 1)) { /* continue loop? */
setobjs2s(L, ra, ra + 1); /* save control variable */
dojump(GETARG_sBx(i)); /* jump back */
ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
}
)
vmcase(OP_SETLIST,

Loading…
Cancel
Save