Browse Source

BIG CHANGE: functions have their own "constant table".

Roberto Ierusalimschy 28 years ago
parent
commit
0892f0e5b7
12 changed files with 278 additions and 277 deletions
  1. 31 15
      func.c
  2. 5 5
      func.h
  3. 4 4
      inout.c
  4. 3 2
      inout.h
  5. 8 9
      lex.c
  6. 156 125
      lua.stx
  7. 36 60
      opcode.c
  8. 3 9
      opcode.h
  9. 23 37
      table.c
  10. 4 6
      table.h
  11. 2 2
      tree.c
  12. 3 3
      tree.h

+ 31 - 15
func.c

@@ -11,6 +11,14 @@
 static TFunc *function_root = NULL;
 
 
+static void luaI_insertfunction (TFunc *f)
+{
+  lua_pack();
+  f->next = function_root;
+  function_root = f;
+  f->marked = 0;
+}
+
 /*
 ** Initialize TFunc struct
 */
@@ -21,29 +29,23 @@ void luaI_initTFunc (TFunc *f)
   f->code = NULL;
   f->lineDefined = 0;
   f->fileName = lua_parsedfile;
+  f->consts = NULL;
+  f->nconsts = 0;
   f->locvars = NULL;
+  luaI_insertfunction(f);
 }
 
-/*
-** Insert function in list for GC
-*/
-void luaI_insertfunction (TFunc *f)
-{
-  lua_pack();
-  f->next = function_root;
-  function_root = f;
-  f->marked = 0;
-}
 
 
 /*
 ** Free function
 */
-void luaI_freefunc (TFunc *f)
+static void luaI_freefunc (TFunc *f)
 {
-  luaI_free (f->code);
-  luaI_free (f->locvars);
-  luaI_free (f);
+  luaI_free(f->code);
+  luaI_free(f->locvars);
+  luaI_free(f->consts);
+  luaI_free(f);
 }
 
 
@@ -56,6 +58,20 @@ void luaI_funcfree (TFunc *l)
   }
 }
 
+
+void luaI_funcmark (TFunc *f)
+{
+  f->marked = 1;
+  if (!f->fileName->marked)
+    f->fileName->marked = 1;
+  if (f->consts) {
+    int i;
+    for (i=0; i<f->nconsts; i++)
+      lua_markobject(&f->consts[i]);
+  }
+}
+
+
 /*
 ** Garbage collection function.
 */
@@ -92,7 +108,7 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
   TObject *f = luaI_Address(func);
   if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
   {
-    *filename = f->value.tf->fileName;
+    *filename = f->value.tf->fileName->str;
     *linedefined = f->value.tf->lineDefined;
   }
   else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)

+ 5 - 5
func.h

@@ -1,5 +1,5 @@
 /*
-** $Id: func.h,v 1.10 1997/07/29 19:44:02 roberto Exp roberto $
+** $Id: func.h,v 1.11 1997/07/29 20:38:45 roberto Exp roberto $
 */
 
 #ifndef func_h
@@ -25,16 +25,16 @@ typedef struct TFunc
   int		marked;
   Byte		*code;
   int		lineDefined;
-  char		*fileName;
+  TaggedString	*fileName;
+  struct TObject *consts;
+  int		nconsts;
   LocVar        *locvars;
 } TFunc;
 
 TFunc *luaI_funccollector (long *cont);
 void luaI_funcfree (TFunc *l);
-void luaI_insertfunction (TFunc *f);
-
+void luaI_funcmark (TFunc *f);
 void luaI_initTFunc (TFunc *f);
-void luaI_freefunc (TFunc *f);
 
 char *luaI_getlocalname (TFunc *func, int local_number, int line);
 

+ 4 - 4
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.70 1997/07/07 21:05:51 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.71 1997/07/29 13:33:15 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <string.h>
@@ -27,7 +27,7 @@ char *rcs_inout="$Id: inout.c,v 2.70 1997/07/07 21:05:51 roberto Exp roberto $";
 
 /* Exported variables */
 Word lua_linenumber;
-char *lua_parsedfile;
+TaggedString *lua_parsedfile;
 
 
 char *luaI_typenames[] = { /* ORDER LUA_T */
@@ -40,7 +40,7 @@ char *luaI_typenames[] = { /* ORDER LUA_T */
 
 void luaI_setparsedfile (char *name)
 {
-  lua_parsedfile = luaI_createfixedstring(name)->str;
+  lua_parsedfile = luaI_createstring(name);
 }
 
 
@@ -399,7 +399,7 @@ void luaI_predefine (void)
     s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
   }
   n = luaI_findsymbolbyname("_VERSION");
-  s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
+  s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = luaI_createstring(LUA_VERSION);
 }
 
 

