From 14b6ab354038d408fc446188de003b405a080998 Mon Sep 17 00:00:00 2001 From: Waldemar Celes Date: Tue, 19 Apr 1994 16:06:15 -0300 Subject: [PATCH] Nova estrategia de alocacao de buffers para codigo intermediario. --- lua.stx | 147 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 52 deletions(-) diff --git a/lua.stx b/lua.stx index 9dca9e76..b37a246e 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 2.1 1994/04/15 19:02:04 celes Exp celes $"; +char *rcs_luastx = "$Id: lua.stx,v 2.2 1994/04/15 21:30:12 celes Exp celes $"; #include #include @@ -14,15 +14,17 @@ char *rcs_luastx = "$Id: lua.stx,v 2.1 1994/04/15 19:02:04 celes Exp celes $"; #include "table.h" #include "lua.h" -#define LISTING 1 +#define LISTING 0 -#ifndef MAXCODE -#define MAXCODE 1024 +#ifndef GAPCODE +#define GAPCODE 50 #endif -static long buffer[MAXCODE*sizeof(Byte)/sizeof(long)]; -static Byte *code = (Byte *)buffer; -static long mainbuffer[MAXCODE]; -static Byte *maincode = (Byte *)mainbuffer; +static Word maxcode; +static Word maxmain; +static Word maxcurr ; +static Byte *code = NULL; +static Byte *maincode; +static Byte *initcode; static Byte *basepc; static Byte *pc; @@ -44,10 +46,22 @@ static int err; /* flag to indicate error */ static void code_byte (Byte c) { - if (pc-basepc>MAXCODE-1) + if (pc-basepc>maxcurr-2) /* 1 byte free to code HALT of main code */ { - lua_error ("code buffer overflow"); - err = 1; + Word d = pc-basepc; + Byte *new = calloc(maxcurr+GAPCODE, sizeof(Byte));; + memcpy(new, basepc, maxcurr*sizeof(Byte)); + maxcurr += GAPCODE; + free(basepc); + basepc=new; + +/* basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); */ + if (basepc == NULL) + { + lua_error ("not enough memory"); + err = 1; + } + pc = basepc+d; } *pc++ = c; } @@ -196,7 +210,7 @@ static void code_number (float f) %token NAME %token DEBUG -%type PrepJump +%type PrepJump %type expr, exprlist, exprlist1, varlist1, typeconstructor %type fieldlist, localdeclist %type ffieldlist, ffieldlist1 @@ -216,9 +230,14 @@ static void code_number (float f) functionlist : /* empty */ - | functionlist { pc=basepc=maincode; nlocalvar=0;} stat sc + | functionlist + { + pc=maincode; basepc=initcode; maxcurr=maxmain; + nlocalvar=0; + } + stat sc { - maincode=pc; + maincode=pc; initcode=basepc; maxmain=maxcurr; } | functionlist function | functionlist setdebug @@ -226,9 +245,19 @@ functionlist : /* empty */ function : FUNCTION NAME { - $$ = lua_findsymbol($2); - pc=basepc=code; + if (code == NULL) /* first function */ + { + code = (Byte *) calloc(GAPCODE, sizeof(Byte)); + if (code == NULL) + { + lua_error("not enough memory"); + err = 1; + } + maxcode = GAPCODE; + } + pc=basepc=code; maxcurr=maxcode; nlocalvar=0; + $$ = lua_findsymbol($2); } '(' parlist ')' { @@ -246,10 +275,16 @@ function : FUNCTION NAME if (lua_debug) code_byte(RESET); code_byte(RETCODE); code_byte(nlocalvar); s_tag($3) = T_FUNCTION; - s_bvalue($3) = calloc (pc-code, sizeof(Byte)); - memcpy (s_bvalue($3), code, (pc-code)*sizeof(Byte)); + s_bvalue($3) = calloc (pc-basepc, sizeof(Byte)); + if (s_bvalue($3) == NULL) + { + lua_error("not enough memory"); + err = 1; + } + memcpy (s_bvalue($3), basepc, (pc-basepc)*sizeof(Byte)); + code = basepc; maxcode=maxcurr; #if LISTING -PrintCode(code,pc,(Byte*)buffer); +PrintCode(code,pc); #endif } ; @@ -273,7 +308,7 @@ sc : /* empty */ | ';' ; stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END { { - Byte *elseinit = $6 + sizeof(Word)+1; + Byte *elseinit = basepc + $6 + sizeof(Word)+1; if (pc - elseinit == 0) /* no else */ { pc -= sizeof(Word)+1; @@ -281,29 +316,29 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END } else { - *($6) = JMP; - code_word_at($6+1, pc - elseinit); + *(basepc+$6) = JMP; + code_word_at(basepc+$6+1, pc - elseinit); } - *($4) = IFFJMP; - code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1)); + *(basepc+$4) = IFFJMP; + code_word_at(basepc+$4+1,elseinit-(basepc+$4+sizeof(Word)+1)); } } - | WHILE {$$ = pc;} expr1 DO PrepJump block PrepJump END + | WHILE {$$=pc-basepc;} expr1 DO PrepJump block PrepJump END { - *($5) = IFFJMP; - code_word_at($5+1, pc - ($5 + sizeof(Word)+1)); + *(basepc+$5) = IFFJMP; + code_word_at(basepc+$5+1, pc - (basepc+$5 + sizeof(Word)+1)); - *($7) = UPJMP; - code_word_at($7+1, pc - $2); + *(basepc+$7) = UPJMP; + code_word_at(basepc+$7+1, pc - (basepc+$2)); } - | REPEAT {$$ = pc;} block UNTIL expr1 PrepJump + | REPEAT {$$=pc-basepc;} block UNTIL expr1 PrepJump { - *($6) = IFFUPJMP; - code_word_at($6+1, pc - $2); + *(basepc+$6) = IFFUPJMP; + code_word_at(basepc+$6+1, pc - (basepc+$2)); } @@ -329,7 +364,7 @@ elsepart : /* empty */ | ELSEIF expr1 THEN PrepJump block PrepJump elsepart { { - Byte *elseinit = $6 + sizeof(Word)+1; + Byte *elseinit = basepc + $6 + sizeof(Word)+1; if (pc - elseinit == 0) /* no else */ { pc -= sizeof(Word)+1; @@ -338,11 +373,11 @@ elsepart : /* empty */ } else { - *($6) = JMP; - code_word_at($6+1, pc - elseinit); + *(basepc+$6) = JMP; + code_word_at(basepc+$6+1, pc - elseinit); } - *($4) = IFFJMP; - code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1)); + *(basepc+$4) = IFFJMP; + code_word_at(basepc+$4+1, elseinit - (basepc+$4 + sizeof(Word)+1)); } } ; @@ -368,7 +403,7 @@ ret : /* empty */ PrepJump : /* empty */ { - $$ = pc; + $$ = pc-basepc; code_byte(0); /* open space */ code_word (0); } @@ -417,14 +452,14 @@ expr : '(' expr ')' { $$ = $2; } | NOT expr1 { code_byte(NOTOP); $$ = 1;} | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1 { - *($3) = ONFJMP; - code_word_at($3+1, pc - ($3 + sizeof(Word)+1)); + *(basepc+$3) = ONFJMP; + code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); $$ = 1; } | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1 { - *($3) = ONTJMP; - code_word_at($3+1, pc - ($3 + sizeof(Word)+1)); + *(basepc+$3) = ONTJMP; + code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); $$ = 1; } ; @@ -432,13 +467,13 @@ expr : '(' expr ')' { $$ = $2; } typeconstructor: '@' { code_byte(PUSHBYTE); - $$ = pc; code_byte(0); + $$ = pc-basepc; code_byte(0); incr_ntemp(); code_byte(CREATEARRAY); } objectname fieldlist { - *($2) = $4; + *(basepc+$2) = $4; if ($3 < 0) /* there is no function to be called */ { $$ = 1; @@ -698,23 +733,32 @@ int yywrap (void) */ int lua_parse (void) { - Byte *initcode = maincode; + Byte *init = maincode = (Byte *) calloc(GAPCODE, sizeof(Byte)); + if (init== NULL) + { + lua_error("not enough memory"); + return 1; + } + initcode = init; + maxmain = GAPCODE; err = 0; if (yyparse () || (err==1)) return 1; *maincode++ = HALT; + init = initcode; #if LISTING - PrintCode(basepc,maincode,(Byte*)mainbuffer); + PrintCode(init,maincode); #endif - if (lua_execute (initcode)) return 1; - maincode = initcode; + if (lua_execute (init)) return 1; + free(init); return 0; } #if LISTING -static void PrintCode (Byte *p, Byte *end, Byte *code) +static void PrintCode (Byte *code, Byte *end) { + Byte *p = code; printf ("\n\nCODE\n"); while (p != end) { @@ -887,7 +931,7 @@ static void PrintCode (Byte *p, Byte *end, Byte *code) printf ("%d RETCODE %d\n", p-code, *(++p)); p++; break; - case HALT: printf ("%d HALT\n", (p++)-code); break; + case HALT: printf ("%d HALT\n", (p++)-code); break; case SETFUNCTION: { CodeWord c1, c2; @@ -907,9 +951,8 @@ static void PrintCode (Byte *p, Byte *end, Byte *code) printf ("%d SETLINE %d\n", n, c.w); } break; - - case RESET: printf ("%d RESET\n", (p++)-code); break; + case RESET: printf ("%d RESET\n", (p++)-code); break; default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break; } }