]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Move const-branch-elision into fold.c
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 202adb64dc08e22ac87011b3920b7b485d64a70d..dd98a83b0f9602931c32d2a3d8e3134f8d4faa79 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -26,6 +26,7 @@
 
 #include "gmqcc.h"
 #include "ast.h"
+#include "parser.h"
 
 #define ast_instantiate(T, ctx, destroyfn)                          \
     T* self = (T*)mem_a(sizeof(T));                                 \
@@ -1221,7 +1222,7 @@ void ast_function_delete(ast_function *self)
     mem_d(self);
 }
 
-static const char* ast_function_label(ast_function *self, const char *prefix)
+const char* ast_function_label(ast_function *self, const char *prefix)
 {
     size_t id;
     size_t len;
@@ -2513,6 +2514,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     ir_block *ontrue_endblock = NULL;
     ir_block *onfalse_endblock = NULL;
     ir_block *merge = NULL;
+    int       fold  = 0;
 
     /* We don't output any value, thus also don't care about r/lvalue */
     (void)out;
@@ -2531,33 +2533,10 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     /* update the block which will get the jump - because short-logic or ternaries may have changed this */
     cond = func->curblock;
 
-    /* eliminate branches if value is constant */
-    if (condval->vtype == TYPE_FLOAT && condval->hasvalue && condval->cvq == CV_CONST) {
-        /* don't generate if statements */
-        if (condval->constval.vfloat == 1.0f && self->on_true) {
-            if (!(ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"))))
-                return false;
-            /* generate */
-            if (!(*(cgen = self->on_true->codegen))((ast_expression*)(self->on_true), func, false, &dummy))
-                return false;
-            if (!ir_block_create_jump(func->curblock, ast_ctx(self), ontrue))
-                return false;
-            func->curblock = ontrue;
-            return true;
-        } else if (condval->constval.vfloat == 0.0f && self->on_false) {
-            if (!(onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"))))
-                return false;
-            /* generate */
-            if (!(*(cgen = self->on_false->codegen))((ast_expression*)(self->on_false), func, false, &dummy))
-                return false;
-            if (!ir_block_create_jump(func->curblock, ast_ctx(self), onfalse))
-                return false;
-            func->curblock = onfalse;
-            return true;
-        }
-    } 
-    /* on-true path */
-
+    /* try constant folding away the if */
+    if ((fold = fold_cond((ast_value*)condval, func, self)) != -1)
+        return fold;
+    
     if (self->on_true) {
         /* create on-true block */
         ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));