aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-19 15:40:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-19 15:40:32 -0300
commitdfe03c7abea6a00925a56239dfaac5be2770396e (patch)
tree0e8e3cd14d37594f29cb20781af668da3a219758 /lua.stx
parent8cd67ac676fd7ff6c085e1ad6675ba6af0cb1fc3 (diff)
downloadlua-dfe03c7abea6a00925a56239dfaac5be2770396e.tar.gz
lua-dfe03c7abea6a00925a56239dfaac5be2770396e.tar.bz2
lua-dfe03c7abea6a00925a56239dfaac5be2770396e.zip
small optimizations (bit scrubbing)
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx126
1 files changed, 67 insertions, 59 deletions
diff --git a/lua.stx b/lua.stx
index fcd8cb65..4829e9ff 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2/* 2/*
3** $Id: $ 3** $Id: lua.stx,v 1.1 1997/09/16 19:33:21 roberto Exp roberto $
4** Syntax analizer and code generator 4** Syntax analizer and code generator
5** See Copyright Notice in lua.h 5** See Copyright Notice in lua.h
6*/ 6*/
@@ -111,6 +111,27 @@ static void code_opcode (OpCode op, int delta)
111} 111}
112 112
113 113
114static void code_push (OpCode op)
115{
116 code_opcode(op, 1);
117}
118
119
120static void code_pop (OpCode op)
121{
122 code_opcode(op, -1);
123}
124
125/* binary operations get 2 arguments and leave one, so they pop one */
126#define code_binop(op) code_pop(op)
127
128
129#define code_neutralop(op) code_byte(op)
130
131/* unary operations get 1 argument and leave one, so they are neutral */
132#define code_unop(op) code_neutralop(op)
133
134
114static void code_word_at (int pc, int n) 135static void code_word_at (int pc, int n)
115{ 136{
116 Word w = n; 137 Word w = n;
@@ -129,11 +150,11 @@ static void code_word (int n)
129static void code_constant (int c) 150static void code_constant (int c)
130{ 151{
131 if (c <= 255) { 152 if (c <= 255) {
132 code_opcode(PUSHCONSTANTB, 1); 153 code_push(PUSHCONSTANTB);
133 code_byte(c); 154 code_byte(c);
134 } 155 }
135 else { 156 else {
136 code_opcode(PUSHCONSTANT, 1); 157 code_push(PUSHCONSTANT);
137 code_word(c); 158 code_word(c);
138 } 159 }
139} 160}
@@ -197,13 +218,13 @@ static void code_number (real f)
197 Word i; 218 Word i;
198 if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) { 219 if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) {
199 /* f has an (short) integer value */ 220 /* f has an (short) integer value */
200 if (i <= 2) code_opcode(PUSH0 + i, 1); 221 if (i <= 2) code_push(PUSH0 + i);
201 else if (i <= 255) { 222 else if (i <= 255) {
202 code_opcode(PUSHBYTE, 1); 223 code_push(PUSHBYTE);
203 code_byte(i); 224 code_byte(i);
204 } 225 }
205 else { 226 else {
206 code_opcode(PUSHWORD, 1); 227 code_push(PUSHWORD);
207 code_word(i); 228 code_word(i);
208 } 229 }
209 } 230 }
@@ -324,9 +345,9 @@ static void pushupvalue (TaggedString *n)
324 luaY_syntaxerror("cannot access an upvalue in current scope", n->str); 345 luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
325 i = indexupvalue(n); 346 i = indexupvalue(n);
326 if (i == 0) 347 if (i == 0)
327 code_opcode(PUSHUPVALUE0, 1); 348 code_push(PUSHUPVALUE0);
328 else { 349 else {
329 code_opcode(PUSHUPVALUE, 1); 350 code_push(PUSHUPVALUE);
330 code_byte(i); 351 code_byte(i);
331 } 352 }
332} 353}
@@ -336,7 +357,7 @@ void luaY_codedebugline (int line)
336{ 357{
337 static int lastline = 0; 358 static int lastline = 0;
338 if (lua_debug && line != lastline) { 359 if (lua_debug && line != lastline) {
339 code_opcode(SETLINE, 0); 360 code_neutralop(SETLINE);
340 code_word(line); 361 code_word(line);
341 lastline = line; 362 lastline = line;
342 } 363 }
@@ -351,7 +372,7 @@ static void adjuststack (int n)
351 } 372 }
352 else if (n < 0) { 373 else if (n < 0) {
353 if (n == -1) 374 if (n == -1)
354 code_opcode(PUSHNIL, 1); 375 code_push(PUSHNIL);
355 else { 376 else {
356 code_opcode(PUSHNILS, -n); 377 code_opcode(PUSHNILS, -n);
357 code_byte(-n); 378 code_byte(-n);
@@ -406,20 +427,20 @@ static void code_args (int dots)
406static void lua_pushvar (vardesc number) 427static void lua_pushvar (vardesc number)
407{ 428{
408 if (number > 0) { /* global var */ 429 if (number > 0) { /* global var */
409 code_opcode(PUSHGLOBAL, 1); 430 code_push(PUSHGLOBAL);
410 code_word(number-1); 431 code_word(number-1);
411 } 432 }
412 else if (number < 0) { /* local var */ 433 else if (number < 0) { /* local var */
413 number = (-number) - 1; 434 number = (-number) - 1;
414 if (number < 10) 435 if (number < 10)
415 code_opcode(PUSHLOCAL0 + number, 1); 436 code_push(PUSHLOCAL0 + number);
416 else { 437 else {
417 code_opcode(PUSHLOCAL, 1); 438 code_push(PUSHLOCAL);
418 code_byte(number); 439 code_byte(number);
419 } 440 }
420 } 441 }
421 else { 442 else {
422 code_opcode(PUSHTABLE, -1); 443 code_pop(GETTABLE);
423 } 444 }
424} 445}
425 446
@@ -429,15 +450,15 @@ static void storevar (vardesc number)
429 if (number == 0) /* indexed var */ 450 if (number == 0) /* indexed var */
430 code_opcode(SETTABLE0, -3); 451 code_opcode(SETTABLE0, -3);
431 else if (number > 0) { /* global var */ 452 else if (number > 0) { /* global var */
432 code_opcode(SETGLOBAL, -1); 453 code_pop(SETGLOBAL);
433 code_word(number-1); 454 code_word(number-1);
434 } 455 }
435 else { /* number < 0 - local var */ 456 else { /* number < 0 - local var */
436 number = (-number) - 1; 457 number = (-number) - 1;
437 if (number < 10) 458 if (number < 10)
438 code_opcode(SETLOCAL0 + number, -1); 459 code_pop(SETLOCAL0 + number);
439 else { 460 else {
440 code_opcode(SETLOCAL, -1); 461 code_pop(SETLOCAL);
441 code_byte(number); 462 code_byte(number);
442 } 463 }
443 } 464 }
@@ -453,7 +474,7 @@ static int lua_codestore (int i, int left)
453 return left; 474 return left;
454 } 475 }
455 else { /* indexed var with values in between*/ 476 else { /* indexed var with values in between*/
456 code_opcode(SETTABLE, -1); 477 code_pop(SETTABLE);
457 code_byte(left+i); /* number of elements between table/index and value */ 478 code_byte(left+i); /* number of elements between table/index and value */
458 return left+2; /* table/index are not poped, since they are not on top */ 479 return left+2; /* table/index are not poped, since they are not on top */
459 } 480 }
@@ -485,7 +506,7 @@ static void code_shortcircuit (int pc, Byte jmp)
485 506
486static void codereturn (void) 507static void codereturn (void)
487{ 508{
488 code_opcode(RETCODE, 0); 509 code_neutralop(RETCODE);
489 code_byte(currState->nlocalvar); 510 code_byte(currState->nlocalvar);
490 currState->stacksize = currState->nlocalvar; 511 currState->stacksize = currState->nlocalvar;
491} 512}
@@ -542,7 +563,7 @@ static void init_func (void)
542static TProtoFunc *close_func (void) 563static TProtoFunc *close_func (void)
543{ 564{
544 TProtoFunc *f = currState->f; 565 TProtoFunc *f = currState->f;
545 code_opcode(ENDCODE, 0); 566 code_neutralop(ENDCODE);
546 f->code[0] = currState->maxstacksize; 567 f->code[0] = currState->maxstacksize;
547 f->code = luaM_reallocvector(f->code, currState->pc, Byte); 568 f->code = luaM_reallocvector(f->code, currState->pc, Byte);
548 f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject); 569 f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
@@ -624,8 +645,7 @@ chunk : statlist ret
624 ; 645 ;
625 646
626statlist : /* empty */ 647statlist : /* empty */
627 | statlist stat sc { if (currState->stacksize != currState->nlocalvar) 648 | statlist stat sc
628 { luaY_error("contagem"); exit(1); }}
629 ; 649 ;
630 650
631sc : /* empty */ | ';' ; 651sc : /* empty */ | ';' ;
@@ -707,7 +727,7 @@ ret : /* empty */
707PrepJump : /* empty */ 727PrepJump : /* empty */
708 { 728 {
709 $$ = currState->pc; 729 $$ = currState->pc;
710 code_opcode(0, 0); /* open space */ 730 code_byte(0); /* open space */
711 code_word(0); 731 code_word(0);
712 } 732 }
713 ; 733 ;
@@ -718,47 +738,35 @@ PrepJumpPop : PrepJump { $$ = $1; deltastack(-1); /* pop condition */ }
718expr1 : expr { adjust_functioncall($1, 1); } 738expr1 : expr { adjust_functioncall($1, 1); }
719 ; 739 ;
720 740
721expr : '(' expr ')' { $$ = $2; } 741expr : '(' expr ')' { $$ = $2; }
722 | expr1 EQ expr1 { code_opcode(EQOP, -1); $$ = 0; } 742 | expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
723 | expr1 '<' expr1 { code_opcode(LTOP, -1); $$ = 0; } 743 | expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
724 | expr1 '>' expr1 { code_opcode(GTOP, -1); $$ = 0; } 744 | expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
725 | expr1 NE expr1 { code_opcode(NEQOP, -1); $$ = 0; } 745 | expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
726 | expr1 LE expr1 { code_opcode(LEOP, -1); $$ = 0; } 746 | expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
727 | expr1 GE expr1 { code_opcode(GEOP, -1); $$ = 0; } 747 | expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
728 | expr1 '+' expr1 { code_opcode(ADDOP, -1); $$ = 0; } 748 | expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
729 | expr1 '-' expr1 { code_opcode(SUBOP, -1); $$ = 0; } 749 | expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
730 | expr1 '*' expr1 { code_opcode(MULTOP, -1); $$ = 0; } 750 | expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
731 | expr1 '/' expr1 { code_opcode(DIVOP, -1); $$ = 0; } 751 | expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
732 | expr1 '^' expr1 { code_opcode(POWOP, -1); $$ = 0; } 752 | expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
733 | expr1 CONC expr1 { code_opcode(CONCOP, -1); $$ = 0; } 753 | expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
734 | '-' expr1 %prec UNARY { code_opcode(MINUSOP, 0); $$ = 0;} 754 | '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
755 | NOT expr1 { code_unop(NOTOP); $$ = 0;}
735 | table { $$ = 0; } 756 | table { $$ = 0; }
736 | varexp { $$ = 0;} 757 | varexp { $$ = 0;}
737 | NUMBER { code_number($1); $$ = 0; } 758 | NUMBER { code_number($1); $$ = 0; }
738 | STRING 759 | STRING { code_string($1); $$ = 0; }
739 { 760 | NIL {code_push(PUSHNIL); $$ = 0; }
740 code_string($1); 761 | functioncall { $$ = $1; }
741 $$ = 0; 762 | expr1 AND PrepJumpPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
742 } 763 | expr1 OR PrepJumpPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
743 | NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
744 | functioncall { $$ = $1; }
745 | NOT expr1 { code_opcode(NOTOP, 0); $$ = 0;}
746 | expr1 AND PrepJumpPop expr1
747 {
748 code_shortcircuit($3, ONFJMP);
749 $$ = 0;
750 }
751 | expr1 OR PrepJumpPop expr1
752 {
753 code_shortcircuit($3, ONTJMP);
754 $$ = 0;
755 }
756 | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; } 764 | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
757 ; 765 ;
758 766
759table : 767table :
760 { 768 {
761 code_opcode(CREATEARRAY, 1); 769 code_push(CREATEARRAY);
762 $<vInt>$ = currState->pc; code_word(0); 770 $<vInt>$ = currState->pc; code_word(0);
763 } 771 }
764 '{' fieldlist '}' 772 '{' fieldlist '}'
@@ -779,7 +787,7 @@ functioncall : funcvalue funcParams
779funcvalue : varexp { $$ = 0; } 787funcvalue : varexp { $$ = 0; }
780 | varexp ':' NAME 788 | varexp ':' NAME
781 { 789 {
782 code_opcode(PUSHSELF, 1); 790 code_push(PUSHSELF);
783 code_word(string_constant($3)); 791 code_word(string_constant($3));
784 $$ = 1; 792 $$ = 1;
785 } 793 }