`const' array in protos breaked in 3 arrays (for strings, numbers, and

prototypes).
This commit is contained in:
Roberto Ierusalimschy 2000-01-25 11:57:18 -02:00
parent 99e340b2ba
commit d11e5adf55
7 changed files with 86 additions and 80 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.4 1999/12/30 18:28:40 roberto Exp roberto $ ** $Id: ldebug.c,v 1.5 2000/01/19 12:00:45 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -94,8 +94,7 @@ static const char *luaG_getname (lua_State *L, const char **name, StkId top) {
if (ttype(f) == LUA_T_LCLMARK) if (ttype(f) == LUA_T_LCLMARK)
f = protovalue(f); f = protovalue(f);
LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function"); LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function");
LUA_ASSERT(L, ttype(&tfvalue(f)->consts[i]) == LUA_T_STRING, ""); *name = tfvalue(f)->strcnst[i]->str;
*name = tsvalue(&tfvalue(f)->consts[i])->str;
return luaO_typename(f+2); return luaO_typename(f+2);
} }
} }

14
lfunc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 1.15 1999/11/22 13:12:07 roberto Exp roberto $ ** $Id: lfunc.c,v 1.16 1999/12/27 17:33:22 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -34,8 +34,12 @@ TProtoFunc *luaF_newproto (lua_State *L) {
f->code = NULL; f->code = NULL;
f->lineDefined = 0; f->lineDefined = 0;
f->source = NULL; f->source = NULL;
f->consts = NULL; f->strcnst = NULL;
f->nconsts = 0; f->nstrcnst = 0;
f->numcnst = NULL;
f->nnumcnst = 0;
f->protocnst = NULL;
f->nprotocnst = 0;
f->locvars = NULL; f->locvars = NULL;
f->next = L->rootproto; f->next = L->rootproto;
L->rootproto = f; L->rootproto = f;
@ -49,7 +53,9 @@ void luaF_freeproto (lua_State *L, TProtoFunc *f) {
L->nblocks -= gcsizeproto(L, f); L->nblocks -= gcsizeproto(L, f);
luaM_free(L, f->code); luaM_free(L, f->code);
luaM_free(L, f->locvars); luaM_free(L, f->locvars);
luaM_free(L, f->consts); luaM_free(L, f->strcnst);
luaM_free(L, f->numcnst);
luaM_free(L, f->protocnst);
luaM_free(L, f); luaM_free(L, f);
} }

8
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 1.38 1999/12/23 18:19:57 roberto Exp roberto $ ** $Id: lgc.c,v 1.39 1999/12/27 17:33:22 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -33,8 +33,10 @@ static void protomark (lua_State *L, TProtoFunc *f) {
int i; int i;
f->marked = 1; f->marked = 1;
strmark(L, f->source); strmark(L, f->source);
for (i=f->nconsts-1; i>=0; i--) for (i=f->nstrcnst-1; i>=0; i--)
markobject(L, &f->consts[i]); strmark(L, f->strcnst[i]);
for (i=f->nprotocnst-1; i>=0; i--)
protomark(L, f->protocnst[i]);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.42 1999/12/27 17:33:22 roberto Exp roberto $ ** $Id: lobject.h,v 1.43 1999/12/29 16:31:15 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -154,14 +154,19 @@ typedef struct TaggedString {
typedef struct TProtoFunc { typedef struct TProtoFunc {
struct TProtoFunc *next; struct TProtoFunc *next;
int marked; int marked;
struct TObject *consts; struct TaggedString **strcnst;
int nconsts; int nstrcnst;
real *numcnst;
int nnumcnst;
struct TProtoFunc **protocnst;
int nprotocnst;
Byte *code; /* ends with opcode ENDCODE */ Byte *code; /* ends with opcode ENDCODE */
int lineDefined; int lineDefined;
TaggedString *source; TaggedString *source;
struct LocVar *locvars; /* ends with line = -1 */ struct LocVar *locvars; /* ends with line = -1 */
} TProtoFunc; } TProtoFunc;
typedef struct LocVar { typedef struct LocVar {
TaggedString *varname; /* NULL signals end of scope */ TaggedString *varname; /* NULL signals end of scope */
int line; int line;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.35 1999/12/27 17:33:22 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.36 1999/12/29 16:31:15 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
*/ */
@ -33,8 +33,10 @@ PUSHNUMBER,/* b - (float)b */
PUSHNUMBERNEGW,/* w - (float)-w */ PUSHNUMBERNEGW,/* w - (float)-w */
PUSHNUMBERNEG,/* b - (float)-b */ PUSHNUMBERNEG,/* b - (float)-b */
PUSHCONSTANTW,/*w - CNST[w] */ PUSHSTRCNSTW,/* w - STRCNST[w] */
PUSHCONSTANT,/* b - CNST[b] */ PUSHSTRCNST,/* b - STRCNST[b] */
PUSHNUMCNSTW,/* w - NUMCNST[w] */
PUSHNUMCNST,/* b - NUMCNST[b] */
PUSHUPVALUE,/* b - Closure[b] */ PUSHUPVALUE,/* b - Closure[b] */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 1.53 2000/01/10 17:34:38 roberto Exp roberto $ ** $Id: lparser.c,v 1.54 2000/01/12 16:24:39 roberto Exp roberto $
** LL(1) Parser and code generator for Lua ** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -209,33 +209,24 @@ static void code_opcode (LexState *ls, OpCode op, int delta) {
} }
static void code_constant (LexState *ls, int c) { static void code_strcnst (LexState *ls, int c) {
code_oparg(ls, PUSHCONSTANT, c, 1); code_oparg(ls, PUSHSTRCNST, c, 1);
} }
static void assertglobal (LexState *ls, int index) { static void assertglobal (LexState *ls, int index) {
TObject *o = &ls->fs->f->consts[index]; luaS_assertglobal(ls->L, ls->fs->f->strcnst[index]);
LUA_ASSERT(ls->L, ttype(o) == LUA_T_STRING, "global name is not a string");
luaS_assertglobal(ls->L, tsvalue(o));
}
static int next_constant (LexState *ls, TProtoFunc *f) {
luaM_growvector(ls->L, f->consts, f->nconsts, 1,
TObject, constantEM, MAX_ARG);
return f->nconsts++;
} }
static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) { static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
TProtoFunc *f = fs->f; TProtoFunc *f = fs->f;
int c = s->constindex; int c = s->constindex;
if (!(c < f->nconsts && if (c >= f->nstrcnst || f->strcnst[c] != s) {
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { luaM_growvector(ls->L, f->strcnst, f->nstrcnst, 1,
c = next_constant(ls, f); TaggedString *, constantEM, MAX_ARG);
ttype(&f->consts[c]) = LUA_T_STRING; c = f->nstrcnst++;
tsvalue(&f->consts[c]) = s; f->strcnst[c] = s;
s->constindex = c; /* hint for next time */ s->constindex = c; /* hint for next time */
} }
return c; return c;
@ -243,7 +234,7 @@ static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
static void code_string (LexState *ls, TaggedString *s) { static void code_string (LexState *ls, TaggedString *s) {
code_constant(ls, string_constant(ls, ls->fs, s)); code_strcnst(ls, string_constant(ls, ls->fs, s));
} }
@ -251,18 +242,15 @@ static void code_string (LexState *ls, TaggedString *s) {
static int real_constant (LexState *ls, real r) { static int real_constant (LexState *ls, real r) {
/* check whether `r' has appeared within the last LIM entries */ /* check whether `r' has appeared within the last LIM entries */
TProtoFunc *f = ls->fs->f; TProtoFunc *f = ls->fs->f;
TObject *cnt = f->consts; int c = f->nnumcnst;
int c = f->nconsts;
int lim = c < LIM ? 0 : c-LIM; int lim = c < LIM ? 0 : c-LIM;
while (--c >= lim) { while (--c >= lim)
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) if (f->numcnst[c] == r) return c;
return c;
}
/* not found; create a new entry */ /* not found; create a new entry */
c = next_constant(ls, f); luaM_growvector(ls->L, f->numcnst, f->nnumcnst, 1,
cnt = f->consts; /* `next_constant' may reallocate this vector */ real, constantEM, MAX_ARG);
ttype(&cnt[c]) = LUA_T_NUMBER; c = f->nnumcnst++;
nvalue(&cnt[c]) = r; f->numcnst[c] = r;
return c; return c;
} }
@ -274,7 +262,7 @@ static void code_number (LexState *ls, real f) {
code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1); code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1);
} }
else else
code_constant(ls, real_constant(ls, f)); code_oparg(ls, PUSHNUMCNST, real_constant(ls, f), 1);
} }
@ -476,7 +464,7 @@ static void code_args (LexState *ls, int nparams, int dots) {
static void unloaddot (LexState *ls, vardesc *v) { static void unloaddot (LexState *ls, vardesc *v) {
/* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */ /* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */
if (v->k == VDOT) { if (v->k == VDOT) {
code_constant(ls, v->info); code_strcnst(ls, v->info);
v->k = VINDEXED; v->k = VINDEXED;
} }
} }
@ -556,19 +544,16 @@ static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
static void func_onstack (LexState *ls, FuncState *func) { static void func_onstack (LexState *ls, FuncState *func) {
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
TProtoFunc *f = fs->f;
int i; int i;
int c = next_constant(ls, fs->f); luaM_growvector(ls->L, f->protocnst, f->nprotocnst, 1,
ttype(&fs->f->consts[c]) = LUA_T_LPROTO; TProtoFunc *, constantEM, MAX_ARG);
fs->f->consts[c].value.tf = func->f; f->protocnst[f->nprotocnst] = func->f;
if (func->nupvalues == 0) for (i=0; i<func->nupvalues; i++)
code_constant(ls, c); lua_pushvar(ls, &func->upvalues[i]);
else { deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */
for (i=0; i<func->nupvalues; i++) code_oparg(ls, CLOSURE, f->nprotocnst++, -func->nupvalues);
lua_pushvar(ls, &func->upvalues[i]); code_byte(ls, (Byte)func->nupvalues);
deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */
code_oparg(ls, CLOSURE, c, -func->nupvalues);
code_byte(ls, (Byte)func->nupvalues);
}
} }
@ -602,7 +587,9 @@ static void close_func (LexState *ls) {
code_opcode(ls, ENDCODE, 0); code_opcode(ls, ENDCODE, 0);
f->code[0] = (Byte)fs->maxstacksize; f->code[0] = (Byte)fs->maxstacksize;
luaM_reallocvector(ls->L, f->code, fs->pc, Byte); luaM_reallocvector(ls->L, f->code, fs->pc, Byte);
luaM_reallocvector(ls->L, f->consts, f->nconsts, TObject); luaM_reallocvector(ls->L, f->strcnst, f->nstrcnst, TaggedString *);
luaM_reallocvector(ls->L, f->numcnst, f->nnumcnst, real);
luaM_reallocvector(ls->L, f->protocnst, f->nprotocnst, TProtoFunc *);
if (fs->nvars != -1) { /* debug information? */ if (fs->nvars != -1) { /* debug information? */
luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar); luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
@ -683,7 +670,7 @@ static int checkname (LexState *ls) {
static TaggedString *str_checkname (LexState *ls) { static TaggedString *str_checkname (LexState *ls) {
int i = checkname(ls); /* this call may realloc `f->consts' */ int i = checkname(ls); /* this call may realloc `f->consts' */
return tsvalue(&ls->fs->f->consts[i]); return ls->fs->f->strcnst[i];
} }
@ -875,7 +862,7 @@ static void recfield (LexState *ls) {
/* recfield -> (NAME | '['exp1']') = exp1 */ /* recfield -> (NAME | '['exp1']') = exp1 */
switch (ls->token) { switch (ls->token) {
case NAME: case NAME:
code_constant(ls, checkname(ls)); code_strcnst(ls, checkname(ls));
break; break;
case '[': case '[':
@ -939,7 +926,7 @@ static void constructor_part (LexState *ls, constdesc *cd) {
if (ls->token == '=') { if (ls->token == '=') {
switch (v.k) { switch (v.k) {
case VGLOBAL: case VGLOBAL:
code_constant(ls, v.info); code_strcnst(ls, v.info);
break; break;
case VLOCAL: case VLOCAL:
code_string(ls, ls->fs->localvar[v.info]); code_string(ls, ls->fs->localvar[v.info]);
@ -1297,7 +1284,7 @@ static int funcname (LexState *ls, vardesc *v) {
needself = (ls->token == ':'); needself = (ls->token == ':');
next(ls); next(ls);
lua_pushvar(ls, v); lua_pushvar(ls, v);
code_constant(ls, checkname(ls)); code_strcnst(ls, checkname(ls));
v->k = VINDEXED; v->k = VINDEXED;
} }
return needself; return needself;

37
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.81 2000/01/19 16:50:30 roberto Exp roberto $ ** $Id: lvm.c,v 1.82 2000/01/24 20:14:07 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -314,7 +314,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
StkId base) { StkId base) {
register StkId top; /* keep top local, for performance */ register StkId top; /* keep top local, for performance */
register const Byte *pc = tf->code; register const Byte *pc = tf->code;
const TObject *consts = tf->consts; TaggedString **strcnst = tf->strcnst;
if (L->callhook) if (L->callhook)
luaD_callHook(L, base-1, L->callhook, "call"); luaD_callHook(L, base-1, L->callhook, "call");
luaD_checkstack(L, (*pc++)+EXTRA_STACK); luaD_checkstack(L, (*pc++)+EXTRA_STACK);
@ -372,9 +372,18 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
top++; top++;
break; break;
case PUSHCONSTANTW: aux += highbyte(L, *pc++); case PUSHSTRCNSTW: aux += highbyte(L, *pc++);
case PUSHCONSTANT: aux += *pc++; case PUSHSTRCNST: aux += *pc++;
*top++ = consts[aux]; ttype(top) = LUA_T_STRING;
tsvalue(top) = strcnst[aux];
top++;
break;
case PUSHNUMCNSTW: aux += highbyte(L, *pc++);
case PUSHNUMCNST: aux += *pc++;
ttype(top) = LUA_T_NUMBER;
nvalue(top) = tf->numcnst[aux];
top++;
break; break;
case PUSHUPVALUE: aux = *pc++; case PUSHUPVALUE: aux = *pc++;
@ -387,8 +396,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
case GETGLOBALW: aux += highbyte(L, *pc++); case GETGLOBALW: aux += highbyte(L, *pc++);
case GETGLOBAL: aux += *pc++; case GETGLOBAL: aux += *pc++;
LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); luaV_getglobal(L, strcnst[aux]->u.s.gv, top);
luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
top++; top++;
break; break;
@ -399,9 +407,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
case GETDOTTEDW: aux += highbyte(L, *pc++); case GETDOTTEDW: aux += highbyte(L, *pc++);
case GETDOTTED: aux += *pc++; case GETDOTTED: aux += *pc++;
LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
ttype(top) = LUA_T_STRING; ttype(top) = LUA_T_STRING;
tsvalue(top++) = tsvalue(&consts[aux]); tsvalue(top++) = strcnst[aux];
luaV_gettable(L, top); luaV_gettable(L, top);
top--; top--;
break; break;
@ -410,9 +417,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
case PUSHSELF: aux += *pc++; { case PUSHSELF: aux += *pc++; {
TObject receiver; TObject receiver;
receiver = *(top-1); receiver = *(top-1);
LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
ttype(top) = LUA_T_STRING; ttype(top) = LUA_T_STRING;
tsvalue(top++) = tsvalue(&consts[aux]); tsvalue(top++) = strcnst[aux];
luaV_gettable(L, top); luaV_gettable(L, top);
*(top-1) = receiver; *(top-1) = receiver;
break; break;
@ -433,8 +439,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
case SETGLOBALW: aux += highbyte(L, *pc++); case SETGLOBALW: aux += highbyte(L, *pc++);
case SETGLOBAL: aux += *pc++; case SETGLOBAL: aux += *pc++;
LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); luaV_setglobal(L, strcnst[aux]->u.s.gv, top);
luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
top--; top--;
break; break;
@ -626,10 +631,10 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
case CLOSUREW: aux += highbyte(L, *pc++); case CLOSUREW: aux += highbyte(L, *pc++);
case CLOSURE: aux += *pc++; case CLOSURE: aux += *pc++;
*top++ = consts[aux]; ttype(top) = LUA_T_LPROTO;
L->top = top; tfvalue(top) = tf->protocnst[aux];
L->top = ++top;
aux = *pc++; /* number of upvalues */ aux = *pc++; /* number of upvalues */
LUA_ASSERT(L, aux>0, "closure with no upvalues");
luaV_closure(L, aux); luaV_closure(L, aux);
luaC_checkGC(L); luaC_checkGC(L);
top -= aux; top -= aux;