+ 3 - 2
inout.h

@@ -1,5 +1,5 @@
 /*
-** $Id: inout.h,v 1.19 1997/06/18 20:35:49 roberto Exp roberto $
+** $Id: inout.h,v 1.20 1997/06/19 18:04:34 roberto Exp roberto $
 */
 
 
@@ -7,12 +7,13 @@
 #define inout_h
 
 #include "types.h"
+#include "tree.h"
 #include <stdio.h>
 
 
 extern Word lua_linenumber;
 extern Word lua_debugline;
-extern char *lua_parsedfile;
+extern TaggedString *lua_parsedfile;
 
 void luaI_setparsedfile (char *name);
 

+ 8 - 9
lex.c

@@ -1,4 +1,4 @@
-char *rcs_lex = "$Id: lex.c,v 3.6 1997/07/01 19:32:41 roberto Exp roberto $";
+char *rcs_lex = "$Id: lex.c,v 3.7 1997/07/29 13:33:15 roberto Exp roberto $";
 
 
 #include <ctype.h>
@@ -62,7 +62,7 @@ void lua_setinput (ZIO *z)
 static void luaI_auxsyntaxerror (char *s)
 {
   luaL_verror("%s;\n> at line %d in file %s",
-               s, lua_linenumber, lua_parsedfile);
+               s, lua_linenumber, lua_parsedfile->str);
 }
 
 static void luaI_auxsynterrbf (char *s, char *token)
@@ -70,7 +70,7 @@ static void luaI_auxsynterrbf (char *s, char *token)
   if (token[0] == 0)
     token = "<eof>";
   luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
-           s, token, lua_linenumber, lua_parsedfile);
+           s, token, lua_linenumber, lua_parsedfile->str);
 }
 
 void luaI_syntaxerror (char *s)
@@ -110,7 +110,7 @@ void luaI_addReserved (void)
   int i;
   for (i=0; i<RESERVEDSIZE; i++)
   {
-    TaggedString *ts = lua_createstring(reserved[i].name);
+    TaggedString *ts = luaI_createstring(reserved[i].name);
     ts->marked = reserved[i].token;  /* reserved word  (always > 255) */
   }
 }
@@ -273,7 +273,7 @@ static int read_long_string (char *yytext, int buffsize)
   } endloop:
   save_and_next();  /* pass the second ']' */
   yytext[tokensize-2] = 0;  /* erases ']]' */
-  luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
+  luaY_lval.pTStr = luaI_createtempstring(yytext+2);
   yytext[tokensize-2] = ']';  /* restores ']]' */
   save(0);
   return STRING;
@@ -368,7 +368,7 @@ int luaY_lex (void)
         }
         next();  /* skip delimiter */
         save(0);
-        luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
+        luaY_lval.pTStr = luaI_createtempstring(yytext+1);
         tokensize--;
         save(del); save(0);  /* restore delimiter */
         return STRING;
@@ -454,11 +454,10 @@ int luaY_lex (void)
             save_and_next();
           } while (isalnum((unsigned char)current) || current == '_');
           save(0);
-          ts = lua_createstring(yytext);
-          if (ts->marked > 2)
+          ts = luaI_createtempstring(yytext);
+          if (ts->marked > 255)
             return ts->marked;  /* reserved word */
           luaY_lval.pTStr = ts;
-          ts->marked = 2;  /* avoid GC */
           return NAME;
         }
     }

+ 156 - 125
lua.stx

@@ -1,10 +1,8 @@
 %{
 
-char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp roberto $";
+char *rcs_luastx = "$Id: lua.stx,v 3.48 1997/07/29 20:38:45 roberto Exp roberto $";
 
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 
 #include "luadebug.h"
 #include "luamem.h"
@@ -40,6 +38,7 @@ struct State {
   int pc;  /* next position to code */
   TaggedString *localvar[MAXLOCALS];  /* store local variable names */
   int nlocalvar;  /* number of active local variables */
+  int maxconsts;  /* size of consts vector */
   int nvars;  /* total number of local variables (for debugging information) */
   int maxvars;  /* = -1 if no debug information */
 } stateMain, stateFunc, *currState;
@@ -69,41 +68,106 @@ static void check_space (int i)
                                      Byte, codeEM, MAX_INT);
 }
 
+
 static void code_byte (Byte c)
 {
   check_space(1);
   currState->f->code[currState->pc++] = c;
 }
 
