Browse Source

no more 'CallInfo' structure

Roberto Ierusalimschy 7 years ago
parent
commit
93fd67b793
5 changed files with 31 additions and 119 deletions
  1. 22 42
      ldo.c
  2. 2 3
      ldo.h
  3. 4 53
      lstate.c
  4. 1 14
      lstate.h
  5. 2 7
      lvm.c

+ 22 - 42
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 2.167 2017/11/03 17:22:54 roberto Exp roberto $
+** $Id: ldo.c,v 2.168 2017/11/03 20:41:05 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -157,15 +157,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
 ** ===================================================================
 */
 static void correctstack (lua_State *L, StkId oldstack) {
-  CallInfo *ci;
   UpVal *up;
   L->top = (L->top - oldstack) + L->stack;
   L->func = (L->func - oldstack) + L->stack;
   for (up = L->openupval; up != NULL; up = up->u.open.next)
     up->v = s2v((uplevel(up) - oldstack) + L->stack);
-  for (ci = L->ci; ci != NULL; ci = ci->previous) {
-    ci->func = (ci->func - oldstack) + L->stack;
-  }
 }
 
 
@@ -223,10 +219,6 @@ void luaD_shrinkstack (lua_State *L) {
   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
   if (goodsize > LUAI_MAXSTACK)
     goodsize = LUAI_MAXSTACK;  /* respect stack limit */
-  if (L->stacksize > LUAI_MAXSTACK)  /* had been handling stack overflow? */
-    luaE_freeCI(L);  /* free all CIs (list grew because of an error) */
-  else
-    luaE_shrinkCI(L);  /* shrink list */
   /* if thread is currently not handling a stack overflow and its
      good size is smaller than current size, shrink its stack */
   if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) &&
@@ -363,7 +355,7 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res,
 ** moves current number of results to proper place; returns 0 iff call
 ** wanted multiple (variable number of) results.
 */
-int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
+int luaD_poscall (lua_State *L, StkId firstResult, int nres) {
   StkId res = L->func;  /* res == final position of 1st result */
   int wanted = res->stkci.nresults;
   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
@@ -376,18 +368,12 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
     /* 'oldpc' for caller function */
     L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc;
   }
-  L->ci = ci->previous;  /* back to caller */
   L->func = res - res->stkci.previous;
-  lua_assert(L->func == L->ci->func);
   /* move results to proper place */
   return moveresults(L, firstResult, res, nres, wanted);
 }
 
 
-
-#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
-
-
 /*
 ** Prepares a function call: checks the stack, creates a new CallInfo
 ** entry, fills in the relevant information, calls hook if needed.
@@ -398,7 +384,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
 int luaD_precall (lua_State *L, StkId func, int nresults) {
   lua_CFunction f;
   TValue *funcv = s2v(func);
-  CallInfo *ci;
   switch (ttype(funcv)) {
     case LUA_TCCL:  /* C closure */
       f = clCvalue(funcv)->f;
