all "divisions" (div,idiv,mod) by zero are not folded, to avoid

problems during compilation + does not fold zero results, as they
can collapse with -0.0 and the ANSI test to distinguish both needs
a division by zero (which we are trying to avoid) + removed macro
'luai_numinvalidop' (as its main use case were divisions by zero)
This commit is contained in:
Roberto Ierusalimschy
2014-12-29 14:49:25 -02:00
parent a1c37f834a
commit 8e5290d81e

34
lcode.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.97 2014/11/24 14:59:22 roberto Exp roberto $ ** $Id: lcode.c,v 2.98 2014/12/19 13:36:32 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -33,14 +33,6 @@
#define MAXREGS 250 #define MAXREGS 250
/* test for x == -0 ('signbit' needs 'math.h') */
#if defined(signbit)
#define isminuszero(x) ((x) == 0.0 && signbit(x))
#else
#define isminuszero(x) ((x) == 0.0 && 1.0/(x) < 0.0)
#endif
#define hasjumps(e) ((e)->t != (e)->f) #define hasjumps(e) ((e)->t != (e)->f)
@@ -364,14 +356,8 @@ int luaK_intK (FuncState *fs, lua_Integer n) {
} }
/*
** Both NaN and -0.0 should not go to the constant table, as they have
** problems with the hashing. (NaN is not a valid key, -0.0 collides
** with +0.0.)
*/
static int luaK_numberK (FuncState *fs, lua_Number r) { static int luaK_numberK (FuncState *fs, lua_Number r) {
TValue o; TValue o;
lua_assert(!luai_numisnan(r) && !isminuszero(r));
setfltvalue(&o, r); setfltvalue(&o, r);
return addk(fs, &o, &o); return addk(fs, &o, &o);
} }
@@ -761,14 +747,14 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
** return false if folding can raise an error ** return false if folding can raise an error
*/ */
static int validop (int op, TValue *v1, TValue *v2) { static int validop (int op, TValue *v1, TValue *v2) {
lua_Integer i;
if (luai_numinvalidop(op, nvalue(v1), nvalue(v2))) return 0;
switch (op) { switch (op) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
lua_Integer i;
return (tointeger(v1, &i) && tointeger(v2, &i)); return (tointeger(v1, &i) && tointeger(v2, &i));
case LUA_OPIDIV: case LUA_OPMOD: /* integer division by 0 */ }
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0); case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
return (nvalue(v2) != 0);
default: return 1; /* everything else is valid */ default: return 1; /* everything else is valid */
} }
} }
@@ -781,15 +767,15 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
TValue v1, v2, res; TValue v1, v2, res;
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
return 0; /* non-numeric operands or not safe to fold */ return 0; /* non-numeric operands or not safe to fold */
luaO_arith(fs->ls->L, op, &v1, &v2, &res); luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
if (ttisinteger(&res)) { if (ttisinteger(&res)) {
e1->k = VKINT; e1->k = VKINT;
e1->u.ival = ivalue(&res); e1->u.ival = ivalue(&res);
} }
else { else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */
lua_Number n = fltvalue(&res); lua_Number n = fltvalue(&res);
if (luai_numisnan(n) || isminuszero(n)) if (luai_numisnan(n) || n == 0)
return 0; /* folds neither NaN nor -0 */ return 0;
e1->k = VKFLT; e1->k = VKFLT;
e1->u.nval = n; e1->u.nval = n;
} }