-static void code_float (real n)
+
+static void code_word_at (int pc, int n)
 {
-  check_space(sizeof(real));
-  memcpy(currState->f->code+currState->pc, &n, sizeof(real));
-  currState->pc += sizeof(real);
+  Word w = n;
+  if (w != n)
+    yyerror("block too big");
+  currState->f->code[pc] = n&0xFF;
+  currState->f->code[pc+1] = n>>8;
 }
 
-static void code_code (TFunc *tf)
+static void code_word (int n)
 {
-  check_space(sizeof(TFunc *));
-  memcpy(currState->f->code+currState->pc, &tf, sizeof(TFunc *));
-  currState->pc += sizeof(TFunc *);
+  code_byte(n&0xFF);
+  code_byte(n>>8);
 }
 
-static void code_word_at (int pc, int n)
+static void code_constant (int c)
 {
-  Word w = n;
-  if (w != n)
-    yyerror("block too big");
-  memcpy(currState->f->code+pc, &w, sizeof(Word));
+  if (c <= 255) {
+    code_byte(PUSHCONSTANTB);
+    code_byte(c);
+  }
+  else {
+    code_byte(PUSHCONSTANT);
+    code_word(c);
+  }
+}
+
+
+static int next_constant (void)
+{
+  if (currState->f->nconsts >= currState->maxconsts) {
+    currState->maxconsts =
+      growvector(&currState->f->consts, currState->maxconsts,
+                 TObject, constantEM, MAX_WORD);
+  }
+  return currState->f->nconsts++;
+}
+
+
+static int string_constant (TaggedString *s)
+{
+  int c = s->u.s.constindex;
+  if (!(0 <= c && c < currState->f->nconsts &&
+      ttype(&currState->f->consts[c]) == LUA_T_STRING &&
+      tsvalue(&currState->f->consts[c]) == s)) {
+    c = next_constant();
+    ttype(&currState->f->consts[c]) = LUA_T_STRING;
+    tsvalue(&currState->f->consts[c]) = s;
+    s->u.s.constindex = c;  /* hint for next time */
+    luaI_releasestring(s);
+  }
+  return c;
 }
 
-static void code_word (Word n)
+
+static void code_string (TaggedString *s)
 {
-  check_space(sizeof(Word));
-  memcpy(currState->f->code+currState->pc, &n, sizeof(Word));
-  currState->pc += sizeof(Word);
+  int c = string_constant(s);
+  code_constant(c);
 }
 
+static void code_float (real n)
+{
+  int c = next_constant();
+  ttype(&currState->f->consts[c]) = LUA_T_NUMBER;
+  nvalue(&currState->f->consts[c]) = n;
+  code_constant(c);
+}
+
+
+static void code_number (float f)
+{ 
+  Word i;
+  if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
+   /* f has an (short) integer value */
+   if (i <= 2) code_byte(PUSH0 + i);
+   else if (i <= 255)
+   {
+    code_byte(PUSHBYTE);
+    code_byte(i);
+   }
+   else
+   {
+    code_byte(PUSHWORD);
+    code_word(i);
+   }
+  }
+  else
+    code_float(f);
+}
+
+
 static void flush_record (int n)
 {
   if (n == 0) return;
@@ -149,6 +213,7 @@ static void luaI_unregisterlocalvar (int line)
 
 static void store_localvar (TaggedString *name, int n)
 {
+  luaI_fixstring(name);  /* local var names cannot be GC */
   if (currState->nlocalvar+n < MAXLOCALS)
     currState->localvar[currState->nlocalvar+n] = name;
   else
@@ -170,40 +235,6 @@ static void add_varbuffer (Long var)
   yyerror ("variable buffer overflow");
 }
 
-static void code_string (Word w)
-{
-  code_byte(PUSHSTRING);
-  code_word(w);
-}
-
-static void code_constant (TaggedString *s)
-{
-  code_string(luaI_findconstant(s));
-}
-
-static void code_number (float f)
-{ 
-  Word i;
-  if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
-   /* f has an (short) integer value */
-   if (i <= 2) code_byte(PUSH0 + i);
-   else if (i <= 255)
-   {
-    code_byte(PUSHBYTE);
-    code_byte(i);
-   }
-   else
-   {
-    code_byte(PUSHWORD);
-    code_word(i);
-   }
-  }
-  else
-  {
-   code_byte(PUSHFLOAT);
-   code_float(f);
-  }
-}
 
 /*
 ** Search a local name and if find return its index. If do not find return -1
@@ -256,55 +287,6 @@ static void lua_codeadjust (int n)
 }
 
 
-static void init_state (TFunc *f)
-{
-  luaI_initTFunc(f);
-  currState->nlocalvar = 0;
-  currState->f = f;
-  currState->pc = 0;
-  currState->codesize = CODE_BLOCK;
-  f->code = newvector(CODE_BLOCK, Byte);
-  if (lua_debug) {
-    currState->nvars = 0;
-    currState->maxvars = 0;
-  }
-  else
-    currState->maxvars = -1;  /* flag no debug information */
-}
-
-
-static void init_func (void)
-{
-  currState = &stateFunc;
-  init_state(new(TFunc));
-  luaI_codedebugline(lua_linenumber);
-}
-
-
-static void codereturn (void)
-{
-  if (currState->nlocalvar == 0)
-    code_byte(RETCODE0);
-  else
-  {
-    code_byte(RETCODE);
-    code_byte(currState->nlocalvar);
-  }
-}
-
-
-static void close_func (void)
-{
-  codereturn();
-  code_byte(ENDCODE);
-  currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte);
-  if (currState->maxvars != -1) {  /* debug information? */
-    luaI_registerlocalvar(NULL, -1);  /* flag end of vector */
-    currState->f->locvars = shrinkvector(currState->f->locvars,
-                                    currState->nvars, LocVar);
-  }
-}
-
 
 void luaI_codedebugline (int line)
 {
@@ -350,7 +332,7 @@ static int close_parlist (int dots)
   else {
     code_byte(VARARGS);
     code_byte(currState->nlocalvar);
-    add_localvar(luaI_createfixedstring("arg"));
+    add_localvar(luaI_createstring("arg"));
   }
   return lua_linenumber;
 }
