]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Merge branch 'master' of github.com:graphitemaster/gmqcc
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 5cb8651d6d5b1e9428c5e3824ee26ce631e277ce..23ed28ab12c74b6a7be67f1bc5fcf4911c07eec5 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -476,7 +476,7 @@ ast_unary* ast_unary_new(lex_ctx ctx, int op,
 
 void ast_unary_delete(ast_unary *self)
 {
-    ast_unref(self->operand);
+    if (self->operand) ast_unref(self->operand);
     ast_expression_delete((ast_expression*)self);
     mem_d(self);
 }
@@ -709,8 +709,8 @@ void ast_ternary_delete(ast_ternary *self)
 
 ast_loop* ast_loop_new(lex_ctx ctx,
                        ast_expression *initexpr,
-                       ast_expression *precond,
-                       ast_expression *postcond,
+                       ast_expression *precond, bool pre_not,
+                       ast_expression *postcond, bool post_not,
                        ast_expression *increment,
                        ast_expression *body)
 {
@@ -723,6 +723,9 @@ ast_loop* ast_loop_new(lex_ctx ctx,
     self->increment = increment;
     self->body      = body;
 
+    self->pre_not   = pre_not;
+    self->post_not  = post_not;
+
     if (initexpr)
         ast_propagate_effects(self, initexpr);
     if (precond)
@@ -1058,10 +1061,10 @@ const char* ast_function_label(ast_function *self, const char *prefix)
     from = self->labelbuf + sizeof(self->labelbuf)-1;
     *from-- = 0;
     do {
-        unsigned int digit = id % 10;
-        *from = digit + '0';
+        *from-- = (id%10) + '0';
         id /= 10;
     } while (id);
+    ++from;
     memcpy(from - len, prefix, len);
     return from - len;
 }
@@ -1502,7 +1505,7 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     }
 
     /* TODO: check return types */
-    if (!self->curblock->is_return)
+    if (!self->curblock->final)
     {
         if (!self->vtype->expression.next ||
             self->vtype->expression.next->expression.vtype == TYPE_VOID)
@@ -1570,10 +1573,9 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu
     {
         ast_expression_codegen *gen;
         if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
-            if (OPTS_FLAG(ALLOW_UNREACHABLE_CODE))
-                continue;
-            compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
-            return false;
+            if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
+                return false;
+            continue;
         }
         gen = self->exprs[i]->expression.codegen;
         if (!(*gen)(self->exprs[i], func, false, out))
@@ -2547,6 +2549,11 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
         else if (bpostcond)  ontrue = bpostcond;
         else                 ontrue = bprecond;
         onfalse = bout;
+        if (self->pre_not) {
+            tmpblock = ontrue;
+            ontrue   = onfalse;
+            onfalse  = tmpblock;
+        }
         if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
             return false;
     }
@@ -2582,6 +2589,11 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
         else if (bincrement) ontrue = bincrement;
         else                 ontrue = bpostcond;
         onfalse = bout;
+        if (self->post_not) {
+            tmpblock = ontrue;
+            ontrue   = onfalse;
+            onfalse  = tmpblock;
+        }
         if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
             return false;
     }