diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-10 14:02:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-10 14:02:32 -0300 |
| commit | b487975344ee90c0410e79e1af08461bdbc0f3da (patch) | |
| tree | 4ae1c9f12afe1edbcc0c8de22abdac4b19e2844d /lcode.c | |
| parent | 94e5545806515bc830982dedc4966bd2756a023e (diff) | |
| download | lua-b487975344ee90c0410e79e1af08461bdbc0f3da.tar.gz lua-b487975344ee90c0410e79e1af08461bdbc0f3da.tar.bz2 lua-b487975344ee90c0410e79e1af08461bdbc0f3da.zip | |
optimization for `while' (first version)
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 27 |
1 files changed, 17 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 1.99 2002/05/07 17:36:56 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.100 2002/05/09 14:14:34 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 | */ |
| @@ -39,6 +39,13 @@ void luaK_nil (FuncState *fs, int from, int n) { | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | void luaK_moveexp (expdesc *e, int offset) { | ||
| 43 | if (e->t != NO_JUMP) e->t += offset; | ||
| 44 | if (e->f != NO_JUMP) e->f += offset; | ||
| 45 | if (e->k == VRELOCABLE || e->k == VJMP) e->info += offset; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 42 | int luaK_jump (FuncState *fs) { | 49 | int luaK_jump (FuncState *fs) { |
| 43 | int j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); | 50 | int j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); |
| 44 | if (j == fs->lasttarget) { /* possible jumps to this jump? */ | 51 | if (j == fs->lasttarget) { /* possible jumps to this jump? */ |
| @@ -515,12 +522,10 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { | |||
| 515 | } | 522 | } |
| 516 | } | 523 | } |
| 517 | luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ | 524 | luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ |
| 518 | luaK_patchtohere(fs, e->t); | ||
| 519 | e->t = NO_JUMP; | ||
| 520 | } | 525 | } |
| 521 | 526 | ||
| 522 | 527 | ||
| 523 | static void luaK_goiffalse (FuncState *fs, expdesc *e) { | 528 | void luaK_goiffalse (FuncState *fs, expdesc *e) { |
| 524 | int pc; /* pc of last jump */ | 529 | int pc; /* pc of last jump */ |
| 525 | luaK_dischargevars(fs, e); | 530 | luaK_dischargevars(fs, e); |
| 526 | switch (e->k) { | 531 | switch (e->k) { |
| @@ -542,8 +547,6 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) { | |||
| 542 | } | 547 | } |
| 543 | } | 548 | } |
| 544 | luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ | 549 | luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ |
| 545 | luaK_patchtohere(fs, e->f); | ||
| 546 | e->f = NO_JUMP; | ||
| 547 | } | 550 | } |
| 548 | 551 | ||
| 549 | 552 | ||
| @@ -607,10 +610,14 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { | |||
| 607 | switch (op) { | 610 | switch (op) { |
| 608 | case OPR_AND: { | 611 | case OPR_AND: { |
| 609 | luaK_goiftrue(fs, v); | 612 | luaK_goiftrue(fs, v); |
| 613 | luaK_patchtohere(fs, v->t); | ||
| 614 | v->t = NO_JUMP; | ||
| 610 | break; | 615 | break; |
| 611 | } | 616 | } |
| 612 | case OPR_OR: { | 617 | case OPR_OR: { |
| 613 | luaK_goiffalse(fs, v); | 618 | luaK_goiffalse(fs, v); |
| 619 | luaK_patchtohere(fs, v->f); | ||
| 620 | v->f = NO_JUMP; | ||
| 614 | break; | 621 | break; |
| 615 | } | 622 | } |
| 616 | case OPR_CONCAT: { | 623 | case OPR_CONCAT: { |
| @@ -727,7 +734,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { | |||
| 727 | } | 734 | } |
| 728 | 735 | ||
| 729 | 736 | ||
| 730 | static int luaK_code (FuncState *fs, Instruction i) { | 737 | int luaK_code (FuncState *fs, Instruction i, int line) { |
| 731 | Proto *f = fs->f; | 738 | Proto *f = fs->f; |
| 732 | int oldsize = f->sizecode; | 739 | int oldsize = f->sizecode; |
| 733 | /* put new instruction in code array */ | 740 | /* put new instruction in code array */ |
| @@ -736,19 +743,19 @@ static int luaK_code (FuncState *fs, Instruction i) { | |||
| 736 | f->code[fs->pc] = i; | 743 | f->code[fs->pc] = i; |
| 737 | if (f->sizecode != oldsize) | 744 | if (f->sizecode != oldsize) |
| 738 | luaM_reallocvector(fs->L, f->lineinfo, oldsize, f->sizecode, int); | 745 | luaM_reallocvector(fs->L, f->lineinfo, oldsize, f->sizecode, int); |
| 739 | f->lineinfo[fs->pc] = fs->ls->lastline; | 746 | f->lineinfo[fs->pc] = line; |
| 740 | return fs->pc++; | 747 | return fs->pc++; |
| 741 | } | 748 | } |
| 742 | 749 | ||
| 743 | 750 | ||
| 744 | int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { | 751 | int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { |
| 745 | lua_assert(getOpMode(o) == iABC); | 752 | lua_assert(getOpMode(o) == iABC); |
| 746 | return luaK_code(fs, CREATE_ABC(o, a, b, c)); | 753 | return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); |
| 747 | } | 754 | } |
| 748 | 755 | ||
| 749 | 756 | ||
| 750 | int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { | 757 | int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { |
| 751 | lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); | 758 | lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); |
| 752 | return luaK_code(fs, CREATE_ABx(o, a, bc)); | 759 | return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); |
| 753 | } | 760 | } |
| 754 | 761 | ||
