Browse Source

small change in handling of unary operations

pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
f5133aa1a5
  1. 27
      lcode.c

27
lcode.c

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.74 2013/12/16 19:06:52 roberto Exp roberto $ ** $Id: lcode.c,v 2.75 2013/12/18 14:12:03 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -791,8 +791,15 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
static void codearith (FuncState *fs, OpCode op, static void codearith (FuncState *fs, OpCode op,
expdesc *e1, expdesc *e2, int line) { expdesc *e1, expdesc *e2, int line) {
if (!constfolding(op, e1, e2)) { /* could not fold operation? */ if (!constfolding(op, e1, e2)) { /* could not fold operation? */
int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; int o1, o2;
int o1 = luaK_exp2RK(fs, e1); if (op == OP_UNM || op == OP_LEN) {
o2 = 0;
o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
}
else { /* regular case (binary operators) */
o2 = luaK_exp2RK(fs, e2);
o1 = luaK_exp2RK(fs, e1);
}
if (o1 > o2) { if (o1 > o2) {
freeexp(fs, e1); freeexp(fs, e1);
freeexp(fs, e2); freeexp(fs, e2);
@ -826,21 +833,13 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
expdesc e2; expdesc e2;
e2.t = e2.f = NO_JUMP; e2.k = VKFLT; e2.u.nval = 0; e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
switch (op) { switch (op) {
case OPR_MINUS: { case OPR_MINUS: case OPR_LEN: {
if (!constfolding(OP_UNM, e, e)) { /* cannot fold it? */ codearith(fs, op - OPR_MINUS + OP_UNM, e, &e2, line);
luaK_exp2anyreg(fs, e);
codearith(fs, OP_UNM, e, &e2, line);
}
break; break;
} }
case OPR_NOT: codenot(fs, e); break; case OPR_NOT: codenot(fs, e); break;
case OPR_LEN: {
luaK_exp2anyreg(fs, e); /* cannot operate on constants */
codearith(fs, OP_LEN, e, &e2, line);
break;
}
default: lua_assert(0); default: lua_assert(0);
} }
} }

Loading…
Cancel
Save