Przeglądaj źródła

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

Roberto Ierusalimschy 25 lat temu
rodzic
commit
d11e5adf55
7 zmienionych plików z 86 dodań i 80 usunięć
  1. 2 3
      ldebug.c
  2. 10 4
      lfunc.c
  3. 5 3
      lgc.c
  4. 8 3
      lobject.h
  5. 5 3
      lopcodes.h
  6. 35 48
      lparser.c
  7. 21 16
      lvm.c

+ 2 - 3
ldebug.c

@@ -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
 ** 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)
       f = protovalue(f);
     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 = tsvalue(&tfvalue(f)->consts[i])->str;
+    *name = tfvalue(f)->strcnst[i]->str;
     return luaO_typename(f+2);
   }
 }

+ 10 - 4
lfunc.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -34,8 +34,12 @@ TProtoFunc *luaF_newproto (lua_State *L) {
   f->code = NULL;
   f->lineDefined = 0;
   f->source = NULL;
-  f->consts = NULL;
-  f->nconsts = 0;
+  f->strcnst = NULL;
+  f->nstrcnst = 0;
+  f->numcnst = NULL;
+  f->nnumcnst = 0;
+  f->protocnst = NULL;
+  f->nprotocnst = 0;
   f->locvars = NULL;
   f->next = L->rootproto;
   L->rootproto = f;
@@ -49,7 +53,9 @@ void luaF_freeproto (lua_State *L, TProtoFunc *f) {
   L->nblocks -= gcsizeproto(L, f);
   luaM_free(L, f->code);
   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);
 }
 

+ 5 - 3
lgc.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -33,8 +33,10 @@ static void protomark (lua_State *L, TProtoFunc *f) {
     int i;
     f->marked = 1;
     strmark(L, f->source);
-    for (i=f->nconsts-1; i>=0; i--)
-      markobject(L, &f->consts[i]);
+    for (i=f->nstrcnst-1; i>=0; i--)
+      strmark(L, f->strcnst[i]);
+    for (i=f->nprotocnst-1; i>=0; i--)
+      protomark(L, f->protocnst[i]);
   }
 }
 

