diff options
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 39 |
1 files changed, 21 insertions, 18 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 1.58 2001/01/29 13:14:49 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.59 2001/01/29 15:26:40 roberto Exp roberto $ |
| 3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -460,25 +460,26 @@ int luaK_code1 (FuncState *fs, OpCode o, int arg1) { | |||
| 460 | int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | 460 | int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { |
| 461 | Proto *f; | 461 | Proto *f; |
| 462 | Instruction i = previous_instruction(fs); | 462 | Instruction i = previous_instruction(fs); |
| 463 | int delta = (int)luaK_opproperties[o].push - (int)luaK_opproperties[o].pop; | 463 | int push = (int)luaK_opproperties[o].push; |
| 464 | int pop = (int)luaK_opproperties[o].pop; | ||
| 464 | int optm = 0; /* 1 when there is an optimization */ | 465 | int optm = 0; /* 1 when there is an optimization */ |
| 465 | switch (o) { | 466 | switch (o) { |
| 466 | case OP_CLOSURE: { | 467 | case OP_CLOSURE: { |
| 467 | delta = -arg2+1; | 468 | pop = arg2; |
| 468 | break; | 469 | break; |
| 469 | } | 470 | } |
| 470 | case OP_SETTABLE: { | 471 | case OP_SETTABLE: { |
| 471 | delta = -arg2; | 472 | pop = arg2; |
| 472 | break; | 473 | break; |
| 473 | } | 474 | } |
| 474 | case OP_SETLIST: { | 475 | case OP_SETLIST: { |
| 475 | if (arg2 == 0) return NO_JUMP; /* nothing to do */ | 476 | if (arg2 == 0) return NO_JUMP; /* nothing to do */ |
| 476 | delta = -arg2; | 477 | pop = arg2; |
| 477 | break; | 478 | break; |
| 478 | } | 479 | } |
| 479 | case OP_SETMAP: { | 480 | case OP_SETMAP: { |
| 480 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ | 481 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ |
| 481 | delta = -2*arg1; | 482 | pop = 2*arg1; |
| 482 | break; | 483 | break; |
| 483 | } | 484 | } |
| 484 | case OP_RETURN: { | 485 | case OP_RETURN: { |
| @@ -491,7 +492,7 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | |||
| 491 | } | 492 | } |
| 492 | case OP_PUSHNIL: { | 493 | case OP_PUSHNIL: { |
| 493 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ | 494 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ |
| 494 | delta = arg1; | 495 | push = arg1; |
| 495 | switch(GET_OPCODE(i)) { | 496 | switch(GET_OPCODE(i)) { |
| 496 | case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break; | 497 | case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break; |
| 497 | default: break; | 498 | default: break; |
| @@ -500,7 +501,7 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | |||
| 500 | } | 501 | } |
| 501 | case OP_POP: { | 502 | case OP_POP: { |
| 502 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ | 503 | if (arg1 == 0) return NO_JUMP; /* nothing to do */ |
| 503 | delta = -arg1; | 504 | pop = arg1; |
| 504 | switch(GET_OPCODE(i)) { | 505 | switch(GET_OPCODE(i)) { |
| 505 | case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break; | 506 | case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break; |
| 506 | default: break; | 507 | default: break; |
| @@ -539,7 +540,7 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | |||
| 539 | break; | 540 | break; |
| 540 | } | 541 | } |
| 541 | case OP_CONCAT: { | 542 | case OP_CONCAT: { |
| 542 | delta = -arg1+1; | 543 | pop = arg1; |
| 543 | switch(GET_OPCODE(i)) { | 544 | switch(GET_OPCODE(i)) { |
| 544 | case OP_CONCAT: /* `a..b..c' */ | 545 | case OP_CONCAT: /* `a..b..c' */ |
| 545 | SETARG_U(i, GETARG_U(i)+1); | 546 | SETARG_U(i, GETARG_U(i)+1); |
| @@ -573,7 +574,7 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | |||
| 573 | case OP_JMPEQ: { | 574 | case OP_JMPEQ: { |
| 574 | if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */ | 575 | if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */ |
| 575 | i = CREATE_0(OP_NOT); | 576 | i = CREATE_0(OP_NOT); |
| 576 | delta = -1; /* just undo effect of previous PUSHNIL */ | 577 | pop = 1; /* just undo effect of previous PUSHNIL */ |
| 577 | optm = 1; | 578 | optm = 1; |
| 578 | } | 579 | } |
| 579 | break; | 580 | break; |
| @@ -637,12 +638,14 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) { | |||
| 637 | break; | 638 | break; |
| 638 | } | 639 | } |
| 639 | default: { | 640 | default: { |
| 640 | lua_assert(delta != VD); | ||
| 641 | break; | 641 | break; |
| 642 | } | 642 | } |
| 643 | } | 643 | } |
| 644 | f = fs->f; | 644 | f = fs->f; |
| 645 | luaK_deltastack(fs, delta); | 645 | lua_assert(push != VD); |
| 646 | lua_assert(pop != VD); | ||
| 647 | luaK_deltastack(fs, push); | ||
| 648 | luaK_deltastack(fs, -pop); | ||
| 646 | if (optm) { /* optimize: put instruction in place of last one */ | 649 | if (optm) { /* optimize: put instruction in place of last one */ |
| 647 | f->code[fs->pc-1] = i; /* change previous instruction */ | 650 | f->code[fs->pc-1] = i; /* change previous instruction */ |
| 648 | return fs->pc-1; /* do not generate new instruction */ | 651 | return fs->pc-1; /* do not generate new instruction */ |
| @@ -668,7 +671,7 @@ const OpProperties luaK_opproperties[NUM_OPCODES] = { | |||
| 668 | {iAB, 0, 0}, /* OP_CALL */ | 671 | {iAB, 0, 0}, /* OP_CALL */ |
| 669 | {iAB, 0, 0}, /* OP_TAILCALL */ | 672 | {iAB, 0, 0}, /* OP_TAILCALL */ |
| 670 | {iU, VD, 0}, /* OP_PUSHNIL */ | 673 | {iU, VD, 0}, /* OP_PUSHNIL */ |
| 671 | {iU, VD, 0}, /* OP_POP */ | 674 | {iU, 0, VD}, /* OP_POP */ |
| 672 | {iS, 1, 0}, /* OP_PUSHINT */ | 675 | {iS, 1, 0}, /* OP_PUSHINT */ |
| 673 | {iU, 1, 0}, /* OP_PUSHSTRING */ | 676 | {iU, 1, 0}, /* OP_PUSHSTRING */ |
| 674 | {iU, 1, 0}, /* OP_PUSHNUM */ | 677 | {iU, 1, 0}, /* OP_PUSHNUM */ |
| @@ -683,16 +686,16 @@ const OpProperties luaK_opproperties[NUM_OPCODES] = { | |||
| 683 | {iU, 1, 0}, /* OP_CREATETABLE */ | 686 | {iU, 1, 0}, /* OP_CREATETABLE */ |
| 684 | {iU, 0, 1}, /* OP_SETLOCAL */ | 687 | {iU, 0, 1}, /* OP_SETLOCAL */ |
| 685 | {iU, 0, 1}, /* OP_SETGLOBAL */ | 688 | {iU, 0, 1}, /* OP_SETGLOBAL */ |
| 686 | {iAB, VD, 0}, /* OP_SETTABLE */ | 689 | {iAB, 0, VD}, /* OP_SETTABLE */ |
| 687 | {iAB, VD, 0}, /* OP_SETLIST */ | 690 | {iAB, 0, VD}, /* OP_SETLIST */ |
| 688 | {iU, VD, 0}, /* OP_SETMAP */ | 691 | {iU, 0, VD}, /* OP_SETMAP */ |
| 689 | {iO, 1, 2}, /* OP_ADD */ | 692 | {iO, 1, 2}, /* OP_ADD */ |
| 690 | {iS, 1, 1}, /* OP_ADDI */ | 693 | {iS, 1, 1}, /* OP_ADDI */ |
| 691 | {iO, 1, 2}, /* OP_SUB */ | 694 | {iO, 1, 2}, /* OP_SUB */ |
| 692 | {iO, 1, 2}, /* OP_MULT */ | 695 | {iO, 1, 2}, /* OP_MULT */ |
| 693 | {iO, 1, 2}, /* OP_DIV */ | 696 | {iO, 1, 2}, /* OP_DIV */ |
| 694 | {iO, 1, 2}, /* OP_POW */ | 697 | {iO, 1, 2}, /* OP_POW */ |
| 695 | {iU, VD, 0}, /* OP_CONCAT */ | 698 | {iU, 1, VD}, /* OP_CONCAT */ |
| 696 | {iO, 1, 1}, /* OP_MINUS */ | 699 | {iO, 1, 1}, /* OP_MINUS */ |
| 697 | {iO, 1, 1}, /* OP_NOT */ | 700 | {iO, 1, 1}, /* OP_NOT */ |
| 698 | {iS, 0, 2}, /* OP_JMPNE */ | 701 | {iS, 0, 2}, /* OP_JMPNE */ |
| @@ -711,6 +714,6 @@ const OpProperties luaK_opproperties[NUM_OPCODES] = { | |||
| 711 | {iS, 0, 3}, /* OP_FORLOOP */ | 714 | {iS, 0, 3}, /* OP_FORLOOP */ |
| 712 | {iS, 3, 0}, /* OP_LFORPREP */ | 715 | {iS, 3, 0}, /* OP_LFORPREP */ |
| 713 | {iS, 0, 4}, /* OP_LFORLOOP */ | 716 | {iS, 0, 4}, /* OP_LFORLOOP */ |
| 714 | {iAB, VD, 0} /* OP_CLOSURE */ | 717 | {iAB, 1, VD} /* OP_CLOSURE */ |
| 715 | }; | 718 | }; |
| 716 | 719 | ||
