From 68f337dfa617732646a4a974eb33b25daf45a1e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Oct 1995 14:13:02 -0300 Subject: [PATCH] Garbage collection of functions + header structure for functions --- fallback.c | 9 ++++++++- fallback.h | 3 ++- hash.c | 6 +++--- inout.c | 4 ++-- lua.stx | 31 ++++++++++++++++++------------- opcode.c | 22 +++++++++++++--------- opcode.h | 11 +++++------ table.c | 6 +++++- 8 files changed, 56 insertions(+), 36 deletions(-) diff --git a/fallback.c b/fallback.c index fd9bd648..e4da2df1 100644 --- a/fallback.c +++ b/fallback.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_fallback="$Id: fallback.c,v 1.12 1995/05/02 18:43:03 roberto Exp roberto $"; +char *rcs_fallback="$Id: fallback.c,v 1.13 1995/10/02 17:03:33 roberto Exp roberto $"; #include #include @@ -168,3 +168,10 @@ void luaI_travlock (void (*fn)(Object *)) fn(&lockArray[i]); } + +void luaI_travfallbacks (void (*fn)(Object *)) +{ + Word i; + for (i=0; i @@ -70,7 +70,7 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */ return (Word)h%nhash(t); /* make it a valid index */ } case LUA_T_FUNCTION: - return (((IntPoint)bvalue(ref))%nhash(t)); + return (((IntPoint)ref->value.tf)%nhash(t)); case LUA_T_CFUNCTION: return (((IntPoint)fvalue(ref))%nhash(t)); case LUA_T_ARRAY: @@ -89,7 +89,7 @@ Bool lua_equalObj (Object *t1, Object *t2) case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); case LUA_T_STRING: return streq(svalue(t1), svalue(t2)); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); - case LUA_T_FUNCTION: return bvalue(t1) == bvalue(t2); + case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf; case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); default: return uvalue(t1) == uvalue(t2); } diff --git a/inout.c b/inout.c index afb0ee52..cfcd224e 100644 --- a/inout.c +++ b/inout.c @@ -5,7 +5,7 @@ ** Also provides some predefined lua functions. */ -char *rcs_inout="$Id: inout.c,v 2.19 1995/05/02 18:43:03 roberto Exp roberto $"; +char *rcs_inout="$Id: inout.c,v 2.20 1995/05/16 17:23:58 roberto Exp $"; #include #include @@ -222,7 +222,7 @@ void lua_print (void) { if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj)); else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj)); - else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj))); + else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf); else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj) ); else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj)); diff --git a/lua.stx b/lua.stx index f8bdb9d7..9bb5b1f4 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 3.18 1995/04/11 17:56:30 celes Exp roberto $"; +char *rcs_luastx = "$Id: lua.stx,v 3.19 1995/06/08 19:47:28 roberto Exp $"; #include #include @@ -13,6 +13,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.18 1995/04/11 17:56:30 celes Exp roberto $" #include "tree.h" #include "table.h" #include "lua.h" +#include "func.h" /* to avoid warnings generated by yacc */ int yyparse (void); @@ -84,10 +85,10 @@ static void code_float (float n) code_byte(code.m.c4); } -static void code_code (Byte *b) +static void code_code (TFunc *tf) { CodeCode code; - code.b = b; + code.tf = tf; code_byte(code.m.c1); code_byte(code.m.c2); code_byte(code.m.c3); @@ -246,7 +247,7 @@ static void init_function (TreeNode *func) if (lua_debug) { code_byte(SETFUNCTION); - code_code((Byte *)luaI_strdup(lua_file[lua_nfile-1])); + code_code((TFunc *)luaI_strdup(lua_file[lua_nfile-1])); code_word(luaI_findconstant(func)); } } @@ -353,14 +354,15 @@ static void yyerror (char *s) /* ** Parse LUA code. */ -void lua_parse (Byte **code) +void lua_parse (TFunc *tf) { - initcode = code; + initcode = &(tf->code); *initcode = newvector(CODE_BLOCK, Byte); maincode = 0; maxmain = CODE_BLOCK; if (yyparse ()) lua_error("parse error"); (*initcode)[maincode++] = RETCODE0; + tf->size = maincode; #if LISTING { static void PrintCode (Byte *c, Byte *end); PrintCode(*initcode,*initcode+maincode); } @@ -378,7 +380,7 @@ void lua_parse (Byte **code) char *pChar; Word vWord; Long vLong; - Byte *pByte; + TFunc *pFunc; TreeNode *pNode; } @@ -401,7 +403,7 @@ void lua_parse (Byte **code) %type ffieldlist, ffieldlist1, semicolonpart %type lfieldlist, lfieldlist1 %type var, singlevar -%type body +%type body %left AND OR %left EQ NE '>' '<' LE GE @@ -437,8 +439,9 @@ function : FUNCTION NAME body { Word func = luaI_findsymbol($2); + luaI_insertfunction($4); /* may take part in GC */ s_tag(func) = LUA_T_FUNCTION; - s_bvalue(func) = $4; + lua_table[func].object.value.tf = $4; } ; @@ -465,8 +468,10 @@ method : FUNCTION NAME ':' NAME body : '(' parlist ')' block END { codereturn(); - $$ = newvector(pc, Byte); - memcpy($$, basepc, pc*sizeof(Byte)); + $$ = new(TFunc); + $$->size = pc; + $$->code = newvector(pc, Byte); + memcpy($$->code, basepc, pc*sizeof(Byte)); funcCode = basepc; maxcode=maxcurr; #if LISTING PrintCode(funcCode,funcCode+pc); @@ -805,7 +810,7 @@ static void PrintCode (Byte *code, Byte *end) int n = p-code; p++; get_code(c,p); - printf ("%d PUSHFUNCTION %p\n", n, c.b); + printf ("%d PUSHFUNCTION %p\n", n, c.tf); } break; @@ -969,7 +974,7 @@ static void PrintCode (Byte *code, Byte *end) p++; get_code(c1,p); get_word(c2,p); - printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.b, c2.w); + printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.tf, c2.w); } break; case SETLINE: diff --git a/opcode.c b/opcode.c index d68db2ad..78fabf25 100644 --- a/opcode.c +++ b/opcode.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_opcode="$Id: opcode.c,v 3.38 1995/05/16 17:23:58 roberto Exp roberto $"; +char *rcs_opcode="$Id: opcode.c,v 3.38 1995/05/16 17:23:58 roberto Exp $"; #include #include @@ -259,7 +259,7 @@ static void do_call (Object *func, StkId base, int nResults, StkId whereRes) if (tag(func) == LUA_T_CFUNCTION) firstResult = callC(fvalue(func), base); else if (tag(func) == LUA_T_FUNCTION) - firstResult = lua_execute(bvalue(func), base); + firstResult = lua_execute(func->value.tf->code, base); else { /* func is not a function */ call_funcFB(func, base, nResults, whereRes); @@ -360,24 +360,26 @@ static int do_protectedrun (Object *function, int nResults) static int do_protectedmain (void) { - Byte *code = NULL; + TFunc tf; int status; StkId oldCBase = CBase; jmp_buf myErrorJmp; jmp_buf *oldErr = errorJmp; errorJmp = &myErrorJmp; + tf.code = NULL; if (setjmp(myErrorJmp) == 0) { Object f; - lua_parse(&code); - tag(&f) = LUA_T_FUNCTION; bvalue(&f) = code; + f.tag = LUA_T_FUNCTION; + f.value.tf = &tf; + lua_parse(&tf); do_call(&f, CBase, 0, CBase); status = 0; } else status = 1; - if (code) - luaI_free(code); + if (tf.code) + luaI_free(tf.code); errorJmp = oldErr; CBase = oldCBase; top = stack+CBase; @@ -793,7 +795,9 @@ static StkId lua_execute (Byte *pc, StkId base) { CodeCode code; get_code(code,pc); - tag(top) = LUA_T_FUNCTION; bvalue(top) = code.b; + luaI_insertfunction(code.tf); /* may take part in GC */ + top->tag = LUA_T_FUNCTION; + top->value.tf = code.tf; incr_top; } break; @@ -1116,7 +1120,7 @@ static StkId lua_execute (Byte *pc, StkId base) CodeWord func; get_code(file,pc); get_word(func,pc); - lua_pushfunction ((char *)file.b, func.w); + lua_pushfunction ((char *)file.tf, func.w); } break; diff --git a/opcode.h b/opcode.h index f7b5859e..3af5597f 100644 --- a/opcode.h +++ b/opcode.h @@ -1,6 +1,6 @@ /* ** TeCGraf - PUC-Rio -** $Id: opcode.h,v 3.10 1994/12/20 21:20:36 roberto Exp celes $ +** $Id: opcode.h,v 3.11 1995/04/11 17:56:30 celes Exp $ */ #ifndef opcode_h @@ -9,6 +9,7 @@ #include "lua.h" #include "types.h" #include "tree.h" +#include "func.h" #ifndef real #define real float @@ -83,7 +84,7 @@ typedef union Cfunction f; real n; TaggedString *ts; - Byte *b; + TFunc *tf; struct Hash *a; void *u; } Value; @@ -104,7 +105,6 @@ typedef struct #define nvalue(o) ((o)->value.n) #define svalue(o) ((o)->value.ts->str) #define tsvalue(o) ((o)->value.ts) -#define bvalue(o) ((o)->value.b) #define avalue(o) ((o)->value.a) #define fvalue(o) ((o)->value.f) #define uvalue(o) ((o)->value.u) @@ -114,7 +114,6 @@ typedef struct #define s_tag(i) (tag(&s_object(i))) #define s_nvalue(i) (nvalue(&s_object(i))) #define s_svalue(i) (svalue(&s_object(i))) -#define s_bvalue(i) (bvalue(&s_object(i))) #define s_avalue(i) (avalue(&s_object(i))) #define s_fvalue(i) (fvalue(&s_object(i))) #define s_uvalue(i) (uvalue(&s_object(i))) @@ -137,7 +136,7 @@ typedef union typedef union { struct {char c1; char c2; char c3; char c4;} m; - Byte *b; + TFunc *tf; } CodeCode; #define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\ code.m.c3 = *pc++; code.m.c4 = *pc++;} @@ -149,7 +148,7 @@ char *lua_strdup (char *l); void lua_setinput (Input fn); /* from "lex.c" module */ char *lua_lasttext (void); /* from "lex.c" module */ int yylex (void); /* from "lex.c" module */ -void lua_parse (Byte **code); /* from "lua.stx" module */ +void lua_parse (TFunc *tf); /* from "lua.stx" module */ void lua_travstack (void (*fn)(Object *)); Object *luaI_Address (lua_Object o); void luaI_pushobject (Object *o); diff --git a/table.c b/table.c index be93d833..05f92c15 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** Module to control static tables */ -char *rcs_table="$Id: table.c,v 2.31 1995/05/16 19:23:55 celes Exp roberto $"; +char *rcs_table="$Id: table.c,v 2.32 1995/09/15 20:47:53 roberto Exp $"; #include @@ -166,6 +166,8 @@ void lua_markobject (Object *o) tsvalue(o)->marked = 1; else if (tag(o) == LUA_T_ARRAY) lua_hashmark (avalue(o)); + else if (o->tag == LUA_T_FUNCTION && !o->value.tf->marked) + o->value.tf->marked = 1; } @@ -182,8 +184,10 @@ void lua_pack (void) lua_travstack(lua_markobject); /* mark stack objects */ lua_travsymbol(lua_markobject); /* mark symbol table objects */ luaI_travlock(lua_markobject); /* mark locked objects */ + luaI_travfallbacks(lua_markobject); /* mark fallbacks */ recovered += lua_strcollector(); recovered += lua_hashcollector(); + recovered += luaI_funccollector(); nentity = 0; /* reset counter */ block=(16*block-7*recovered)/12; /* adapt block size */ if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;