+ 8 - 3
lobject.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -154,14 +154,19 @@ typedef struct TaggedString {
 typedef struct TProtoFunc {
   struct TProtoFunc *next;
   int marked;
-  struct TObject *consts;
-  int nconsts;
+  struct TaggedString **strcnst;
+  int nstrcnst;
+  real *numcnst;
+  int nnumcnst;
+  struct TProtoFunc **protocnst;
+  int nprotocnst;
   Byte *code;  /* ends with opcode ENDCODE */
   int lineDefined;
   TaggedString  *source;
   struct LocVar *locvars;  /* ends with line = -1 */
 } TProtoFunc;
 
+
 typedef struct LocVar {
   TaggedString *varname;           /* NULL signals end of scope */
   int line;

+ 5 - 3
lopcodes.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -33,8 +33,10 @@ PUSHNUMBER,/*	b	-		(float)b			*/
 PUSHNUMBERNEGW,/* w	-		(float)-w			*/
 PUSHNUMBERNEG,/* b	-		(float)-b			*/
 
-PUSHCONSTANTW,/*w	-		CNST[w]				*/
-PUSHCONSTANT,/*	b	-		CNST[b]				*/
+PUSHSTRCNSTW,/*	w	-		STRCNST[w]			*/
+PUSHSTRCNST,/*	b	-		STRCNST[b]			*/
+PUSHNUMCNSTW,/*	w	-		NUMCNST[w]			*/
+PUSHNUMCNST,/*	b	-		NUMCNST[b]			*/
 
 PUSHUPVALUE,/*	b	-		Closure[b]			*/
 

+ 35 - 48
lparser.c

@@ -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
 ** 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) {
-  code_oparg(ls, PUSHCONSTANT, c, 1);
+static void code_strcnst (LexState *ls, int c) {
+  code_oparg(ls, PUSHSTRCNST, c, 1);
 }
 
 
 static void assertglobal (LexState *ls, int index) {
-  TObject *o = &ls->fs->f->consts[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++;
+  luaS_assertglobal(ls->L, ls->fs->f->strcnst[index]);
 }
 
 
 static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
   TProtoFunc *f = fs->f;
   int c = s->constindex;
-  if (!(c < f->nconsts &&
-      ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
-    c = next_constant(ls, f);
-    ttype(&f->consts[c]) = LUA_T_STRING;
-    tsvalue(&f->consts[c]) = s;
+  if (c >= f->nstrcnst || f->strcnst[c] != s) {
+    luaM_growvector(ls->L, f->strcnst, f->nstrcnst, 1,
+                    TaggedString *, constantEM, MAX_ARG);
+    c = f->nstrcnst++;
+    f->strcnst[c] = s;
     s->constindex = c;  /* hint for next time */
   }
   return c;
@@ -243,7 +234,7 @@ static int string_constant (LexState *ls, FuncState *fs, 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) {
   /* check whether `r' has appeared within the last LIM entries */
   TProtoFunc *f = ls->fs->f;
-  TObject *cnt = f->consts;
-  int c = f->nconsts;
+  int c = f->nnumcnst;
   int lim = c < LIM ? 0 : c-LIM;
-  while (--c >= lim) {
-    if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
-      return c;
-  }
+  while (--c >= lim)
+    if (f->numcnst[c] == r) return c;
   /* not found; create a new entry */
-  c = next_constant(ls, f);
-  cnt = f->consts;  /* `next_constant' may reallocate this vector */
-  ttype(&cnt[c]) = LUA_T_NUMBER;
-  nvalue(&cnt[c]) = r;
+  luaM_growvector(ls->L, f->numcnst, f->nnumcnst, 1,
+                  real, constantEM, MAX_ARG);
+  c = f->nnumcnst++;
+  f->numcnst[c] = r;
   return c;
 }
 
@@ -274,7 +262,7 @@ static void code_number (LexState *ls, real f) {
     code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1);
   }
   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) {
   /* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */
   if (v->k == VDOT) {
-    code_constant(ls, v->info);
+    code_strcnst(ls, v->info);
     v->k = VINDEXED;
   }
 }
@@ -556,19 +544,16 @@ static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
 
 static void func_onstack (LexState *ls, FuncState *func) {
   FuncState *fs = ls->fs;
+  TProtoFunc *f = fs->f;
   int i;
-  int c = next_constant(ls, fs->f);
-  ttype(&fs->f->consts[c]) = LUA_T_LPROTO;
-  fs->f->consts[c].value.tf = func->f;
-  if (func->nupvalues == 0)
-    code_constant(ls, c);
-  else {
-    for (i=0; i<func->nupvalues; i++)
-      lua_pushvar(ls, &func->upvalues[i]);
-    deltastack(ls, 1);  /* CLOSURE puts one extra element (before poping) */
-    code_oparg(ls, CLOSURE, c, -func->nupvalues);
-    code_byte(ls, (Byte)func->nupvalues);
-  }
+  luaM_growvector(ls->L, f->protocnst, f->nprotocnst, 1,
+                  TProtoFunc *, constantEM, MAX_ARG);
+  f->protocnst[f->nprotocnst] = func->f;
+  for (i=0; i<func->nupvalues; i++)
+    lua_pushvar(ls, &func->upvalues[i]);
+  deltastack(ls, 1);  /* CLOSURE puts one extra element (before poping) */
+  code_oparg(ls, CLOSURE, f->nprotocnst++, -func->nupvalues);
+  code_byte(ls, (Byte)func->nupvalues);
 }
 
 
@@ -602,7 +587,9 @@ static void close_func (LexState *ls) {
   code_opcode(ls, ENDCODE, 0);
   f->code[0] = (Byte)fs->maxstacksize;
   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? */
     luaI_registerlocalvar(ls, NULL, -1);  /* flag end of vector */
     luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
@@ -683,7 +670,7 @@ static int checkname (LexState *ls) {
 
 static TaggedString *str_checkname (LexState *ls) {
   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 */
   switch (ls->token) {
     case NAME:
-      code_constant(ls, checkname(ls));
+      code_strcnst(ls, checkname(ls));
       break;
 
     case '[':
@@ -939,7 +926,7 @@ static void constructor_part (LexState *ls, constdesc *cd) {
       if (ls->token == '=') {
         switch (v.k) {
           case VGLOBAL:
-            code_constant(ls, v.info);
+            code_strcnst(ls, v.info);
             break;
           case VLOCAL:
             code_string(ls, ls->fs->localvar[v.info]);
@@ -1297,7 +1284,7 @@ static int funcname (LexState *ls, vardesc *v) {
     needself = (ls->token == ':');
     next(ls);
     lua_pushvar(ls, v);
-    code_constant(ls, checkname(ls));
+    code_strcnst(ls, checkname(ls));
     v->k = VINDEXED;
   }
   return needself;

+ 21 - 16
lvm.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -314,7 +314,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
                     StkId base) {
   register StkId top;  /* keep top local, for performance */
   register const Byte *pc = tf->code;
-  const TObject *consts = tf->consts;
+  TaggedString **strcnst = tf->strcnst;
   if (L->callhook)
     luaD_callHook(L, base-1, L->callhook, "call");
   luaD_checkstack(L, (*pc++)+EXTRA_STACK);
@@ -372,9 +372,18 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         top++;
         break;
 
-      case PUSHCONSTANTW: aux += highbyte(L, *pc++);
-      case PUSHCONSTANT:  aux += *pc++;
-        *top++ = consts[aux];
+      case PUSHSTRCNSTW: aux += highbyte(L, *pc++);
+      case PUSHSTRCNST:  aux += *pc++;
+        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;
 
       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 GETGLOBAL:  aux += *pc++;
-        LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
-        luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
+        luaV_getglobal(L, strcnst[aux]->u.s.gv, top);
         top++;
         break;
 
@@ -399,9 +407,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
 
       case GETDOTTEDW: aux += highbyte(L, *pc++);
       case GETDOTTED:  aux += *pc++;
-        LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         ttype(top) = LUA_T_STRING;
-        tsvalue(top++) = tsvalue(&consts[aux]);
+        tsvalue(top++) = strcnst[aux];
         luaV_gettable(L, top);
         top--;
         break;
@@ -410,9 +417,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
       case PUSHSELF:  aux += *pc++; {
         TObject receiver;
         receiver = *(top-1);
-        LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         ttype(top) = LUA_T_STRING;
-        tsvalue(top++) = tsvalue(&consts[aux]);
+        tsvalue(top++) = strcnst[aux];
         luaV_gettable(L, top);
         *(top-1) = receiver;
         break;
@@ -433,8 +439,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
 
       case SETGLOBALW: aux += highbyte(L, *pc++);
       case SETGLOBAL:  aux += *pc++;
-        LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
-        luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
+        luaV_setglobal(L, strcnst[aux]->u.s.gv, top);
         top--;
         break;
 
@@ -626,10 +631,10 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
 
       case CLOSUREW: aux += highbyte(L, *pc++);
       case CLOSURE:  aux += *pc++;
-        *top++ = consts[aux];
-        L->top = top;
+        ttype(top) = LUA_T_LPROTO;
+        tfvalue(top) = tf->protocnst[aux];
+        L->top = ++top;
         aux = *pc++;  /* number of upvalues */
-        LUA_ASSERT(L, aux>0, "closure with no upvalues");
         luaV_closure(L, aux);
         luaC_checkGC(L);
         top -= aux;