]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.cpp
fixed mixing of old and new framemacro syntax
[xonotic/gmqcc.git] / ast.cpp
diff --git a/ast.cpp b/ast.cpp
index 7b444f8a402dfdf18efaab0ae3d171c1174972ae..8b4a97714192eeae9a7e2ce2424b271a7950fc8e 100644 (file)
--- a/ast.cpp
+++ b/ast.cpp
@@ -301,11 +301,11 @@ ast_binary::ast_binary(lex_ctx_t ctx, int op,
         /* make a-(-b) => a + b */
         if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
             if (op == INSTR_SUB_F) {
-                op = INSTR_ADD_F;
+                op = m_op = INSTR_ADD_F;
                 right = normal;
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
             } else if (op == INSTR_SUB_V) {
-                op = INSTR_ADD_V;
+                op = m_op = INSTR_ADD_V;
                 right = normal;
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
             }
@@ -2293,7 +2293,6 @@ bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
     ir_block *ontrue_endblock = nullptr;
     ir_block *onfalse_endblock = nullptr;
     ir_block *merge = nullptr;
-    int folded = 0;
 
     /* We don't output any value, thus also don't care about r/lvalue */
     (void)out;
@@ -2305,16 +2304,22 @@ bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
     }
     m_outr = (ir_value*)1;
 
+    /* try constant folding away the condition */
+    switch (fold::cond_ifthen((ast_value*)m_cond, this)) {
+    case 0:
+        return true;
+    case fold::ON_TRUE:
+        return m_on_true->codegen(func, false, out);
+    case fold::ON_FALSE:
+        return m_on_false->codegen(func, false, out);
+    }
+
     /* generate the condition */
     if (!m_cond->codegen(func, false, &condval))
         return false;
     /* update the block which will get the jump - because short-logic or ternaries may have changed this */
     cond = func->m_curblock;
 
-    /* try constant folding away the condition */
-    if ((folded = fold::cond_ifthen(condval, func, this)) != -1)
-        return folded;
-
     if (m_on_true) {
         /* create on-true block */
         ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
@@ -2391,7 +2396,6 @@ bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
     ir_block *ontrue, *ontrue_out = nullptr;
     ir_block *onfalse, *onfalse_out = nullptr;
     ir_block *merge;
-    int folded = 0;
 
     /* Ternary can never create an lvalue... */
     if (lvalue)
@@ -2407,6 +2411,16 @@ bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
         return true;
     }
 
+    /* try constant folding away the condition */
+    switch (fold::cond_ternary((ast_value*)m_cond, this)) {
+    case 0:
+        return true;
+    case fold::ON_TRUE:
+        return m_on_true->codegen(func, false, out);
+    case fold::ON_FALSE:
+        return m_on_false->codegen(func, false, out);
+    }
+
     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
 
     /* generate the condition */
@@ -2415,10 +2429,6 @@ bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
         return false;
     cond_out = func->m_curblock;
 
-    /* try constant folding away the condition */
-    if ((folded = fold::cond_ternary(condval, func, this)) != -1)
-        return folded;
-
     /* create on-true block */
     ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
     if (!ontrue)