small optimizations; try to find previous use when coding a real constant.

This commit is contained in:
Roberto Ierusalimschy 1997-07-31 17:46:59 -03:00
parent e8292f076d
commit abbf14cd32
1 changed files with 45 additions and 37 deletions

82
lua.stx
View File

@ -1,6 +1,6 @@
%{ %{
char *rcs_luastx = "$Id: lua.stx,v 3.48 1997/07/29 20:38:45 roberto Exp roberto $"; char *rcs_luastx = "$Id: lua.stx,v 3.49 1997/07/30 22:00:50 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
@ -50,8 +50,6 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list;
static int nvarbuffer=0; /* number of variables at a list */ static int nvarbuffer=0; /* number of variables at a list */
#define MAXFIELDS FIELDS_PER_FLUSH*2
int lua_debug = 0; int lua_debug = 0;
/* Internal functions */ /* Internal functions */
@ -106,65 +104,75 @@ static void code_constant (int c)
static int next_constant (void) static int next_constant (void)
{ {
if (currState->f->nconsts >= currState->maxconsts) { TFunc *f = currState->f;
if (f->nconsts >= currState->maxconsts) {
currState->maxconsts = currState->maxconsts =
growvector(&currState->f->consts, currState->maxconsts, growvector(&f->consts, currState->maxconsts, TObject,
TObject, constantEM, MAX_WORD); constantEM, MAX_WORD);
} }
return currState->f->nconsts++; return f->nconsts++;
} }
static int string_constant (TaggedString *s) static int string_constant (TaggedString *s)
{ {
TFunc *f = currState->f;
int c = s->u.s.constindex; int c = s->u.s.constindex;
if (!(0 <= c && c < currState->f->nconsts && if (!(0 <= c && c < f->nconsts &&
ttype(&currState->f->consts[c]) == LUA_T_STRING && ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
tsvalue(&currState->f->consts[c]) == s)) {
c = next_constant(); c = next_constant();
ttype(&currState->f->consts[c]) = LUA_T_STRING; ttype(&f->consts[c]) = LUA_T_STRING;
tsvalue(&currState->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 */
luaI_releasestring(s);
} }
luaI_releasestring(s);
return c; return c;
} }
static void code_string (TaggedString *s) static void code_string (TaggedString *s)
{ {
int c = string_constant(s); code_constant(string_constant(s));
code_constant(c);
} }
static void code_float (real n)
#define LIM 10
static int real_constant (real r)
{ {
int c = next_constant(); /* check whether 'r' has appeared within the last LIM entries */
ttype(&currState->f->consts[c]) = LUA_T_NUMBER; TObject *cnt = currState->f->consts;
nvalue(&currState->f->consts[c]) = n; int c = currState->f->nconsts;
code_constant(c); 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 new entry */
c = next_constant();
cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
ttype(&cnt[c]) = LUA_T_NUMBER;
nvalue(&cnt[c]) = r;
return c;
} }
static void code_number (float f) static void code_number (real f)
{ {
Word i; Word i;
if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) {
/* f has an (short) integer value */ /* f has an (short) integer value */
if (i <= 2) code_byte(PUSH0 + i); if (i <= 2) code_byte(PUSH0 + i);
else if (i <= 255) else if (i <= 255) {
{ code_byte(PUSHBYTE);
code_byte(PUSHBYTE); code_byte(i);
code_byte(i); }
} else {
else code_byte(PUSHWORD);
{ code_word(i);
code_byte(PUSHWORD); }
code_word(i);
}
} }
else else
code_float(f); code_constant(real_constant(f));
} }
@ -483,7 +491,7 @@ void lua_parse (TFunc *tf)
%union %union
{ {
int vInt; int vInt;
float vFloat; real vReal;
char *pChar; char *pChar;
Long vLong; Long vLong;
TaggedString *pTStr; TaggedString *pTStr;
@ -498,7 +506,7 @@ void lua_parse (TFunc *tf)
%token LOCAL %token LOCAL
%token FUNCTION %token FUNCTION
%token DOTS %token DOTS
%token <vFloat> NUMBER %token <vReal> NUMBER
%token <pTStr> NAME STRING %token <pTStr> NAME STRING
%type <vLong> PrepJump %type <vLong> PrepJump