mirror of https://github.com/rusefi/lua.git
Nova estrategia de alocacao de buffers para codigo intermediario.
This commit is contained in:
parent
3ee5e71d0b
commit
14b6ab3540
141
lua.stx
141
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 <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -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 "table.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#define LISTING 1
|
#define LISTING 0
|
||||||
|
|
||||||
#ifndef MAXCODE
|
#ifndef GAPCODE
|
||||||
#define MAXCODE 1024
|
#define GAPCODE 50
|
||||||
#endif
|
#endif
|
||||||
static long buffer[MAXCODE*sizeof(Byte)/sizeof(long)];
|
static Word maxcode;
|
||||||
static Byte *code = (Byte *)buffer;
|
static Word maxmain;
|
||||||
static long mainbuffer[MAXCODE];
|
static Word maxcurr ;
|
||||||
static Byte *maincode = (Byte *)mainbuffer;
|
static Byte *code = NULL;
|
||||||
|
static Byte *maincode;
|
||||||
|
static Byte *initcode;
|
||||||
static Byte *basepc;
|
static Byte *basepc;
|
||||||
static Byte *pc;
|
static Byte *pc;
|
||||||
|
|
||||||
|
@ -44,11 +46,23 @@ static int err; /* flag to indicate error */
|
||||||
|
|
||||||
static void code_byte (Byte c)
|
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");
|
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;
|
err = 1;
|
||||||
}
|
}
|
||||||
|
pc = basepc+d;
|
||||||
|
}
|
||||||
*pc++ = c;
|
*pc++ = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +210,7 @@ static void code_number (float f)
|
||||||
%token <pChar> NAME
|
%token <pChar> NAME
|
||||||
%token <vInt> DEBUG
|
%token <vInt> DEBUG
|
||||||
|
|
||||||
%type <pByte> PrepJump
|
%type <vWord> PrepJump
|
||||||
%type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor
|
%type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor
|
||||||
%type <vInt> fieldlist, localdeclist
|
%type <vInt> fieldlist, localdeclist
|
||||||
%type <vInt> ffieldlist, ffieldlist1
|
%type <vInt> ffieldlist, ffieldlist1
|
||||||
|
@ -216,9 +230,14 @@ static void code_number (float f)
|
||||||
|
|
||||||
|
|
||||||
functionlist : /* empty */
|
functionlist : /* empty */
|
||||||
| functionlist { pc=basepc=maincode; nlocalvar=0;} stat sc
|
| functionlist
|
||||||
{
|
{
|
||||||
maincode=pc;
|
pc=maincode; basepc=initcode; maxcurr=maxmain;
|
||||||
|
nlocalvar=0;
|
||||||
|
}
|
||||||
|
stat sc
|
||||||
|
{
|
||||||
|
maincode=pc; initcode=basepc; maxmain=maxcurr;
|
||||||
}
|
}
|
||||||
| functionlist function
|
| functionlist function
|
||||||
| functionlist setdebug
|
| functionlist setdebug
|
||||||
|
@ -226,9 +245,19 @@ functionlist : /* empty */
|
||||||
|
|
||||||
function : FUNCTION NAME
|
function : FUNCTION NAME
|
||||||
{
|
{
|
||||||
$<vWord>$ = lua_findsymbol($2);
|
if (code == NULL) /* first function */
|
||||||
pc=basepc=code;
|
{
|
||||||
|
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;
|
nlocalvar=0;
|
||||||
|
$<vWord>$ = lua_findsymbol($2);
|
||||||
}
|
}
|
||||||
'(' parlist ')'
|
'(' parlist ')'
|
||||||
{
|
{
|
||||||
|
@ -246,10 +275,16 @@ function : FUNCTION NAME
|
||||||
if (lua_debug) code_byte(RESET);
|
if (lua_debug) code_byte(RESET);
|
||||||
code_byte(RETCODE); code_byte(nlocalvar);
|
code_byte(RETCODE); code_byte(nlocalvar);
|
||||||
s_tag($<vWord>3) = T_FUNCTION;
|
s_tag($<vWord>3) = T_FUNCTION;
|
||||||
s_bvalue($<vWord>3) = calloc (pc-code, sizeof(Byte));
|
s_bvalue($<vWord>3) = calloc (pc-basepc, sizeof(Byte));
|
||||||
memcpy (s_bvalue($<vWord>3), code, (pc-code)*sizeof(Byte));
|
if (s_bvalue($<vWord>3) == NULL)
|
||||||
|
{
|
||||||
|
lua_error("not enough memory");
|
||||||
|
err = 1;
|
||||||
|
}
|
||||||
|
memcpy (s_bvalue($<vWord>3), basepc, (pc-basepc)*sizeof(Byte));
|
||||||
|
code = basepc; maxcode=maxcurr;
|
||||||
#if LISTING
|
#if LISTING
|
||||||
PrintCode(code,pc,(Byte*)buffer);
|
PrintCode(code,pc);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -273,7 +308,7 @@ sc : /* empty */ | ';' ;
|
||||||
stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
|
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 */
|
if (pc - elseinit == 0) /* no else */
|
||||||
{
|
{
|
||||||
pc -= sizeof(Word)+1;
|
pc -= sizeof(Word)+1;
|
||||||
|
@ -281,29 +316,29 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*($6) = JMP;
|
*(basepc+$6) = JMP;
|
||||||
code_word_at($6+1, pc - elseinit);
|
code_word_at(basepc+$6+1, pc - elseinit);
|
||||||
}
|
}
|
||||||
*($4) = IFFJMP;
|
*(basepc+$4) = IFFJMP;
|
||||||
code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1));
|
code_word_at(basepc+$4+1,elseinit-(basepc+$4+sizeof(Word)+1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
| WHILE {$<pByte>$ = pc;} expr1 DO PrepJump block PrepJump END
|
| WHILE {$<vWord>$=pc-basepc;} expr1 DO PrepJump block PrepJump END
|
||||||
|
|
||||||
{
|
{
|
||||||
*($5) = IFFJMP;
|
*(basepc+$5) = IFFJMP;
|
||||||
code_word_at($5+1, pc - ($5 + sizeof(Word)+1));
|
code_word_at(basepc+$5+1, pc - (basepc+$5 + sizeof(Word)+1));
|
||||||
|
|
||||||
*($7) = UPJMP;
|
*(basepc+$7) = UPJMP;
|
||||||
code_word_at($7+1, pc - $<pByte>2);
|
code_word_at(basepc+$7+1, pc - (basepc+$<vWord>2));
|
||||||
}
|
}
|
||||||
|
|
||||||
| REPEAT {$<pByte>$ = pc;} block UNTIL expr1 PrepJump
|
| REPEAT {$<vWord>$=pc-basepc;} block UNTIL expr1 PrepJump
|
||||||
|
|
||||||
{
|
{
|
||||||
*($6) = IFFUPJMP;
|
*(basepc+$6) = IFFUPJMP;
|
||||||
code_word_at($6+1, pc - $<pByte>2);
|
code_word_at(basepc+$6+1, pc - (basepc+$<vWord>2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -329,7 +364,7 @@ elsepart : /* empty */
|
||||||
| ELSEIF expr1 THEN PrepJump block PrepJump elsepart
|
| 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 */
|
if (pc - elseinit == 0) /* no else */
|
||||||
{
|
{
|
||||||
pc -= sizeof(Word)+1;
|
pc -= sizeof(Word)+1;
|
||||||
|
@ -338,11 +373,11 @@ elsepart : /* empty */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*($6) = JMP;
|
*(basepc+$6) = JMP;
|
||||||
code_word_at($6+1, pc - elseinit);
|
code_word_at(basepc+$6+1, pc - elseinit);
|
||||||
}
|
}
|
||||||
*($4) = IFFJMP;
|
*(basepc+$4) = IFFJMP;
|
||||||
code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1));
|
code_word_at(basepc+$4+1, elseinit - (basepc+$4 + sizeof(Word)+1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -368,7 +403,7 @@ ret : /* empty */
|
||||||
|
|
||||||
PrepJump : /* empty */
|
PrepJump : /* empty */
|
||||||
{
|
{
|
||||||
$$ = pc;
|
$$ = pc-basepc;
|
||||||
code_byte(0); /* open space */
|
code_byte(0); /* open space */
|
||||||
code_word (0);
|
code_word (0);
|
||||||
}
|
}
|
||||||
|
@ -417,14 +452,14 @@ expr : '(' expr ')' { $$ = $2; }
|
||||||
| NOT expr1 { code_byte(NOTOP); $$ = 1;}
|
| NOT expr1 { code_byte(NOTOP); $$ = 1;}
|
||||||
| expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1
|
| expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1
|
||||||
{
|
{
|
||||||
*($3) = ONFJMP;
|
*(basepc+$3) = ONFJMP;
|
||||||
code_word_at($3+1, pc - ($3 + sizeof(Word)+1));
|
code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1));
|
||||||
$$ = 1;
|
$$ = 1;
|
||||||
}
|
}
|
||||||
| expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1
|
| expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1
|
||||||
{
|
{
|
||||||
*($3) = ONTJMP;
|
*(basepc+$3) = ONTJMP;
|
||||||
code_word_at($3+1, pc - ($3 + sizeof(Word)+1));
|
code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1));
|
||||||
$$ = 1;
|
$$ = 1;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -432,13 +467,13 @@ expr : '(' expr ')' { $$ = $2; }
|
||||||
typeconstructor: '@'
|
typeconstructor: '@'
|
||||||
{
|
{
|
||||||
code_byte(PUSHBYTE);
|
code_byte(PUSHBYTE);
|
||||||
$<pByte>$ = pc; code_byte(0);
|
$<vWord>$ = pc-basepc; code_byte(0);
|
||||||
incr_ntemp();
|
incr_ntemp();
|
||||||
code_byte(CREATEARRAY);
|
code_byte(CREATEARRAY);
|
||||||
}
|
}
|
||||||
objectname fieldlist
|
objectname fieldlist
|
||||||
{
|
{
|
||||||
*($<pByte>2) = $4;
|
*(basepc+$<vWord>2) = $4;
|
||||||
if ($3 < 0) /* there is no function to be called */
|
if ($3 < 0) /* there is no function to be called */
|
||||||
{
|
{
|
||||||
$$ = 1;
|
$$ = 1;
|
||||||
|
@ -698,23 +733,32 @@ int yywrap (void)
|
||||||
*/
|
*/
|
||||||
int lua_parse (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;
|
err = 0;
|
||||||
if (yyparse () || (err==1)) return 1;
|
if (yyparse () || (err==1)) return 1;
|
||||||
*maincode++ = HALT;
|
*maincode++ = HALT;
|
||||||
|
init = initcode;
|
||||||
#if LISTING
|
#if LISTING
|
||||||
PrintCode(basepc,maincode,(Byte*)mainbuffer);
|
PrintCode(init,maincode);
|
||||||
#endif
|
#endif
|
||||||
if (lua_execute (initcode)) return 1;
|
if (lua_execute (init)) return 1;
|
||||||
maincode = initcode;
|
free(init);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if LISTING
|
#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");
|
printf ("\n\nCODE\n");
|
||||||
while (p != end)
|
while (p != end)
|
||||||
{
|
{
|
||||||
|
@ -909,7 +953,6 @@ static void PrintCode (Byte *p, Byte *end, Byte *code)
|
||||||
break;
|
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;
|
default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue