macros to do jumps

This commit is contained in:
Roberto Ierusalimschy 2000-09-20 14:57:08 -03:00
parent ab7aceb980
commit eb822c314a
2 changed files with 23 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 1.110 2000/08/22 17:44:17 roberto Exp roberto $ ** $Id: lparser.c,v 1.111 2000/08/31 14:08:27 roberto Exp roberto $
** LL(1) Parser and code generator for Lua ** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -841,8 +841,8 @@ static void forbody (LexState *ls, int nvar, OpCode prepfor, OpCode loopfor) {
check(ls, TK_DO); check(ls, TK_DO);
adjustlocalvars(ls, nvar); /* scope for control variables */ adjustlocalvars(ls, nvar); /* scope for control variables */
block(ls); block(ls);
luaK_patchlist(fs, prep, luaK_getlabel(fs));
luaK_patchlist(fs, luaK_code1(fs, loopfor, NO_JUMP), blockinit); luaK_patchlist(fs, luaK_code1(fs, loopfor, NO_JUMP), blockinit);
luaK_patchlist(fs, prep, luaK_getlabel(fs));
removelocalvars(ls, nvar); removelocalvars(ls, nvar);
} }

39
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.134 2000/09/05 19:33:32 roberto Exp roberto $ ** $Id: lvm.c,v 1.135 2000/09/11 17:38:42 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -336,6 +336,9 @@ static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
} }
#define dojump(pc, i) { int d = GETARG_S(i); pc += d; }
/* /*
** Executes the given Lua function. Parameters are between [base,top). ** Executes the given Lua function. Parameters are between [base,top).
** Returns n such that the the results are between [n,top). ** Returns n such that the the results are between [n,top).
@ -578,54 +581,54 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_JMPNE: { case OP_JMPNE: {
top -= 2; top -= 2;
if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i); if (!luaO_equalObj(top, top+1)) dojump(pc, i);
break; break;
} }
case OP_JMPEQ: { case OP_JMPEQ: {
top -= 2; top -= 2;
if (luaO_equalObj(top, top+1)) pc += GETARG_S(i); if (luaO_equalObj(top, top+1)) dojump(pc, i);
break; break;
} }
case OP_JMPLT: { case OP_JMPLT: {
top -= 2; top -= 2;
if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); if (luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
break; break;
} }
case OP_JMPLE: { /* a <= b === !(b<a) */ case OP_JMPLE: { /* a <= b === !(b<a) */
top -= 2; top -= 2;
if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); if (!luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
break; break;
} }
case OP_JMPGT: { /* a > b === (b<a) */ case OP_JMPGT: { /* a > b === (b<a) */
top -= 2; top -= 2;
if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); if (luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
break; break;
} }
case OP_JMPGE: { /* a >= b === !(a<b) */ case OP_JMPGE: { /* a >= b === !(a<b) */
top -= 2; top -= 2;
if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); if (!luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
break; break;
} }
case OP_JMPT: { case OP_JMPT: {
if (ttype(--top) != TAG_NIL) pc += GETARG_S(i); if (ttype(--top) != TAG_NIL) dojump(pc, i);
break; break;
} }
case OP_JMPF: { case OP_JMPF: {
if (ttype(--top) == TAG_NIL) pc += GETARG_S(i); if (ttype(--top) == TAG_NIL) dojump(pc, i);
break; break;
} }
case OP_JMPONT: { case OP_JMPONT: {
if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i); if (ttype(top-1) == TAG_NIL) top--;
else top--; else dojump(pc, i);
break; break;
} }
case OP_JMPONF: { case OP_JMPONF: {
if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i); if (ttype(top-1) != TAG_NIL) top--;
else top--; else dojump(pc, i);
break; break;
} }
case OP_JMP: { case OP_JMP: {
pc += GETARG_S(i); dojump(pc, i);
break; break;
} }
case OP_PUSHNILJMP: { case OP_PUSHNILJMP: {
@ -644,7 +647,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
nvalue(top-3) > nvalue(top-2) : nvalue(top-3) > nvalue(top-2) :
nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */ nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */
top -= 3; /* remove control variables */ top -= 3; /* remove control variables */
pc += GETARG_S(i)+1; /* jump to loop end */ dojump(pc, i); /* jump to loop end */
} }
break; break;
} }
@ -659,7 +662,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
nvalue(top-3) < nvalue(top-2)) nvalue(top-3) < nvalue(top-2))
top -= 3; /* end loop: remove control variables */ top -= 3; /* end loop: remove control variables */
else else
pc += GETARG_S(i); /* repeat loop */ dojump(pc, i); /* repeat loop */
break; break;
} }
case OP_LFORPREP: { case OP_LFORPREP: {
@ -669,7 +672,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
node = luaH_next(L, hvalue(top-1), &luaO_nilobject); node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
if (node == NULL) { /* `empty' loop? */ if (node == NULL) { /* `empty' loop? */
top--; /* remove table */ top--; /* remove table */
pc += GETARG_S(i)+1; /* jump to loop end */ dojump(pc, i); /* jump to loop end */
} }
else { else {
top += 2; /* index,value */ top += 2; /* index,value */
@ -687,7 +690,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
else { else {
*(top-2) = *key(node); *(top-2) = *key(node);
*(top-1) = *val(node); *(top-1) = *val(node);
pc += GETARG_S(i); /* repeat loop */ dojump(pc, i); /* repeat loop */
} }
break; break;
} }