- case opid2('-', 'P'):
- return isfloat (a) ? fold_constgen_float (fold, fold_immvalue_float(a))
- : isvector(a) ? fold_constgen_vector(fold, vec3_neg(fold_immvalue_vector(a)))
- : NULL;
- case opid2('!', 'P'):
- return isfloat (a) ? fold_constgen_float (fold, !fold_immvalue_float(a))
- : isvector(a) ? fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)))
- : isstring(a) ? fold_constgen_float (fold, !fold_immvalue_string(a) || OPTS_FLAG(TRUE_EMPTY_STRINGS) ? 0 : !*fold_immvalue_string(a))
- : NULL;
- case opid1('+'):
- return isfloats(a,b) ? fold_constgen_float (fold, fold_immvalue_float(a) + fold_immvalue_float(b))
- : isvectors(a,b) ? fold_constgen_vector(fold, vec3_add(fold_immvalue_vector(a), fold_immvalue_vector(b)))
- : NULL;
- case opid1('-'):
- return isfloats(a,b) ? fold_constgen_float (fold, fold_immvalue_float(a) - fold_immvalue_float(b))
- : isvectors(a,b) ? fold_constgen_vector(fold, vec3_sub(fold_immvalue_vector(a), fold_immvalue_vector(b)))
- : NULL;
- case opid1('%'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) % ((qcint_t)fold_immvalue_float(b))))
- : NULL;
- case opid1('|'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))))
- : NULL;
- case opid1('&'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))))
- : NULL;
- case opid1('^'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))))
- : isvectors(a,b) ? fold_constgen_vector(fold, vec3_xor (fold_immvalue_vector(a), fold_immvalue_vector(b)))
- : isvector(a)&&isfloat(b) ? fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float (b)))
- : NULL;
- case opid2('<','<'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcuint_t)(fold_immvalue_float(a)) << ((qcuint_t)fold_immvalue_float(b)))))
- : NULL;
- case opid2('>','>'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)(((qcuint_t)(fold_immvalue_float(a)) >> ((qcuint_t)fold_immvalue_float(b)))))
- : NULL;
- case opid2('*','*'):
- return isfloats(a,b) ? fold_constgen_float (fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)))
- : NULL;
- case opid2('!','='):
- return isfloats(a,b) ? fold_constgen_float (fold, fold_immvalue_float(a) != fold_immvalue_float(b))
- : NULL;
- case opid2('=','='):
- return isfloats(a,b) ? fold_constgen_float (fold, fold_immvalue_float(a) == fold_immvalue_float(b))
- : NULL;
- case opid2('~','P'):
- return isfloat(a) ? fold_constgen_float (fold, ~(qcint_t)fold_immvalue_float(a))
- : NULL;
-
- case opid1('*'): return fold_op_mul(fold, a, b);
- case opid1('/'):
- /* TODO: seperate function for this case */
- return NULL;
- case opid2('|','|'):
- /* TODO: seperate function for this case */
- return NULL;
- case opid2('&','&'):
- /* TODO: seperate function for this case */
- return NULL;
- case opid2('?',':'):
- /* TODO: seperate function for this case */
- return NULL;
- case opid3('<','=','>'):
- /* TODO: seperate function for this case */
- return NULL;
+ fold_op_case(2, ('-', 'P'), neg, (fold, a));
+ fold_op_case(2, ('!', 'P'), not, (fold, a));
+ fold_op_case(1, ('+'), add, (fold, a, b));
+ fold_op_case(1, ('-'), sub, (fold, a, b));
+ fold_op_case(1, ('*'), mul, (fold, a, b));
+ fold_op_case(1, ('/'), div, (fold, a, b));
+ fold_op_case(1, ('%'), mod, (fold, a, b));
+ fold_op_case(1, ('|'), bor, (fold, a, b));
+ fold_op_case(1, ('&'), band, (fold, a, b));
+ fold_op_case(1, ('^'), xor, (fold, a, b));
+ fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
+ fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
+ fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
+ fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
+ fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
+ fold_op_case(2, ('*', '*'), exp, (fold, a, b));
+ fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
+ fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
+ fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
+ fold_op_case(2, ('~', 'P'), bnot, (fold, a));
+ fold_op_case(2, ('>', '<'), cross, (fold, a, b));
+ }
+ #undef fold_op_case
+ compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
+ return NULL;
+}
+
+/*
+ * Constant folding for compiler intrinsics, simaler approach to operator
+ * folding, primarly: individual functions for each intrinsics to fold,
+ * and a generic selection function.
+ */
+static GMQCC_INLINE ast_expression *fold_intrin_mod(fold_t *fold, ast_value *lhs, ast_value *rhs) {
+ return fold_constgen_float(
+ fold,
+ fmodf(
+ fold_immvalue_float(lhs),
+ fold_immvalue_float(rhs)
+ )
+ );
+}
+
+static GMQCC_INLINE ast_expression *fold_intrin_pow(fold_t *fold, ast_value *lhs, ast_value *rhs) {
+ return fold_constgen_float(
+ fold,
+ powf(
+ fold_immvalue_float(lhs),
+ fold_immvalue_float(rhs)
+ )
+ );
+}
+
+static GMQCC_INLINE ast_expression *fold_intrin_exp(fold_t *fold, ast_value *value) {
+ return fold_constgen_float(fold, exp(fold_immvalue_float(value)));
+}
+
+static GMQCC_INLINE ast_expression *fold_intrin_isnan(fold_t *fold, ast_value *value) {
+ return fold_constgen_float(fold, isnan(fold_immvalue_float(value)) != 0.0f);
+}
+
+static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *value) {
+ return fold_constgen_float(fold, fabs(fold_immvalue_float(value)));
+}
+
+ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
+ if (!strcmp(intrin, "mod")) return fold_intrin_mod (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
+ if (!strcmp(intrin, "pow")) return fold_intrin_pow (fold, (ast_value*)arg[0], (ast_value*)arg[1]);
+ if (!strcmp(intrin, "exp")) return fold_intrin_exp (fold, (ast_value*)arg[0]);
+ if (!strcmp(intrin, "isnan")) return fold_intrin_isnan(fold, (ast_value*)arg[0]);
+ if (!strcmp(intrin, "fabs")) return fold_intrin_fabs (fold, (ast_value*)arg[0]);
+
+ return NULL;
+}
+
+/*
+ * These are all the actual constant folding methods that happen in between
+ * the AST/IR stage of the compiler , i.e eliminating branches for const
+ * expressions, which is the only supported thing so far. We undefine the
+ * testing macros here because an ir_value is differant than an ast_value.
+ */
+#undef expect
+#undef isfloat
+#undef isstring
+#undef isvector
+#undef fold_immvalue_float
+#undef fold_immvalue_string
+#undef fold_immvalue_vector
+#undef fold_can_1
+#undef fold_can_2
+
+#define isfloat(X) ((X)->vtype == TYPE_FLOAT)
+/*#define isstring(X) ((X)->vtype == TYPE_STRING)*/
+/*#define isvector(X) ((X)->vtype == TYPE_VECTOR)*/
+#define fold_immvalue_float(X) ((X)->constval.vfloat)
+#define fold_immvalue_vector(X) ((X)->constval.vvec)
+/*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
+#define fold_can_1(X) ((X)->hasvalue && (X)->cvq == CV_CONST)
+/*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
+
+ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
+ ast_value *load;
+
+ if (!ast_istype(left, ast_value) || !fold_can_1((load = (ast_value*)right)))
+ return NULL;
+
+ switch (op) {
+ case INSTR_MUL_F:
+ case INSTR_DIV_F:
+ if (fold_immvalue_float(load) == 1.0f) {
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ return (ast_expression*)left;
+ }
+ break;
+
+
+ case INSTR_ADD_F:
+ case INSTR_SUB_F:
+ if (fold_immvalue_float(load) == 0.0f) {
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ return (ast_expression*)left;
+ }
+ break;
+
+ case INSTR_MUL_V:
+ if (vec3_cmp(fold_immvalue_vector(load), vec3_create(1, 1, 1))) {
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ return (ast_expression*)left;
+ }
+ break;
+
+ case INSTR_ADD_V:
+ case INSTR_SUB_V:
+ if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ return (ast_expression*)left;
+ }
+ break;