@@ -423,6 +405,65 @@ static void code_shortcircuit (int pc, Byte jmp)
 }
 
 
+static void init_state (TFunc *f)
+{
+  currState->nlocalvar = 0;
+  currState->f = f;
+  currState->pc = 0;
+  currState->codesize = CODE_BLOCK;
+  f->code = newvector(CODE_BLOCK, Byte);
+  currState->maxconsts = 0;
+  if (lua_debug) {
+    currState->nvars = 0;
+    currState->maxvars = 0;
+  }
+  else
+    currState->maxvars = -1;  /* flag no debug information */
+}
+
+
+static void init_func (Long v)
+{
+  TFunc *f = new(TFunc);
+  int c = next_constant();
+  ttype(&currState->f->consts[c]) = LUA_T_FUNCTION;
+  currState->f->consts[c].value.tf = f;
+  code_constant(c);
+  storesinglevar(v);
+  currState = &stateFunc;
+  luaI_initTFunc(f);
+  init_state(f);
+  luaI_codedebugline(lua_linenumber);
+}
+
+
+static void codereturn (void)
+{
+  if (currState->nlocalvar == 0)
+    code_byte(RETCODE0);
+  else
+  {
+    code_byte(RETCODE);
+    code_byte(currState->nlocalvar);
+  }
+}
+
+
+static void close_func (void)
+{
+  codereturn();
+  code_byte(ENDCODE);
+  currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte);
+  currState->f->consts = shrinkvector(currState->f->consts,
+                                      currState->f->nconsts, TObject);
+  if (currState->maxvars != -1) {  /* debug information? */
+    luaI_registerlocalvar(NULL, -1);  /* flag end of vector */
+    currState->f->locvars = shrinkvector(currState->f->locvars,
+                                    currState->nvars, LocVar);
+  }
+}
+
+
 /*
 ** Parse LUA code.
 */
@@ -444,9 +485,7 @@ void lua_parse (TFunc *tf)
  int   vInt;
  float vFloat;
  char *pChar;
- Word  vWord;
  Long  vLong;
- TFunc *pFunc;
  TaggedString *pTStr;
 }
 
@@ -460,8 +499,7 @@ void lua_parse (TFunc *tf)
 %token FUNCTION
 %token DOTS
 %token <vFloat> NUMBER
-%token <vWord>  STRING
-%token <pTStr>  NAME 
+%token <pTStr>  NAME STRING
 
 %type <vLong> PrepJump
 %type <vLong> exprlist, exprlist1  /* if > 0, points to function return
@@ -473,8 +511,7 @@ void lua_parse (TFunc *tf)
 %type <vInt>  ffieldlist, ffieldlist1, semicolonpart
 %type <vInt>  lfieldlist, lfieldlist1
 %type <vInt>  parlist, parlist1, par
-%type <vLong> var, singlevar, funcname
-%type <pFunc> body
+%type <vLong> var, singlevar
 
 %left AND OR
 %left EQ NE '>' '<' LE GE
@@ -495,28 +532,21 @@ chunklist : /* empty */
 	  ;
 
 function     : FUNCTION funcname body 	 
