From d38cf133676423a47a22d64a29b02bc9e5fab7b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 7 Feb 2021 19:05:06 +0100 Subject: [PATCH] :art: constant fold int-float --- src/lib/prim.c | 93 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/src/lib/prim.c b/src/lib/prim.c index 178dccd3..8d18b465 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -41,32 +41,45 @@ uint pot(const m_int x){ return check_exp(env, exp_self(bin));\ } -#define BINARY_FOLD(ntype, name, TYPE, OP, pre, post, func, ctype, exptype, member) \ -static OP_CHECK(opck_##ntype##_##name) { \ - /*const*/ Exp_Binary *bin = (Exp_Binary*)data; \ - const Type t = env->gwion->type[TYPE]; \ - if(!exp_self(bin)->pos.first.line)return t; \ - pre \ - if(!func(bin->lhs) || !func(bin->rhs)) \ - return t; \ - post \ - const ctype num = bin->lhs->d.prim.d.member OP bin->rhs->d.prim.d.member; \ - free_exp(env->gwion->mp, bin->lhs); \ - free_exp(env->gwion->mp, bin->rhs); \ - exp_self(bin)->exp_type = ae_exp_primary; \ - exp_self(bin)->d.prim.prim_type = exptype; \ - exp_self(bin)->d.prim.d.num = num; \ - return t; \ +#define BINARY_FOLD(ntype, name, TYPE, OP, pre, post, funcl, funcr, ctype, exptype, member) \ +static OP_CHECK(opck_##ntype##_##name) { \ + /*const*/ Exp_Binary *bin = (Exp_Binary*)data; \ + const Type t = env->gwion->type[TYPE]; \ + if(!exp_self(bin)->pos.first.line)return t; \ + pre \ + if(!funcl(bin->lhs) || !funcr(bin->rhs)) \ + return t; \ + post \ + const ctype num = bin->lhs->d.prim.d.member OP bin->rhs->d.prim.d.member; \ + free_exp(env->gwion->mp, bin->lhs); \ + free_exp(env->gwion->mp, bin->rhs); \ + exp_self(bin)->exp_type = ae_exp_primary; \ + exp_self(bin)->d.prim.prim_type = exptype; \ + exp_self(bin)->d.prim.d.num = num; \ + return t; \ } #define BINARY_INT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(int, name, TYPE, OP, pre, post, is_prim_int, m_int, ae_prim_num, num) + BINARY_FOLD(int, name, TYPE, OP, pre, post, is_prim_int, is_prim_int, m_int, ae_prim_num, num) BINARY_INT_FOLD(add, et_int, +,,) BINARY_INT_FOLD(sub, et_int, -,,) BINARY_INT_FOLD(mul, et_int, *,POWEROF2_OPT(name, <<),) BINARY_INT_FOLD(div, et_int, /,POWEROF2_OPT(name, >>),if(bin->rhs->d.prim.d.num == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"))) BINARY_INT_FOLD(mod, et_int, %,, if(bin->rhs->d.prim.d.num == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"))) +BINARY_INT_FOLD(gt, et_int, >,,) +BINARY_INT_FOLD(lt, et_int, <,,) +BINARY_INT_FOLD(ge, et_int, >=,,) +BINARY_INT_FOLD(le, et_int, <=,,) +BINARY_INT_FOLD(sl, et_int, <<,,) +BINARY_INT_FOLD(sr, et_int, >>,,) +BINARY_INT_FOLD(sand, et_int, &,,) +BINARY_INT_FOLD(sor, et_int, |,,) +BINARY_INT_FOLD(xor, et_int, ^,,) +BINARY_INT_FOLD(and, et_bool, &&,,) +BINARY_INT_FOLD(or, et_bool, ||,,) +BINARY_INT_FOLD(eq, et_bool, ==,,) +BINARY_INT_FOLD(neq, et_bool, !=,,) GWION_IMPORT(int_op) { GWI_BB(gwi_oper_ini(gwi, "int", "int", "int")) @@ -82,20 +95,6 @@ GWION_IMPORT(int_op) { return gwi_oper_end(gwi, "%", int_modulo); } -BINARY_INT_FOLD(gt, et_int, >,,) -BINARY_INT_FOLD(lt, et_int, <,,) -BINARY_INT_FOLD(ge, et_int, >=,,) -BINARY_INT_FOLD(le, et_int, <=,,) -BINARY_INT_FOLD(sl, et_int, <<,,) -BINARY_INT_FOLD(sr, et_int, >>,,) -BINARY_INT_FOLD(sand, et_int, &,,) -BINARY_INT_FOLD(sor, et_int, |,,) -BINARY_INT_FOLD(xor, et_int, ^,,) -BINARY_INT_FOLD(and, et_bool, &&,,) -BINARY_INT_FOLD(or, et_bool, ||,,) -BINARY_INT_FOLD(eq, et_bool, ==,,) -BINARY_INT_FOLD(neq, et_bool, !=,,) - static GWION_IMPORT(int_logical) { GWI_BB(gwi_oper_ini(gwi, "int", "int", "int")) GWI_BB(gwi_oper_add(gwi, opck_int_gt)) @@ -257,16 +256,40 @@ static OP_CHECK(opck_implicit_i2f) { #define CHECK_IF(op, check, func) _CHECK_OP(op, check, int_float_##func) #define CHECK_FI(op, check, func) _CHECK_OP(op, check, float_int_##func) +#define BINARY_INT_FLOAT_FOLD(name, TYPE, OP, pre, post) \ + BINARY_FOLD(int_float, name, TYPE, OP, pre, post, is_prim_int, is_prim_float, m_float, ae_prim_float, fnum) + +BINARY_INT_FLOAT_FOLD(add, et_float, +,,) +BINARY_INT_FLOAT_FOLD(sub, et_float, -,,) +BINARY_INT_FLOAT_FOLD(mul, et_float, *,POWEROF2_OPT(name, <<),) +BINARY_INT_FLOAT_FOLD(div, et_float, /,POWEROF2_OPT(name, >>),if(bin->rhs->d.prim.d.fnum == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"))) +BINARY_INT_FLOAT_FOLD(gt, et_bool, >,,) +BINARY_INT_FLOAT_FOLD(ge, et_bool, >=,,) +BINARY_INT_FLOAT_FOLD(lt, et_bool, <=,,) +BINARY_INT_FLOAT_FOLD(le, et_bool, <=,,) +BINARY_INT_FLOAT_FOLD(and, et_bool, &&,,) +BINARY_INT_FLOAT_FOLD(or, et_bool, ||,,) +BINARY_INT_FLOAT_FOLD(eq, et_bool, ==,,) +BINARY_INT_FLOAT_FOLD(neq, et_bool, !=,,) + static GWION_IMPORT(intfloat) { GWI_BB(gwi_oper_ini(gwi, "int", "float", "int")) + GWI_BB(gwi_oper_add(gwi, opck_int_float_gt)) GWI_BB(gwi_oper_end(gwi, ">", int_float_gt)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_ge)) GWI_BB(gwi_oper_end(gwi, ">=", int_float_ge)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_lt)) GWI_BB(gwi_oper_end(gwi, "<", int_float_lt)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_le)) GWI_BB(gwi_oper_end(gwi, "<=", int_float_le)) GWI_BB(gwi_oper_ini(gwi, "int", "float", "float")) + GWI_BB(gwi_oper_add(gwi, opck_int_float_add)) GWI_BB(gwi_oper_end(gwi, "+", int_float_plus)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_mul)) GWI_BB(gwi_oper_end(gwi, "*", int_float_mul)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_sub)) GWI_BB(gwi_oper_end(gwi, "-", int_float_minus)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_div)) GWI_BB(gwi_oper_end(gwi, "/", int_float_div)) CHECK_IF("=>", rassign, r_assign) CHECK_IF("+=>", rassign, r_plus) @@ -276,9 +299,13 @@ static GWION_IMPORT(intfloat) { _CHECK_OP("$", cast_i2f, CastI2F) _CHECK_OP("@implicit", implicit_i2f, CastI2F) GWI_BB(gwi_oper_ini(gwi, "int", "float", "bool")) + GWI_BB(gwi_oper_add(gwi, opck_int_float_and)) GWI_BB(gwi_oper_end(gwi, "&&", int_float_and)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_or)) GWI_BB(gwi_oper_end(gwi, "||", int_float_or)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_eq)) GWI_BB(gwi_oper_end(gwi, "==", int_float_eq)) + GWI_BB(gwi_oper_add(gwi, opck_int_float_neq)) GWI_BB(gwi_oper_end(gwi, "!=", int_float_neq)) return GW_OK; } @@ -372,7 +399,7 @@ static GWION_IMPORT(time) { #define BINARY_FLOAT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, m_int, ae_prim_float, fnum) + BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, is_prim_float, m_int, ae_prim_float, fnum) BINARY_FLOAT_FOLD(add, et_float, +,,) BINARY_FLOAT_FOLD(sub, et_float, -,,) @@ -445,7 +472,7 @@ static GWION_IMPORT(float) { GWION_IMPORT(prim) { GWI_BB(import_int(gwi)) // const folded GWI_BB(import_float(gwi)) // const folded - GWI_BB(import_intfloat(gwi)) + GWI_BB(import_intfloat(gwi)) // const folded GWI_BB(import_floatint(gwi)) GWI_BB(import_dur(gwi)) return import_time(gwi); -- 2.43.0