Skip to content

Commit

Permalink
new way to control `pc' of running functions
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed Jul 16, 2003
1 parent 5a761e3 commit fa26d29
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 147 deletions.
19 changes: 2 additions & 17 deletions ldebug.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.153 2003/05/14 12:09:12 roberto Exp roberto $
** $Id: ldebug.c,v 1.154 2003/07/10 11:59:06 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -30,14 +30,8 @@
static const char *getfuncname (CallInfo *ci, const char **name);


#define isLua(ci) (!((ci)->state & CI_C))


static int currentpc (CallInfo *ci) {
if (!isLua(ci)) return -1; /* function is not a Lua function? */
if (ci->state & CI_HASFRAME) /* function has a frame? */
ci->u.l.savedpc = *ci->u.l.pc; /* use `pc' from there */
/* function's pc is saved */
return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
}

Expand All @@ -51,14 +45,6 @@ static int currentline (CallInfo *ci) {
}


void luaG_inithooks (lua_State *L) {
CallInfo *ci;
for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
currentpc(ci);
L->hookinit = 1;
}


/*
** this function can be called asynchronous (e.g. during a signal)
*/
Expand All @@ -71,7 +57,6 @@ LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
L->basehookcount = count;
resethookcount(L);
L->hookmask = cast(lu_byte, mask);
L->hookinit = 0;
return 1;
}

Expand All @@ -97,7 +82,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
lua_lock(L);
for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
level--;
if (!(ci->state & CI_C)) /* Lua function? */
if (f_isLua(ci)) /* Lua function? */
level -= ci->u.l.tailcalls; /* skip lost tail calls */
}
if (level > 0 || ci == L->base_ci) status = 0; /* there is no such level */
Expand Down
3 changes: 1 addition & 2 deletions ldebug.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldebug.h,v 1.31 2002/08/20 20:03:05 roberto Exp roberto $
** $Id: ldebug.h,v 1.32 2002/11/18 11:01:55 roberto Exp $
** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h
*/
Expand All @@ -18,7 +18,6 @@
#define resethookcount(L) (L->hookcount = L->basehookcount)