-	       { 
-		code_byte(PUSHFUNCTION);
-		code_code($3);
-		storesinglevar($2);
-	       }
 	       ;
 
-funcname	: var { $$ =$1; init_func(); }
+funcname	: var { init_func($1); }
 		| varexp ':' NAME
 	{
-	  code_constant($3);
-	  $$ = 0;  /* indexed variable */
-	  init_func();
-	  add_localvar(luaI_createfixedstring("self"));
+	  code_string($3);
+	  init_func(0);  /* indexed variable */
+	  add_localvar(luaI_createstring("self"));
 	}
 		;
 
 body :  '(' parlist ')' block END
 	{
 	  close_func();
-          $$ = currState->f;
-	  $$->lineDefined = $2;
+	  currState->f->lineDefined = $2;
 	  currState = &stateMain;  /* change back to main code */
 	}
 		;
@@ -658,7 +688,7 @@ funcvalue    : varexp { $$ = 0; }
 	     | varexp ':' NAME 
 	     { 
                code_byte(PUSHSELF); 
-	       code_word(luaI_findconstant($3));
+	       code_word(string_constant($3));
                $$ = 1;
 	     }
 	     ;
@@ -735,7 +765,7 @@ ffield      : ffieldkey '=' expr1
            ;
 
 ffieldkey   : '[' expr1 ']'
-	    | NAME { code_constant($1); }
+	    | NAME { code_string($1); }
 	    ;
 
 lfieldlist  : /* empty */ { $$ = 0; }
@@ -771,7 +801,7 @@ var	  :	singlevar { $$ = $1; }
 	  }
 	  |	varexp '.' NAME
 	  {
-	    code_constant($3);
+	    code_string($3);
 	    $$ = 0;		/* indexed variable */
 	  }
 	  ;
@@ -783,6 +813,7 @@ singlevar :	NAME
 	    $$ = luaI_findsymbol($1)+1;  /* return positive value */
            else
 	    $$ = -(local+1);		/* return negative value */
+           luaI_fixstring($1);  /* cannot GC variable names */
 	  }
 	  ;
 

+ 36 - 60
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 4.18 1997/07/29 13:35:06 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 4.19 1997/07/29 21:11:10 roberto Exp roberto $";
 
 #include <setjmp.h>
 #include <stdio.h>
@@ -26,6 +26,9 @@ char *rcs_opcode="$Id: opcode.c,v 4.18 1997/07/29 13:35:06 roberto Exp roberto $
 #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
 
 
+#define get_word(w,pc)	{w=*pc+(*(pc+1)<<8); pc+=2;}
+
+
 #define STACK_SIZE 	128
 
 #ifndef STACK_LIMIT
@@ -69,7 +72,7 @@ lua_LHFunction lua_linehook = NULL;
 lua_CHFunction lua_callhook = NULL;
 
 
-static StkId lua_execute (Byte *pc, StkId base);
+static StkId lua_execute (TFunc *func, StkId base);
 static void do_call (StkId base, int nResults);
 
 
@@ -169,7 +172,7 @@ static int lua_tostring (TObject *obj)
       sprintf (s, "%d", i);
     else
       sprintf (s, "%g", nvalue(obj));
-    tsvalue(obj) = lua_createstring(s);
+    tsvalue(obj) = luaI_createstring(s);
     ttype(obj) = LUA_T_STRING;
     return 0;
   }
