From be00cd2a6b0f7d26dfc9ba74c262a5574f6521d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Nov 2002 13:46:44 -0200 Subject: easier way to keep `base' correct --- ldo.c | 9 ++------- lstate.h | 3 +-- lvm.c | 35 ++++++++++++++++++----------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/ldo.c b/ldo.c index 01632bdf..f781da5e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.204 2002/11/18 18:45:38 roberto Exp roberto $ +** $Id: ldo.c,v 1.205 2002/11/21 15:16:04 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -107,13 +107,8 @@ static void correctstack (lua_State *L, TObject *oldstack) { for (up = L->openupval; up != NULL; up = up->gch.next) gcotouv(up)->v = (gcotouv(up)->v - oldstack) + L->stack; for (ci = L->base_ci; ci <= L->ci; ci++) { - StkId newbase = (ci->base - oldstack) + L->stack; ci->top = (ci->top - oldstack) + L->stack; - if (ci->state & CI_HASFRAME) { /* Lua function with active frame? */ - lua_assert(*ci->u.l.pb == ci->base); - *ci->u.l.pb = newbase; /* correct frame */ - } - ci->base = newbase; + ci->base = (ci->base - oldstack) + L->stack; } L->base = L->ci->base; } diff --git a/lstate.h b/lstate.h index 971349ea..507de477 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 1.103 2002/11/18 15:23:43 roberto Exp roberto $ +** $Id: lstate.h,v 1.104 2002/11/21 15:16:04 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -80,7 +80,6 @@ typedef struct CallInfo { struct { /* for Lua functions */ const Instruction *savedpc; const Instruction **pc; /* points to `pc' variable in `luaV_execute' */ - StkId *pb; /* points to `base' variable in `luaV_execute' */ } l; struct { /* for C functions */ int dummy; /* just to avoid an empty struct */ diff --git a/lvm.c b/lvm.c index a9f297c5..27d2b956 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.265 2002/11/21 14:18:01 roberto Exp roberto $ +** $Id: lvm.c,v 1.266 2002/11/21 15:16:04 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -352,6 +352,8 @@ static void Arith (lua_State *L, StkId ra, #define runtime_check(L, c) { if (!(c)) return 0; } #define RA(i) (base+GETARG_A(i)) +/* to be used after possible stack reallocation */ +#define XRA(i) (L->base+GETARG_A(i)) #define RB(i) (base+GETARG_B(i)) #define RKB(i) ((GETARG_B(i) < MAXSTACK) ? RB(i) : k+GETARG_B(i)-MAXSTACK) #define RC(i) (base+GETARG_C(i)) @@ -363,12 +365,10 @@ static void Arith (lua_State *L, StkId ra, StkId luaV_execute (lua_State *L) { - StkId base; LClosure *cl; TObject *k; const Instruction *pc; callentry: /* entry point when calling new functions */ - L->ci->u.l.pb = &base; L->ci->u.l.pc = &pc; if (L->hookmask & LUA_MASKCALL) luaD_callhook(L, LUA_HOOKCALL, -1); @@ -377,13 +377,12 @@ StkId luaV_execute (lua_State *L) { L->ci->state == (CI_SAVEDPC | CI_CALLING)); L->ci->state = CI_HASFRAME; /* activate frame */ pc = L->ci->u.l.savedpc; - base = L->base; - cl = &clvalue(base - 1)->l; + cl = &clvalue(L->base - 1)->l; k = cl->p->k; /* main loop of interpreter */ for (;;) { const Instruction i = *pc++; - StkId ra; + StkId base, ra; if (L->hookmask >= LUA_MASKLINE && (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { traceexec(L); @@ -394,9 +393,10 @@ StkId luaV_execute (lua_State *L) { } } /* warning!! several calls may realloc the stack and invalidate `ra' */ + base = L->base; ra = RA(i); lua_assert(L->ci->state & CI_HASFRAME); - lua_assert(base == L->base && base == L->ci->base); + lua_assert(base == L->ci->base); lua_assert(L->top <= L->stack + L->stacksize && L->top >= base); lua_assert(L->top == L->ci->top || GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || @@ -434,7 +434,7 @@ StkId luaV_execute (lua_State *L) { v = luaH_getstr(hvalue(&cl->g), tsvalue(rb)); if (!ttisnil(v)) { setobj2s(ra, v); } else - setobj2s(RA(i), luaV_index(L, &cl->g, rb, 0)); + setobj2s(XRA(i), luaV_index(L, &cl->g, rb, 0)); break; } case OP_GETTABLE: { @@ -444,10 +444,10 @@ StkId luaV_execute (lua_State *L) { const TObject *v = luaH_get(hvalue(rb), rc); if (!ttisnil(v)) { setobj2s(ra, v); } else - setobj2s(RA(i), luaV_index(L, rb, rc, 0)); + setobj2s(XRA(i), luaV_index(L, rb, rc, 0)); } else - setobj2s(RA(i), luaV_getnotable(L, rb, rc, 0)); + setobj2s(XRA(i), luaV_getnotable(L, rb, rc, 0)); break; } case OP_SETGLOBAL: { @@ -480,10 +480,10 @@ StkId luaV_execute (lua_State *L) { const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc)); if (!ttisnil(v)) { setobj2s(ra, v); } else - setobj2s(RA(i), luaV_index(L, rb, rc, 0)); + setobj2s(XRA(i), luaV_index(L, rb, rc, 0)); } else - setobj2s(RA(i), luaV_getnotable(L, rb, rc, 0)); + setobj2s(XRA(i), luaV_getnotable(L, rb, rc, 0)); break; } case OP_ADD: { @@ -552,7 +552,7 @@ StkId luaV_execute (lua_State *L) { int b = GETARG_B(i); int c = GETARG_C(i); luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */ - setobjs2s(RA(i), base+b); + setobjs2s(XRA(i), base+b); luaC_checkGC(L); break; } @@ -610,10 +610,11 @@ StkId luaV_execute (lua_State *L) { } else { /* tail call: put new frame in place of previous one */ int aux; - StkId ra1 = RA(i); /* `luaD_precall' may change the stack */ + base = (L->ci - 1)->base; /* `luaD_precall' may change the stack */ + ra = RA(i); if (L->openupval) luaF_close(L, base); - for (aux = 0; ra1+aux < L->top; aux++) /* move frame down */ - setobjs2s(base+aux-1, ra1+aux); + for (aux = 0; ra+aux < L->top; aux++) /* move frame down */ + setobjs2s(base+aux-1, ra+aux); (L->ci - 1)->top = L->top = base+aux; /* correct top */ lua_assert(L->ci->state & CI_SAVEDPC); (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc; @@ -673,7 +674,7 @@ StkId luaV_execute (lua_State *L) { L->top = ra+5; luaD_call(L, ra+2, GETARG_C(i) + 1); L->top = L->ci->top; - if (ttisnil(RA(i)+2)) pc++; /* skip jump (break loop) */ + if (ttisnil(XRA(i)+2)) pc++; /* skip jump (break loop) */ else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */ break; } -- cgit v1.2.3-55-g6feb