aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-21 12:18:03 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-21 12:18:03 -0200
commit14c3aa12b5a609f45b9be49e11ee862f9193a014 (patch)
tree6ebd4e3472ee2e1b9c3f8e3c2a896f8e9d31a268 /lvm.c
parentf3ca52bfa994edacfa8fbbc6f1014724b384de9a (diff)
downloadlua-14c3aa12b5a609f45b9be49e11ee862f9193a014.tar.gz
lua-14c3aa12b5a609f45b9be49e11ee862f9193a014.tar.bz2
lua-14c3aa12b5a609f45b9be49e11ee862f9193a014.zip
more direct implementation for tail calls.
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c42
1 files changed, 17 insertions, 25 deletions
diff --git a/lvm.c b/lvm.c
index ab28ea72..b673db91 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.311 2017/11/16 12:59:14 roberto Exp roberto $ 2** $Id: lvm.c,v 2.312 2017/11/20 12:57:39 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -1429,31 +1429,23 @@ void luaV_execute (lua_State *L) {
1429 vmbreak; 1429 vmbreak;
1430 } 1430 }
1431 vmcase(OP_TAILCALL) { 1431 vmcase(OP_TAILCALL) {
1432 int b = GETARG_B(i); 1432 int b = GETARG_B(i); /* number of arguments + 1 (function) */
1433 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 1433 if (b != 0)
1434 L->top = ra + b;
1435 else /* previous instruction set top */
1436 b = L->top - ra;
1434 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 1437 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
1435 savepc(L); 1438 if (!ttisfunction(s2v(ra))) { /* not a function? */
1436 if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ 1439 /* try to get '__call' metamethod */
1437 updatetrap(ci); 1440 Protect(ra = luaD_tryfuncTM(L, ra));
1438 else { 1441 b++; /* there is now one extra argument */
1439 /* tail call: put called frame (n) in place of caller one (o) */ 1442 }
1440 CallInfo *nci = L->ci; /* called frame (new) */ 1443 if (!ttisLclosure(s2v(ra))) /* C function? */
1441 CallInfo *oci = nci->previous; /* caller frame (old) */ 1444 Protect(luaD_precall(L, ra, LUA_MULTRET)); /* call it */
1442 StkId nfunc = nci->func; /* called function */ 1445 else { /* tail call */
1443 StkId ofunc = oci->func; /* caller function */ 1446 if (cl->p->sizep > 0) /* close upvalues from previous call */
1444 /* last stack slot filled by 'precall' */ 1447 luaF_close(L, ci->func + 1);
1445 StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams; 1448 luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
1446 int aux;
1447 /* close all upvalues from previous call */
1448 if (cl->p->sizep > 0) luaF_close(L, ofunc + 1);
1449 /* move new frame into old one */
1450 for (aux = 0; nfunc + aux < lim; aux++)
1451 setobjs2s(L, ofunc + aux, nfunc + aux);
1452 oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
1453 oci->u.l.savedpc = nci->u.l.savedpc;
1454 oci->callstatus |= CIST_TAIL; /* function was tail called */
1455 ci = L->ci = oci; /* remove new frame */
1456 lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize);
1457 goto newframe; /* restart luaV_execute over new Lua function */ 1449 goto newframe; /* restart luaV_execute over new Lua function */
1458 } 1450 }
1459 vmbreak; 1451 vmbreak;