@@ -267,7 +270,8 @@ static void callHook (StkId base, lua_Type type, int isreturn)
   {
     TObject *f = stack+base-1;
     if (type == LUA_T_MARK)
-      (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
+      (*lua_callhook)(Ref(f), f->value.tf->fileName->str,
+                      f->value.tf->lineDefined);
     else
       (*lua_callhook)(Ref(f), "(C)", -1);
   }
@@ -324,7 +328,7 @@ static void do_call (StkId base, int nResults)
   }
   else if (ttype(func) == LUA_T_FUNCTION) {
     ttype(func) = LUA_T_MARK;
-    firstResult = lua_execute(func->value.tf->code, base);
+    firstResult = lua_execute(func->value.tf, base);
   }
   else { /* func is not a function */
     /* Check the tag method for invalid functions */
@@ -630,14 +634,17 @@ int luaI_dorun (TFunc *tf)
 
 int lua_domain (void)
 {
-  TFunc tf;
   int status;
+  TFunc *tf = new(TFunc);
   jmp_buf myErrorJmp;
   jmp_buf *oldErr = errorJmp;
   errorJmp = &myErrorJmp;
-  luaI_initTFunc(&tf);
+  luaI_initTFunc(tf);
+  adjustC(1);  /* one slot for the pseudo-function */
+  stack[CLS_current.base].ttype = LUA_T_FUNCTION;
+  stack[CLS_current.base].value.tf = tf;
   if (setjmp(myErrorJmp) == 0) {
-    lua_parse(&tf);
+    lua_parse(tf);
     status = 0;
   }
   else {
@@ -645,9 +652,8 @@ int lua_domain (void)
     status = 1;
   }
   if (status == 0)
-    status = luaI_dorun(&tf);
+    status = do_protectedrun(MULT_RET);
   errorJmp = oldErr;
-  luaI_free(tf.code);
   return status;
 }
 
@@ -952,7 +958,7 @@ void lua_pushstring (char *s)
     ttype(top) = LUA_T_NIL;
   else
   {
-    tsvalue(top) = lua_createstring(s);
+    tsvalue(top) = luaI_createstring(s);
     ttype(top) = LUA_T_STRING;
   }
   incr_top;
@@ -1088,7 +1094,7 @@ static void adjust_varargs (StkId first_extra_arg)
   /* store counter in field "n" */ {
     TObject index, extra;
     ttype(&index) = LUA_T_STRING;
-    tsvalue(&index) = lua_createstring("n");
+    tsvalue(&index) = luaI_createstring("n");
     ttype(&extra) = LUA_T_NUMBER;
     nvalue(&extra) = nvararg;
     *(lua_hashdefine(avalue(&arg), &index)) = extra;
@@ -1104,8 +1110,9 @@ static void adjust_varargs (StkId first_extra_arg)
 ** [stack+base,top). Returns n such that the the results are between
 ** [stack+n,top).
 */
-static StkId lua_execute (Byte *pc, StkId base)
+static StkId lua_execute (TFunc *func, StkId base)
 {
+  Byte *pc = func->code;
   if (lua_callhook)
     callHook (base, LUA_T_MARK, 0);
  while (1)
@@ -1133,35 +1140,6 @@ static StkId lua_execute (Byte *pc, StkId base)
    }
    break;
 
-   case PUSHFLOAT:
-   {
-    real num;
-    get_float(num,pc);
-    ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
-    incr_top;
-   }
-   break;
-
-   case PUSHSTRING:
-   {
-    Word w;
-    get_word(w,pc);
-    ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
-    incr_top;
-   }
-   break;
-
-   case PUSHFUNCTION:
-   {
-    TFunc *f;
-    get_code(f,pc);
-    luaI_insertfunction(f);  /* may take part in GC */
-    top->ttype = LUA_T_FUNCTION;
-    top->value.tf = f;
-    incr_top;
-   }
-   break;
-
    case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
    case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
    case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
@@ -1187,7 +1165,7 @@ static StkId lua_execute (Byte *pc, StkId base)
      TObject receiver = *(top-1);
      Word w;
      get_word(w,pc);
-     ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
+     *top = func->consts[w];
      incr_top;
      pushsubscript();
      *top = receiver;
@@ -1195,6 +1173,20 @@ static StkId lua_execute (Byte *pc, StkId base)
      break;
    }
 
+   case PUSHCONSTANTB: {
+     *top = func->consts[*pc++];
+     incr_top;
+     break;
+   }
+
+   case PUSHCONSTANT: {
+     Word w;
+     get_word(w,pc);
+     *top = func->consts[w];
+     incr_top;
+     break;
+   }
+
    case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
    case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
    case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
@@ -1241,22 +1233,6 @@ static StkId lua_execute (Byte *pc, StkId base)
    }
    break;
 
-   case STORERECORD:  /* opcode obsolete: supersed by STOREMAP */
-   {
-    int n = *(pc++);
-    TObject *arr = top-n-1;
-    while (n)
-    {
-     Word w;
-     get_word(w,pc);
-     ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
-     *(lua_hashdefine (avalue(arr), top)) = *(top-1);
-     top--;
-     n--;
-    }
-   }
-   break;
-
    case STOREMAP: {
      int n = *(pc++);
      TObject *arr = top-(2*n)-1;
@@ -1382,7 +1358,7 @@ static StkId lua_execute (Byte *pc, StkId base)
      if (tostring(l) || tostring(r))
        call_binTM(IM_CONCAT, "unexpected type for concatenation");
      else {
-       tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
+       tsvalue(l) = luaI_createstring(lua_strconc(svalue(l),svalue(r)));
        --top;
      }
    }

