baby steps to remove 'CallInfo': keeping 'L->func' correct

This commit is contained in:
Roberto Ierusalimschy 2017-10-31 15:54:35 -02:00
parent ad5dcdcf0f
commit c5482468fd
5 changed files with 26 additions and 9 deletions

14
ldo.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $
** $Id: ldo.c,v 2.162 2017/07/27 13:50:16 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -160,6 +160,7 @@ static void correctstack (lua_State *L, StkId oldstack) {
CallInfo *ci;
UpVal *up;
L->top = (L->top - oldstack) + L->stack;
L->func = (L->func - oldstack) + L->stack;
for (up = L->openupval; up != NULL; up = up->u.open.next)
up->v = s2v((uplevel(up) - oldstack) + L->stack);
for (ci = L->ci; ci != NULL; ci = ci->previous) {
@ -369,6 +370,8 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
}
res = ci->func; /* res == final position of 1st result */
L->ci = ci->previous; /* back to caller */
L->func -= L->func->stkci.previous;
lua_assert(L->func == L->ci->func);
/* move results to proper place */
return moveresults(L, firstResult, res, nres, wanted);
}
@ -400,7 +403,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = func;
func->stkci.previous = func - L->func;
L->func = ci->func = func;
ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last);
ci->callstatus = 0;
@ -424,7 +428,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
luaT_adjustvarargs(L, p, n);
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = func;
func->stkci.previous = func - L->func;
L->func = ci->func = func;
L->top = ci->top = func + 1 + fsize;
lua_assert(ci->top <= L->stack_last);
ci->u.l.savedpc = p->code; /* starting point */
@ -558,6 +563,7 @@ static int recover (lua_State *L, int status) {
luaF_close(L, oldtop);
seterrorobj(L, status, oldtop);
L->ci = ci;
L->func = ci->func;
L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */
L->nny = 0; /* should be zero to be yieldable */
luaD_shrinkstack(L);
@ -693,6 +699,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t old_top, ptrdiff_t ef) {
int status;
CallInfo *old_ci = L->ci;
ptrdiff_t oldfunc = savestack(L, L->func);
lu_byte old_allowhooks = L->allowhook;
unsigned short old_nny = L->nny;
ptrdiff_t old_errfunc = L->errfunc;
@ -703,6 +710,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
luaF_close(L, oldtop); /* close possible pending closures */
seterrorobj(L, status, oldtop);
L->ci = old_ci;
L->func = restorestack(L, oldfunc);
L->allowhook = old_allowhooks;
L->nny = old_nny;
luaD_shrinkstack(L);

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 2.124 2017/06/27 11:35:31 roberto Exp roberto $
** $Id: lobject.h,v 2.125 2017/06/29 15:06:44 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -311,6 +311,10 @@ typedef struct TValue {
typedef union StackValue {
TValue val;
struct {
TValuefields;
unsigned short previous; /* difference to previous 'func' */
} stkci;
} StackValue;

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.c,v 2.141 2017/06/29 15:06:44 roberto Exp roberto $
** $Id: lstate.c,v 2.142 2017/10/11 12:38:45 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -153,8 +153,10 @@ static void stack_init (lua_State *L1, lua_State *L) {
ci = &L1->base_ci;
ci->next = ci->previous = NULL;
ci->callstatus = 0;
ci->func = L1->top;
setnilvalue(s2v(L1->top++)); /* 'function' entry for this 'ci' */
L1->func = ci->func = L1->top;
L1->func->stkci.previous = 0; /* end of linked list */
setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
L1->top++;
ci->top = L1->top + LUA_MINSTACK;
L1->ci = ci;
}
@ -215,6 +217,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
G(L) = g;
L->stack = NULL;
L->ci = NULL;
L->func = NULL;
L->nci = 0;
L->stacksize = 0;
L->twups = L; /* thread has no upvalues */

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.h,v 2.143 2017/06/12 14:21:44 roberto Exp roberto $
** $Id: lstate.h,v 2.144 2017/07/27 13:50:16 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -192,6 +192,7 @@ struct lua_State {
StkId top; /* first free slot in the stack */
global_State *l_G;
CallInfo *ci; /* call info for current function */
StkId func; /* current function */
const Instruction *oldpc; /* last pc traced */
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */

3
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.298 2017/10/04 15:49:24 roberto Exp roberto $
** $Id: lvm.c,v 2.299 2017/10/04 21:56:32 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -1390,6 +1390,7 @@ void luaV_execute (lua_State *L) {
oci->u.l.savedpc = nci->u.l.savedpc;
oci->callstatus |= CIST_TAIL; /* function was tail called */
ci = L->ci = oci; /* remove new frame */
L->func = ofunc;
lua_assert(L->top ==
oci->func + 1 + getproto(s2v(ofunc))->maxstacksize);
goto newframe; /* restart luaV_execute over new Lua function */