From 901d76009346d76996679c02deee708bf225e91e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 11 Jun 2021 13:41:07 -0300 Subject: Simpler implementation for tail calls Tail calls handled by 'luaD_precall', like regular calls, to avoid code duplication. --- lvm.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'lvm.c') 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 */ -- cgit v1.2.3-55-g6feb