small refactoring.

This commit is contained in:
Roberto Ierusalimschy
2000-03-24 09:18:30 -03:00
parent 415ee1a24f
commit 47b4bf5964

48
lcode.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 1.15 2000/03/17 14:46:04 roberto Exp roberto $ ** $Id: lcode.c,v 1.16 2000/03/20 19:15:37 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -408,49 +408,47 @@ static void concatlists (FuncState *fs, int *l1, int l2) {
} }
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) { static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
Instruction *previous; Instruction *previous;
int *golist = &v->u.l.f;
int *exitlist = &v->u.l.t;
if (invert) { /* interchange `golist' and `exitlist' */
int *temp = golist; golist = exitlist; exitlist = temp;
}
discharge1(fs, v); discharge1(fs, v);
previous = &fs->f->code[fs->pc-1]; previous = &fs->f->code[fs->pc-1];
LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line"); LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
if (ISJUMP(GET_OPCODE(*previous))) if (ISJUMP(GET_OPCODE(*previous))) {
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); if (invert)
else { SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP;
luaK_jump(fs, jump);
} }
insert_last(fs, &v->u.l.f); else
luaK_patchlist(fs, v->u.l.t, luaK_getlabel(fs)); luaK_jump(fs, jump);
v->u.l.t = NO_JUMP; insert_last(fs, exitlist);
luaK_patchlist(fs, *golist, luaK_getlabel(fs));
*golist = NO_JUMP;
}
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
luaK_testgo(fs, v, 1, keepvalue ? OP_ONFJMP : OP_IFFJMP);
} }
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) { void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
Instruction previous; luaK_testgo(fs, v, 0, keepvalue ? OP_ONTJMP : OP_IFTJMP);
discharge1(fs, v);
previous = fs->f->code[fs->pc-1];
LUA_ASSERT(L, GET_OPCODE(previous) != OP_SETLINE, "bad place to set line");
if (!ISJUMP(GET_OPCODE(previous))) {
OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP;
luaK_jump(fs, jump);
}
insert_last(fs, &v->u.l.t);
luaK_patchlist(fs, v->u.l.f, luaK_getlabel(fs));
v->u.l.f = NO_JUMP;
} }
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
if (discharge(fs, v)) return; if (!discharge(fs, v)) { /* `v' is an expression? */
else { /* is an expression */
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]); OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line"); LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) { if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
/* it is an expression without jumps */ /* it is an expression without jumps */
if (onlyone && v->k == VEXP) if (onlyone)
luaK_setcallreturns(fs, 1); /* call must return 1 value */ luaK_setcallreturns(fs, 1); /* call must return 1 value */
return;
} }
else { /* expression has jumps... */ else { /* expression has jumps... */
int p_nil = 0; /* position of an eventual PUSHNIL */ int p_nil = 0; /* position of an eventual PUSHNIL */