+ 3 - 9
opcode.h

@@ -1,6 +1,6 @@
 /*
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.35 1997/07/04 14:55:37 roberto Exp roberto $
+** $Id: opcode.h,v 3.36 1997/07/29 20:38:06 roberto Exp roberto $
 */
 
 #ifndef opcode_h
@@ -47,9 +47,6 @@ PUSH1,/*		-		1.0  */
 PUSH2,/*		-		2.0  */
 PUSHBYTE,/*	b	-		(float)b  */
 PUSHWORD,/*	w	-		(float)w  */
-PUSHFLOAT,/*	f	-		f  */
-PUSHSTRING,/*	w	-		STR[w]  */
-PUSHFUNCTION,/*	p	-		FUN(p)  */
 PUSHLOCAL0,/*		-		LOC[0]  */
 PUSHLOCAL1,/*		-		LOC[1]  */
 PUSHLOCAL2,/*		-		LOC[2]  */
@@ -111,6 +108,8 @@ RETCODE,/*	b	-		-  */
 SETLINE,/*	w	-		-		LINE=w  */
 VARARGS,/*	b	v_b...v_1	{v_1...v_b;n=b}  */
 STOREMAP,/*	b	v_b k_b ...v_1 k_1 t	-	t[k_i]=v_i  */
+PUSHCONSTANTB,/*b	-		const[b] */
+PUSHCONSTANT,/* w	-		const[w] */
 ENDCODE = 127
 } OpCode;
 
@@ -153,11 +152,6 @@ typedef struct TObject
 #define s_fvalue(i)	(fvalue(&s_object(i)))
 #define s_uvalue(i)	(uvalue(&s_object(i)))
 
-#define get_word(code,pc) {memcpy(&code, pc, sizeof(Word)); pc+=sizeof(Word);}
-#define get_float(code,pc){memcpy(&code, pc, sizeof(real)); pc+=sizeof(real);}
-#define get_code(code,pc) {memcpy(&code, pc, sizeof(TFunc *)); \
-                           pc+=sizeof(TFunc *);}
-
 
 /* Exported functions */
 void    lua_parse      (TFunc *tf);	/* from "lua.stx" module */

+ 23 - 37
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.72 1997/06/17 18:09:31 roberto Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.73 1997/07/07 16:44:26 roberto Exp roberto $";
 
 #include "luamem.h"
 #include "auxlib.h"
@@ -24,14 +24,17 @@ Symbol *lua_table = NULL;
 Word lua_ntable = 0;
 static Long lua_maxsymbol = 0;
 
-TaggedString **lua_constant = NULL;
-Word lua_nconstant = 0;
-static Long lua_maxconstant = 0;
-
 
 #define GARBAGE_BLOCK 100
 
 
+static TaggedString *luaI_createfixedstring (char *name)
+{
+  TaggedString *ts = luaI_createstring(name);
+  luaI_fixstring(ts);
+  return ts;
+}
+
 void luaI_initsymbol (void)
 {
   lua_maxsymbol = BUFFER_BLOCK;
@@ -40,16 +43,11 @@ void luaI_initsymbol (void)
 }
 
 
-/*
-** Initialise constant table with pre-defined constants
-*/
 void luaI_initconstant (void)
 {
- lua_maxconstant = BUFFER_BLOCK;
- lua_constant = newvector(lua_maxconstant, TaggedString *);
- /* pre-register mem error messages, to avoid loop when error arises */
- luaI_findconstantbyname(tableEM);
- luaI_findconstantbyname(memEM);
+  /* pre-register mem error messages, to avoid loop when error arises */
+  luaI_createfixedstring(tableEM);
+  luaI_createfixedstring(memEM);
 }
 
 
@@ -79,35 +77,25 @@ Word luaI_findsymbolbyname (char *name)
 }
 
 
-/*
-** Given a tree node, check it is has a correspondent constant index. If not,
-** allocate it.
-*/
-Word luaI_findconstant (TaggedString *t)
+void luaI_releasestring (TaggedString *t)
 {
- if (t->u.s.constindex == NOT_USED)
- {
-  if (lua_nconstant == lua_maxconstant)
-    lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
-                        constantEM, MAX_WORD);
-  t->u.s.constindex = lua_nconstant;
-  lua_constant[lua_nconstant] = t;
-  lua_nconstant++;
- }
- return t->u.s.constindex;
+  if (t->marked == 2)  /* string has temporary mark? */
+    t->marked = 0;
 }
 
 
