diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-02-13 18:38:20 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-02-13 18:38:20 -0200 |
| commit | 7f3d01c200df33b67d1f4b0198adae7ea7af8e10 (patch) | |
| tree | 2b40697bd4f825e913c9bcac2905f49f6c8d64cf | |
| parent | 0ea84a3e799b45ea4a0e3dc2274e6034f0d2fb06 (diff) | |
| download | lua-7f3d01c200df33b67d1f4b0198adae7ea7af8e10.tar.gz lua-7f3d01c200df33b67d1f4b0198adae7ea7af8e10.tar.bz2 lua-7f3d01c200df33b67d1f4b0198adae7ea7af8e10.zip | |
otimizacao do codigo para construtores.
correcao do tamanho do buffer de codigo.
Diffstat (limited to '')
| -rw-r--r-- | lua.stx | 173 |
1 files changed, 115 insertions, 58 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 1.2 1993/12/22 21:19:23 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 1.3 1993/12/28 16:42:29 roberto Exp roberto $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| @@ -12,6 +12,8 @@ char *rcs_luastx = "$Id: lua.stx,v 1.2 1993/12/22 21:19:23 roberto Exp roberto $ | |||
| 12 | #include "table.h" | 12 | #include "table.h" |
| 13 | #include "lua.h" | 13 | #include "lua.h" |
| 14 | 14 | ||
| 15 | #define LISTING 0 | ||
| 16 | |||
| 15 | #ifndef ALIGNMENT | 17 | #ifndef ALIGNMENT |
| 16 | #define ALIGNMENT (sizeof(void *)) | 18 | #define ALIGNMENT (sizeof(void *)) |
| 17 | #endif | 19 | #endif |
| @@ -19,7 +21,7 @@ char *rcs_luastx = "$Id: lua.stx,v 1.2 1993/12/22 21:19:23 roberto Exp roberto $ | |||
| 19 | #ifndef MAXCODE | 21 | #ifndef MAXCODE |
| 20 | #define MAXCODE 1024 | 22 | #define MAXCODE 1024 |
| 21 | #endif | 23 | #endif |
| 22 | static long buffer[MAXCODE]; | 24 | static long buffer[MAXCODE*sizeof(Byte)/sizeof(long)]; |
| 23 | static Byte *code = (Byte *)buffer; | 25 | static Byte *code = (Byte *)buffer; |
| 24 | static long mainbuffer[MAXCODE]; | 26 | static long mainbuffer[MAXCODE]; |
| 25 | static Byte *maincode = (Byte *)mainbuffer; | 27 | static Byte *maincode = (Byte *)mainbuffer; |
| @@ -27,16 +29,20 @@ static Byte *basepc; | |||
| 27 | static Byte *pc; | 29 | static Byte *pc; |
| 28 | 30 | ||
| 29 | #define MAXVAR 32 | 31 | #define MAXVAR 32 |
| 30 | static long varbuffer[MAXVAR]; | 32 | static long varbuffer[MAXVAR]; /* variables in an assignment list; |
| 31 | static Byte nvarbuffer=0; /* number of variables at a list */ | 33 | it's long to store negative Word values */ |
| 34 | static int nvarbuffer=0; /* number of variables at a list */ | ||
| 35 | |||
| 36 | static Word localvar[STACKGAP]; /* store local variable names */ | ||
| 37 | static int nlocalvar=0; /* number of local variables */ | ||
| 32 | 38 | ||
| 33 | static Word localvar[STACKGAP]; | 39 | #define MAXFIELDS FIELDS_PER_FLUSH*2 |
| 34 | static Byte nlocalvar=0; /* number of local variables */ | 40 | static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ |
| 41 | static int nfields=0; | ||
| 35 | static int ntemp; /* number of temporary var into stack */ | 42 | static int ntemp; /* number of temporary var into stack */ |
| 36 | static int err; /* flag to indicate error */ | 43 | static int err; /* flag to indicate error */ |
| 37 | 44 | ||
| 38 | /* Internal functions */ | 45 | /* Internal functions */ |
| 39 | #define align(n) align_n(sizeof(n)) | ||
| 40 | 46 | ||
| 41 | static void code_byte (Byte c) | 47 | static void code_byte (Byte c) |
| 42 | { | 48 | { |
| @@ -48,6 +54,14 @@ static void code_byte (Byte c) | |||
| 48 | *pc++ = c; | 54 | *pc++ = c; |
| 49 | } | 55 | } |
| 50 | 56 | ||
| 57 | #define align(t,n) align_n(sizeof(t),n) | ||
| 58 | static void align_n (unsigned size, int gap) | ||
| 59 | { | ||
| 60 | if (size > ALIGNMENT) size = ALIGNMENT; | ||
| 61 | while (((pc+gap-code)%size) != 0) /* +gap to include BYTECODEs */ | ||
| 62 | code_byte (NOP); | ||
| 63 | } | ||
| 64 | |||
| 51 | static void code_word (Word n) | 65 | static void code_word (Word n) |
| 52 | { | 66 | { |
| 53 | if (pc-basepc>MAXCODE-sizeof(Word)) | 67 | if (pc-basepc>MAXCODE-sizeof(Word)) |
| @@ -70,6 +84,43 @@ static void code_float (float n) | |||
| 70 | pc += sizeof(float); | 84 | pc += sizeof(float); |
| 71 | } | 85 | } |
| 72 | 86 | ||
| 87 | static void push_field (Word name) | ||
| 88 | { | ||
| 89 | if (nfields < STACKGAP-1) | ||
| 90 | fields[nfields++] = name; | ||
| 91 | else | ||
| 92 | { | ||
| 93 | lua_error ("too many fields in a constructor"); | ||
| 94 | err = 1; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | static void flush_record (int n) | ||
| 99 | { | ||
| 100 | int i; | ||
| 101 | if (n == 0) return; | ||
| 102 | align(Word,2); /* two bytes before the actual word */ | ||
| 103 | code_byte(STORERECORD); | ||
| 104 | code_byte(n); | ||
| 105 | for (i=0; i<n; i++) | ||
| 106 | code_word(lua_findconstant(s_name(fields[--nfields]))); | ||
| 107 | ntemp -= n; | ||
| 108 | } | ||
| 109 | |||
| 110 | static void flush_list (int m, int n) | ||
| 111 | { | ||
| 112 | if (n == 0) return; | ||
| 113 | if (m == 0) | ||
| 114 | code_byte(STORELIST0); | ||
| 115 | else | ||
| 116 | { | ||
| 117 | code_byte(STORELIST); | ||
| 118 | code_byte(m); | ||
| 119 | } | ||
| 120 | code_byte(n); | ||
| 121 | ntemp-=n; | ||
| 122 | } | ||
| 123 | |||
| 73 | static void incr_ntemp (void) | 124 | static void incr_ntemp (void) |
| 74 | { | 125 | { |
| 75 | if (ntemp+nlocalvar+MAXVAR+1 < STACKGAP) | 126 | if (ntemp+nlocalvar+MAXVAR+1 < STACKGAP) |
| @@ -103,13 +154,6 @@ static void incr_nvarbuffer (void) | |||
| 103 | } | 154 | } |
| 104 | } | 155 | } |
| 105 | 156 | ||
| 106 | static void align_n (unsigned size) | ||
| 107 | { | ||
| 108 | if (size > ALIGNMENT) size = ALIGNMENT; | ||
| 109 | while (((pc+1-code)%size) != 0) /* +1 to include BYTECODE */ | ||
| 110 | code_byte (NOP); | ||
| 111 | } | ||
| 112 | |||
| 113 | static void code_number (float f) | 157 | static void code_number (float f) |
| 114 | { Word i = (Word)f; | 158 | { Word i = (Word)f; |
| 115 | if (f == (float)i) /* f has an (short) integer value */ | 159 | if (f == (float)i) /* f has an (short) integer value */ |
| @@ -122,14 +166,14 @@ static void code_number (float f) | |||
| 122 | } | 166 | } |
| 123 | else | 167 | else |
| 124 | { | 168 | { |
| 125 | align(Word); | 169 | align(Word,1); |
| 126 | code_byte(PUSHWORD); | 170 | code_byte(PUSHWORD); |
| 127 | code_word(i); | 171 | code_word(i); |
| 128 | } | 172 | } |
| 129 | } | 173 | } |
| 130 | else | 174 | else |
| 131 | { | 175 | { |
| 132 | align(float); | 176 | align(float,1); |
| 133 | code_byte(PUSHFLOAT); | 177 | code_byte(PUSHFLOAT); |
| 134 | code_float(f); | 178 | code_float(f); |
| 135 | } | 179 | } |
| @@ -179,7 +223,13 @@ static void code_number (float f) | |||
| 179 | 223 | ||
| 180 | 224 | ||
| 181 | functionlist : /* empty */ | 225 | functionlist : /* empty */ |
| 182 | | functionlist {pc=basepc=maincode; nlocalvar=0;} stat sc {maincode=pc;} | 226 | | functionlist { pc=basepc=maincode; nlocalvar=0;} stat sc |
| 227 | { | ||
| 228 | maincode=pc; | ||
| 229 | #if LISTING | ||
| 230 | PrintCode(basepc,maincode); | ||
| 231 | #endif | ||
| 232 | } | ||
| 183 | | functionlist function | 233 | | functionlist function |
| 184 | | functionlist setdebug | 234 | | functionlist setdebug |
| 185 | ; | 235 | ; |
| @@ -188,7 +238,7 @@ function : FUNCTION NAME {pc=basepc=code; nlocalvar=0;} '(' parlist ')' | |||
| 188 | { | 238 | { |
| 189 | if (lua_debug) | 239 | if (lua_debug) |
| 190 | { | 240 | { |
| 191 | align(Word); | 241 | align(Word,1); |
| 192 | code_byte(SETFUNCTION); | 242 | code_byte(SETFUNCTION); |
| 193 | code_word($1); | 243 | code_word($1); |
| 194 | code_word($2); | 244 | code_word($2); |
| @@ -203,6 +253,9 @@ function : FUNCTION NAME {pc=basepc=code; nlocalvar=0;} '(' parlist ')' | |||
| 203 | s_tag($2) = T_FUNCTION; | 253 | s_tag($2) = T_FUNCTION; |
| 204 | s_bvalue($2) = calloc (pc-code, sizeof(Byte)); | 254 | s_bvalue($2) = calloc (pc-code, sizeof(Byte)); |
| 205 | memcpy (s_bvalue($2), code, (pc-code)*sizeof(Byte)); | 255 | memcpy (s_bvalue($2), code, (pc-code)*sizeof(Byte)); |
| 256 | #if LISTING | ||
| 257 | PrintCode(code,pc); | ||
| 258 | #endif | ||
| 206 | } | 259 | } |
| 207 | ; | 260 | ; |
| 208 | 261 | ||
| @@ -214,7 +267,7 @@ stat : { | |||
| 214 | ntemp = 0; | 267 | ntemp = 0; |
| 215 | if (lua_debug) | 268 | if (lua_debug) |
| 216 | { | 269 | { |
| 217 | align(Word); code_byte(SETLINE); code_word(lua_linenumber); | 270 | align(Word,1); code_byte(SETLINE); code_word(lua_linenumber); |
| 218 | } | 271 | } |
| 219 | } | 272 | } |
| 220 | stat1 | 273 | stat1 |
| @@ -311,7 +364,7 @@ block : {$<vInt>$ = nlocalvar;} statlist {ntemp = 0;} ret | |||
| 311 | ; | 364 | ; |
| 312 | 365 | ||
| 313 | ret : /* empty */ | 366 | ret : /* empty */ |
| 314 | | { if (lua_debug){align(Word);code_byte(SETLINE);code_word(lua_linenumber);}} | 367 | | { if (lua_debug){align(Word,1);code_byte(SETLINE);code_word(lua_linenumber);}} |
| 315 | RETURN exprlist sc | 368 | RETURN exprlist sc |
| 316 | { | 369 | { |
| 317 | if (lua_debug) code_byte(RESET); | 370 | if (lua_debug) code_byte(RESET); |
| @@ -321,7 +374,7 @@ ret : /* empty */ | |||
| 321 | 374 | ||
| 322 | PrepJump : /* empty */ | 375 | PrepJump : /* empty */ |
| 323 | { | 376 | { |
| 324 | align(Word); | 377 | align(Word,1); |
| 325 | $$ = pc; | 378 | $$ = pc; |
| 326 | code_byte(0); /* open space */ | 379 | code_byte(0); /* open space */ |
| 327 | code_word (0); | 380 | code_word (0); |
| @@ -354,7 +407,7 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 354 | | NUMBER { code_number($1); $$ = 1; } | 407 | | NUMBER { code_number($1); $$ = 1; } |
| 355 | | STRING | 408 | | STRING |
| 356 | { | 409 | { |
| 357 | align(Word); | 410 | align(Word,1); |
| 358 | code_byte(PUSHSTRING); | 411 | code_byte(PUSHSTRING); |
| 359 | code_word($1); | 412 | code_word($1); |
| 360 | $$ = 1; | 413 | $$ = 1; |
| @@ -366,7 +419,7 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 366 | $$ = 0; | 419 | $$ = 0; |
| 367 | if (lua_debug) | 420 | if (lua_debug) |
| 368 | { | 421 | { |
| 369 | align(Word); code_byte(SETLINE); code_word(lua_linenumber); | 422 | align(Word,1); code_byte(SETLINE); code_word(lua_linenumber); |
| 370 | } | 423 | } |
| 371 | } | 424 | } |
| 372 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} | 425 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} |
| @@ -410,7 +463,7 @@ typeconstructor: '@' | |||
| 410 | $$ = 0; | 463 | $$ = 0; |
| 411 | if (lua_debug) | 464 | if (lua_debug) |
| 412 | { | 465 | { |
| 413 | align(Word); code_byte(SETLINE); code_word(lua_linenumber); | 466 | align(Word,1); code_byte(SETLINE); code_word(lua_linenumber); |
| 414 | } | 467 | } |
| 415 | } | 468 | } |
| 416 | } | 469 | } |
| @@ -447,8 +500,12 @@ objectname : /* empty */ {$$=-1;} | |||
| 447 | | NAME {$$=$1;} | 500 | | NAME {$$=$1;} |
| 448 | ; | 501 | ; |
| 449 | 502 | ||
| 450 | fieldlist : '{' ffieldlist '}' { $$ = $2; } | 503 | fieldlist : '{' ffieldlist '}' { flush_record($2%FIELDS_PER_FLUSH); $$ = $2; } |
| 451 | | '[' lfieldlist ']' { $$ = $2; } | 504 | | '[' lfieldlist ']' |
| 505 | { | ||
| 506 | flush_list($2/FIELDS_PER_FLUSH, $2%FIELDS_PER_FLUSH); | ||
| 507 | $$ = $2; | ||
| 508 | } | ||
| 452 | ; | 509 | ; |
| 453 | 510 | ||
| 454 | ffieldlist : /* empty */ { $$ = 0; } | 511 | ffieldlist : /* empty */ { $$ = 0; } |
| @@ -456,39 +513,29 @@ ffieldlist : /* empty */ { $$ = 0; } | |||
| 456 | ; | 513 | ; |
| 457 | 514 | ||
| 458 | ffieldlist1 : ffield {$$=1;} | 515 | ffieldlist1 : ffield {$$=1;} |
| 459 | | ffieldlist1 ',' ffield {$$=$1+1;} | 516 | | ffieldlist1 ',' ffield |
| 517 | { | ||
| 518 | $$=$1+1; | ||
| 519 | if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH); | ||
| 520 | } | ||
| 460 | ; | 521 | ; |
| 461 | 522 | ||
| 462 | ffield : NAME | 523 | ffield : NAME '=' expr1 { push_field($1); } |
| 463 | { | ||
| 464 | align(Word); | ||
| 465 | code_byte(PUSHSTRING); | ||
| 466 | code_word(lua_findconstant (s_name($1))); | ||
| 467 | incr_ntemp(); | ||
| 468 | } | ||
| 469 | '=' expr1 | ||
| 470 | { | ||
| 471 | code_byte(STOREFIELD); | ||
| 472 | ntemp-=2; | ||
| 473 | } | ||
| 474 | ; | 524 | ; |
| 475 | 525 | ||
| 476 | lfieldlist : /* empty */ { $$ = 0; } | 526 | lfieldlist : /* empty */ { $$ = 0; } |
| 477 | | lfieldlist1 { $$ = $1; } | 527 | | lfieldlist1 { $$ = $1; } |
| 478 | ; | 528 | ; |
| 479 | 529 | ||
| 480 | lfieldlist1 : { code_number(1); } lfield {$$=1;} | 530 | lfieldlist1 : expr1 {$$=1;} |
| 481 | | lfieldlist1 ',' { code_number($1+1); } lfield | 531 | | lfieldlist1 ',' expr1 |
| 482 | {$$=$1+1;} | 532 | { |
| 533 | $$=$1+1; | ||
| 534 | if ($$%FIELDS_PER_FLUSH == 0) | ||
| 535 | flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH); | ||
| 536 | } | ||
| 483 | ; | 537 | ; |
| 484 | 538 | ||
| 485 | lfield : expr1 | ||
| 486 | { | ||
| 487 | code_byte(STOREFIELD); | ||
| 488 | ntemp-=2; | ||
| 489 | } | ||
| 490 | ; | ||
| 491 | |||
| 492 | varlist1 : var | 539 | varlist1 : var |
| 493 | { | 540 | { |
| 494 | nvarbuffer = 0; | 541 | nvarbuffer = 0; |
| @@ -517,7 +564,7 @@ var : NAME | |||
| 517 | } | 564 | } |
| 518 | | var {lua_pushvar ($1);} '.' NAME | 565 | | var {lua_pushvar ($1);} '.' NAME |
| 519 | { | 566 | { |
| 520 | align(Word); | 567 | align(Word,1); |
| 521 | code_byte(PUSHSTRING); | 568 | code_byte(PUSHSTRING); |
| 522 | code_word(lua_findconstant (s_name($4))); incr_ntemp(); | 569 | code_word(lua_findconstant (s_name($4))); incr_ntemp(); |
| 523 | $$ = 0; /* indexed variable */ | 570 | $$ = 0; /* indexed variable */ |
| @@ -556,7 +603,7 @@ static void lua_pushvar (long number) | |||
| 556 | { | 603 | { |
| 557 | if (number > 0) /* global var */ | 604 | if (number > 0) /* global var */ |
| 558 | { | 605 | { |
| 559 | align(Word); | 606 | align(Word,1); |
| 560 | code_byte(PUSHGLOBAL); | 607 | code_byte(PUSHGLOBAL); |
| 561 | code_word(number-1); | 608 | code_word(number-1); |
| 562 | incr_ntemp(); | 609 | incr_ntemp(); |
| @@ -589,7 +636,7 @@ static void lua_codestore (int i) | |||
| 589 | { | 636 | { |
| 590 | if (varbuffer[i] > 0) /* global var */ | 637 | if (varbuffer[i] > 0) /* global var */ |
| 591 | { | 638 | { |
| 592 | align(Word); | 639 | align(Word,1); |
| 593 | code_byte(STOREGLOBAL); | 640 | code_byte(STOREGLOBAL); |
| 594 | code_word(varbuffer[i]-1); | 641 | code_word(varbuffer[i]-1); |
| 595 | } | 642 | } |
| @@ -652,13 +699,12 @@ int lua_parse (void) | |||
| 652 | } | 699 | } |
| 653 | 700 | ||
| 654 | 701 | ||
| 655 | #if 0 | 702 | #if LISTING |
| 656 | 703 | ||
| 657 | static void PrintCode (void) | 704 | static void PrintCode (Byte *p, Byte *end) |
| 658 | { | 705 | { |
| 659 | Byte *p = code; | ||
| 660 | printf ("\n\nCODE\n"); | 706 | printf ("\n\nCODE\n"); |
| 661 | while (p != pc) | 707 | while (p != end) |
| 662 | { | 708 | { |
| 663 | switch ((OpCode)*p) | 709 | switch ((OpCode)*p) |
| 664 | { | 710 | { |
| @@ -707,7 +753,7 @@ static void PrintCode (void) | |||
| 707 | p++; | 753 | p++; |
| 708 | break; | 754 | break; |
| 709 | case STORELOCAL: | 755 | case STORELOCAL: |
| 710 | printf ("%d STORELOCAK %d\n", p-code, *(++p)); | 756 | printf ("%d STORELOCAL %d\n", p-code, *(++p)); |
| 711 | p++; | 757 | p++; |
| 712 | break; | 758 | break; |
| 713 | case STOREGLOBAL: | 759 | case STOREGLOBAL: |
| @@ -718,7 +764,18 @@ static void PrintCode (void) | |||
| 718 | case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p)); | 764 | case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p)); |
| 719 | p++; | 765 | p++; |
| 720 | break; | 766 | break; |
| 721 | case STOREFIELD: printf ("%d STOREFIELD\n", (p++)-code); break; | 767 | case STORELIST0: |
| 768 | printf("%d STORELIST0 %d\n", p-code, *(++p)); | ||
| 769 | p++; | ||
| 770 | break; | ||
| 771 | case STORELIST: | ||
| 772 | printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2)); | ||
| 773 | p+=3; | ||
| 774 | break; | ||
| 775 | case STORERECORD: | ||
| 776 | printf("%d STORERECORD %d\n", p-code, *(++p)); | ||
| 777 | p += *p*sizeof(Word) + 1; | ||
| 778 | break; | ||
| 722 | case ADJUST: | 779 | case ADJUST: |
| 723 | printf ("%d ADJUST %d\n", p-code, *(++p)); | 780 | printf ("%d ADJUST %d\n", p-code, *(++p)); |
| 724 | p++; | 781 | p++; |
