Browse Source

Simpler implementation for tail calls

Tail calls handled by 'luaD_precall', like regular calls, to avoid
code duplication.
Roberto Ierusalimschy 4 years ago
parent
commit
901d760093
3 changed files with 33 additions and 39 deletions
  1. 24 24
      ldo.c
  2. 2 2
      ldo.h
  3. 7 13
      lvm.c

+ 24 - 24
ldo.c

@@ -474,26 +474,16 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
 
 
 
 
 /*
 /*
-** Prepare a function for a tail call, building its call info on top
-** of the current call info. 'narg1' is the number of arguments plus 1
-** (so that it includes the function itself).
+** In a tail call, move function and parameters to previous call frame.
+** (This is done only when no more errors can occur before entering the
+** new function, to keep debug information always consistent.)
 */
 */
-void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
-  Proto *p = clLvalue(s2v(func))->p;
-  int fsize = p->maxstacksize;  /* frame size */
-  int nfixparams = p->numparams;
+static void moveparams (lua_State *L, StkId prevf, StkId func, int narg) {
   int i;
   int i;
-  for (i = 0; i < narg1; i++)  /* move down function and arguments */
-    setobjs2s(L, ci->func + i, func + i);
-  checkstackGC(L, fsize);
-  func = ci->func;  /* moved-down function */
-  for (; narg1 <= nfixparams; narg1++)
-    setnilvalue(s2v(func + narg1));  /* complete missing arguments */
-  ci->top = func + 1 + fsize;  /* top for new function */
-  lua_assert(ci->top <= L->stack_last);
-  ci->u.l.savedpc = p->code;  /* starting point */
-  ci->callstatus |= CIST_TAIL;
-  L->top = func + narg1;  /* set top */
+  narg++;  /* function itself will be moved, too */
+  for (i = 0; i < narg; i++)  /* move down function and arguments */
+    setobjs2s(L, prevf + i, func + i);
+  L->top = prevf + narg;  /* correct top */
 }
 }
 
 
 
 
@@ -504,8 +494,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
 ** to be executed, if it was a Lua function. Otherwise (a C function)
 ** to be executed, if it was a Lua function. Otherwise (a C function)
 ** returns NULL, with all the results on the stack, starting at the
 ** returns NULL, with all the results on the stack, starting at the
 ** original function position.
 ** original function position.
+** For regular calls, 'delta1' is 0. For tail calls, 'delta1' is the
+** 'delta' (correction of base for vararg functions) plus 1, so that it
+** cannot be zero. Like 'moveparams', this correction can only be done
+** when no more errors can occur in the call.
 */
 */
-CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
+CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) {
   lua_CFunction f;
   lua_CFunction f;
  retry:
  retry:
   switch (ttypetag(s2v(func))) {
   switch (ttypetag(s2v(func))) {
@@ -542,12 +536,18 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
       int nfixparams = p->numparams;
       int nfixparams = p->numparams;
       int fsize = p->maxstacksize;  /* frame size */
       int fsize = p->maxstacksize;  /* frame size */
       checkstackGCp(L, fsize, func);
       checkstackGCp(L, fsize, func);
-      L->ci = ci = next_ci(L);
-      ci->nresults = nresults;
+      if (delta1) {  /* tail call? */
+        ci = L->ci;  /* reuse stack frame */
+        ci->func -= delta1 - 1;  /* correct 'func' */
+        moveparams(L, ci->func, func, narg);
+      }
+      else {  /* regular call */
+        L->ci = ci = next_ci(L);  /* new frame */
+        ci->func = func;
+        ci->nresults = nresults;
+      }
       ci->u.l.savedpc = p->code;  /* starting point */
       ci->u.l.savedpc = p->code;  /* starting point */
       ci->top = func + 1 + fsize;
       ci->top = func + 1 + fsize;
-      ci->func = func;
-      L->ci = ci;
       for (; narg < nfixparams; narg++)
       for (; narg < nfixparams; narg++)
         setnilvalue(s2v(L->top++));  /* complete missing arguments */
         setnilvalue(s2v(L->top++));  /* complete missing arguments */
       lua_assert(ci->top <= L->stack_last);
       lua_assert(ci->top <= L->stack_last);
@@ -572,7 +572,7 @@ static void ccall (lua_State *L, StkId func, int nResults, int inc) {
   L->nCcalls += inc;
   L->nCcalls += inc;
   if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
   if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
     luaE_checkcstack(L);
     luaE_checkcstack(L);
-  if ((ci = luaD_precall(L, func, nResults)) != NULL) {  /* Lua function? */
+  if ((ci = luaD_precall(L, func, nResults, 0)) != NULL) {  /* Lua function? */
     ci->callstatus = CIST_FRESH;  /* mark that it is a "fresh" execute */
     ci->callstatus = CIST_FRESH;  /* mark that it is a "fresh" execute */
     luaV_execute(L, ci);  /* call it */
     luaV_execute(L, ci);  /* call it */
   }
   }

+ 2 - 2
ldo.h

@@ -58,8 +58,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
                                         int fTransfer, int nTransfer);
                                         int fTransfer, int nTransfer);
 LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
 LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
-LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
-LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
+LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nresults,
+                                                            int delta1);
 LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
 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 void luaD_callnoyield (lua_State *L, StkId func, int nResults);
 LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);
 LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);

