diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 16:44:42 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 16:44:42 -0200 |
commit | f5133aa1a55cee96b535ff788764437deacdc26a (patch) | |
tree | 6b124ace99b2fb437262c6e7209c3f58da7de844 /lcode.c | |
parent | c0edab0f6de39ba5ae2e2f1540fa98f1b507afec (diff) | |
download | lua-f5133aa1a55cee96b535ff788764437deacdc26a.tar.gz lua-f5133aa1a55cee96b535ff788764437deacdc26a.tar.bz2 lua-f5133aa1a55cee96b535ff788764437deacdc26a.zip |
small change in handling of unary operations
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 27 |
1 files changed, 13 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.74 2013/12/16 19:06:52 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.75 2013/12/18 14:12:03 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 | */ |
@@ -791,8 +791,15 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { | |||
791 | static void codearith (FuncState *fs, OpCode op, | 791 | static void codearith (FuncState *fs, OpCode op, |
792 | expdesc *e1, expdesc *e2, int line) { | 792 | expdesc *e1, expdesc *e2, int line) { |
793 | if (!constfolding(op, e1, e2)) { /* could not fold operation? */ | 793 | if (!constfolding(op, e1, e2)) { /* could not fold operation? */ |
794 | int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; | 794 | int o1, o2; |
795 | int o1 = luaK_exp2RK(fs, e1); | 795 | if (op == OP_UNM || op == OP_LEN) { |
796 | o2 = 0; | ||
797 | o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */ | ||
798 | } | ||
799 | else { /* regular case (binary operators) */ | ||
800 | o2 = luaK_exp2RK(fs, e2); | ||
801 | o1 = luaK_exp2RK(fs, e1); | ||
802 | } | ||
796 | if (o1 > o2) { | 803 | if (o1 > o2) { |
797 | freeexp(fs, e1); | 804 | freeexp(fs, e1); |
798 | freeexp(fs, e2); | 805 | freeexp(fs, e2); |
@@ -826,21 +833,13 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, | |||
826 | 833 | ||
827 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { | 834 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { |
828 | expdesc e2; | 835 | expdesc e2; |
829 | e2.t = e2.f = NO_JUMP; e2.k = VKFLT; e2.u.nval = 0; | 836 | e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0; |
830 | switch (op) { | 837 | switch (op) { |
831 | case OPR_MINUS: { | 838 | case OPR_MINUS: case OPR_LEN: { |
832 | if (!constfolding(OP_UNM, e, e)) { /* cannot fold it? */ | 839 | codearith(fs, op - OPR_MINUS + OP_UNM, e, &e2, line); |
833 | luaK_exp2anyreg(fs, e); | ||
834 | codearith(fs, OP_UNM, e, &e2, line); | ||
835 | } | ||
836 | break; | 840 | break; |
837 | } | 841 | } |
838 | case OPR_NOT: codenot(fs, e); break; | 842 | case OPR_NOT: codenot(fs, e); break; |
839 | case OPR_LEN: { | ||
840 | luaK_exp2anyreg(fs, e); /* cannot operate on constants */ | ||
841 | codearith(fs, OP_LEN, e, &e2, line); | ||
842 | break; | ||
843 | } | ||
844 | default: lua_assert(0); | 843 | default: lua_assert(0); |
845 | } | 844 | } |
846 | } | 845 | } |