aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-10-25 11:05:51 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-10-25 11:05:51 -0200
commit9efc257d9d774501af4538a289729f3e8e5e3d7e (patch)
treec4113f56f9bdcf6e1e66ac11f2cf5483387f3bc8 /lua.stx
parentfa71304e54f304ceceddef3a4b97b38228822e94 (diff)
downloadlua-9efc257d9d774501af4538a289729f3e8e5e3d7e.tar.gz
lua-9efc257d9d774501af4538a289729f3e8e5e3d7e.tar.bz2
lua-9efc257d9d774501af4538a289729f3e8e5e3d7e.zip
new method to keep debug line information: current line is stored on the
Lua stack, just below (new) base, with tag LUA_T_LINE. SETLINE opcodes are generated by lex.
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx214
1 files changed, 125 insertions, 89 deletions
diff --git a/lua.stx b/lua.stx
index ed839466..d0f832d7 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.20 1995/10/04 14:20:26 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 3.21 1995/10/17 11:58:41 roberto Exp roberto $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -37,6 +37,7 @@ static Byte *basepc;
37static int maincode; 37static int maincode;
38static int pc; 38static int pc;
39 39
40
40#define MAXVAR 32 41#define MAXVAR 32
41static Long varbuffer[MAXVAR]; /* variables in an assignment list; 42static Long varbuffer[MAXVAR]; /* variables in an assignment list;
42 it's long to store negative Word values */ 43 it's long to store negative Word values */
@@ -235,15 +236,17 @@ static void lua_codeadjust (int n)
235 } 236 }
236} 237}
237 238
238static void init_function (TreeNode *func) 239static void change2main (void)
239{ 240{
240 if (funcCode == NULL) /* first function */ 241 /* (re)store main values */
241 { 242 pc=maincode; basepc=*initcode; maxcurr=maxmain;
242 funcCode = newvector(CODE_BLOCK, Byte); 243 nlocalvar=0;
243 maxcode = CODE_BLOCK; 244}
244 } 245
245 pc=0; basepc=funcCode; maxcurr=maxcode; 246static void savemain (void)
246 nlocalvar=0; 247{
248 /* save main values */
249 maincode=pc; *initcode=basepc; maxmain=maxcurr;
247} 250}
248 251
249static void codereturn (void) 252static void codereturn (void)
@@ -257,29 +260,43 @@ static void codereturn (void)
257 } 260 }
258} 261}
259 262
260static void codedebugline (void) 263void luaI_codedebugline (int line)
261{ 264{
262 if (lua_debug) 265 static int lastline = 0;
266 if (lua_debug && line != lastline)
263 { 267 {
264 code_byte(SETLINE); 268 code_byte(SETLINE);
265 code_word(lua_linenumber); 269 code_word(line);
270 lastline = line;
266 } 271 }
267} 272}
268 273
269static void adjust_mult_assign (int vars, int exps, int temps) 274static int adjust_functioncall (Long exp, int i)
270{ 275{
271 if (exps < 0) 276 if (exp <= 0)
277 return -exp; /* exp is -list length */
278 else
272 { 279 {
273 int r = vars - (-exps-1); 280 int temp = basepc[exp];
274 if (r >= 0) 281 basepc[exp] = i;
275 code_byte(r); 282 return temp+i;
283 }
284}
285
286static void adjust_mult_assign (int vars, Long exps, int temps)
287{
288 if (exps > 0)
289 { /* must correct function call */
290 int diff = vars - basepc[exps];
291 if (diff >= 0)
292 adjust_functioncall(exps, diff);
276 else 293 else
277 { 294 {
278 code_byte(0); 295 adjust_functioncall(exps, 0);
279 lua_codeadjust(temps); 296 lua_codeadjust(temps);
280 } 297 }
281 } 298 }
282 else if (vars != exps) 299 else if (vars != -exps)
283 lua_codeadjust(temps); 300 lua_codeadjust(temps);
284} 301}
285 302
@@ -349,11 +366,14 @@ static void yyerror (char *s)
349*/ 366*/
350void lua_parse (TFunc *tf) 367void lua_parse (TFunc *tf)
351{ 368{
369 lua_debug = 0;
352 initcode = &(tf->code); 370 initcode = &(tf->code);
353 *initcode = newvector(CODE_BLOCK, Byte); 371 *initcode = newvector(CODE_BLOCK, Byte);
354 maincode = 0; 372 maincode = 0;
355 maxmain = CODE_BLOCK; 373 maxmain = CODE_BLOCK;
374 change2main();
356 if (yyparse ()) lua_error("parse error"); 375 if (yyparse ()) lua_error("parse error");
376 savemain();
357 (*initcode)[maincode++] = RETCODE0; 377 (*initcode)[maincode++] = RETCODE0;
358 tf->size = maincode; 378 tf->size = maincode;
359#if LISTING 379#if LISTING
@@ -391,10 +411,15 @@ void lua_parse (TFunc *tf)
391%token <vInt> DEBUG 411%token <vInt> DEBUG
392 412
393%type <vLong> PrepJump 413%type <vLong> PrepJump
394%type <vInt> expr, exprlist, exprlist1, varlist1, funcParams, funcvalue 414%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
415 counter (which has list length); if <= 0, -list lenght */
416%type <vLong> functioncall, expr /* if != 0, points to function return
417 counter */
418%type <vInt> varlist1, funcParams, funcvalue
395%type <vInt> fieldlist, localdeclist, decinit 419%type <vInt> fieldlist, localdeclist, decinit
396%type <vInt> ffieldlist, ffieldlist1, semicolonpart 420%type <vInt> ffieldlist, ffieldlist1, semicolonpart
397%type <vInt> lfieldlist, lfieldlist1 421%type <vInt> lfieldlist, lfieldlist1
422%type <vInt> functiontoken
398%type <vLong> var, singlevar 423%type <vLong> var, singlevar
399%type <pFunc> body 424%type <pFunc> body
400 425
@@ -411,63 +436,63 @@ void lua_parse (TFunc *tf)
411 436
412 437
413functionlist : /* empty */ 438functionlist : /* empty */
414 | functionlist 439 | functionlist globalstat
415 {
416 pc=maincode; basepc=*initcode; maxcurr=maxmain;
417 nlocalvar=0;
418 }
419 stat sc
420 {
421 maincode=pc; *initcode=basepc; maxmain=maxcurr;
422 }
423 | functionlist function 440 | functionlist function
424 | functionlist method 441 | functionlist method
425 | functionlist setdebug
426 ; 442 ;
427 443
428function : FUNCTION NAME 444globalstat : stat sc
429 { 445 | setdebug
430 init_function($2); 446 ;
431 $<vInt>$ = lua_linenumber; 447
432 } 448function : functiontoken NAME body
433 body
434 { 449 {
435 Word func = luaI_findsymbol($2); 450 Word func = luaI_findsymbol($2);
436 luaI_insertfunction($4); /* may take part in GC */ 451 luaI_insertfunction($3); /* may take part in GC */
437 s_tag(func) = LUA_T_FUNCTION; 452 s_tag(func) = LUA_T_FUNCTION;
438 lua_table[func].object.value.tf = $4; 453 lua_table[func].object.value.tf = $3;
439 $4->lineDefined = $<vInt>3; 454 $3->lineDefined = $1;
440 $4->name1 = $2->ts.str; 455 $3->name1 = $2->ts.str;
441 $4->name2 = NULL; 456 $3->name2 = NULL;
442 $4->fileName = lua_parsedfile; 457 $3->fileName = lua_parsedfile;
443 } 458 }
444 ; 459 ;
445 460
446method : FUNCTION NAME ':' NAME 461method : functiontoken NAME ':' NAME
447 { 462 {
448 init_function($4);
449 add_localvar(luaI_findsymbolbyname("self")); 463 add_localvar(luaI_findsymbolbyname("self"));
450 $<vInt>$ = lua_linenumber;
451 } 464 }
452 body 465 body
453 { 466 {
454 /* assign function to table field */ 467 /* assign function to table field */
455 pc=maincode; basepc=*initcode; maxcurr=maxmain;
456 nlocalvar=0;
457 lua_pushvar(luaI_findsymbol($2)+1); 468 lua_pushvar(luaI_findsymbol($2)+1);
458 code_byte(PUSHSTRING); 469 code_byte(PUSHSTRING);
459 code_word(luaI_findconstant($4)); 470 code_word(luaI_findconstant($4));
460 code_byte(PUSHFUNCTION); 471 code_byte(PUSHFUNCTION);
461 code_code($6); 472 code_code($6);
462 code_byte(STOREINDEXED0); 473 code_byte(STOREINDEXED0);
463 maincode=pc; *initcode=basepc; maxmain=maxcurr; 474 $6->lineDefined = $1;
464 $6->lineDefined = $<vInt>5;
465 $6->name1 = $4->ts.str; 475 $6->name1 = $4->ts.str;
466 $6->name2 = $2->ts.str; 476 $6->name2 = $2->ts.str;
467 $6->fileName = lua_parsedfile; 477 $6->fileName = lua_parsedfile;
468 } 478 }
469 ; 479 ;
470 480
481functiontoken : FUNCTION
482 {
483 if (funcCode == NULL) /* first function */
484 {
485 funcCode = newvector(CODE_BLOCK, Byte);
486 maxcode = CODE_BLOCK;
487 }
488 savemain(); /* save main values */
489 /* set func values */
490 pc=0; basepc=funcCode; maxcurr=maxcode;
491 nlocalvar=0;
492 $$ = lua_linenumber;
493 }
494 ;
495
471body : '(' parlist ')' block END 496body : '(' parlist ')' block END
472 { 497 {
473 codereturn(); 498 codereturn();
@@ -475,10 +500,12 @@ body : '(' parlist ')' block END
475 $$->size = pc; 500 $$->size = pc;
476 $$->code = newvector(pc, Byte); 501 $$->code = newvector(pc, Byte);
477 memcpy($$->code, basepc, pc*sizeof(Byte)); 502 memcpy($$->code, basepc, pc*sizeof(Byte));
503 /* save func values */
478 funcCode = basepc; maxcode=maxcurr; 504 funcCode = basepc; maxcode=maxcurr;
479#if LISTING 505#if LISTING
480 PrintCode(funcCode,funcCode+pc); 506 PrintCode(funcCode,funcCode+pc);
481#endif 507#endif
508 change2main(); /* change back to main code */
482 } 509 }
483 ; 510 ;
484 511
@@ -488,11 +515,7 @@ statlist : /* empty */
488 515
489sc : /* empty */ | ';' ; 516sc : /* empty */ | ';' ;
490 517
491stat : { codedebugline(); } stat1 ; 518stat : IF expr1 THEN PrepJump block PrepJump elsepart END
492
493cond : { codedebugline(); } expr1 ;
494
495stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
496 { codeIf($4, $6); } 519 { codeIf($4, $6); }
497 520
498 | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END 521 | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
@@ -503,7 +526,7 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
503 code_word_at(basepc+$7+1, pc - ($<vLong>2)); 526 code_word_at(basepc+$7+1, pc - ($<vLong>2));
504 } 527 }
505 528
506 | REPEAT {$<vLong>$=pc;} block UNTIL cond PrepJump 529 | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
507 { 530 {
508 basepc[$6] = IFFUPJMP; 531 basepc[$6] = IFFUPJMP;
509 code_word_at(basepc+$6+1, pc - ($<vLong>2)); 532 code_word_at(basepc+$6+1, pc - ($<vLong>2));
@@ -520,7 +543,7 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
520 lua_codeadjust (0); 543 lua_codeadjust (0);
521 } 544 }
522 } 545 }
523 | functioncall { code_byte(0); } 546 | functioncall
524 | LOCAL localdeclist decinit 547 | LOCAL localdeclist decinit
525 { nlocalvar += $2; 548 { nlocalvar += $2;
526 adjust_mult_assign($2, $3, 0); 549 adjust_mult_assign($2, $3, 0);
@@ -529,7 +552,7 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
529 552
530elsepart : /* empty */ 553elsepart : /* empty */
531 | ELSE block 554 | ELSE block
532 | ELSEIF cond THEN PrepJump block PrepJump elsepart 555 | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
533 { codeIf($4, $6); } 556 { codeIf($4, $6); }
534 ; 557 ;
535 558
@@ -544,9 +567,9 @@ block : {$<vInt>$ = nlocalvar;} statlist ret
544 ; 567 ;
545 568
546ret : /* empty */ 569ret : /* empty */
547 | RETURN { codedebugline(); } exprlist sc 570 | RETURN exprlist sc
548 { 571 {
549 if ($3 < 0) code_byte(MULT_RET); 572 adjust_functioncall($2, MULT_RET);
550 codereturn(); 573 codereturn();
551 } 574 }
552 ; 575 ;
@@ -558,46 +581,49 @@ PrepJump : /* empty */
558 code_word (0); 581 code_word (0);
559 } 582 }
560 583
561expr1 : expr { if ($1 == 0) code_byte(1); } 584expr1 : expr
585 {
586 adjust_functioncall($1, 1);
587 }
562 ; 588 ;
563 589
564expr : '(' expr ')' { $$ = $2; } 590expr : '(' expr ')' { $$ = $2; }
565 | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; } 591 | expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
566 | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; } 592 | expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
567 | expr1 '>' expr1 { code_byte(GTOP); $$ = 1; } 593 | expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
568 | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; } 594 | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
569 | expr1 LE expr1 { code_byte(LEOP); $$ = 1; } 595 | expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
570 | expr1 GE expr1 { code_byte(GEOP); $$ = 1; } 596 | expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
571 | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; } 597 | expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
572 | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; } 598 | expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
573 | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; } 599 | expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
574 | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; } 600 | expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
575 | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; } 601 | expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
576 | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; } 602 | expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
577 | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;} 603 | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
578 | table { $$ = 1; } 604 | table { $$ = 0; }
579 | varexp { $$ = 1;} 605 | varexp { $$ = 0;}
580 | NUMBER { code_number($1); $$ = 1; } 606 | NUMBER { code_number($1); $$ = 0; }
581 | STRING 607 | STRING
582 { 608 {
583 code_byte(PUSHSTRING); 609 code_byte(PUSHSTRING);
584 code_word($1); 610 code_word($1);
585 $$ = 1; 611 $$ = 0;
586 } 612 }
587 | NIL {code_byte(PUSHNIL); $$ = 1; } 613 | NIL {code_byte(PUSHNIL); $$ = 0; }
588 | functioncall { $$ = 0; } 614 | functioncall { $$ = $1; }
589 | NOT expr1 { code_byte(NOTOP); $$ = 1;} 615 | NOT expr1 { code_byte(NOTOP); $$ = 0;}
590 | expr1 AND PrepJump {code_byte(POP); } expr1 616 | expr1 AND PrepJump {code_byte(POP); } expr1
591 { 617 {
592 basepc[$3] = ONFJMP; 618 basepc[$3] = ONFJMP;
593 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1)); 619 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
594 $$ = 1; 620 $$ = 0;
595 } 621 }
596 | expr1 OR PrepJump {code_byte(POP); } expr1 622 | expr1 OR PrepJump {code_byte(POP); } expr1
597 { 623 {
598 basepc[$3] = ONTJMP; 624 basepc[$3] = ONTJMP;
599 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1)); 625 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
600 $$ = 1; 626 $$ = 0;
601 } 627 }
602 ; 628 ;
603 629
@@ -613,7 +639,12 @@ table :
613 ; 639 ;
614 640
615functioncall : funcvalue funcParams 641functioncall : funcvalue funcParams
616 { code_byte(CALLFUNC); code_byte($1+$2); } 642 {
643 code_byte(CALLFUNC);
644 code_byte($1+$2);
645 $$ = pc;
646 code_byte(0); /* may be modified by other rules */
647 }
617 ; 648 ;
618 649
619funcvalue : varexp { $$ = 0; } 650funcvalue : varexp { $$ = 0; }
@@ -626,7 +657,7 @@ funcvalue : varexp { $$ = 0; }
626 ; 657 ;
627 658
628funcParams : '(' exprlist ')' 659funcParams : '(' exprlist ')'
629 { if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; } 660 { $$ = adjust_functioncall($2, 1); }
630 | table { $$ = 1; } 661 | table { $$ = 1; }
631 ; 662 ;
632 663
@@ -634,11 +665,15 @@ exprlist : /* empty */ { $$ = 0; }
634 | exprlist1 { $$ = $1; } 665 | exprlist1 { $$ = $1; }
635 ; 666 ;
636 667
637exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; } 668exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
638 | exprlist1 ',' { if ($1 < 0) code_byte(1); } expr 669 | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
639 { 670 {
640 int r = $1 < 0 ? -$1 : $1; 671 if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
641 $$ = ($4 == 0) ? -(r+1) : r+1; 672 else
673 {
674 adjust_functioncall($4, $<vLong>3);
675 $$ = $4;
676 }
642 } 677 }
643 ; 678 ;
644 679
@@ -757,7 +792,8 @@ decinit : /* empty */ { $$ = 0; }
757 | '=' exprlist1 { $$ = $2; } 792 | '=' exprlist1 { $$ = $2; }
758 ; 793 ;
759 794
760setdebug : DEBUG {lua_debug = $1;} 795setdebug : DEBUG { lua_debug = $1; }
796 ;
761 797
762%% 798%%
763 799