diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 12:12:03 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 12:12:03 -0200 |
| commit | c0edab0f6de39ba5ae2e2f1540fa98f1b507afec (patch) | |
| tree | 9401b36cf92a0f37ae6a9cf052c99ff11787246e /lcode.c | |
| parent | a948054a1951cd526c732d6a0e16d99cae837d49 (diff) | |
| download | lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.tar.gz lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.tar.bz2 lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.zip | |
first implementation of bitwise operators '&' (band), '|' (bor),
and '~' (bxor)
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 37 |
1 files changed, 24 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 2.73 2013/12/16 14:30:22 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.74 2013/12/16 19:06:52 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,18 +750,27 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { | |||
| 750 | } | 750 | } |
| 751 | 751 | ||
| 752 | 752 | ||
| 753 | /* return false if folding can raise an error */ | ||
| 754 | static int validop (OpCode op, TValue *v1, TValue *v2) { | ||
| 755 | lua_Integer i; | ||
| 756 | switch (op) { | ||
| 757 | case OP_IDIV: /* division by 0 and conversion errors */ | ||
| 758 | return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0); | ||
| 759 | case OP_BAND: case OP_BOR: case OP_BXOR: /* conversion errors */ | ||
| 760 | return (tointeger(v1, &i) && tointeger(v2, &i)); | ||
| 761 | case OP_MOD: /* integer module by 0 */ | ||
| 762 | return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0); | ||
| 763 | case OP_POW: /* negative integer exponentiation */ | ||
| 764 | return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0); | ||
| 765 | default: return 1; /* everything else is valid */ | ||
| 766 | } | ||
| 767 | } | ||
| 768 | |||
| 769 | |||
| 753 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { | 770 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { |
| 754 | TValue v1, v2, res; | 771 | TValue v1, v2, res; |
| 755 | lua_Integer i; | 772 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) |
| 756 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2)) | 773 | return 0; /* non-numeric operands or not safe to fold */ |
| 757 | return 0; /* non-numeric operands */ | ||
| 758 | if (op == OP_IDIV && | ||
| 759 | (!tointeger(&v1, &i) || !tointeger(&v2, &i) || i == 0)) | ||
| 760 | return 0; /* avoid division by 0 and conversion errors */ | ||
| 761 | if (ttisinteger(&v1) && ttisinteger(&v2) && /* for integer operations... */ | ||
| 762 | ((op == OP_MOD && ivalue(&v2) == 0) || /* ...avoid module by 0... */ | ||
| 763 | (op == OP_POW && ivalue(&v2) < 0))) /* ...and negative exponents */ | ||
| 764 | return 0; | ||
| 765 | lua_assert(OP_IDIV - OP_ADD + LUA_OPADD == LUA_OPIDIV); | 774 | lua_assert(OP_IDIV - OP_ADD + LUA_OPADD == LUA_OPIDIV); |
| 766 | luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); | 775 | luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); |
| 767 | if (ttisinteger(&res)) { | 776 | if (ttisinteger(&res)) { |
| @@ -853,7 +862,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { | |||
| 853 | } | 862 | } |
| 854 | case OPR_ADD: case OPR_SUB: | 863 | case OPR_ADD: case OPR_SUB: |
| 855 | case OPR_MUL: case OPR_DIV: case OPR_IDIV: | 864 | case OPR_MUL: case OPR_DIV: case OPR_IDIV: |
| 856 | case OPR_MOD: case OPR_POW: { | 865 | case OPR_MOD: case OPR_POW: |
| 866 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: { | ||
| 857 | if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); | 867 | if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); |
| 858 | break; | 868 | break; |
| 859 | } | 869 | } |
| @@ -897,7 +907,8 @@ void luaK_posfix (FuncState *fs, BinOpr op, | |||
| 897 | break; | 907 | break; |
| 898 | } | 908 | } |
| 899 | case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: | 909 | case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: |
| 900 | case OPR_IDIV: case OPR_MOD: case OPR_POW: { | 910 | case OPR_IDIV: case OPR_MOD: case OPR_POW: |
| 911 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: { | ||
| 901 | codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line); | 912 | codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line); |
| 902 | break; | 913 | break; |
| 903 | } | 914 | } |
