new way to code globals, using const table instead of putting global

index inside the opcode.
This commit is contained in:
Roberto Ierusalimschy 1997-09-19 18:17:52 -03:00
parent dfe03c7abe
commit 2079cfe8fa
3 changed files with 56 additions and 32 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -35,7 +35,8 @@ PUSHLOCAL7,/* - LOC[7] */
PUSHLOCAL8,/* - LOC[8] */ PUSHLOCAL8,/* - LOC[8] */
PUSHLOCAL9,/* - LOC[9] */ PUSHLOCAL9,/* - LOC[9] */
PUSHLOCAL,/* b - LOC[b] */ PUSHLOCAL,/* b - LOC[b] */
PUSHGLOBAL,/* w - VAR[w] */ PUSHGLOBALB,/* b - VAR[CNST[b]] */
PUSHGLOBAL,/* w - VAR[CNST[w]] */
GETTABLE,/* i t t[i] */ GETTABLE,/* i t t[i] */
PUSHSELF,/* w t t t[CNST[w]] */ PUSHSELF,/* w t t t[CNST[w]] */
CREATEARRAY,/* w - newarray(size = w) */ CREATEARRAY,/* w - newarray(size = w) */
@ -51,7 +52,8 @@ SETLOCAL7,/* x - LOC[7]=x */
SETLOCAL8,/* x - LOC[8]=x */ SETLOCAL8,/* x - LOC[8]=x */
SETLOCAL9,/* x - LOC[9]=x */ SETLOCAL9,/* x - LOC[9]=x */
SETLOCAL,/* b x - LOC[b]=x */ SETLOCAL,/* b x - LOC[b]=x */
SETGLOBAL,/* w x - VAR[w]=x */ SETGLOBALB,/* b x - VAR[CNST[b]]=x */
SETGLOBAL,/* w x - VAR[CNST[w]]=x */
SETTABLE0,/* v i t - t[i]=v */ SETTABLE0,/* v i t - t[i]=v */
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */ SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
SETLIST0,/* b v_b...v_1 t - t[i]=v_i */ SETLIST0,/* b v_b...v_1 t - t[i]=v_i */

58
lua.stx
View File