+ 7 - 13
lvm.c

@@ -1632,11 +1632,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
           L->top = ra + b;  /* top signals number of arguments */
           L->top = ra + b;  /* top signals number of arguments */
         /* else previous instruction set top */
         /* else previous instruction set top */
         savepc(L);  /* in case of errors */
         savepc(L);  /* in case of errors */
-        if ((newci = luaD_precall(L, ra, nresults)) == NULL)
+        if ((newci = luaD_precall(L, ra, nresults, 0)) == NULL)
           updatetrap(ci);  /* C call; nothing else to be done */
           updatetrap(ci);  /* C call; nothing else to be done */
         else {  /* Lua call: run function in this same C frame */
         else {  /* Lua call: run function in this same C frame */
           ci = newci;
           ci = newci;
-          ci->callstatus = 0;  /* call re-uses 'luaV_execute' */
+          ci->callstatus = 0;
           goto startfunc;
           goto startfunc;
         }
         }
         vmbreak;
         vmbreak;
@@ -1648,21 +1648,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
         int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
         int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
         if (b != 0)
         if (b != 0)
           L->top = ra + b;
           L->top = ra + b;
-        else  /* previous instruction set top */
-          b = cast_int(L->top - ra);
+        /* else previous instruction set top */
         savepc(ci);  /* several calls here can raise errors */
         savepc(ci);  /* several calls here can raise errors */
         if (TESTARG_k(i)) {
         if (TESTARG_k(i)) {
           luaF_closeupval(L, base);  /* close upvalues from current call */
           luaF_closeupval(L, base);  /* close upvalues from current call */
           lua_assert(L->tbclist < base);  /* no pending tbc variables */
           lua_assert(L->tbclist < base);  /* no pending tbc variables */
           lua_assert(base == ci->func + 1);
           lua_assert(base == ci->func + 1);
         }
         }
-        while (!ttisfunction(s2v(ra))) {  /* not a function? */
-          luaD_tryfuncTM(L, ra);  /* try '__call' metamethod */
-          b++;  /* there is now one extra argument */
-          checkstackGCp(L, 1, ra);
+        if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) {  /* Lua function? */
+          ci->callstatus |= CIST_TAIL;
+          goto startfunc;  /* execute the callee */
         }
         }
-        if (!ttisLclosure(s2v(ra))) {  /* C function? */
-          luaD_precall(L, ra, LUA_MULTRET);  /* call it */
+        else {  /* C function */
           updatetrap(ci);
           updatetrap(ci);
           updatestack(ci);  /* stack may have been relocated */
           updatestack(ci);  /* stack may have been relocated */
           ci->func -= delta;  /* restore 'func' (if vararg) */
           ci->func -= delta;  /* restore 'func' (if vararg) */
@@ -1670,9 +1667,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
           updatetrap(ci);  /* 'luaD_poscall' can change hooks */
           updatetrap(ci);  /* 'luaD_poscall' can change hooks */
           goto ret;  /* caller returns after the tail call */
           goto ret;  /* caller returns after the tail call */
         }
         }
-        ci->func -= delta;  /* restore 'func' (if vararg) */
-        luaD_pretailcall(L, ci, ra, b);  /* prepare call frame */
-        goto startfunc;  /* execute the callee */
       }
       }
       vmcase(OP_RETURN) {
       vmcase(OP_RETURN) {
         int n = GETARG_B(i) - 1;  /* number of results */
         int n = GETARG_B(i) - 1;  /* number of results */