From 3ee1efa7b2dd4a3d47d0baeeb3896275f4b630d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Apr 2002 11:37:09 -0300 Subject: some simplifications --- lcode.c | 88 ++++++++++++++++++++++++++--------------------------------------- 1 file changed, 35 insertions(+), 53 deletions(-) (limited to 'lcode.c') diff --git a/lcode.c b/lcode.c index 32a56d14..2595b958 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.94 2002/04/02 20:34:53 roberto Exp roberto $ +** $Id: lcode.c,v 1.95 2002/04/09 18:49:30 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -293,33 +293,6 @@ static int code_label (FuncState *fs, int A, int b, int jump) { } -static void dischargejumps (FuncState *fs, expdesc *e, int reg) { - if (e->k == VJMP || hasjumps(e)) { - int final; /* position after whole expression */ - int p_f = NO_JUMP; /* position of an eventual PUSH false */ - int p_t = NO_JUMP; /* position of an eventual PUSH true */ - if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) || - need_value(fs, e->t, OP_TESTT)) { - /* expression needs values */ - if (e->k != VJMP) { - luaK_getlabel(fs); /* these instruction may be jump target */ - luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */ - } - else { /* last expression is a conditional (test + jump) */ - fs->pc--; /* remove its jump */ - lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT)); - } - p_t = code_label(fs, reg, 1, 1); - p_f = code_label(fs, reg, 0, 0); - } - final = luaK_getlabel(fs); - luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f); - luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t); - } - e->f = e->t = NO_JUMP; -} - - static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); switch (e->k) { @@ -340,7 +313,15 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { SETARG_A(*pc, reg); break; } - default: return; + case VNONRELOC: { + if (reg != e->info) + luaK_codeABC(fs, OP_MOVE, reg, e->info, 0); + break; + } + default: { + lua_assert(e->k == VVOID || e->k == VJMP); + return; /* nothing to do... */ + } } e->info = reg; e->k = VNONRELOC; @@ -357,36 +338,39 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) { discharge2reg(fs, e, reg); - switch (e->k) { - case VVOID: { - return; /* nothing to do... */ - } - case VNONRELOC: { - if (reg != e->info) - luaK_codeABC(fs, OP_MOVE, reg, e->info, 0); - break; - } - case VJMP: { - break; - } - default: { - lua_assert(0); /* cannot happen */ - break; + if (e->k == VJMP || hasjumps(e)) { + int final; /* position after whole expression */ + int p_f = NO_JUMP; /* position of an eventual PUSH false */ + int p_t = NO_JUMP; /* position of an eventual PUSH true */ + if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) || + need_value(fs, e->t, OP_TESTT)) { + /* expression needs values */ + if (e->k != VJMP) { + luaK_getlabel(fs); /* these instruction may be jump target */ + luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */ + } + else { /* last expression is a conditional (test + jump) */ + fs->pc--; /* remove its jump */ + lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT)); + } + p_t = code_label(fs, reg, 1, 1); + p_f = code_label(fs, reg, 0, 0); } + final = luaK_getlabel(fs); + luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f); + luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t); } - dischargejumps(fs, e, reg); + e->f = e->t = NO_JUMP; e->info = reg; e->k = VNONRELOC; } void luaK_exp2nextreg (FuncState *fs, expdesc *e) { - int reg; luaK_dischargevars(fs, e); freeexp(fs, e); - reg = fs->freereg; luaK_reserveregs(fs, 1); - luaK_exp2reg(fs, e, reg); + luaK_exp2reg(fs, e, fs->freereg - 1); } @@ -395,7 +379,7 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) { if (!hasjumps(e)) return e->info; /* exp is already in a register */ if (e->info >= fs->nactloc) { /* reg. is not a local? */ - dischargejumps(fs, e, e->info); /* put value on it */ + luaK_exp2reg(fs, e, e->info); /* put value on it */ return e->info; } } @@ -440,23 +424,20 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) { case VLOCAL: { freeexp(fs, exp); luaK_exp2reg(fs, exp, var->info); - break; + return; } case VUPVAL: { int e = luaK_exp2anyreg(fs, exp); - freereg(fs, e); luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0); break; } case VGLOBAL: { int e = luaK_exp2anyreg(fs, exp); - freereg(fs, e); luaK_codeABc(fs, OP_SETGLOBAL, e, var->info); break; } case VINDEXED: { int e = luaK_exp2anyreg(fs, exp); - freereg(fs, e); luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux); break; } @@ -465,6 +446,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) { break; } } + freeexp(fs, exp); } -- cgit v1.2.3-55-g6feb