summaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-06-11 13:41:07 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-06-11 13:41:07 -0300
commit901d76009346d76996679c02deee708bf225e91e (patch)
tree51fb561c607c498e5dd229ab26c439645715c49e /lvm.c
parentc0ed74c1e130aa6d80e5ffc7b6e32b433aca1765 (diff)
downloadlua-github-901d76009346d76996679c02deee708bf225e91e.tar.gz
Simpler implementation for tail calls
Tail calls handled by 'luaD_precall', like regular calls, to avoid code duplication.
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c20
1 files changed, 7 insertions, 13 deletions
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 */