void luaG_inithooks (lua_State *L);
void luaG_typeerror (lua_State *L, const TObject *o, const char *opname);
void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2);
Expand Down
47 changes: 21 additions & 26 deletions ldo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.218 2003/05/13 19:22:19 roberto Exp roberto $
** $Id: ldo.c,v 1.219 2003/05/14 21:02:39 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -243,6 +243,7 @@ StkId luaD_precall (lua_State *L, StkId func) {
cl = &clvalue(func)->l;
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st;
Proto *p = cl->p;
if (p->is_vararg) /* varargs? */
adjust_varargs(L, p->numparams, func+1);
Expand All @@ -252,9 +253,8 @@ StkId luaD_precall (lua_State *L, StkId func) {
ci->top = L->base + p->maxstacksize;
ci->u.l.savedpc = p->code; /* starting point */
ci->u.l.tailcalls = 0;
ci->state = CI_SAVEDPC;
while (L->top < ci->top)
setnilvalue(L->top++);
for (st = L->top; st < ci->top; st++)
setnilvalue(st);
L->top = ci->top;
return NULL;
}
Expand All @@ -265,7 +265,6 @@ StkId luaD_precall (lua_State *L, StkId func) {
ci = ++L->ci; /* now `enter' new function */
L->base = L->ci->base = restorestack(L, funcr) + 1;
ci->top = L->top + LUA_MINSTACK;
ci->state = CI_C; /* a C function */
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
Expand All @@ -279,7 +278,7 @@ StkId luaD_precall (lua_State *L, StkId func) {
static StkId callrethooks (lua_State *L, StkId firstResult) {
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
luaD_callhook(L, LUA_HOOKRET, -1);
if (!(L->ci->state & CI_C)) { /* Lua function? */
if (f_isLua(L->ci)) { /* Lua function? */
while (L->ci->u.l.tailcalls--) /* call hook for eventual tail calls */
luaD_callhook(L, LUA_HOOKTAILRET, -1);
}
Expand Down Expand Up @@ -313,7 +312,6 @@ void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
*/
void luaD_call (lua_State *L, StkId func, int nResults) {
StkId firstResult;
lua_assert(!(L->ci->state & CI_CALLING));
if (++L->nCcalls >= LUA_MAXCCALLS) {
if (L->nCcalls == LUA_MAXCCALLS)
luaG_runerror(L, "C stack overflow");
Expand All @@ -322,7 +320,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
}
firstResult = luaD_precall(L, func);
if (firstResult == NULL) /* is a Lua function? */
firstResult = luaV_execute(L); /* call it */
firstResult = luaV_execute(L, 1); /* call it */
luaD_poscall(L, nResults, firstResult);
L->nCcalls--;
luaC_checkGC(L);
Expand All @@ -333,29 +331,28 @@ static void resume (lua_State *L, void *ud) {
StkId firstResult;
int nargs = *cast(int *, ud);
CallInfo *ci = L->ci;
if (ci == L->base_ci) { /* no activation record? */
if (nargs >= L->top - L->base)
luaG_runerror(L, "cannot resume dead coroutine");
luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
if (!L->isSuspended) {
if (ci == L->base_ci) { /* no activation record? */
if (nargs >= L->top - L->base)
luaG_runerror(L, "cannot resume dead coroutine");
luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
}
else
luaG_runerror(L, "cannot resume non-suspended coroutine");
}
else if (ci->state & CI_YIELD) { /* inside a yield? */
if (ci->state & CI_C) { /* `common' yield? */
else { /* resumming from previous yield */
if (!f_isLua(ci)) { /* `common' yield? */
/* finish interrupted execution of `OP_CALL' */
int nresults;
lua_assert((ci-1)->state & CI_SAVEDPC);
lua_assert(GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_CALL ||
GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_TAILCALL);
nresults = GETARG_C(*((ci-1)->u.l.savedpc - 1)) - 1;
luaD_poscall(L, nresults, L->top - nargs); /* complete it */
if (nresults >= 0) L->top = L->ci->top;
}
else { /* yielded inside a hook: just continue its execution */
ci->state &= ~CI_YIELD;
}
} /* else yielded inside a hook: just continue its execution */
}
else
luaG_runerror(L, "cannot resume non-suspended coroutine");
firstResult = luaV_execute(L);
L->isSuspended = 0;
firstResult = luaV_execute(L, L->ci - L->base_ci);
if (firstResult != NULL) /* return? */
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
}
Expand Down Expand Up @@ -388,17 +385,15 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
ci = L->ci;
if (L->nCcalls > 0)
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
if (ci->state & CI_C) { /* usual yield */
if ((ci-1)->state & CI_C)
luaG_runerror(L, "cannot yield a C function");
if (!f_isLua(ci)) { /* usual yield */
if (L->top - nresults > L->base) { /* is there garbage in the stack? */
int i;
for (i=0; i<nresults; i++) /* move down results */
setobjs2s(L->base + i, L->top - nresults + i);
L->top = L->base + nresults;
}
} /* else it's an yield inside a hook: nothing to do */
ci->state |= CI_YIELD;
L->isSuspended = 1;
lua_unlock(L);
return -1;
}
Expand Down
3 changes: 1 addition & 2 deletions lgc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 1.173 2003/05/16 18:58:39 roberto Exp roberto $
** $Id: lgc.c,v 1.174 2003/07/07 13:32:19 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -232,7 +232,6 @@ static void traversestack (GCState *st, lua_State *L1) {
lim = L1->top;
for (ci = L1->base_ci; ci <= L1->ci; ci++) {
lua_assert(ci->top <= L1->stack_last);
lua_assert(ci->state & (CI_C | CI_HASFRAME | CI_SAVEDPC));
if (lim < ci->top) lim = ci->top;
}
for (o = L1->stack; o < L1->top; o++)
Expand Down
6 changes: 3 additions & 3 deletions lgc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lgc.h,v 1.18 2003/02/10 17:32:50 roberto Exp roberto $
** $Id: lgc.h,v 1.19 2003/02/28 19:45:15 roberto Exp $
** Garbage Collector
** See Copyright Notice in lua.h
*/
Expand All @@ -11,8 +11,8 @@
#include "lobject.h"


#define luaC_checkGC(L) { lua_assert(!(L->ci->state & CI_CALLING)); \
if (G(L)->nblocks >= G(L)->GCthreshold) luaC_collectgarbage(L); }
#define luaC_checkGC(L) { if (G(L)->nblocks >= G(L)->GCthreshold) \
luaC_collectgarbage(L); }


void luaC_separateudata (lua_State *L);
Expand Down
6 changes: 3 additions & 3 deletions lstate.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lstate.c,v 1.122 2003/03/18 12:50:04 roberto Exp roberto $
** $Id: lstate.c,v 1.123 2003/04/03 13:35:34 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -68,7 +68,6 @@ static void stack_init (lua_State *L1, lua_State *L) {
L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
L1->ci = L1->base_ci;
L1->ci->state = CI_C; /* not a Lua function */
setnilvalue(L1->top++); /* `function' entry for this `ci' */
L1->base = L1->ci->base = L1->top;
L1->ci->top = L1->top + LUA_MINSTACK;
Expand Down Expand Up @@ -128,13 +127,14 @@ static void preinit_state (lua_State *L) {
L->stacksize = 0;
L->errorJmp = NULL;
L->hook = NULL;
L->hookmask = L->hookinit = 0;
L->hookmask = 0;
L->basehookcount = 0;
L->allowhook = 1;
resethookcount(L);
L->openupval = NULL;
L->size_ci = 0;
L->nCcalls = 0;
L->isSuspended = 0;
L->base_ci = L->ci = NULL;
L->errfunc = 0;
setnilvalue(gt(L));
Expand Down
20 changes: 4 additions & 16 deletions lstate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lstate.h,v 1.109 2003/02/27 11:52:30 roberto Exp roberto $
** $Id: lstate.h,v 1.110 2003/04/28 19:26:16 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -75,11 +75,9 @@ typedef struct stringtable {
typedef struct CallInfo {
StkId base; /* base for called function */
StkId top; /* top for this function */
int state; /* bit fields; see below */
union {
struct { /* for Lua functions */
const Instruction *savedpc;
const Instruction **pc; /* points to `pc' variable in `luaV_execute' */
int tailcalls; /* number of tail calls lost under this entry */
} l;
struct { /* for C functions */
Expand All @@ -89,20 +87,10 @@ typedef struct CallInfo {
} CallInfo;


/*
** bit fields for `CallInfo.state'
*/
#define CI_C (1<<0) /* 1 if function is a C function */
/* 1 if (Lua) function has an active `luaV_execute' running it */
#define CI_HASFRAME (1<<1)
/* 1 if Lua function is calling another Lua function (and therefore its
`pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
#define CI_CALLING (1<<2)
#define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */
#define CI_YIELD (1<<4) /* 1 if thread is suspended */


#define ci_func(ci) (clvalue((ci)->base - 1))
#define f_isLua(ci) (!ci_func(ci)->c.isC)
#define isLua(ci) (ttisfunction((ci)->base - 1) && f_isLua(ci))


/*
Expand Down Expand Up @@ -143,7 +131,7 @@ struct lua_State {
unsigned short nCcalls; /* number of nested C calls */
lu_byte hookmask;
lu_byte allowhook;
lu_byte hookinit;
lu_byte isSuspended;
int basehookcount;
int hookcount;
lua_Hook hook;
Expand Down
Loading

0 comments on commit fa26d29

Please sign in to comment.