first implementation of centralized global state.

This commit is contained in:
Roberto Ierusalimschy
1997-11-19 15:29:23 -02:00
parent 9cdeb275e7
commit 592a3f289b
25 changed files with 782 additions and 829 deletions

247
lua.stx
View File

@@ -1,6 +1,6 @@
%{
/*
** $Id: lua.stx,v 1.16 1997/10/30 18:47:19 roberto Exp roberto $
** $Id: lua.stx,v 1.17 1997/11/07 15:09:49 roberto Exp roberto $
** Syntax analizer and code generator
** See Copyright Notice in lua.h
*/
@@ -16,13 +16,13 @@
#include "lmem.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "lua.h"
#include "luadebug.h"
#include "lzio.h"
/* to avoid warnings generated by yacc */
int luaY_parse (void);
@@ -61,7 +61,7 @@ typedef long vardesc;
#define dotindex(v) ((-(v))-1)
/* state needed to generate code for a given function */
typedef struct State {
typedef struct FuncState {
TProtoFunc *f; /* current function header */
int pc; /* next position to code */
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
@@ -75,19 +75,19 @@ typedef struct State {
int maxconsts; /* size of f->consts */
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
vardesc upvalues[MAXUPVALUES]; /* upvalues */
} State;
static State *mainState, *currState;
} FuncState;
#define YYPURE 1
void luaY_syntaxerror (char *s, char *token)
{
if (token[0] == 0)
token = "<eof>";
luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
s, token, luaX_linenumber, mainState->f->fileName->str);
s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
}
@@ -99,16 +99,16 @@ void luaY_error (char *s)
static void check_pc (int n)
{
if (currState->pc+n > currState->maxcode)
currState->maxcode = luaM_growvector(&currState->f->code,
currState->maxcode, Byte, codeEM, MAX_INT);
if (L->currState->pc+n > L->currState->maxcode)
L->currState->maxcode = luaM_growvector(&L->currState->f->code,
L->currState->maxcode, Byte, codeEM, MAX_INT);
}
static void movecode_up (int d, int s, int n)
{
while (n--)
currState->f->code[d+n] = currState->f->code[s+n];
L->currState->f->code[d+n] = L->currState->f->code[s+n];
}
@@ -116,24 +116,24 @@ static void movecode_down (int d, int s, int n)
{
int i;
for (i=0; i<n; i++)
currState->f->code[d+i] = currState->f->code[s+i];
L->currState->f->code[d+i] = L->currState->f->code[s+i];
}
static void code_byte (Byte c)
{
check_pc(1);
currState->f->code[currState->pc++] = c;
L->currState->f->code[L->currState->pc++] = c;
}
static void deltastack (int delta)
{
currState->stacksize += delta;
if (currState->stacksize > currState->maxstacksize) {
if (currState->stacksize > 255)
L->currState->stacksize += delta;
if (L->currState->stacksize > L->currState->maxstacksize) {
if (L->currState->stacksize > 255)
luaY_error("function/expression too complex (limit 256)");
currState->maxstacksize = currState->stacksize;
L->currState->maxstacksize = L->currState->stacksize;
}
}
@@ -142,18 +142,18 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
{
deltastack(delta);
if (arg < builtin) {
currState->f->code[pc] = op+1+arg;
L->currState->f->code[pc] = op+1+arg;
return 1;
}
else if (arg <= 255) {
currState->f->code[pc] = op;
currState->f->code[pc+1] = arg;
L->currState->f->code[pc] = op;
L->currState->f->code[pc+1] = arg;
return 2;
}
else if (arg <= MAX_WORD) {
currState->f->code[pc] = op+1+builtin;
currState->f->code[pc+1] = arg&0xFF;
currState->f->code[pc+2] = arg>>8;
L->currState->f->code[pc] = op+1+builtin;
L->currState->f->code[pc+1] = arg&0xFF;
L->currState->f->code[pc+2] = arg>>8;
return 3;
}
else luaY_error("code too long (limit 64K)");
@@ -164,13 +164,13 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
static int fix_opcode (int pc, OpCode op, int builtin, int arg)
{
if (arg < builtin) { /* close space */
movecode_down(pc+1, pc+2, currState->pc-(pc+2));
currState->pc--;
movecode_down(pc+1, pc+2, L->currState->pc-(pc+2));
L->currState->pc--;
}
else if (arg > 255) { /* open space */
check_pc(1);
movecode_up(pc+1, pc, currState->pc-pc);
currState->pc++;
movecode_up(pc+1, pc, L->currState->pc-pc);
L->currState->pc++;
}
return code_oparg_at(pc, op, builtin, arg, 0) - 2;
}
@@ -179,7 +179,7 @@ static int fix_opcode (int pc, OpCode op, int builtin, int arg)
static void code_oparg (OpCode op, int builtin, int arg, int delta)
{
check_pc(3); /* maximum code size */
currState->pc += code_oparg_at(currState->pc, op, builtin, arg, delta);
L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
}
@@ -214,7 +214,7 @@ static void code_constant (int c)
}
static int next_constant (State *cs)
static int next_constant (FuncState *cs)
{
TProtoFunc *f = cs->f;
if (f->nconsts >= cs->maxconsts) {
@@ -225,7 +225,7 @@ static int next_constant (State *cs)
}
static int string_constant (TaggedString *s, State *cs)
static int string_constant (TaggedString *s, FuncState *cs)
{
TProtoFunc *f = cs->f;
int c = s->constindex;
@@ -242,7 +242,7 @@ static int string_constant (TaggedString *s, State *cs)
static void code_string (TaggedString *s)
{
code_constant(string_constant(s, currState));
code_constant(string_constant(s, L->currState));
}
@@ -250,16 +250,16 @@ static void code_string (TaggedString *s)
static int real_constant (real r)
{
/* check whether 'r' has appeared within the last LIM entries */
TObject *cnt = currState->f->consts;
int c = currState->f->nconsts;
TObject *cnt = L->currState->f->consts;
int c = L->currState->f->nconsts;
int lim = c < LIM ? 0 : c-LIM;
while (--c >= lim) {
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
return c;
}
/* not found; create a luaM_new entry */
c = next_constant(currState);
cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
c = next_constant(L->currState);
cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
ttype(&cnt[c]) = LUA_T_NUMBER;
nvalue(&cnt[c]) = r;
return c;
@@ -292,13 +292,13 @@ static void flush_list (int m, int n)
static void luaI_registerlocalvar (TaggedString *varname, int line)
{
if (currState->maxvars != -1) { /* debug information? */
if (currState->nvars >= currState->maxvars)
currState->maxvars = luaM_growvector(&currState->f->locvars,
currState->maxvars, LocVar, "", MAX_WORD);
currState->f->locvars[currState->nvars].varname = varname;
currState->f->locvars[currState->nvars].line = line;
currState->nvars++;
if (L->currState->maxvars != -1) { /* debug information? */
if (L->currState->nvars >= L->currState->maxvars)
L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
L->currState->maxvars, LocVar, "", MAX_WORD);
L->currState->f->locvars[L->currState->nvars].varname = varname;
L->currState->f->locvars[L->currState->nvars].line = line;
L->currState->nvars++;
}
}
@@ -311,17 +311,17 @@ static void luaI_unregisterlocalvar (int line)
static void store_localvar (TaggedString *name, int n)
{
if (currState->nlocalvar+n < MAXLOCALS)
currState->localvar[currState->nlocalvar+n] = name;
if (L->currState->nlocalvar+n < MAXLOCALS)
L->currState->localvar[L->currState->nlocalvar+n] = name;
else
luaY_error("too many local variables (limit 32)");
luaI_registerlocalvar(name, luaX_linenumber);
luaI_registerlocalvar(name, L->lexstate->linenumber);
}
static void add_localvar (TaggedString *name)
{
store_localvar(name, 0);
currState->nlocalvar++;
L->currState->nlocalvar++;
}
@@ -342,11 +342,11 @@ static void add_varbuffer (vardesc var, int n)
{
if (n >= MAXVAR)
luaY_error("variable buffer overflow (limit 32)");
currState->varbuffer[n] = var2store(var);
L->currState->varbuffer[n] = var2store(var);
}
static int aux_localname (TaggedString *n, State *st)
static int aux_localname (TaggedString *n, FuncState *st)
{
int i;
for (i=st->nlocalvar-1; i >= 0; i--)
@@ -355,12 +355,12 @@ static int aux_localname (TaggedString *n, State *st)
}
static vardesc singlevar (TaggedString *n, State *st)
static vardesc singlevar (TaggedString *n, FuncState *st)
{
int i = aux_localname(n, st);
if (i == -1) { /* check shadowing */
int l;
for (l=1; l<=(st-mainState); l++)
for (l=1; l<=(st-L->mainState); l++)
if (aux_localname(n, st-l) >= 0)
luaY_syntaxerror("cannot access a variable in outer scope", n->str);
return string_constant(n, st)+MINGLOBAL; /* global value */
@@ -371,16 +371,16 @@ static vardesc singlevar (TaggedString *n, State *st)
static int indexupvalue (TaggedString *n)
{
vardesc v = singlevar(n, currState-1);
vardesc v = singlevar(n, L->currState-1);
int i;
for (i=0; i<currState->nupvalues; i++) {
if (currState->upvalues[i] == v)
for (i=0; i<L->currState->nupvalues; i++) {
if (L->currState->upvalues[i] == v)
return i;
}
/* new one */
if (++(currState->nupvalues) > MAXUPVALUES)
if (++(L->currState->nupvalues) > MAXUPVALUES)
luaY_error("too many upvalues in a single function (limit 16)");
currState->upvalues[i] = v; /* i = currState->nupvalues - 1 */
L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
return i;
}
@@ -388,9 +388,9 @@ static int indexupvalue (TaggedString *n)
static void pushupvalue (TaggedString *n)
{
int i;
if (currState == mainState)
if (L->currState == L->mainState)
luaY_error("cannot access upvalue in main");
if (aux_localname(n, currState) >= 0)
if (aux_localname(n, L->currState) >= 0)
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
i = indexupvalue(n);
code_oparg(PUSHUPVALUE, 2, i, 1);
@@ -399,10 +399,9 @@ static void pushupvalue (TaggedString *n)
void luaY_codedebugline (int line)
{
static int lastline = 0;
if (lua_debug && line != lastline) {
if (lua_debug && line != L->lexstate->lastline) {
code_oparg(SETLINE, 0, line, 0);
lastline = line;
L->lexstate->lastline = line;
}
}
@@ -421,10 +420,10 @@ static long adjust_functioncall (long exp, int nresults)
if (exp <= 0)
return -exp; /* exp is -list length */
else {
int temp = currState->f->code[exp];
int nparams = currState->f->code[exp-1];
int temp = L->currState->f->code[exp];
int nparams = L->currState->f->code[exp-1];
exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
currState->f->code[exp] = nparams;
L->currState->f->code[exp] = nparams;
if (nresults != MULT_RET)
deltastack(nresults);
deltastack(-(nparams+1));
@@ -436,7 +435,7 @@ static long adjust_functioncall (long exp, int nresults)
static void adjust_mult_assign (int vars, long exps)
{
if (exps > 0) { /* must correct function call */
int diff = currState->f->code[exps] - vars;
int diff = L->currState->f->code[exps] - vars;
if (diff < 0)
adjust_functioncall(exps, -diff);
else {
@@ -450,11 +449,11 @@ static void adjust_mult_assign (int vars, long exps)
static void code_args (int nparams, int dots)
{
currState->nlocalvar += nparams;
L->currState->nlocalvar += nparams;
if (!dots)
code_oparg(ARGS, 0, currState->nlocalvar, currState->nlocalvar);
code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
else {
code_oparg(VARARGS, 0, currState->nlocalvar, currState->nlocalvar+1);
code_oparg(VARARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar+1);
add_localvar(luaS_new("arg"));
}
}
@@ -487,9 +486,9 @@ static void storevar (vardesc var)
/* returns how many elements are left as 'garbage' on the stack */
static int lua_codestore (int i, int left)
{
if (currState->varbuffer[i] != 0 || /* global or local var or */
if (L->currState->varbuffer[i] != 0 || /* global or local var or */
left+i == 0) { /* indexed var without values in between */
storevar(currState->varbuffer[i]);
storevar(L->currState->varbuffer[i]);
return left;
}
else { /* indexed var with values in between*/
@@ -508,7 +507,7 @@ static int fix_jump (int pc, OpCode op, int n)
static void fix_upjmp (OpCode op, int pos)
{
int delta = currState->pc+JMPSIZE - pos; /* jump is relative */
int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
if (delta > 255) delta++;
code_oparg(op, 0, delta, 0);
}
@@ -517,38 +516,38 @@ static void fix_upjmp (OpCode op, int pos)
static void codeIf (int thenAdd, int elseAdd)
{
int elseinit = elseAdd+JMPSIZE;
if (currState->pc == elseinit) { /* no else part */
currState->pc -= JMPSIZE;
elseinit = currState->pc;
if (L->currState->pc == elseinit) { /* no else part */
L->currState->pc -= JMPSIZE;
elseinit = L->currState->pc;
}
else
elseinit += fix_jump(elseAdd, JMP, currState->pc);
elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
fix_jump(thenAdd, IFFJMP, elseinit);
}
static void code_shortcircuit (int pc, OpCode op)
{
fix_jump(pc, op, currState->pc);
fix_jump(pc, op, L->currState->pc);
}
static void codereturn (void)
{
code_oparg(RETCODE, 0, currState->nlocalvar, 0);
currState->stacksize = currState->nlocalvar;
code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
L->currState->stacksize = L->currState->nlocalvar;
}
static void func_onstack (TProtoFunc *f)
{
int i;
int nupvalues = (currState+1)->nupvalues;
int c = next_constant(currState);
ttype(&currState->f->consts[c]) = LUA_T_PROTO;
currState->f->consts[c].value.tf = (currState+1)->f;
int nupvalues = (L->currState+1)->nupvalues;
int c = next_constant(L->currState);
ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
L->currState->f->consts[c].value.tf = (L->currState+1)->f;
for (i=0; i<nupvalues; i++)
lua_pushvar((currState+1)->upvalues[i]);
lua_pushvar((L->currState+1)->upvalues[i]);
code_constant(c);
code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
}
@@ -557,48 +556,48 @@ static void func_onstack (TProtoFunc *f)
static void init_state (TaggedString *filename)
{
TProtoFunc *f = luaF_newproto();
currState->stacksize = 0;
currState->maxstacksize = 0;
currState->nlocalvar = 0;
currState->nupvalues = 0;
currState->f = f;
L->currState->stacksize = 0;
L->currState->maxstacksize = 0;
L->currState->nlocalvar = 0;
L->currState->nupvalues = 0;
L->currState->f = f;
f->fileName = filename;
currState->pc = 0;
currState->maxcode = 0;
L->currState->pc = 0;
L->currState->maxcode = 0;
f->code = NULL;
currState->maxconsts = 0;
L->currState->maxconsts = 0;
if (lua_debug) {
currState->nvars = 0;
currState->maxvars = 0;
L->currState->nvars = 0;
L->currState->maxvars = 0;
}
else
currState->maxvars = -1; /* flag no debug information */
L->currState->maxvars = -1; /* flag no debug information */
code_byte(0); /* to be filled with stacksize */
}
static void init_func (void)
{
if (currState-mainState >= MAXSTATES-1)
if (L->currState-L->mainState >= MAXSTATES-1)
luaY_error("too many nested functions (limit 6)");
currState++;
init_state(mainState->f->fileName);
luaY_codedebugline(luaX_linenumber);
currState->f->lineDefined = luaX_linenumber;
L->currState++;
init_state(L->mainState->f->fileName);
luaY_codedebugline(L->lexstate->linenumber);
L->currState->f->lineDefined = L->lexstate->linenumber;
}
static TProtoFunc *close_func (void)
{
TProtoFunc *f = currState->f;
TProtoFunc *f = L->currState->f;
code_neutralop(ENDCODE);
f->code[0] = currState->maxstacksize;
f->code = luaM_reallocvector(f->code, currState->pc, Byte);
f->code[0] = L->currState->maxstacksize;
f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
if (currState->maxvars != -1) { /* debug information? */
if (L->currState->maxvars != -1) { /* debug information? */
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar);
f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
}
currState--;
L->currState--;
return f;
}
@@ -608,8 +607,10 @@ static TProtoFunc *close_func (void)
*/
TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
{
State state[MAXSTATES];
currState = mainState = &state[0];
struct LexState lexstate;
FuncState state[MAXSTATES];
L->currState = L->mainState = &state[0];
L->lexstate = &lexstate;
luaX_setinput(z);
init_state(luaS_new(chunkname));
if (luaY_parse ()) lua_error("parse error");
@@ -685,10 +686,10 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
int expsize = $3-$2;
int newpos = $2+JMPSIZE;
check_pc(expsize);
memcpy(&currState->f->code[currState->pc],
&currState->f->code[$2], expsize);
movecode_down($2, $3, currState->pc-$2);
newpos += fix_jump($2, JMP, currState->pc-expsize);
memcpy(&L->currState->f->code[L->currState->pc],
&L->currState->f->code[$2], expsize);
movecode_down($2, $3, L->currState->pc-$2);
newpos += fix_jump($2, JMP, L->currState->pc-expsize);
fix_upjmp(IFTUPJMP, newpos);
}}
@@ -712,18 +713,18 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
| LOCAL localnamelist decinit
{
currState->nlocalvar += $2;
L->currState->nlocalvar += $2;
adjust_mult_assign($2, $3);
}
| FUNCTION funcname body { func_onstack($3); storevar($2); }
;
block : {$<vInt>$ = currState->nlocalvar;} chunk
block : {$<vInt>$ = L->currState->nlocalvar;} chunk
{
adjuststack(currState->nlocalvar - $<vInt>1);
for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
luaI_unregisterlocalvar(luaX_linenumber);
adjuststack(L->currState->nlocalvar - $<vInt>1);
for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
luaI_unregisterlocalvar(L->lexstate->linenumber);
}
;
@@ -753,13 +754,13 @@ ret : /* empty */
}
;
GetPC : /* empty */ { $$ = currState->pc; }
GetPC : /* empty */ { $$ = L->currState->pc; }
;
SaveWord : /* empty */
{ $$ = currState->pc;
{ $$ = L->currState->pc;
check_pc(JMPSIZE);
currState->pc += JMPSIZE; /* open space */
L->currState->pc += JMPSIZE; /* open space */
}
;
@@ -808,7 +809,7 @@ functioncall : funcvalue funcParams
{
code_byte(0); /* save space for opcode */
code_byte($1+$2); /* number of parameters */
$$ = currState->pc;
$$ = L->currState->pc;
code_byte(0); /* must be adjusted by other rules */
}
;
@@ -816,7 +817,7 @@ functioncall : funcvalue funcParams
funcvalue : varexp { $$ = 0; }
| varexp ':' NAME
{
code_oparg(PUSHSELF, 0, string_constant($3, currState), 1);
code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
$$ = 1;
}
;
@@ -834,7 +835,7 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
{
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
else {
currState->f->code[$4] = $<vLong>3; /* store list length */
L->currState->f->code[$4] = $<vLong>3; /* store list length */
$$ = $4;
}
}
@@ -899,9 +900,9 @@ varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
| varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
;
var : NAME { $$ = singlevar($1, currState); }
var : NAME { $$ = singlevar($1, L->currState); }
| varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
| varexp '.' NAME { $$ = (-string_constant($3, currState))-1; }
| varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
;
varexp : var { lua_pushvar($1); }