Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 901d760

Browse files
committed
Simpler implementation for tail calls
Tail calls handled by 'luaD_precall', like regular calls, to avoid code duplication.
1 parent c0ed74c commit 901d760

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

ldo.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -474,26 +474,16 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
474474

475475

476476
/*
477-
** Prepare a function for a tail call, building its call info on top
478-
** of the current call info. 'narg1' is the number of arguments plus 1
479-
** (so that it includes the function itself).
477+
** In a tail call, move function and parameters to previous call frame.
478+
** (This is done only when no more errors can occur before entering the
479+
** new function, to keep debug information always consistent.)
480480
*/
481-
void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
482-
Proto *p = clLvalue(s2v(func))->p;
483-
int fsize = p->maxstacksize; /* frame size */
484-
int nfixparams = p->numparams;
481+
static void moveparams (lua_State *L, StkId prevf, StkId func, int narg) {
485482
int i;
486-
for (i = 0; i < narg1; i++) /* move down function and arguments */
487-
setobjs2s(L, ci->func + i, func + i);
488-
checkstackGC(L, fsize);
489-
func = ci->func; /* moved-down function */
490-
for (; narg1 <= nfixparams; narg1++)
491-
setnilvalue(s2v(func + narg1)); /* complete missing arguments */
492-
ci->top = func + 1 + fsize; /* top for new function */
493-
lua_assert(ci->top <= L->stack_last);
494-
ci->u.l.savedpc = p->code; /* starting point */
495-
ci->callstatus |= CIST_TAIL;
496-
L->top = func + narg1; /* set top */
483+
narg++; /* function itself will be moved, too */
484+
for (i = 0; i < narg; i++) /* move down function and arguments */
485+
setobjs2s(L, prevf + i, func + i);
486+
L->top = prevf + narg; /* correct top */
497487
}
498488

499489

@@ -504,8 +494,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
504494
** to be executed, if it was a Lua function. Otherwise (a C function)
505495
** returns NULL, with all the results on the stack, starting at the
506496
** original function position.
497+
** For regular calls, 'delta1' is 0. For tail calls, 'delta1' is the
498+
** 'delta' (correction of base for vararg functions) plus 1, so that it
499+
** cannot be zero. Like 'moveparams', this correction can only be done
500+
** when no more errors can occur in the call.
507501
*/
508-
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
502+
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) {
509503
lua_CFunction f;
510504
retry:
511505
switch (ttypetag(s2v(func))) {
@@ -542,12 +536,18 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
542536
int nfixparams = p->numparams;
543537
int fsize = p->maxstacksize; /* frame size */
544538
checkstackGCp(L, fsize, func);
545-
L->ci = ci = next_ci(L);
546-
ci->nresults = nresults;
539+
if (delta1) { /* tail call? */
540+
ci = L->ci; /* reuse stack frame */
541+
ci->func -= delta1 - 1; /* correct 'func' */
542+
moveparams(L, ci->func, func, narg);
543+
}
544+
else { /* regular call */
545+
L->ci = ci = next_ci(L); /* new frame */
546+
ci->func = func;
547+
ci->nresults = nresults;
548+
}
547549
ci->u.l.savedpc = p->code; /* starting point */
548550
ci->top = func + 1 + fsize;
549-
ci->func = func;
550-
L->ci = ci;
551551
for (; narg < nfixparams; narg++)
552552
setnilvalue(s2v(L->top++)); /* complete missing arguments */
553553
lua_assert(ci->top <= L->stack_last);
@@ -572,7 +572,7 @@ static void ccall (lua_State *L, StkId func, int nResults, int inc) {
572572
L->nCcalls += inc;
573573
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
574574
luaE_checkcstack(L);
575-
if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
575+
if ((ci = luaD_precall(L, func, nResults, 0)) != NULL) { /* Lua function? */
576576
ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */
577577
luaV_execute(L, ci); /* call it */
578578
}

ldo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
5858
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
5959
int fTransfer, int nTransfer);
6060
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
61-
LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
62-
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
61+
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nresults,
62+
int delta1);
6363
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
6464
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
6565
LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func);

lvm.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,11 +1632,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
16321632
L->top = ra + b; /* top signals number of arguments */
16331633
/* else previous instruction set top */
16341634
savepc(L); /* in case of errors */
1635-
if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1635+
if ((newci = luaD_precall(L, ra, nresults, 0)) == NULL)
16361636
updatetrap(ci); /* C call; nothing else to be done */
16371637
else { /* Lua call: run function in this same C frame */
16381638
ci = newci;
1639-
ci->callstatus = 0; /* call re-uses 'luaV_execute' */
1639+
ci->callstatus = 0;
16401640
goto startfunc;
16411641
}
16421642
vmbreak;
@@ -1648,31 +1648,25 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
16481648
int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
16491649
if (b != 0)
16501650
L->top = ra + b;
1651-
else /* previous instruction set top */
1652-
b = cast_int(L->top - ra);
1651+
/* else previous instruction set top */
16531652
savepc(ci); /* several calls here can raise errors */
16541653
if (TESTARG_k(i)) {
16551654
luaF_closeupval(L, base); /* close upvalues from current call */
16561655
lua_assert(L->tbclist < base); /* no pending tbc variables */
16571656
lua_assert(base == ci->func + 1);
16581657
}
1659-
while (!ttisfunction(s2v(ra))) { /* not a function? */
1660-
luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
1661-
b++; /* there is now one extra argument */
1662-
checkstackGCp(L, 1, ra);
1658+
if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) { /* Lua function? */
1659+
ci->callstatus |= CIST_TAIL;
1660+
goto startfunc; /* execute the callee */
16631661
}
1664-
if (!ttisLclosure(s2v(ra))) { /* C function? */
1665-
luaD_precall(L, ra, LUA_MULTRET); /* call it */
1662+
else { /* C function */
16661663
updatetrap(ci);
16671664
updatestack(ci); /* stack may have been relocated */
16681665
ci->func -= delta; /* restore 'func' (if vararg) */
16691666
luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */
16701667
updatetrap(ci); /* 'luaD_poscall' can change hooks */
16711668
goto ret; /* caller returns after the tail call */
16721669
}
1673-
ci->func -= delta; /* restore 'func' (if vararg) */
1674-
luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
1675-
goto startfunc; /* execute the callee */
16761670
}
16771671
vmcase(OP_RETURN) {
16781672
int n = GETARG_B(i) - 1; /* number of results */

0 commit comments

Comments
 (0)