some improvements in stack control

This commit is contained in:
Roberto Ierusalimschy
2002-03-20 09:52:32 -03:00
parent 48e732e07d
commit 63a614e145
4 changed files with 22 additions and 17 deletions

19
ldo.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.163 2002/03/11 12:45:00 roberto Exp roberto $ ** $Id: ldo.c,v 1.164 2002/03/15 17:17:16 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -101,22 +101,24 @@ void luaD_growstack (lua_State *L, int n) {
** Open a hole inside the stack at `pos' ** Open a hole inside the stack at `pos'
*/ */
static void luaD_openstack (lua_State *L, StkId pos) { static void luaD_openstack (lua_State *L, StkId pos) {
int i = L->top-pos; StkId p;
while (i--) setobj(pos+i+1, pos+i); for (p = L->top; p > pos; p--) setobj(p, p-1);
incr_top(L); incr_top(L);
} }
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
L->ci->top = L->top; ptrdiff_t top = savestack(L, L->top);
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
L->ci->top += LUA_MINSTACK;
L->allowhooks = 0; /* cannot call hooks inside a hook */ L->allowhooks = 0; /* cannot call hooks inside a hook */
lua_unlock(L); lua_unlock(L);
(*hook)(L, ar); (*hook)(L, ar);
lua_lock(L); lua_lock(L);
lua_assert(L->allowhooks == 0); lua_assert(L->allowhooks == 0);
L->allowhooks = 1; L->allowhooks = 1;
L->top = L->ci->top; L->ci->top -= LUA_MINSTACK;
L->top = restorestack(L, top);
} }
@@ -218,8 +220,7 @@ StkId luaD_precall (lua_State *L, StkId func) {
ci->savedpc = p->code; /* starting point */ ci->savedpc = p->code; /* starting point */
if (p->is_vararg) /* varargs? */ if (p->is_vararg) /* varargs? */
adjust_varargs(L, p->numparams); adjust_varargs(L, p->numparams);
if (ci->base > L->stack_last - p->maxstacksize) luaD_checkstack(L, p->maxstacksize);
luaD_growstack(L, p->maxstacksize);
ci->line = 0; ci->line = 0;
ci->top = ci->base + p->maxstacksize; ci->top = ci->base + p->maxstacksize;
while (L->top < ci->top) while (L->top < ci->top)
@@ -245,9 +246,9 @@ StkId luaD_precall (lua_State *L, StkId func) {
void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
StkId res; StkId res;
if (L->callhook) { if (L->callhook) {
StkId stack = L->stack; /* next call may change stack */ ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
luaD_callHook(L, L->callhook, "return"); luaD_callHook(L, L->callhook, "return");
firstResult = (firstResult - stack) + L->stack; firstResult = restorestack(L, fr);
} }
res = L->ci->base - 1; /* func == final position of 1st result */ res = L->ci->base - 1; /* func == final position of 1st result */
L->ci--; L->ci--;

9
ldo.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ ** $Id: ldo.h,v 1.40 2002/01/30 17:27:53 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -19,7 +19,12 @@
#define incr_top(L) \ #define incr_top(L) \
{if (L->top >= L->stack_last) luaD_growstack(L, 1); L->top++;} {if (L->top >= L->stack_last) luaD_growstack(L, 1); L->top++;}
#define luaD_checkstack(L,n) if (L->stack_last-(n)<=L->top) luaD_growstack(L, n) #define luaD_checkstack(L,n) \
if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \
luaD_growstack(L, n)
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
#define restorestack(L,n) ((TObject *)((char *)L->stack + (n)))
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook); void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 1.86 2002/03/07 18:14:29 roberto Exp roberto $ ** $Id: lstate.c,v 1.87 2002/03/11 12:45:00 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -33,7 +33,7 @@ static void stack_init (lua_State *L, lua_State *OL) {
L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo); L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo);
L->ci = L->base_ci; L->ci = L->base_ci;
L->ci->base = L->top; L->ci->base = L->top;
L->ci->top = L->top; L->ci->top = L->top + LUA_MINSTACK;
L->ci->pc = NULL; L->ci->pc = NULL;
L->size_ci = BASIC_CI_SIZE; L->size_ci = BASIC_CI_SIZE;
L->end_ci = L->base_ci + L->size_ci; L->end_ci = L->base_ci + L->size_ci;

7
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.219 2002/03/08 19:10:32 roberto Exp roberto $ ** $Id: lvm.c,v 1.220 2002/03/19 12:45:25 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -94,15 +94,14 @@ static void traceexec (lua_State *L, lua_Hook linehook) {
static void callTMres (lua_State *L, const TObject *f, static void callTMres (lua_State *L, const TObject *f,
const TObject *p1, const TObject *p2, TObject *result ) { const TObject *p1, const TObject *p2, TObject *result ) {
StkId stack = L->stack; ptrdiff_t res = savestack(L, result);
setobj(L->top, f); /* push function */ setobj(L->top, f); /* push function */
setobj(L->top+1, p1); /* 1st argument */ setobj(L->top+1, p1); /* 1st argument */
setobj(L->top+2, p2); /* 2nd argument */ setobj(L->top+2, p2); /* 2nd argument */
luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
L->top += 3; L->top += 3;
luaD_call(L, L->top - 3, 1); luaD_call(L, L->top - 3, 1);
if (stack != L->stack) /* stack changed? */ result = restorestack(L, res); /* previous call may change stack */
result = (result - stack) + L->stack; /* correct pointer */
setobj(result, --L->top); /* get result */ setobj(result, --L->top); /* get result */
} }