]> git.xonotic.org Git - xonotic/gmqcc.git/blob - fold.c
52bedaeae467a3a456256460d386f0dc87b19852
[xonotic/gmqcc.git] / fold.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <string.h>
24 #include <math.h>
25
26 #include "ast.h"
27 #include "parser.h"
28
29 #define FOLD_STRING_UNTRANSLATE_HTSIZE 1024
30 #define FOLD_STRING_DOTRANSLATE_HTSIZE 1024
31
32 /*
33  * There is two stages to constant folding in GMQCC: there is the parse
34  * stage constant folding, where, witht he help of the AST, operator
35  * usages can be constant folded. Then there is the constant folding
36  * in the IR for things like eliding if statements, can occur.
37  * 
38  * This file is thus, split into two parts.
39  */
40
41 #define isfloat(X)      (((ast_expression*)(X))->vtype == TYPE_FLOAT)
42 #define isvector(X)     (((ast_expression*)(X))->vtype == TYPE_VECTOR)
43 #define isstring(X)     (((ast_expression*)(X))->vtype == TYPE_STRING)
44 #define isfloats(X,Y)   (isfloat  (X) && isfloat (Y))
45
46 /*
47  * Implementation of basic vector math for vec3_t, for trivial constant
48  * folding.
49  * 
50  * TODO: gcc/clang hinting for autovectorization
51  */
52 static GMQCC_INLINE vec3_t vec3_add(vec3_t a, vec3_t b) {
53     vec3_t out;
54     out.x = a.x + b.x;
55     out.y = a.y + b.y;
56     out.z = a.z + b.z;
57     return out;
58 }
59
60 static GMQCC_INLINE vec3_t vec3_sub(vec3_t a, vec3_t b) {
61     vec3_t out;
62     out.x = a.x + b.x;
63     out.y = a.y + b.y;
64     out.z = a.z + b.z;
65     return out;
66 }
67
68 static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) {
69     vec3_t out;
70     out.x = -a.x;
71     out.y = -a.y;
72     out.z = -a.z;
73     return out;
74 }
75
76 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
77     vec3_t out;
78     out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
79     out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
80     out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
81     return out;
82 }
83
84 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
85     vec3_t out;
86     out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
87     out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
88     out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
89     return out;
90 }
91
92 static GMQCC_INLINE qcfloat_t vec3_mulvv(vec3_t a, vec3_t b) {
93     return (a.x * b.x + a.y * b.y + a.z * b.z);
94 }
95
96 static GMQCC_INLINE vec3_t vec3_mulvf(vec3_t a, qcfloat_t b) {
97     vec3_t out;
98     out.x = a.x * b;
99     out.y = a.y * b;
100     out.z = a.z * b;
101     return out;
102 }
103
104 static GMQCC_INLINE bool vec3_cmp(vec3_t a, vec3_t b) {
105     return a.x == b.x &&
106            a.y == b.y &&
107            a.z == b.z;
108 }
109
110 static GMQCC_INLINE vec3_t vec3_create(float x, float y, float z) {
111     vec3_t out;
112     out.x = x;
113     out.y = y;
114     out.z = z;
115     return out;
116 }
117
118 static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
119     return (!a.x && !a.y && !a.z);
120 }
121
122 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
123     return (a.x && a.y && a.z);
124 }
125
126 static lex_ctx_t fold_ctx(fold_t *fold) {
127     lex_ctx_t ctx;
128     if (fold->parser->lex)
129         return parser_ctx(fold->parser);
130
131     memset(&ctx, 0, sizeof(ctx));
132     return ctx;
133 }
134
135 static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
136     switch (v->expression.vtype) {
137         case TYPE_FLOAT:
138             return !!v->constval.vfloat;
139         case TYPE_INTEGER:
140             return !!v->constval.vint;
141         case TYPE_VECTOR: 
142             if (OPTS_FLAG(CORRECT_LOGIC))
143                 return vec3_pbool(v->constval.vvec);
144             return !!(v->constval.vvec.x);
145         case TYPE_STRING:
146             if (!v->constval.vstring)
147                 return false;
148             if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
149                 return true;
150             return !!v->constval.vstring[0];
151         default:
152             compile_error(fold_ctx(fold), "internal error: fold_immediate_true on invalid type");
153             break;
154     }
155     return !!v->constval.vfunc;
156 }
157
158 /* Handy macros to determine if an ast_value can be constant folded. */
159 #define fold_can_1(X)  \
160     (ast_istype(((ast_expression*)(X)), ast_value) && (X)->hasvalue && ((X)->cvq == CV_CONST) && \
161                 ((ast_expression*)(X))->vtype != TYPE_FUNCTION)
162
163 #define fold_can_2(X, Y) (fold_can_1(X) && fold_can_1(Y))
164
165 #define fold_immvalue_float(E)  ((E)->constval.vfloat)
166 #define fold_immvalue_vector(E) ((E)->constval.vvec)
167 #define fold_immvalue_string(E) ((E)->constval.vstring)
168
169 fold_t *fold_init(parser_t *parser) {
170     fold_t *fold                 = (fold_t*)mem_a(sizeof(fold_t));
171     fold->parser                 = parser;
172     fold->imm_float              = NULL;
173     fold->imm_vector             = NULL;
174     fold->imm_string             = NULL;
175     fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
176     fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
177
178     /*
179      * prime the tables with common constant values at constant
180      * locations.
181      */
182     (void)fold_constgen_float (fold,  0.0f);
183     (void)fold_constgen_float (fold,  1.0f);
184     (void)fold_constgen_float (fold, -1.0f);
185
186     (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
187
188     return fold;
189 }
190
191 bool fold_generate(fold_t *fold, ir_builder *ir) {
192     /* generate globals for immediate folded values */
193     size_t     i;
194     ast_value *cur;
195
196     for (i = 0; i < vec_size(fold->imm_float);   ++i)
197         if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
198     for (i = 0; i < vec_size(fold->imm_vector);  ++i)
199         if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
200     for (i = 0; i < vec_size(fold->imm_string);  ++i)
201         if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
202
203     return true;
204
205 err:
206     con_out("failed to generate global %s\n", cur->name);
207     ir_builder_delete(ir);
208     return false;
209 }
210
211 void fold_cleanup(fold_t *fold) {
212     size_t i;
213
214     for (i = 0; i < vec_size(fold->imm_float);  ++i) ast_delete(fold->imm_float[i]);
215     for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
216     for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
217
218     vec_free(fold->imm_float);
219     vec_free(fold->imm_vector);
220     vec_free(fold->imm_string);
221
222     util_htdel(fold->imm_string_untranslate);
223     util_htdel(fold->imm_string_dotranslate);
224
225     mem_d(fold);
226 }
227
228 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value) {
229     ast_value  *out = NULL;
230     size_t      i;
231
232     for (i = 0; i < vec_size(fold->imm_float); i++) {
233         if (fold->imm_float[i]->constval.vfloat == value)
234             return (ast_expression*)fold->imm_float[i];
235     }
236
237     out                  = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
238     out->cvq             = CV_CONST;
239     out->hasvalue        = true;
240     out->constval.vfloat = value;
241
242     vec_push(fold->imm_float, out);
243
244     return (ast_expression*)out;
245 }
246
247 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
248     ast_value *out;
249     size_t     i;
250
251     for (i = 0; i < vec_size(fold->imm_vector); i++) {
252         if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
253             return (ast_expression*)fold->imm_vector[i];
254     }
255
256     out                = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
257     out->cvq           = CV_CONST;
258     out->hasvalue      = true;
259     out->constval.vvec = value;
260
261     vec_push(fold->imm_vector, out);
262
263     return (ast_expression*)out;
264 }
265
266 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
267     hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
268     ast_value    *out   = NULL;
269     size_t        hash  = util_hthash(table, str);
270
271     if ((out = (ast_value*)util_htgeth(table, str, hash)))
272         return (ast_expression*)out;
273
274     if (translate) {
275         char name[32];
276         util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
277         out                    = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
278         out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
279     } else
280         out                    = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
281
282     out->cvq              = CV_CONST;
283     out->hasvalue         = true;
284     out->isimm            = true;
285     out->constval.vstring = parser_strdup(str);
286
287     vec_push(fold->imm_string, out);
288     util_htseth(table, str, hash, out);
289
290     return (ast_expression*)out;
291 }
292
293
294 static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, ast_value *sel, const char *set) {
295     /*
296      * vector-component constant folding works by matching the component sets
297      * to eliminate expensive operations on whole-vectors (3 components at runtime).
298      * to achive this effect in a clean manner this function generalizes the 
299      * values through the use of a set paramater, which is used as an indexing method
300      * for creating the elided ast binary expression.
301      *
302      * Consider 'n 0 0' where y, and z need to be tested for 0, and x is
303      * used as the value in a binary operation generating an INSTR_MUL instruction
304      * to acomplish the indexing of the correct component value we use set[0], set[1], set[2]
305      * as x, y, z, where the values of those operations return 'x', 'y', 'z'. Because
306      * of how ASCII works we can easily deliniate:
307      * vec.z is the same as set[2]-'x' for when set[2] is 'z', 'z'-'x' results in a
308      * literal value of 2, using this 2, we know that taking the address of vec->x (float)
309      * and indxing it with this literal will yeild the immediate address of that component
310      * 
311      * Of course more work needs to be done to generate the correct index for the ast_member_new
312      * call, which is no problem: set[0]-'x' suffices that job.
313      */
314     qcfloat_t x = (&vec.x)[set[0]-'x'];
315     qcfloat_t y = (&vec.x)[set[1]-'x'];
316     qcfloat_t z = (&vec.x)[set[2]-'x'];
317
318     if (!y && !z) {
319         ast_expression *out;
320         ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
321         out                        = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
322         out->node.keep             = false;
323         ((ast_member*)out)->rvalue = true;
324         if (x != -1)
325             return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x), out);
326     }
327     return NULL;
328 }
329
330
331 static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
332     if (isfloat(a)) {
333         if (fold_can_1(a))
334             return fold_constgen_float(fold, -fold_immvalue_float(a));
335     } else if (isvector(a)) {
336         if (fold_can_1(a))
337             return fold_constgen_vector(fold, vec3_neg(fold_immvalue_vector(a)));
338     }
339     return NULL;
340 }
341
342 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
343     if (isfloat(a)) {
344         if (fold_can_1(a))
345             return fold_constgen_float(fold, !fold_immvalue_float(a));
346     } else if (isvector(a)) {
347         if (fold_can_1(a))
348             return fold_constgen_float(fold, vec3_notf(fold_immvalue_vector(a)));
349     } else if (isstring(a)) {
350         if (fold_can_1(a)) {
351             if (OPTS_FLAG(TRUE_EMPTY_STRINGS))
352                 return fold_constgen_float(fold, !fold_immvalue_string(a));
353             else
354                 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a));
355         }
356     }
357     return NULL;
358 }
359
360 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
361     if (isfloat(a)) {
362         if (fold_can_2(a, b))
363             return fold_constgen_float(fold, fold_immvalue_float(a) + fold_immvalue_float(b));
364     } else if (isvector(a)) {
365         if (fold_can_2(a, b))
366             return fold_constgen_vector(fold, vec3_add(fold_immvalue_vector(a), fold_immvalue_vector(b)));
367     }
368     return NULL;
369 }
370
371 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
372     if (isfloat(a)) {
373         if (fold_can_2(a, b))
374             return fold_constgen_float(fold, fold_immvalue_float(a) - fold_immvalue_float(b));
375     } else if (isvector(a)) {
376         if (fold_can_2(a, b))
377             return fold_constgen_vector(fold, vec3_sub(fold_immvalue_vector(a), fold_immvalue_vector(b)));
378     }
379     return NULL;
380 }
381
382 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
383     if (isfloat(a)) {
384         if (isvector(b)) {
385             if (fold_can_2(a, b))
386                 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a)));
387         } else {
388             if (fold_can_2(a, b))
389                 return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b));
390         }
391     } else if (isvector(a)) {
392         if (isfloat(b)) {
393             if (fold_can_2(a, b))
394                 return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
395         } else {
396             if (fold_can_2(a, b)) {
397                 return fold_constgen_float(fold, vec3_mulvv(fold_immvalue_vector(a), fold_immvalue_vector(b)));
398             } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(a)) {
399                 ast_expression *out;
400                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "xyz"))) return out;
401                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "yxz"))) return out;
402                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(a), b, "zxy"))) return out;
403             } else if (OPTS_OPTIMIZATION(OPTIM_VECTOR_COMPONENTS) && fold_can_1(b)) {
404                 ast_expression *out;
405                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "xyz"))) return out;
406                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "yxz"))) return out;
407                 if ((out = fold_op_mul_vec(fold, fold_immvalue_vector(b), a, "zxy"))) return out;
408             }
409         }
410     }
411     return NULL;
412 }
413
414 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
415     if (isfloat(a)) {
416         if (fold_can_2(a, b))
417             return fold_constgen_float(fold, fold_immvalue_float(a) / fold_immvalue_float(b));
418     } else if (isvector(a)) {
419         if (fold_can_2(a, b))
420             return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
421         else {
422             return (ast_expression*)ast_binary_new(
423                 fold_ctx(fold),
424                 INSTR_MUL_VF,
425                 (ast_expression*)a,
426                 (fold_can_1(b))
427                     ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b))
428                     : (ast_expression*)ast_binary_new(
429                                             fold_ctx(fold),
430                                             INSTR_DIV_F,
431                                             (ast_expression*)fold->imm_float[1],
432                                             (ast_expression*)b
433                     )
434             );
435         }
436     }
437     return NULL;
438 }
439
440 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
441     if (fold_can_2(a, b))
442         return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) % ((qcint_t)fold_immvalue_float(b))));
443     return NULL;
444 }
445
446 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
447     if (fold_can_2(a, b))
448         return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) | ((qcint_t)fold_immvalue_float(b))));
449     return NULL;
450 }
451
452 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
453     if (fold_can_2(a, b))
454         return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) & ((qcint_t)fold_immvalue_float(b))));
455     return NULL;
456 }
457
458 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
459     if (isfloat(a)) {
460         if (fold_can_2(a, b))
461             return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))));
462     } else {
463         if (isvector(b)) {
464             if (fold_can_2(a, b))
465                 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
466         } else {
467             if (fold_can_2(a, b))
468                 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
469         }
470     }
471     return NULL;
472 }
473
474 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
475     if (fold_can_2(a, b) && isfloats(a, b))
476         return fold_constgen_float(fold, (qcfloat_t)((qcuint_t)(fold_immvalue_float(a)) << (qcuint_t)(fold_immvalue_float(b))));
477     return NULL;
478 }
479
480 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
481     if (fold_can_2(a, b) && isfloats(a, b))
482         return fold_constgen_float(fold, (qcfloat_t)((qcuint_t)(fold_immvalue_float(a)) >> (qcuint_t)(fold_immvalue_float(b))));
483     return NULL;
484 }
485
486 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float or) {
487     if (fold_can_2(a, b)) {
488         if (OPTS_FLAG(PERL_LOGIC)) {
489             if (fold_immediate_true(fold, a))
490                 return (ast_expression*)b;
491         } else {
492             return fold_constgen_float (
493                 fold, 
494                 ((or) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
495                       : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
496                             ? 1
497                             : 0
498             );
499         }
500     }
501     return NULL;
502 }
503
504 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
505     if (fold_can_1(a)) {
506         return fold_immediate_true(fold, a)
507                     ? (ast_expression*)b
508                     : (ast_expression*)c;
509     }
510     return NULL;
511 }
512
513 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
514     if (fold_can_2(a, b))
515         return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)));
516     return NULL;
517 }
518
519 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
520     if (fold_can_2(a,b)) {
521         if (fold_immvalue_float(a) <  fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[2];
522         if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
523         if (fold_immvalue_float(a) >  fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
524     }
525     return NULL;
526 }
527
528 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
529     if (fold_can_2(a, b)) {
530         return fold_constgen_float(
531                     fold,
532                     (ne) ? (fold_immvalue_float(a) != fold_immvalue_float(b))
533                          : (fold_immvalue_float(a) == fold_immvalue_float(b))
534                 );
535     }
536     return NULL;
537 }
538
539 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
540     if (fold_can_1(a))
541         return fold_constgen_float(fold, ~((qcint_t)fold_immvalue_float(a)));
542     return NULL;
543 }
544
545 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
546     ast_value *a = (ast_value*)opexprs[0];
547     ast_value *b = (ast_value*)opexprs[1];
548     ast_value *c = (ast_value*)opexprs[2];
549
550     /* can a fold operation be applied to this operator usage? */
551     if (!info->folds)
552         return NULL;
553
554     switch(info->operands) {
555         case 3: if(!c) return NULL;
556         case 2: if(!b) return NULL;
557         case 1:
558         if(!a) {
559             compile_error(fold_ctx(fold), "interal error: fold_op no operands to fold\n");
560             return NULL;
561         }
562     }
563
564     switch(info->id) {
565         case opid2('-','P'):     return fold_op_neg    (fold, a);
566         case opid2('!','P'):     return fold_op_not    (fold, a);
567         case opid1('+'):         return fold_op_add    (fold, a, b);
568         case opid1('-'):         return fold_op_sub    (fold, a, b);
569         case opid1('*'):         return fold_op_mul    (fold, a, b);
570         case opid1('/'):         return fold_op_div    (fold, a, b);
571         case opid1('%'):         return fold_op_mod    (fold, a, b);
572         case opid1('|'):         return fold_op_bor    (fold, a, b);
573         case opid1('&'):         return fold_op_band   (fold, a, b);
574         case opid1('^'):         return fold_op_xor    (fold, a, b);
575         case opid2('<','<'):     return fold_op_lshift (fold, a, b);
576         case opid2('>','>'):     return fold_op_rshift (fold, a, b);
577         case opid2('|','|'):     return fold_op_andor  (fold, a, b, true);
578         case opid2('&','&'):     return fold_op_andor  (fold, a, b, false);
579         case opid2('?',':'):     return fold_op_tern   (fold, a, b, c);
580         case opid2('*','*'):     return fold_op_exp    (fold, a, b);
581         case opid3('<','=','>'): return fold_op_lteqgt (fold, a, b);
582         case opid2('!','='):     return fold_op_cmp    (fold, a, b, true);
583         case opid2('=','='):     return fold_op_cmp    (fold, a, b, false);
584         case opid2('~','P'):     return fold_op_bnot   (fold, a);
585     }
586     compile_error(fold_ctx(fold), "internal error: attempted to constant for unsupported operator");
587     return NULL;
588 }
589
590 /*
591  * These are all the actual constant folding methods that happen in between
592  * the AST/IR stage of the compiler , i.e eliminating branches for const
593  * expressions, which is the only supported thing so far. We undefine the
594  * testing macros here because an ir_value is differant than an ast_value.
595  */
596 #undef isfloat
597 #undef isstring
598 #undef isvector
599 #undef fold_immvalue_float
600 #undef fold_immvalue_string
601 #undef fold_immvalue_vector
602 #undef fold_can_1
603 #undef fold_can_2
604
605 #define isfloat(X)              ((X)->vtype == TYPE_FLOAT)
606 /*#define isstring(X)             ((X)->vtype == TYPE_STRING)*/
607 /*#define isvector(X)             ((X)->vtype == TYPE_VECTOR)*/
608 #define fold_immvalue_float(X)  ((X)->constval.vfloat)
609 /*#define fold_immvalue_vector(X) ((X)->constval.vvec)*/
610 /*#define fold_immvalue_string(X) ((X)->constval.vstring)*/
611 #define fold_can_1(X)           ((X)->hasvalue && (X)->cvq == CV_CONST)
612 /*#define fold_can_2(X,Y)         (fold_can_1(X) && fold_can_1(Y))*/
613
614
615 int fold_cond(ir_value *condval, ast_function *func, ast_ifthen *branch) {
616     if (isfloat(condval) && fold_can_1(condval) && OPTS_OPTIMIZATION(OPTIM_CONST_FOLD_DCE)) {
617         ast_expression_codegen *cgen;
618         ir_block               *elide;
619         ir_value               *dummy;
620         bool                    istrue  = (fold_immvalue_float(condval) == 1.0f && branch->on_true);
621         bool                    isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
622         ast_expression         *path    = (istrue)  ? branch->on_true  :
623                                           (isfalse) ? branch->on_false : NULL;
624         if (!path)
625             return false;
626         if (!(elide = ir_function_create_block(ast_ctx(branch), func->ir_func, ast_function_label(func, ((istrue) ? "ontrue" : "onfalse")))))
627             return false;
628         if (!(*(cgen = path->codegen))((ast_expression*)path, func, false, &dummy))
629             return false;
630         if (!ir_block_create_jump(func->curblock, ast_ctx(branch), elide))
631             return false;
632         /*
633          * now the branch has been eliminates, and the correct block for the constant evaluation
634          * is expanded into the current block for the function.
635          */
636         func->curblock = elide;
637         return true;
638     }
639     return -1; /* nothing done */
640 }