@@ -408,10 +393,9 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
      Cfunc: {
       int n;  /* number of returns */
       checkstackp(L, LUA_MINSTACK, func);  /* ensure minimum stack size */
-      ci = next_ci(L);  /* now 'enter' new function */
       func->stkci.nresults = nresults;
       func->stkci.previous = func - L->func;
-      L->func = ci->func = func;
+      L->func = func;
       setfunctop(func, L->top + LUA_MINSTACK);
       lua_assert(functop(func) <= L->stack_last);
       callstatus(func) = 0;
@@ -421,7 +405,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
       n = (*f)(L);  /* do the actual call */
       lua_lock(L);
       api_checknelems(L, n);
-      luaD_poscall(L, ci, L->top - n, n);
+      luaD_poscall(L, L->top - n, n);
       return 1;
     }
     case LUA_TLCL: {  /* Lua function: prepare its call */
@@ -433,11 +417,10 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
         setnilvalue(s2v(L->top++));  /* complete missing arguments */
       if (p->is_vararg)
         luaT_adjustvarargs(L, p, n);
-      ci = next_ci(L);  /* now 'enter' new function */
       func->stkci.nresults = nresults;
       func->stkci.previous = func - L->func;
       func->stkci.framesize = fsize + 1;  /* size includes function itself */
-      L->func = ci->func = func;
+      L->func = func;
       L->top = func + 1 + fsize;
       lua_assert(functop(func) <= L->stack_last);
       func->stkci.u.l.savedpc = p->code;  /* starting point */
@@ -500,7 +483,6 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
 ** continuation function.
 */
 static void finishCcall (lua_State *L, int status) {
-  CallInfo *ci = L->ci;
   StkId func = L->func;
   int n;
   /* must have a continuation and must be able to call it */
@@ -519,7 +501,7 @@ static void finishCcall (lua_State *L, int status) {
   n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx);
   lua_lock(L);
   api_checknelems(L, n);
-  luaD_poscall(L, ci, L->top - n, n);  /* finish 'luaD_precall' */
+  luaD_poscall(L, L->top - n, n);  /* finish 'luaD_precall' */
 }
 
 
@@ -534,7 +516,7 @@ static void finishCcall (lua_State *L, int status) {
 static void unroll (lua_State *L, void *ud) {
   if (ud != NULL)  /* error status? */
     finishCcall(L, *(int *)ud);  /* finish 'lua_pcallk' callee */
-  while (L->ci != &L->base_ci) {  /* something in the stack */
+  while (L->func != L->stack) {  /* something in the stack */
     if (!isLua(L->func))  /* C function? */
       finishCcall(L, LUA_YIELD);  /* complete its execution */
     else {  /* Lua function */
@@ -549,11 +531,13 @@ static void unroll (lua_State *L, void *ud) {
 ** Try to find a suspended protected call (a "recover point") for the
 ** given thread.
 */
-static CallInfo *findpcall (lua_State *L) {
-  CallInfo *ci;
-  for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
-    if (callstatus(ci->func) & CIST_YPCALL)
-      return ci;
+static StkId findpcall (lua_State *L) {
+  StkId func;
+  for (func = L->func;
+       func->stkci.previous != 0;
+       func -= func->stkci.previous) {
+    if (callstatus(func) & CIST_YPCALL)
+      return func;
   }
   return NULL;  /* no pending pcall */
 }
@@ -566,18 +550,17 @@ static CallInfo *findpcall (lua_State *L) {
 */
 static int recover (lua_State *L, int status) {
   StkId oldtop;
-  CallInfo *ci = findpcall(L);
-  if (ci == NULL) return 0;  /* no recovery point */
+  StkId recf = findpcall(L);
+  if (recf == NULL) return 0;  /* no recovery point */
   /* "finish" luaD_pcall */
-  oldtop = ci->func + ci->func->stkci.u2.funcidx;
+  oldtop = recf + recf->stkci.u2.funcidx;
   luaF_close(L, oldtop);
   seterrorobj(L, status, oldtop);
-  L->ci = ci;
-  L->func = ci->func;
-  L->allowhook = getoah(callstatus(L->func));  /* restore original 'allowhook' */
+  L->func = recf;
+  L->allowhook = getoah(callstatus(recf));  /* restore original 'allowhook' */
   L->nny = 0;  /* should be zero to be yieldable */
   luaD_shrinkstack(L);
-  L->errfunc = ci->func->stkci.u.c.old_errfunc;
+  L->errfunc = recf->stkci.u.c.old_errfunc;
   return 1;  /* continue running the coroutine */
 }
 
@@ -606,7 +589,6 @@ static int resume_error (lua_State *L, const char *msg, int narg) {
 static void resume (lua_State *L, void *ud) {
   int n = *(cast(int*, ud));  /* number of arguments */
   StkId firstArg = L->top - n;  /* first argument */
-  CallInfo *ci = L->ci;
   StkId func = L->func;
   if (L->status == LUA_OK) {  /* starting a coroutine? */
     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
@@ -626,7 +608,7 @@ static void resume (lua_State *L, void *ud) {
         api_checknelems(L, n);
         firstArg = L->top - n;  /* yield results come from continuation */
       }
-      luaD_poscall(L, ci, firstArg, n);  /* finish 'luaD_precall' */
+      luaD_poscall(L, firstArg, n);  /* finish 'luaD_precall' */
     }
     unroll(L, NULL);  /* run continuation */
   }
@@ -639,7 +621,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
   unsigned short oldnny = L->nny;  /* save "number of non-yieldable" calls */
   lua_lock(L);
   if (L->status == LUA_OK) {  /* may be starting a coroutine */
-    if (L->ci != &L->base_ci)  /* not in base level? */
+    if (L->func != L->stack)  /* not in base level? */
       return resume_error(L, "cannot resume non-suspended coroutine", nargs);
   }
   else if (L->status != LUA_YIELD)
@@ -712,7 +694,6 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
 int luaD_pcall (lua_State *L, Pfunc func, void *u,
                 ptrdiff_t old_top, ptrdiff_t ef) {
   int status;
-  CallInfo *old_ci = L->ci;
   ptrdiff_t oldfunc = savestack(L, L->func);
   lu_byte old_allowhooks = L->allowhook;
   unsigned short old_nny = L->nny;
@@ -723,7 +704,6 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
     StkId oldtop = restorestack(L, old_top);
     luaF_close(L, oldtop);  /* close possible pending closures */
     seterrorobj(L, status, oldtop);
-    L->ci = old_ci;
     L->func = restorestack(L, oldfunc);
     L->allowhook = old_allowhooks;
     L->nny = old_nny;

+ 2 - 3
ldo.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.h,v 2.30 2017/05/13 12:57:20 roberto Exp roberto $
+** $Id: ldo.h,v 2.31 2017/06/29 15:06:44 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -52,8 +52,7 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
 LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
 LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
                                         ptrdiff_t oldtop, ptrdiff_t ef);
-LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
-                                          int nres);
+LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres);
 LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
 LUAI_FUNC void luaD_growstack (lua_State *L, int n);
 LUAI_FUNC void luaD_shrinkstack (lua_State *L);

+ 4 - 53
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp roberto $
+** $Id: lstate.c,v 2.144 2017/11/03 12:12:30 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -97,51 +97,8 @@ void luaE_setdebt (global_State *g, l_mem debt) {
 }
 
 
-CallInfo *luaE_extendCI (lua_State *L) {
-  CallInfo *ci = luaM_new(L, CallInfo);
-  lua_assert(L->ci->next == NULL);
-  L->ci->next = ci;
-  ci->previous = L->ci;
-  ci->next = NULL;
-  L->nci++;
-  return ci;
-}
-
-
-/*
-** free all CallInfo structures not in use by a thread
-*/
-void luaE_freeCI (lua_State *L) {
-  CallInfo *ci = L->ci;
-  CallInfo *next = ci->next;
-  ci->next = NULL;
-  while ((ci = next) != NULL) {
-    next = ci->next;
-    luaM_free(L, ci);
-    L->nci--;
-  }
-}
-
-
-/*
-** free half of the CallInfo structures not in use by a thread
-*/
-void luaE_shrinkCI (lua_State *L) {
-  CallInfo *ci = L->ci;
-  CallInfo *next2;  /* next's next */
-  /* while there are two nexts */
-  while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
-    luaM_free(L, ci->next);  /* free next */
-    L->nci--;
-    ci->next = next2;  /* remove 'next' from the list */
-    next2->previous = ci;
-    ci = next2;  /* keep next's next */
-  }
-}
-
-
 static void stack_init (lua_State *L1, lua_State *L) {
-  int i; CallInfo *ci;
+  int i;
   /* initialize stack array */
   L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
   L1->stacksize = BASIC_STACK_SIZE;
@@ -149,24 +106,19 @@ static void stack_init (lua_State *L1, lua_State *L) {
     setnilvalue(s2v(L1->stack + i));  /* erase new stack */
   L1->top = L1->stack;
   L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
-  /* initialize first ci */
-  ci = &L1->base_ci;
-  ci->next = ci->previous = NULL;
-  L1->func = ci->func = L1->top;
+  /* initialize first 'function' */
+  L1->func = L1->stack;
   L1->func->stkci.previous = 0;  /* end of linked list */
   L1->func->stkci.framesize = LUA_MINSTACK + 1;
   callstatus(L1->func) = 0;
   setnilvalue(s2v(L1->top));  /* 'function' entry for this 'ci' */
   L1->top++;
-  L1->ci = ci;
 }
 
 
 static void freestack (lua_State *L) {
   if (L->stack == NULL)
     return;  /* stack not completely built yet */
-  L->ci = &L->base_ci;  /* free the entire 'ci' list */
-  luaE_freeCI(L);
   lua_assert(L->nci == 0);
   luaM_freearray(L, L->stack, L->stacksize);  /* free stack array */
 }
@@ -216,7 +168,6 @@ static void f_luaopen (lua_State *L, void *ud) {
 static void preinit_thread (lua_State *L, global_State *g) {
   G(L) = g;
   L->stack = NULL;
-  L->ci = NULL;
   L->func = NULL;
   L->nci = 0;
   L->stacksize = 0;

+ 1 - 14
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.147 2017/11/03 12:12:30 roberto Exp roberto $
+** $Id: lstate.h,v 2.148 2017/11/03 17:22:54 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -81,14 +81,6 @@ typedef struct stringtable {
 } stringtable;
 
 
-/*
-** Information about a call.
-*/
-typedef struct CallInfo {
-  StkId func;  /* function index in the stack */
-  struct CallInfo *previous, *next;  /* dynamic call link */
-} CallInfo;
-
 
 /*
 ** Bits in CallInfo status
@@ -170,7 +162,6 @@ struct lua_State {
   lu_byte status;
   StkId top;  /* first free slot in the stack */
   global_State *l_G;
-  CallInfo *ci;  /* call info for current function */
   StkId func;  /* current function */
   const Instruction *oldpc;  /* last pc traced */
   StkId stack_last;  /* last free slot in the stack */
@@ -179,7 +170,6 @@ struct lua_State {
   GCObject *gclist;
   struct lua_State *twups;  /* list of threads with open upvalues */
   struct lua_longjmp *errorJmp;  /* current error recover point */
-  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
   volatile lua_Hook hook;
   ptrdiff_t errfunc;  /* current error handling function (stack index) */
   int stacksize;
@@ -235,9 +225,6 @@ union GCUnion {
 
 LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
-LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
-LUAI_FUNC void luaE_freeCI (lua_State *L);
-LUAI_FUNC void luaE_shrinkCI (lua_State *L);
 
 
 #endif

+ 2 - 7
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 2.303 2017/11/03 17:22:54 roberto Exp roberto $
+** $Id: lvm.c,v 2.304 2017/11/03 19:33:22 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -796,7 +796,6 @@ void luaV_finishOp (lua_State *L) {
 
 
 void luaV_execute (lua_State *L) {
-  CallInfo *ci = L->ci;
   LClosure *cl;
   TValue *k;
   StkId base = L->func + 1;  /* local copy of 'L->func + 1' */
@@ -804,7 +803,6 @@ void luaV_execute (lua_State *L) {
   const Instruction *pc;  /* local copy of 'basepc(base)' */
   callstatus(base - 1) |= CIST_FRESH;  /* fresh invocation of 'luaV_execute" */
  newframe:  /* reentry point when frame changes (call/return) */
-  lua_assert(ci == L->ci);
   cl = clLvalue(s2v(L->func));  /* local reference to function's closure */
   k = cl->p->k;  /* local reference to function's constant table */
   updatemask(L);
@@ -1361,7 +1359,6 @@ void luaV_execute (lua_State *L) {
           /* else leave top for next instruction */
         }
         else {  /* Lua function */
-          ci = L->ci;
           base = L->func + 1;
           goto newframe;  /* restart luaV_execute over new Lua function */
         }
@@ -1391,7 +1388,6 @@ void luaV_execute (lua_State *L) {
           L->top = functop(ofunc);  /* correct top */
           ofunc->stkci.u.l.savedpc = nfunc->stkci.u.l.savedpc;
           callstatus(ofunc) |= CIST_TAIL;  /* function was tail called */
-          ci = L->ci = L->ci->previous;  /* remove new frame */
           base = ofunc + 1;
           L->func = ofunc;
           lua_assert(L->top == base + getproto(s2v(ofunc))->maxstacksize);
@@ -1403,11 +1399,10 @@ void luaV_execute (lua_State *L) {
         int b = GETARG_B(i);
         if (cl->p->sizep > 0) luaF_close(L, base);
         savepc(base);
-        b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
+        b = luaD_poscall(L, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
         if (callstatus(base - 1) & CIST_FRESH)  /* local 'base' still from callee */
           return;  /* external invocation: return */
         else {  /* invocation via reentry: continue execution */
-          ci = L->ci;
           base = L->func + 1;
           if (b) L->top = functop(base - 1);
           lua_assert(isLua(base - 1));