Browse Source

some improvements in stack control

Roberto Ierusalimschy 23 years ago
parent
commit
63a614e145
4 changed files with 22 additions and 17 deletions
  1. 10 9
      ldo.c
  2. 7 2
      ldo.h
  3. 2 2
      lstate.c
  4. 3 4
      lvm.c

+ 10 - 9
ldo.c

@@ -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
 ** 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'
 */
 static void luaD_openstack (lua_State *L, StkId pos) {
-  int i = L->top-pos; 
-  while (i--) setobj(pos+i+1, pos+i);
+  StkId p;
+  for (p = L->top; p > pos; p--) setobj(p, p-1);
   incr_top(L);
 }
 
 
 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 */
+  L->ci->top += LUA_MINSTACK;
   L->allowhooks = 0;  /* cannot call hooks inside a hook */
   lua_unlock(L);
   (*hook)(L, ar);
   lua_lock(L);
   lua_assert(L->allowhooks == 0);
   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 */
     if (p->is_vararg)  /* varargs? */
       adjust_varargs(L, p->numparams);
-    if (ci->base > L->stack_last - p->maxstacksize)
-      luaD_growstack(L, p->maxstacksize);
+    luaD_checkstack(L, p->maxstacksize);
     ci->line = 0;
     ci->top = ci->base + p->maxstacksize;
     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) { 
   StkId res;
   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");
-    firstResult = (firstResult - stack) + L->stack;
+    firstResult = restorestack(L, fr);
   }
   res = L->ci->base - 1;  /* func == final position of 1st result */
   L->ci--;

+ 7 - 2
ldo.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -19,7 +19,12 @@
 #define incr_top(L) \
 	{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);

+ 2 - 2
lstate.c

@@ -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
 ** 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->ci = L->base_ci;
   L->ci->base = L->top;
-  L->ci->top = L->top;
+  L->ci->top = L->top + LUA_MINSTACK;
   L->ci->pc = NULL;
   L->size_ci = BASIC_CI_SIZE;
   L->end_ci = L->base_ci + L->size_ci;

+ 3 - 4
lvm.c

@@ -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
 ** 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,
                        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+1, p1);  /* 1st argument */
   setobj(L->top+2, p2);  /* 2nd argument */
   luaD_checkstack(L, 3);  /* cannot check before (could invalidate p1, p2) */
   L->top += 3;
   luaD_call(L, L->top - 3, 1);
-  if (stack != L->stack)  /* stack changed? */
-    result = (result - stack) + L->stack;  /* correct pointer */
+  result = restorestack(L, res);  /* previous call may change stack */
   setobj(result, --L->top);  /* get result */
 }