X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ast.c;h=bcc2f8ef3f4bc4fb3887facfa047a6c6c6fef712;hp=7a8f6cc1c57fab709dea8e31b069fce2699d12b6;hb=24fc2e5146d7ebb213b2690ed99c8eb61cafd81e;hpb=d6ca5673dc7f2760faca1200477808d919a290ca diff --git a/ast.c b/ast.c index 7a8f6cc..bcc2f8e 100644 --- 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,8 +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; - /* on-true path */ - + /* try constant folding away the if */ + if ((fold = fold_cond(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"));