@ -1,6 +1,6 @@
%{ %{
/* /*
** $Id: lua.stx,v 1.1 1997/09/16 19:33:21 roberto Exp roberto $ ** $Id: lua.stx,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $
** Syntax analizer and code generator ** Syntax analizer and code generator
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,7 +11,6 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "ldo.h" #include "ldo.h"
#include "lfunc.h" #include "lfunc.h"
#include "lglobal.h"
#include "llex.h" #include "llex.h"
#include "lmem.h" #include "lmem.h"
#include "lopcodes.h" #include "lopcodes.h"
@ -51,7 +50,7 @@ typedef long vardesc;
/* state needed to generate code for a given function */ /* state needed to generate code for a given function */
static struct State { typedef struct State {
TProtoFunc *f; /* current function header */ TProtoFunc *f; /* current function header */
int pc; /* next position to code */ int pc; /* next position to code */
TaggedString *localvar[MAXLOCALS]; /* store local variable names */ TaggedString *localvar[MAXLOCALS]; /* store local variable names */
@ -64,7 +63,9 @@ static struct State {
int maxconsts; /* size of f->consts */ int maxconsts; /* size of f->consts */
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */ vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
vardesc upvalues[MAXUPVALUES]; /* upvalues */ vardesc upvalues[MAXUPVALUES]; /* upvalues */
} *mainState, *currState; } State;
static State *mainState, *currState;
@ -160,25 +161,24 @@ static void code_constant (int c)
} }
static int next_constant (void) static int next_constant (State *cs)
{ {
TProtoFunc *f = currState->f; TProtoFunc *f = cs->f;
if (f->nconsts >= currState->maxconsts) { if (f->nconsts >= cs->maxconsts) {
currState->maxconsts = cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
luaM_growvector(&f->consts, currState->maxconsts, TObject,
constantEM, MAX_WORD); constantEM, MAX_WORD);
} }
return f->nconsts++; return f->nconsts++;
} }
static int string_constant (TaggedString *s) static int string_constant (TaggedString *s, State *cs)
{ {
TProtoFunc *f = currState->f; TProtoFunc *f = cs->f;
int c = s->u.s.constindex; int c = s->u.s.constindex;
if (!(0 <= c && c < f->nconsts && if (!(0 <= c && c < f->nconsts &&
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
c = next_constant(); c = next_constant(cs);
ttype(&f->consts[c]) = LUA_T_STRING; ttype(&f->consts[c]) = LUA_T_STRING;
tsvalue(&f->consts[c]) = s; tsvalue(&f->consts[c]) = s;
s->u.s.constindex = c; /* hint for next time */ s->u.s.constindex = c; /* hint for next time */
@ -189,7 +189,7 @@ static int string_constant (TaggedString *s)
static void code_string (TaggedString *s) static void code_string (TaggedString *s)
{ {
code_constant(string_constant(s)); code_constant(string_constant(s, currState));
} }
@ -205,7 +205,7 @@ static int real_constant (real r)
return c; return c;
} }
/* not found; create a luaM_new entry */ /* not found; create a luaM_new entry */
c = next_constant(); c = next_constant(currState);
cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
ttype(&cnt[c]) = LUA_T_NUMBER; ttype(&cnt[c]) = LUA_T_NUMBER;
nvalue(&cnt[c]) = r; nvalue(&cnt[c]) = r;
@ -297,7 +297,7 @@ static void add_varbuffer (vardesc var, int n)
} }
static int aux_localname (TaggedString *n, struct State *st) static int aux_localname (TaggedString *n, State *st)
{ {
int i; int i;
for (i=st->nlocalvar-1; i >= 0; i--) for (i=st->nlocalvar-1; i >= 0; i--)
@ -306,7 +306,7 @@ static int aux_localname (TaggedString *n, struct State *st)
} }
static vardesc singlevar (TaggedString *n, struct State *st) static vardesc singlevar (TaggedString *n, State *st)
{ {
int i = aux_localname(n, st); int i = aux_localname(n, st);
if (i == -1) { /* check shadowing */ if (i == -1) { /* check shadowing */
@ -314,7 +314,7 @@ static vardesc singlevar (TaggedString *n, struct State *st)
for (l=1; l<=(st-mainState); l++) for (l=1; l<=(st-mainState); l++)
if (aux_localname(n, st-l) >= 0) if (aux_localname(n, st-l) >= 0)
luaY_syntaxerror("cannot access a variable in outer scope", n->str); luaY_syntaxerror("cannot access a variable in outer scope", n->str);
return luaG_findsymbol(n)+1; /* positive value */ return string_constant(n, st)+1; /* positive value */
} }
else return -(i+1); /* negative value */ else return -(i+1); /* negative value */
} }
@ -427,8 +427,15 @@ static void code_args (int dots)
static void lua_pushvar (vardesc number) static void lua_pushvar (vardesc number)
{ {
if (number > 0) { /* global var */ if (number > 0) { /* global var */
number--;
if (number <= 255) {
code_push(PUSHGLOBALB);
code_byte(number);
}
else {
code_push(PUSHGLOBAL); code_push(PUSHGLOBAL);
code_word(number-1); code_word(number);
}
} }
else if (number < 0) { /* local var */ else if (number < 0) { /* local var */
number = (-number) - 1; number = (-number) - 1;
@ -450,8 +457,15 @@ static void storevar (vardesc number)
if (number == 0) /* indexed var */ if (number == 0) /* indexed var */
code_opcode(SETTABLE0, -3); code_opcode(SETTABLE0, -3);
else if (number > 0) { /* global var */ else if (number > 0) { /* global var */
number--;
if (number <= 255) {
code_pop(SETGLOBALB);
code_byte(number);
}
else {
code_pop(SETGLOBAL); code_pop(SETGLOBAL);
code_word(number-1); code_word(number);
}
} }
else { /* number < 0 - local var */ else { /* number < 0 - local var */
number = (-number) - 1; number = (-number) - 1;
@ -516,7 +530,7 @@ static void func_onstack (TProtoFunc *f)
{ {
int i; int i;
int nupvalues = (currState+1)->f->nupvalues; int nupvalues = (currState+1)->f->nupvalues;
int c = next_constant(); int c = next_constant(currState);
ttype(&currState->f->consts[c]) = LUA_T_PROTO; ttype(&currState->f->consts[c]) = LUA_T_PROTO;
currState->f->consts[c].value.tf = (currState+1)->f; currState->f->consts[c].value.tf = (currState+1)->f;
for (i=0; i<nupvalues; i++) for (i=0; i<nupvalues; i++)
@ -581,7 +595,7 @@ static TProtoFunc *close_func (void)
*/ */
TProtoFunc *luaY_parser (ZIO *z, char *chunkname) TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
{ {
struct State state[MAXSTATES]; State state[MAXSTATES];
currState = mainState = &state[0]; currState = mainState = &state[0];
luaX_setinput(z); luaX_setinput(z);
init_state(luaS_new(chunkname)); init_state(luaS_new(chunkname));
@ -788,7 +802,7 @@ funcvalue : varexp { $$ = 0; }
| varexp ':' NAME | varexp ':' NAME
{ {
code_push(PUSHSELF); code_push(PUSHSELF);
code_word(string_constant($3)); code_word(string_constant($3, currState));
$$ = 1; $$ = 1;
} }
; ;

14
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: lvm.c,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -326,8 +326,12 @@ StkId luaV_execute (Closure *cl, StkId base)
*luaD_stack.top++ = *((luaD_stack.stack+base) + (*pc++)); *luaD_stack.top++ = *((luaD_stack.stack+base) + (*pc++));
break; break;
case PUSHGLOBALB:
luaV_getglobal(luaG_findsymbol(tsvalue(&func->consts[*pc++])));
break;
case PUSHGLOBAL: case PUSHGLOBAL:
luaV_getglobal(get_word(pc)); luaV_getglobal(luaG_findsymbol(tsvalue(&func->consts[get_word(pc)])));
break; break;
case GETTABLE: case GETTABLE:
@ -369,8 +373,12 @@ StkId luaV_execute (Closure *cl, StkId base)
case SETLOCAL: case SETLOCAL:
*((luaD_stack.stack+base) + (*pc++)) = *(--luaD_stack.top); break; *((luaD_stack.stack+base) + (*pc++)) = *(--luaD_stack.top); break;
case SETGLOBALB:
luaV_setglobal(luaG_findsymbol(tsvalue(&func->consts[*pc++])));
break;
case SETGLOBAL: case SETGLOBAL:
luaV_setglobal(get_word(pc)); luaV_setglobal(luaG_findsymbol(tsvalue(&func->consts[get_word(pc)])));
break; break;
case SETTABLE0: case SETTABLE0: