diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-20 09:52:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-20 09:52:32 -0300 |
| commit | 63a614e1453b6b03b89b5d47efa476acd5f9d1d2 (patch) | |
| tree | 203b7f497a7252e3afeb3651a7c6a09c5a3537b0 | |
| parent | 48e732e07d21d585982d1c53be0d9031f021f014 (diff) | |
| download | lua-63a614e1453b6b03b89b5d47efa476acd5f9d1d2.tar.gz lua-63a614e1453b6b03b89b5d47efa476acd5f9d1d2.tar.bz2 lua-63a614e1453b6b03b89b5d47efa476acd5f9d1d2.zip | |
some improvements in stack control
| -rw-r--r-- | ldo.c | 19 | ||||
| -rw-r--r-- | ldo.h | 9 | ||||
| -rw-r--r-- | lstate.c | 4 | ||||
| -rw-r--r-- | lvm.c | 7 |
4 files changed, 22 insertions, 17 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.163 2002/03/11 12:45:00 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.164 2002/03/15 17:17:16 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -101,22 +101,24 @@ void luaD_growstack (lua_State *L, int n) { | |||
| 101 | ** Open a hole inside the stack at `pos' | 101 | ** Open a hole inside the stack at `pos' |
| 102 | */ | 102 | */ |
| 103 | static void luaD_openstack (lua_State *L, StkId pos) { | 103 | static void luaD_openstack (lua_State *L, StkId pos) { |
| 104 | int i = L->top-pos; | 104 | StkId p; |
| 105 | while (i--) setobj(pos+i+1, pos+i); | 105 | for (p = L->top; p > pos; p--) setobj(p, p-1); |
| 106 | incr_top(L); | 106 | incr_top(L); |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | 109 | ||
| 110 | static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { | 110 | static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { |
| 111 | L->ci->top = L->top; | 111 | ptrdiff_t top = savestack(L, L->top); |
| 112 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | 112 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
| 113 | L->ci->top += LUA_MINSTACK; | ||
| 113 | L->allowhooks = 0; /* cannot call hooks inside a hook */ | 114 | L->allowhooks = 0; /* cannot call hooks inside a hook */ |
| 114 | lua_unlock(L); | 115 | lua_unlock(L); |
| 115 | (*hook)(L, ar); | 116 | (*hook)(L, ar); |
| 116 | lua_lock(L); | 117 | lua_lock(L); |
| 117 | lua_assert(L->allowhooks == 0); | 118 | lua_assert(L->allowhooks == 0); |
| 118 | L->allowhooks = 1; | 119 | L->allowhooks = 1; |
| 119 | L->top = L->ci->top; | 120 | L->ci->top -= LUA_MINSTACK; |
| 121 | L->top = restorestack(L, top); | ||
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | 124 | ||
| @@ -218,8 +220,7 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
| 218 | ci->savedpc = p->code; /* starting point */ | 220 | ci->savedpc = p->code; /* starting point */ |
| 219 | if (p->is_vararg) /* varargs? */ | 221 | if (p->is_vararg) /* varargs? */ |
| 220 | adjust_varargs(L, p->numparams); | 222 | adjust_varargs(L, p->numparams); |
| 221 | if (ci->base > L->stack_last - p->maxstacksize) | 223 | luaD_checkstack(L, p->maxstacksize); |
| 222 | luaD_growstack(L, p->maxstacksize); | ||
| 223 | ci->line = 0; | 224 | ci->line = 0; |
| 224 | ci->top = ci->base + p->maxstacksize; | 225 | ci->top = ci->base + p->maxstacksize; |
| 225 | while (L->top < ci->top) | 226 | while (L->top < ci->top) |
| @@ -245,9 +246,9 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
| 245 | void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { | 246 | void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { |
| 246 | StkId res; | 247 | StkId res; |
| 247 | if (L->callhook) { | 248 | if (L->callhook) { |
| 248 | StkId stack = L->stack; /* next call may change stack */ | 249 | ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ |
| 249 | luaD_callHook(L, L->callhook, "return"); | 250 | luaD_callHook(L, L->callhook, "return"); |
| 250 | firstResult = (firstResult - stack) + L->stack; | 251 | firstResult = restorestack(L, fr); |
| 251 | } | 252 | } |
| 252 | res = L->ci->base - 1; /* func == final position of 1st result */ | 253 | res = L->ci->base - 1; /* func == final position of 1st result */ |
| 253 | L->ci--; | 254 | L->ci--; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: ldo.h,v 1.40 2002/01/30 17:27:53 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -19,7 +19,12 @@ | |||
| 19 | #define incr_top(L) \ | 19 | #define incr_top(L) \ |
| 20 | {if (L->top >= L->stack_last) luaD_growstack(L, 1); L->top++;} | 20 | {if (L->top >= L->stack_last) luaD_growstack(L, 1); L->top++;} |
| 21 | 21 | ||
| 22 | #define luaD_checkstack(L,n) if (L->stack_last-(n)<=L->top) luaD_growstack(L, n) | 22 | #define luaD_checkstack(L,n) \ |
| 23 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \ | ||
| 24 | luaD_growstack(L, n) | ||
| 25 | |||
| 26 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) | ||
| 27 | #define restorestack(L,n) ((TObject *)((char *)L->stack + (n))) | ||
| 23 | 28 | ||
| 24 | 29 | ||
| 25 | void luaD_lineHook (lua_State *L, int line, lua_Hook linehook); | 30 | void luaD_lineHook (lua_State *L, int line, lua_Hook linehook); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 1.86 2002/03/07 18:14:29 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.87 2002/03/11 12:45:00 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -33,7 +33,7 @@ static void stack_init (lua_State *L, lua_State *OL) { | |||
| 33 | L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo); | 33 | L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo); |
| 34 | L->ci = L->base_ci; | 34 | L->ci = L->base_ci; |
| 35 | L->ci->base = L->top; | 35 | L->ci->base = L->top; |
| 36 | L->ci->top = L->top; | 36 | L->ci->top = L->top + LUA_MINSTACK; |
| 37 | L->ci->pc = NULL; | 37 | L->ci->pc = NULL; |
| 38 | L->size_ci = BASIC_CI_SIZE; | 38 | L->size_ci = BASIC_CI_SIZE; |
| 39 | L->end_ci = L->base_ci + L->size_ci; | 39 | L->end_ci = L->base_ci + L->size_ci; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.219 2002/03/08 19:10:32 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.220 2002/03/19 12:45:25 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -94,15 +94,14 @@ static void traceexec (lua_State *L, lua_Hook linehook) { | |||
| 94 | 94 | ||
| 95 | static void callTMres (lua_State *L, const TObject *f, | 95 | static void callTMres (lua_State *L, const TObject *f, |
| 96 | const TObject *p1, const TObject *p2, TObject *result ) { | 96 | const TObject *p1, const TObject *p2, TObject *result ) { |
| 97 | StkId stack = L->stack; | 97 | ptrdiff_t res = savestack(L, result); |
| 98 | setobj(L->top, f); /* push function */ | 98 | setobj(L->top, f); /* push function */ |
| 99 | setobj(L->top+1, p1); /* 1st argument */ | 99 | setobj(L->top+1, p1); /* 1st argument */ |
| 100 | setobj(L->top+2, p2); /* 2nd argument */ | 100 | setobj(L->top+2, p2); /* 2nd argument */ |
| 101 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ | 101 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ |
| 102 | L->top += 3; | 102 | L->top += 3; |
| 103 | luaD_call(L, L->top - 3, 1); | 103 | luaD_call(L, L->top - 3, 1); |
| 104 | if (stack != L->stack) /* stack changed? */ | 104 | result = restorestack(L, res); /* previous call may change stack */ |
| 105 | result = (result - stack) + L->stack; /* correct pointer */ | ||
| 106 | setobj(result, --L->top); /* get result */ | 105 | setobj(result, --L->top); /* get result */ |
| 107 | } | 106 | } |
| 108 | 107 | ||