-Word  luaI_findconstantbyname (char *name)
+void luaI_fixstring (TaggedString *t)
 {
-  return luaI_findconstant(luaI_createfixedstring(name));
+  if (t->marked < 3)
+    t->marked = 3;  /* avoid GC */
 }
 
-TaggedString *luaI_createfixedstring (char *name)
+
+TaggedString *luaI_createtempstring (char *name)
 {
-  TaggedString *ts = lua_createstring(name);
+  TaggedString *ts = luaI_createstring(name);
   if (!ts->marked)
-    ts->marked = 2;  /* avoid GC */
+    ts->marked = 2;  /* avoid (temporarily) GC */
   return ts;
 }
 
@@ -131,9 +119,6 @@ static char *lua_travsymbol (int (*fn)(TObject *))
 }
 
 
-/*
-** Mark an object if it is a string or a unmarked array.
-*/
 int lua_markobject (TObject *o)
 {/* if already marked, does not change mark value */
  if (ttype(o) == LUA_T_USERDATA ||
@@ -143,7 +128,7 @@ int lua_markobject (TObject *o)
    lua_hashmark (avalue(o));
  else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
            && !o->value.tf->marked)
-   o->value.tf->marked = 1;
+   luaI_funcmark(o->value.tf);
  return 0;
 }
 
@@ -208,6 +193,7 @@ long lua_collectgarbage (long limit)
   luaI_hashfree(freetable);
   luaI_strfree(freestr);
   luaI_funcfree(freefunc);
+/*printf("total %d  coletados %d\n", (int)gc_nentity, (int)recovered);*/
   return recovered;
 } 
 

+ 4 - 6
table.h

@@ -1,7 +1,7 @@
 /*
 ** Module to control static tables
 ** TeCGraf - PUC-Rio
-** $Id: table.h,v 2.24 1997/04/07 14:48:53 roberto Exp roberto $
+** $Id: table.h,v 2.25 1997/05/26 14:42:36 roberto Exp roberto $
 */
 
 #ifndef table_h
@@ -19,18 +19,16 @@ typedef struct
 
 extern Symbol *lua_table;
 extern Word lua_ntable;
-extern TaggedString **lua_constant;
-extern Word lua_nconstant;
 
 void luaI_initsymbol (void);
 void  luaI_initconstant (void);
 Word  luaI_findsymbolbyname (char *name);
 Word  luaI_findsymbol      (TaggedString *t);
-Word  luaI_findconstant    (TaggedString *t);
-Word  luaI_findconstantbyname (char *name);
 int luaI_globaldefined (char *name);
 void luaI_nextvar (void);
-TaggedString *luaI_createfixedstring  (char *str);
+TaggedString *luaI_createtempstring (char *name);
+void luaI_releasestring (TaggedString *t);
+void luaI_fixstring (TaggedString *t);
 int   lua_markobject      (TObject *o);
 int luaI_ismarked (TObject *o);
 void  lua_pack            (void);

+ 2 - 2
tree.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
  
-char *rcs_tree="$Id: tree.c,v 1.27 1997/06/09 17:28:14 roberto Exp roberto $";
+char *rcs_tree="$Id: tree.c,v 1.28 1997/06/11 14:24:40 roberto Exp roberto $";
 
 
 #include <string.h>
@@ -149,7 +149,7 @@ TaggedString *luaI_createudata (void *udata, int tag)
   return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
 }
 
-TaggedString *lua_createstring (char *str)
+TaggedString *luaI_createstring (char *str)
 {
   return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
 }

+ 3 - 3
tree.h

@@ -1,7 +1,7 @@
 /*
 ** tree.h
 ** TecCGraf - PUC-Rio
-** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
+** $Id: tree.h,v 1.18 1997/06/09 17:28:14 roberto Exp roberto $
 */
 
 #ifndef tree_h
@@ -19,7 +19,7 @@ typedef struct TaggedString
   union {
     struct {
       Word varindex;  /* != NOT_USED  if this is a symbol */
-      Word constindex;  /* != NOT_USED  if this is a constant */
+      Word constindex;  /* hint to reuse constant indexes */
     } s;
     void *v;  /* if this is a userdata, here is its value */
   } u;
@@ -29,7 +29,7 @@ typedef struct TaggedString
 } TaggedString;
  
 
-TaggedString *lua_createstring (char *str);
+TaggedString *luaI_createstring (char *str);
 TaggedString *luaI_createudata (void *udata, int tag);
 TaggedString *luaI_strcollector (long *cont);
 void luaI_strfree (TaggedString *l);