first version of Lua "stackless"

This commit is contained in:
Roberto Ierusalimschy
2001-12-18 18:52:30 -02:00
parent 101cee3032
commit e04f7ed450
8 changed files with 49 additions and 50 deletions

4
lapi.c
View File

@@ -517,9 +517,7 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
lua_lock(L); lua_lock(L);
api_checknelems(L, nargs+1); api_checknelems(L, nargs+1);
func = L->top - (nargs+1); func = L->top - (nargs+1);
luaD_call(L, func); luaD_call(L, func, nresults);
if (nresults != LUA_MULTRET)
luaD_adjusttop(L, func + nresults);
lua_unlock(L); lua_unlock(L);
} }

View File

@@ -31,8 +31,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci,
static int isLmark (CallInfo *ci) { static int isLmark (CallInfo *ci) {
lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION); return (ttype(ci->base - 1) == LUA_TFUNCTION && !ci_func(ci)->c.isC);
return (ci && ci->prev && !ci_func(ci)->c.isC);
} }
@@ -58,23 +57,17 @@ LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
static CallInfo *ci_stack (lua_State *L, StkId obj) { static CallInfo *ci_stack (lua_State *L, StkId obj) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
while (ci->base > obj) ci = ci->prev; while (ci->base > obj) ci--;
return (ci != &L->basefunc) ? ci : NULL; return ci;
} }
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
CallInfo *ci;
int status; int status;
lua_lock(L); lua_lock(L);
ci = L->ci; if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
while (level-- && ci != &L->basefunc) {
lua_assert(ci->base > ci->prev->base);
ci = ci->prev;
}
if (ci == &L->basefunc) status = 0; /* there is no such level */
else { else {
ar->_ci = ci; ar->_ci = (L->ci - L->base_ci) - level;
status = 1; status = 1;
} }
lua_unlock(L); lua_unlock(L);
@@ -84,8 +77,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
int refi; int refi;
if (lineinfo == NULL || pc == -1) if (lineinfo == NULL) return -1; /* no line info */
return -1; /* no line info or function is not active */
refi = prefi ? *prefi : 0; refi = prefi ? *prefi : 0;
if (lineinfo[refi] < 0) if (lineinfo[refi] < 0)
refline += -lineinfo[refi++]; refline += -lineinfo[refi++];
@@ -115,10 +107,11 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
static int currentpc (CallInfo *ci) { static int currentpc (CallInfo *ci) {
lua_assert(isLmark(ci)); lua_assert(isLmark(ci));
if (ci->pc) if (ci->savedpc)
return (ci->savedpc - ci_func(ci)->l.p->code) - 1;
else if (ci->pc)
return (*ci->pc - ci_func(ci)->l.p->code) - 1; return (*ci->pc - ci_func(ci)->l.p->code) - 1;
else else return 0;
return -1; /* function is not active */
} }
@@ -144,7 +137,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
Proto *fp; Proto *fp;
lua_lock(L); lua_lock(L);
name = NULL; name = NULL;
ci = ar->_ci; ci = L->base_ci + ar->_ci;
fp = getluaproto(ci); fp = getluaproto(ci);
if (fp) { /* is a Lua function? */ if (fp) { /* is a Lua function? */
name = luaF_getlocalname(fp, n, currentpc(ci)); name = luaF_getlocalname(fp, n, currentpc(ci));
@@ -162,7 +155,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
Proto *fp; Proto *fp;
lua_lock(L); lua_lock(L);
name = NULL; name = NULL;
ci = ar->_ci; ci = L->base_ci + ar->_ci;
fp = getluaproto(ci); fp = getluaproto(ci);
L->top--; /* pop new value */ L->top--; /* pop new value */
if (fp) { /* is a Lua function? */ if (fp) { /* is a Lua function? */
@@ -231,7 +224,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
int status = 1; int status = 1;
lua_lock(L); lua_lock(L);
if (*what != '>') { /* function is active? */ if (*what != '>') { /* function is active? */
ci = ar->_ci; ci = L->base_ci + ar->_ci;
f = ci->base - 1; f = ci->base - 1;
} }
else { else {
@@ -246,7 +239,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
break; break;
} }
case 'l': { case 'l': {
ar->currentline = currentline(ci); ar->currentline = (ci) ? currentline(ci) : -1;
break; break;
} }
case 'u': { case 'u': {
@@ -495,14 +488,13 @@ static const char *getobjname (lua_State *L, StkId obj, const char **name) {
static const char *getfuncname (lua_State *L, CallInfo *ci, static const char *getfuncname (lua_State *L, CallInfo *ci,
const char **name) { const char **name) {
ci = ci->prev; /* calling function */ ci--; /* calling function */
if (ci == &L->basefunc || !isLmark(ci)) if (ci == L->base_ci || !isLmark(ci))
return NULL; /* not an active Lua function */ return NULL; /* not an active Lua function */
else { else {
Proto *p = ci_func(ci)->l.p; Proto *p = ci_func(ci)->l.p;
int pc = currentpc(ci); int pc = currentpc(ci);
Instruction i; Instruction i;
if (pc == -1) return NULL; /* function is not activated */
i = p->code[pc]; i = p->code[pc];
return (GET_OPCODE(i) == OP_CALL return (GET_OPCODE(i) == OP_CALL
? getobjname(L, ci->base+GETARG_A(i), name) ? getobjname(L, ci->base+GETARG_A(i), name)

7
ldo.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.34 2001/06/08 19:00:57 roberto Exp $ ** $Id: ldo.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -25,7 +25,10 @@
void luaD_init (lua_State *L, int stacksize); void luaD_init (lua_State *L, int stacksize);
void luaD_adjusttop (lua_State *L, StkId newtop); void luaD_adjusttop (lua_State *L, StkId newtop);
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook); void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
void luaD_call (lua_State *L, StkId func); void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event);
StkId luaD_precall (lua_State *L, StkId func);
void luaD_call (lua_State *L, StkId func, int nResults);
void luaD_poscall (lua_State *L, int wanted, StkId firstResult);
void luaD_stackerror (lua_State *L); void luaD_stackerror (lua_State *L);
void luaD_error (lua_State *L, const char *s); void luaD_error (lua_State *L, const char *s);

3
lgc.c
View File

@@ -363,8 +363,7 @@ static void do1gcTM (lua_State *L, Udata *udata) {
setobj(top, tm); setobj(top, tm);
setuvalue(top+1, udata); setuvalue(top+1, udata);
L->top += 2; L->top += 2;
luaD_call(L, top); luaD_call(L, top, 0);
L->top = top; /* restore top */
} }
} }

View File

@@ -248,20 +248,6 @@ typedef struct Table {
#define sizenode(t) (twoto((t)->lsizenode)) #define sizenode(t) (twoto((t)->lsizenode))
#define sizearray(t) ((t)->sizearray) #define sizearray(t) ((t)->sizearray)
/*
** informations about a call (for debugging)
*/
typedef struct CallInfo {
struct CallInfo *prev; /* linked list */
StkId base; /* base for called function */
const Instruction **pc; /* current pc of called function */
int lastpc; /* last pc traced */
int line; /* current line */
int refi; /* current index in `lineinfo' */
} CallInfo;
#define ci_func(ci) (clvalue((ci)->base - 1))
extern const TObject luaO_nilobject; extern const TObject luaO_nilobject;

View File

@@ -87,12 +87,12 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
L->_G = NULL; L->_G = NULL;
L->stack = NULL; L->stack = NULL;
L->stacksize = 0; L->stacksize = 0;
L->ci = &L->basefunc;
L->basefunc.prev = NULL;
L->errorJmp = NULL; L->errorJmp = NULL;
L->callhook = NULL; L->callhook = NULL;
L->linehook = NULL; L->linehook = NULL;
L->openupval = NULL; L->openupval = NULL;
L->size_ci = 0;
L->base_ci = NULL;
L->allowhooks = 1; L->allowhooks = 1;
L->next = L->previous = L; L->next = L->previous = L;
so.stacksize = stacksize; so.stacksize = stacksize;
@@ -130,6 +130,7 @@ static void close_state (lua_State *L, lua_State *OL) {
luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
luaM_freelem(NULL, L->_G); luaM_freelem(NULL, L->_G);
} }
luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
luaM_freearray(OL, L->stack, L->stacksize, TObject); luaM_freearray(OL, L->stack, L->stacksize, TObject);
luaM_freelem(OL, L); luaM_freelem(OL, L);
} }

View File

@@ -69,6 +69,24 @@ typedef struct stringtable {
} stringtable; } stringtable;
/*
** informations about a call
*/
typedef struct CallInfo {
StkId base; /* base for called function */
const Instruction *savedpc;
lua_Hook linehook;
/* extra information for debugging */
const Instruction **pc;
int lastpc; /* last pc traced */
int line; /* current line */
int refi; /* current index in `lineinfo' */
} CallInfo;
#define ci_func(ci) (clvalue((ci)->base - 1))
/* /*
** `global state', shared by all threads of this state ** `global state', shared by all threads of this state
*/ */
@@ -98,6 +116,9 @@ struct lua_State {
StkId stack_last; /* last free slot in the stack */ StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */ StkId stack; /* stack base */
int stacksize; int stacksize;
CallInfo *end_ci; /* points after end of ci array*/
CallInfo *base_ci; /* array of CallInfo's */
int size_ci; /* size of array `base_ci' */
global_State *_G; global_State *_G;
lua_Hook callhook; lua_Hook callhook;
lua_Hook linehook; lua_Hook linehook;
@@ -106,7 +127,6 @@ struct lua_State {
UpVal *openupval; /* list of open upvalues in this stack */ UpVal *openupval; /* list of open upvalues in this stack */
lua_State *next; /* circular double linked list of states */ lua_State *next; /* circular double linked list of states */
lua_State *previous; lua_State *previous;
CallInfo basefunc;
}; };

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: luadebug.h,v 1.20 2001/04/06 21:17:37 roberto Exp $ ** $Id: luadebug.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
** Debugging API ** Debugging API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -39,7 +39,7 @@ struct lua_Debug {
const char *source; /* (S) */ const char *source; /* (S) */
char short_src[LUA_IDSIZE]; /* (S) */ char short_src[LUA_IDSIZE]; /* (S) */
/* private part */ /* private part */
struct CallInfo *_ci; /* active function */ int _ci; /* active function */
}; };