Skip to content

Commit

Permalink
first version of Lua "stackless"
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed Dec 18, 2001
1 parent 101cee3 commit e04f7ed
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 50 deletions.
4 changes: 1 addition & 3 deletions lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,7 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
lua_lock(L);
api_checknelems(L, nargs+1);
func = L->top - (nargs+1);
luaD_call(L, func);
if (nresults != LUA_MULTRET)
luaD_adjusttop(L, func + nresults);
luaD_call(L, func, nresults);
lua_unlock(L);
}

Expand Down
40 changes: 16 additions & 24 deletions ldebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci,


static int isLmark (CallInfo *ci) {
lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
return (ci && ci->prev && !ci_func(ci)->c.isC);
return (ttype(ci->base - 1) == LUA_TFUNCTION && !ci_func(ci)->c.isC);
}


Expand All @@ -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) {
CallInfo *ci = L->ci;
while (ci->base > obj) ci = ci->prev;
return (ci != &L->basefunc) ? ci : NULL;
while (ci->base > obj) ci--;
return ci;
}


LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
CallInfo *ci;
int status;
lua_lock(L);
ci = L->ci;
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 */
if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
else {
ar->_ci = ci;
ar->_ci = (L->ci - L->base_ci) - level;
status = 1;
}
lua_unlock(L);
Expand All @@ -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 refi;
if (lineinfo == NULL || pc == -1)
return -1; /* no line info or function is not active */
if (lineinfo == NULL) return -1; /* no line info */
refi = prefi ? *prefi : 0;
if (lineinfo[refi] < 0)
refline += -lineinfo[refi++];
Expand Down Expand Up @@ -115,10 +107,11 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {

static int currentpc (CallInfo *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;
else
return -1; /* function is not active */
else return 0;
}


Expand All @@ -144,7 +137,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
Proto *fp;
lua_lock(L);
name = NULL;
ci = ar->_ci;
ci = L->base_ci + ar->_ci;
fp = getluaproto(ci);
if (fp) { /* is a Lua function? */
name = luaF_getlocalname(fp, n, currentpc(ci));
Expand All @@ -162,7 +155,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
Proto *fp;
lua_lock(L);
name = NULL;
ci = ar->_ci;
ci = L->base_ci + ar->_ci;
fp = getluaproto(ci);
L->top--; /* pop new value */
if (fp) { /* is a Lua function? */
Expand Down Expand Up @@ -231,7 +224,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
int status = 1;
lua_lock(L);
if (*what != '>') { /* function is active? */
ci = ar->_ci;
ci = L->base_ci + ar->_ci;
f = ci->base - 1;
}
else {
Expand All @@ -246,7 +239,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
break;
}
case 'l': {
ar->currentline = currentline(ci);
ar->currentline = (ci) ? currentline(ci) : -1;
break;
}
case 'u': {
Expand Down Expand Up @@ -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,
const char **name) {
ci = ci->prev; /* calling function */
if (ci == &L->basefunc || !isLmark(ci))
ci--; /* calling function */
if (ci == L->base_ci || !isLmark(ci))
return NULL; /* not an active Lua function */
else {
Proto *p = ci_func(ci)->l.p;
int pc = currentpc(ci);
Instruction i;
if (pc == -1) return NULL; /* function is not activated */
i = p->code[pc];
return (GET_OPCODE(i) == OP_CALL
? getobjname(L, ci->base+GETARG_A(i), name)
Expand Down
7 changes: 5 additions & 2 deletions ldo.h
Original file line number Diff line number Diff line change
@@ -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
** See Copyright Notice in lua.h
*/
Expand All @@ -25,7 +25,10 @@
void luaD_init (lua_State *L, int stacksize);
void luaD_adjusttop (lua_State *L, StkId newtop);
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_error (lua_State *L, const char *s);
Expand Down
3 changes: 1 addition & 2 deletions lgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ static void do1gcTM (lua_State *L, Udata *udata) {
setobj(top, tm);
setuvalue(top+1, udata);
L->top += 2;
luaD_call(L, top);
L->top = top; /* restore top */
luaD_call(L, top, 0);
}
}

Expand Down
14 changes: 0 additions & 14 deletions lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,6 @@ typedef struct Table {
#define sizenode(t) (twoto((t)->lsizenode))
#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;

Expand Down
5 changes: 3 additions & 2 deletions lstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
L->_G = NULL;
L->stack = NULL;
L->stacksize = 0;
L->ci = &L->basefunc;
L->basefunc.prev = NULL;
L->errorJmp = NULL;
L->callhook = NULL;
L->linehook = NULL;
L->openupval = NULL;
L->size_ci = 0;
L->base_ci = NULL;
L->allowhooks = 1;
L->next = L->previous = L;
so.stacksize = stacksize;
Expand Down Expand Up @@ -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_freelem(NULL, L->_G);
}
luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
luaM_freearray(OL, L->stack, L->stacksize, TObject);
luaM_freelem(OL, L);
}
Expand Down
22 changes: 21 additions & 1 deletion lstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ typedef struct 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
*/
Expand Down Expand Up @@ -98,6 +116,9 @@ struct lua_State {
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */
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;
lua_Hook callhook;
lua_Hook linehook;
Expand All @@ -106,7 +127,6 @@ struct lua_State {
UpVal *openupval; /* list of open upvalues in this stack */
lua_State *next; /* circular double linked list of states */
lua_State *previous;
CallInfo basefunc;
};


Expand Down
4 changes: 2 additions & 2 deletions luadebug.h
Original file line number Diff line number Diff line change
@@ -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
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -39,7 +39,7 @@ struct lua_Debug {
const char *source; /* (S) */
char short_src[LUA_IDSIZE]; /* (S) */
/* private part */
struct CallInfo *_ci; /* active function */
int _ci; /* active function */
};


Expand Down

0 comments on commit e04f7ed

Please sign in to comment.