aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lcode.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/lcode.c b/lcode.c
index 235a0a91..64f36e69 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.78 2014/01/27 13:34:32 roberto Exp $ 2** $Id: lcode.c,v 2.79 2014/02/06 19:55:55 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*/
@@ -750,29 +750,34 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
750} 750}
751 751
752 752
753/* return false if folding can raise an error */ 753/*
754** return false if folding can raise an error
755*/
754static int validop (OpCode op, TValue *v1, TValue *v2) { 756static int validop (OpCode op, TValue *v1, TValue *v2) {
755 lua_Integer i; 757 lua_Integer i;
756 switch (op) { 758 switch (op) {
757 case OP_IDIV: /* division by 0 and conversion errors */ 759 case LUA_OPIDIV: /* division by 0 and conversion errors */
758 return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0); 760 return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0);
759 case OP_BAND: case OP_BOR: case OP_BXOR: 761 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
760 case OP_SHL: case OP_SHR: case OP_BNOT: /* conversion errors */ 762 case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */
761 return (tointeger(v1, &i) && tointeger(v2, &i)); 763 return (tointeger(v1, &i) && tointeger(v2, &i));
762 case OP_MOD: /* integer module by 0 */ 764 case LUA_OPMOD: /* integer module by 0 */
763 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0); 765 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
764 case OP_POW: /* negative integer exponentiation */ 766 case LUA_OPPOW: /* negative integer exponentiation */
765 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0); 767 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
766 default: return 1; /* everything else is valid */ 768 default: return 1; /* everything else is valid */
767 } 769 }
768} 770}
769 771
770 772
771static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { 773/*
774** Try to "constant-fold" an operation; return 1 iff successful
775*/
776static int constfolding (int op, expdesc *e1, expdesc *e2) {
772 TValue v1, v2, res; 777 TValue v1, v2, res;
773 if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) 778 if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
774 return 0; /* non-numeric operands or not safe to fold */ 779 return 0; /* non-numeric operands or not safe to fold */
775 luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); 780 luaO_arith(NULL, op, &v1, &v2, &res);
776 if (ttisinteger(&res)) { 781 if (ttisinteger(&res)) {
777 e1->k = VKINT; 782 e1->k = VKINT;
778 e1->u.ival = ivalue(&res); 783 e1->u.ival = ivalue(&res);
@@ -790,7 +795,7 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
790 795
791static void codearith (FuncState *fs, OpCode op, 796static void codearith (FuncState *fs, OpCode op,
792 expdesc *e1, expdesc *e2, int line) { 797 expdesc *e1, expdesc *e2, int line) {
793 if (!constfolding(op, e1, e2)) { /* could not fold operation? */ 798 if (!constfolding(op - OP_ADD + LUA_OPADD, e1, e2)) {
794 int o1, o2; 799 int o1, o2;
795 if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { 800 if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
796 o2 = 0; 801 o2 = 0;