From 901d76009346d76996679c02deee708bf225e91e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 11 Jun 2021 13:41:07 -0300 Subject: [PATCH] Simpler implementation for tail calls Tail calls handled by 'luaD_precall', like regular calls, to avoid code duplication. --- ldo.c | 48 ++++++++++++++++++++++++------------------------ ldo.h | 4 ++-- lvm.c | 20 +++++++------------- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/ldo.c b/ldo.c index 7135079b..a410461b 100644 --- a/ldo.c +++ b/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; - 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) ** returns NULL, with all the results on the stack, starting at the ** 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; retry: switch (ttypetag(s2v(func))) { @@ -542,12 +536,18 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ 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->top = func + 1 + fsize; - ci->func = func; - L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ 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; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) 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 */ luaV_execute(L, ci); /* call it */ } diff --git a/ldo.h b/ldo.h index 6bf0ed86..6edc4450 100644 --- a/ldo.h +++ b/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, int fTransfer, int nTransfer); 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_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lvm.c b/lvm.c index e4b1903e..485b9caa 100644 --- a/lvm.c +++ b/lvm.c @@ -1632,11 +1632,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ 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 */ else { /* Lua call: run function in this same C frame */ ci = newci; - ci->callstatus = 0; /* call re-uses 'luaV_execute' */ + ci->callstatus = 0; goto startfunc; } vmbreak; @@ -1648,21 +1648,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) 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 */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ lua_assert(L->tbclist < base); /* no pending tbc variables */ 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); updatestack(ci); /* stack may have been relocated */ 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 */ 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) { int n = GETARG_B(i) - 1; /* number of results */