]> git.xonotic.org Git - xonotic/gmqcc.git/blob - fold.h
allow comparing explicitly against nil
[xonotic/gmqcc.git] / fold.h
1 #ifndef GMQCC_FOLD_HDR
2 #define GMQCC_FOLD_HDR
3 #include "lexer.h"
4 #include "gmqcc.h"
5
6 struct ir_builder;
7 struct ir_value;
8
9 struct ast_function;
10 struct ast_ifthen;
11 struct ast_ternary;
12 struct ast_expression;
13 struct ast_value;
14
15 struct parser_t;
16
17 struct fold {
18     fold();
19     fold(parser_t *parser);
20     ~fold();
21
22     // Bitmask describing which branches of a conditional to take after folding.
23     // Zero indicates all the branches can be removed.
24     // ON_TRUE means ON_FALSE can be removed.
25     // ON_FALSE means ON_TRUE can be removed.
26     // ON_TRUE | ON_FALSE means nothing can be removed.
27     enum {
28         ON_TRUE  = 1 << 0,
29         ON_FALSE = 1 << 1,
30     };
31
32     bool generate(ir_builder *ir);
33     ast_expression *op(const oper_info *info, ast_expression **opexprs);
34     ast_expression *intrinsic(const char *intrinsic, size_t n_args, ast_expression **args);
35
36     static uint32_t cond_ternary(ast_value *condval, ast_ternary *branch);
37     static uint32_t cond_ifthen(ast_value *condval, ast_ifthen *branch);
38
39     static ast_expression *superfluous(ast_expression *left, ast_expression *right, int op);
40     static ast_expression *binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right);
41
42     ast_expression *constgen_float(qcfloat_t value, bool inexact);
43     ast_expression *constgen_vector(vec3_t value);
44     ast_expression *constgen_string(const char *str, bool translate);
45     ast_expression *constgen_string(const std::string &str, bool translate);
46
47     ast_value *imm_float(size_t index) const { return m_imm_float[index]; }
48     ast_value *imm_vector(size_t index) const { return m_imm_vector[index]; }
49
50 protected:
51     static qcfloat_t immvalue_float(ast_value *value);
52     static vec3_t immvalue_vector(ast_value *value);
53     static const char *immvalue_string(ast_value *value);
54
55     lex_ctx_t ctx();
56
57     bool immediate_true(ast_value *v);
58
59     bool check_except_float_impl(void (*callback)(void), ast_value *a, ast_value *b);
60     bool check_inexact_float(ast_value *a, ast_value *b);
61
62     ast_expression *op_mul_vec(vec3_t vec, ast_value *sel, const char *set);
63     ast_expression *op_neg(ast_value *a);
64     ast_expression *op_not(ast_value *a);
65     ast_expression *op_add(ast_value *a, ast_value *b);
66     ast_expression *op_sub(ast_value *a, ast_value *b);
67     ast_expression *op_mul(ast_value *a, ast_value *b);
68     ast_expression *op_div(ast_value *a, ast_value *b);
69     ast_expression *op_mod(ast_value *a, ast_value *b);
70     ast_expression *op_bor(ast_value *a, ast_value *b);
71     ast_expression *op_band(ast_value *a, ast_value *b);
72     ast_expression *op_xor(ast_value *a, ast_value *b);
73     ast_expression *op_lshift(ast_value *a, ast_value *b);
74     ast_expression *op_rshift(ast_value *a, ast_value *b);
75     ast_expression *op_andor(ast_value *a, ast_value *b, float expr);
76     ast_expression *op_tern(ast_value *a, ast_value *b, ast_value *c);
77     ast_expression *op_exp(ast_value *a, ast_value *b);
78     ast_expression *op_lteqgt(ast_value *a, ast_value *b);
79     ast_expression *op_ltgt(ast_value *a, ast_value *b, bool lt);
80     ast_expression *op_cmp(ast_value *a, ast_value *b, bool ne);
81     ast_expression *op_bnot(ast_value *a);
82     ast_expression *op_cross(ast_value *a, ast_value *b);
83     ast_expression *op_length(ast_value *a);
84
85     ast_expression *intrinsic_isfinite(ast_value *a);
86     ast_expression *intrinsic_isinf(ast_value *a);
87     ast_expression *intrinsic_isnan(ast_value *a);
88     ast_expression *intrinsic_isnormal(ast_value *a);
89     ast_expression *intrinsic_signbit(ast_value *a);
90     ast_expression *intrinsic_acosh(ast_value *a);
91     ast_expression *intrinsic_asinh(ast_value *a);
92     ast_expression *intrinsic_atanh(ast_value *a);
93     ast_expression *intrinsic_exp(ast_value *a);
94     ast_expression *intrinsic_exp2(ast_value *a);
95     ast_expression *intrinsic_expm1(ast_value *a);
96     ast_expression *intrinsic_pow(ast_value *lhs, ast_value *rhs);
97     ast_expression *intrinsic_mod(ast_value *lhs, ast_value *rhs);
98     ast_expression *intrinsic_fabs(ast_value *a);
99
100     ast_expression* intrinsic_nan(void);
101     ast_expression* intrinsic_epsilon(void);
102     ast_expression* intrinsic_inf(void);
103
104     static qcfloat_t immvalue_float(ir_value *value);
105     static vec3_t immvalue_vector(ir_value *value);
106
107     static uint32_t cond(ast_value *condval, ast_ifthen *branch);
108
109 private:
110     friend struct intrin;
111
112     std::vector<ast_value*> m_imm_float;
113     std::vector<ast_value*> m_imm_vector;
114     std::vector<ast_value*> m_imm_string;
115     hash_table_t *m_imm_string_untranslate; /* map<string, ast_value*> */
116     hash_table_t *m_imm_string_dotranslate; /* map<string, ast_value*> */
117     parser_t *m_parser;
118     bool m_initialized;
119 };
120
121 #endif