small changes to reduce stack usage by the parser

This commit is contained in:
Roberto Ierusalimschy 2003-07-09 12:36:38 -03:00
parent 66ed154c89
commit 00180bb133
2 changed files with 36 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 1.210 2003/05/14 12:32:46 roberto Exp roberto $ ** $Id: lparser.c,v 1.211 2003/05/14 21:02:39 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -39,9 +39,9 @@
typedef struct BlockCnt { typedef struct BlockCnt {
struct BlockCnt *previous; /* chain */ struct BlockCnt *previous; /* chain */
int breaklist; /* list of jumps out of this loop */ int breaklist; /* list of jumps out of this loop */
int nactvar; /* # active local variables outside the breakable structure */ lu_byte nactvar; /* # active locals outside the breakable structure */
int upval; /* true if some variable in the block is an upvalue */ lu_byte upval; /* true if some variable in the block is an upvalue */
int isbreakable; /* true if `block' is a loop */ lu_byte isbreakable; /* true if `block' is a loop */
} BlockCnt; } BlockCnt;
@ -187,7 +187,8 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues, luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues,
TString *, MAX_INT, ""); TString *, MAX_INT, "");
fs->f->upvalues[f->nups] = name; fs->f->upvalues[f->nups] = name;
fs->upvalues[f->nups] = *v; fs->upvalues[f->nups].k = v->k;
fs->upvalues[f->nups].info = v->info;
return f->nups++; return f->nups++;
} }
@ -266,7 +267,7 @@ static void code_params (LexState *ls, int nparams, TString *dots) {
Proto *f = fs->f; Proto *f = fs->f;
adjustlocalvars(ls, nparams); adjustlocalvars(ls, nparams);
luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters"); luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters");
f->numparams = cast(lu_byte, fs->nactvar); f->numparams = fs->nactvar;
if (!dots) if (!dots)
f->is_vararg = 0; f->is_vararg = 0;
else { else {
@ -925,12 +926,14 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
} }
static void cond (LexState *ls, expdesc *v) { static int cond (LexState *ls) {
/* cond -> exp */ /* cond -> exp */
expr(ls, v); /* read condition */ expdesc v;
if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */ expr(ls, &v); /* read condition */
luaK_goiftrue(ls->fs, v); if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
luaK_patchtohere(ls->fs, v->t); luaK_goiftrue(ls->fs, &v);
luaK_patchtohere(ls->fs, v.t);
return v.f;
} }
@ -998,14 +1001,14 @@ static void repeatstat (LexState *ls, int line) {
/* repeatstat -> REPEAT block UNTIL cond */ /* repeatstat -> REPEAT block UNTIL cond */
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
int repeat_init = luaK_getlabel(fs); int repeat_init = luaK_getlabel(fs);
expdesc v; int flist;
BlockCnt bl; BlockCnt bl;
enterblock(fs, &bl, 1); enterblock(fs, &bl, 1);
next(ls); next(ls);
block(ls); block(ls);
check_match(ls, TK_UNTIL, TK_REPEAT, line); check_match(ls, TK_UNTIL, TK_REPEAT, line);
cond(ls, &v); flist = cond(ls);
luaK_patchlist(fs, v.f, repeat_init); luaK_patchlist(fs, flist, repeat_init);
leaveblock(fs); leaveblock(fs);
} }
@ -1104,34 +1107,36 @@ static void forstat (LexState *ls, int line) {
} }
static void test_then_block (LexState *ls, expdesc *v) { static int test_then_block (LexState *ls) {
/* test_then_block -> [IF | ELSEIF] cond THEN block */ /* test_then_block -> [IF | ELSEIF] cond THEN block */
int flist;
next(ls); /* skip IF or ELSEIF */ next(ls); /* skip IF or ELSEIF */
cond(ls, v); flist = cond(ls);
check(ls, TK_THEN); check(ls, TK_THEN);
block(ls); /* `then' part */ block(ls); /* `then' part */
return flist;
} }
static void ifstat (LexState *ls, int line) { static void ifstat (LexState *ls, int line) {
/* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
expdesc v; int flist;
int escapelist = NO_JUMP; int escapelist = NO_JUMP;
test_then_block(ls, &v); /* IF cond THEN block */ flist = test_then_block(ls); /* IF cond THEN block */
while (ls->t.token == TK_ELSEIF) { while (ls->t.token == TK_ELSEIF) {
luaK_concat(fs, &escapelist, luaK_jump(fs)); luaK_concat(fs, &escapelist, luaK_jump(fs));
luaK_patchtohere(fs, v.f); luaK_patchtohere(fs, flist);
test_then_block(ls, &v); /* ELSEIF cond THEN block */ flist = test_then_block(ls); /* ELSEIF cond THEN block */
} }
if (ls->t.token == TK_ELSE) { if (ls->t.token == TK_ELSE) {
luaK_concat(fs, &escapelist, luaK_jump(fs)); luaK_concat(fs, &escapelist, luaK_jump(fs));
luaK_patchtohere(fs, v.f); luaK_patchtohere(fs, flist);
next(ls); /* skip ELSE (after patch, for correct line info) */ next(ls); /* skip ELSE (after patch, for correct line info) */
block(ls); /* `else' part */ block(ls); /* `else' part */
} }
else else
luaK_concat(fs, &escapelist, v.f); luaK_concat(fs, &escapelist, flist);
luaK_patchtohere(fs, escapelist); luaK_patchtohere(fs, escapelist);
check_match(ls, TK_END, TK_IF, line); check_match(ls, TK_END, TK_IF, line);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.46 2002/12/19 11:11:55 roberto Exp roberto $ ** $Id: lparser.h,v 1.47 2003/02/11 10:46:24 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -41,6 +41,12 @@ typedef struct expdesc {
} expdesc; } expdesc;
typedef struct upvaldesc {
expkind k;
int info;
} upvaldesc;
struct BlockCnt; /* defined in lparser.c */ struct BlockCnt; /* defined in lparser.c */
@ -59,8 +65,8 @@ typedef struct FuncState {
int nk; /* number of elements in `k' */ int nk; /* number of elements in `k' */
int np; /* number of elements in `p' */ int np; /* number of elements in `p' */
int nlocvars; /* number of elements in `locvars' */ int nlocvars; /* number of elements in `locvars' */
int nactvar; /* number of active local variables */ lu_byte nactvar; /* number of active local variables */
expdesc upvalues[MAXUPVALUES]; /* upvalues */ upvaldesc upvalues[MAXUPVALUES]; /* upvalues */
int actvar[MAXVARS]; /* declared-variable stack */ int actvar[MAXVARS]; /* declared-variable stack */
} FuncState; } FuncState;