]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Fix murmur hash seeding
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 31df700d2b0e0e1e21235bc9f5380786270af9b7..93d2a173f9c4a37686229c223e936ac38f7e94a8 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -71,8 +71,9 @@ static void ast_expression_init(ast_expression *self,
     self->expression.next     = NULL;
     self->expression.outl     = NULL;
     self->expression.outr     = NULL;
-    self->expression.variadic = false;
     self->expression.params   = NULL;
+    self->expression.count    = 0;
+    self->expression.flags    = 0;
 }
 
 static void ast_expression_delete(ast_expression *self)
@@ -107,7 +108,8 @@ ast_value* ast_value_copy(const ast_value *self)
     }
     fromex   = &self->expression;
     selfex = &cp->expression;
-    selfex->variadic = fromex->variadic;
+    selfex->count    = fromex->count;
+    selfex->flags    = fromex->flags;
     for (i = 0; i < vec_size(fromex->params); ++i) {
         ast_value *v = ast_value_copy(fromex->params[i]);
         if (!v) {
@@ -132,7 +134,8 @@ bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
     }
     fromex   = &other->expression;
     selfex = &self->expression;
-    selfex->variadic = fromex->variadic;
+    selfex->count    = fromex->count;
+    selfex->flags    = fromex->flags;
     for (i = 0; i < vec_size(fromex->params); ++i) {
         ast_value *v = ast_value_copy(fromex->params[i]);
         if (!v)
@@ -183,7 +186,8 @@ ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
         else
             selfex->next = NULL;
 
-        selfex->variadic = fromex->variadic;
+        selfex->count    = fromex->count;
+        selfex->flags    = fromex->flags;
         for (i = 0; i < vec_size(fromex->params); ++i) {
             ast_value *v = ast_value_copy(fromex->params[i]);
             if (!v) {
@@ -205,7 +209,7 @@ bool ast_compare_type(ast_expression *a, ast_expression *b)
         return false;
     if (vec_size(a->expression.params) != vec_size(b->expression.params))
         return false;
-    if (a->expression.variadic != b->expression.variadic)
+    if (a->expression.flags != b->expression.flags)
         return false;
     if (vec_size(a->expression.params)) {
         size_t i;
@@ -393,8 +397,13 @@ ast_binary* ast_binary_new(lex_ctx ctx, int op,
 
     if (op >= INSTR_EQ_F && op <= INSTR_GT)
         self->expression.vtype = TYPE_FLOAT;
-    else if (op == INSTR_AND || op == INSTR_OR ||
-             op == INSTR_BITAND || op == INSTR_BITOR)
+    else if (op == INSTR_AND || op == INSTR_OR) {
+        if (OPTS_FLAG(PERL_LOGIC))
+            ast_type_adopt(self, right);
+        else
+            self->expression.vtype = TYPE_FLOAT;
+    }
+    else if (op == INSTR_BITAND || op == INSTR_BITOR)
         self->expression.vtype = TYPE_FLOAT;
     else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
         self->expression.vtype = TYPE_VECTOR;
@@ -429,16 +438,10 @@ ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
 
     self->keep_dest = false;
 
-    self->expression.vtype = left->expression.vtype;
-    if (left->expression.next) {
-        self->expression.next = ast_type_copy(ctx, left);
-        if (!self->expression.next) {
-            ast_delete(self);
-            return NULL;
-        }
+    if (!ast_type_adopt(self, left)) {
+        ast_delete(self);
+        return NULL;
     }
-    else
-        self->expression.next = NULL;
 
     return self;
 }
@@ -855,11 +858,6 @@ ast_call* ast_call_new(lex_ctx ctx,
     self->params = NULL;
     self->func   = funcexpr;
 
-/*
-    self->expression.vtype = funcexpr->expression.next->expression.vtype;
-    if (funcexpr->expression.next->expression.next)
-        self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
-*/
     ast_type_adopt(self, funcexpr->expression.next);
 
     return self;
@@ -915,16 +913,10 @@ ast_store* ast_store_new(lex_ctx ctx, int op,
     self->dest = dest;
     self->source = source;
 
-    self->expression.vtype = dest->expression.vtype;
-    if (dest->expression.next) {
-        self->expression.next = ast_type_copy(ctx, dest);
-        if (!self->expression.next) {
-            ast_delete(self);
-            return NULL;
-        }
+    if (!ast_type_adopt(self, dest)) {
+        ast_delete(self);
+        return NULL;
     }
-    else
-        self->expression.next = NULL;
 
     return self;
 }
@@ -950,10 +942,19 @@ ast_block* ast_block_new(lex_ctx ctx)
     return self;
 }
 
-void ast_block_add_expr(ast_block *self, ast_expression *e)
+bool ast_block_add_expr(ast_block *self, ast_expression *e)
 {
     ast_propagate_effects(self, e);
     vec_push(self->exprs, e);
+    if (self->expression.next) {
+        ast_delete(self->expression.next);
+        self->expression.next = NULL;
+    }
+    if (!ast_type_adopt(self, e)) {
+        compile_error(ast_ctx(self), "internal error: failed to adopt type");
+        return false;
+    }
+    return true;
 }
 
 void ast_block_collect(ast_block *self, ast_expression *expr)
@@ -982,14 +983,8 @@ bool ast_block_set_type(ast_block *self, ast_expression *from)
 {
     if (self->expression.next)
         ast_delete(self->expression.next);
-    self->expression.vtype = from->expression.vtype;
-    if (from->expression.next) {
-        self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
-        if (!self->expression.next)
-            return false;
-    }
-    else
-        self->expression.next = NULL;
+    if (!ast_type_adopt(self, from))
+        return false;
     return true;
 }
 
@@ -1054,7 +1049,7 @@ const char* ast_function_label(ast_function *self, const char *prefix)
     size_t len;
     char  *from;
 
-    if (!opts_dump && !opts_dumpfin)
+    if (!opts.dump && !opts.dumpfin)
         return NULL;
 
     id  = (self->labelcount++);
@@ -1151,7 +1146,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             }
 
             /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
-            if (!array->expression.count || array->expression.count > opts_max_array_size)
+            if (!array->expression.count || array->expression.count > opts.max_array_size)
                 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
 
             elemtype = &array->expression.next->expression;
@@ -1163,6 +1158,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 return false;
             }
             v->context = ast_ctx(self);
+            v->unique_life = true;
             array->ir_v = self->ir_v = v;
 
             namelen = strlen(self->name);
@@ -1180,6 +1176,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                     return false;
                 }
                 array->ir_values[ai]->context = ast_ctx(self);
+                array->ir_values[ai]->unique_life = true;
             }
             mem_d(name);
         }
@@ -1203,7 +1200,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
         int vtype = elemtype->vtype;
 
         /* same as with field arrays */
-        if (!self->expression.count || self->expression.count > opts_max_array_size)
+        if (!self->expression.count || self->expression.count > opts.max_array_size)
             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
 
         v = ir_builder_create_global(ir, self->name, vtype);
@@ -1212,6 +1209,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             return false;
         }
         v->context = ast_ctx(self);
+        v->unique_life = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1228,6 +1226,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 return false;
             }
             self->ir_values[ai]->context = ast_ctx(self);
+            self->ir_values[ai]->unique_life = true;
         }
         mem_d(name);
     }
@@ -1322,7 +1321,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         }
 
         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
-        if (!self->expression.count || self->expression.count > opts_max_array_size) {
+        if (!self->expression.count || self->expression.count > opts.max_array_size) {
             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
         }
 
@@ -1338,6 +1337,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
             return false;
         }
         v->context = ast_ctx(self);
+        v->unique_life = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1352,6 +1352,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
                 return false;
             }
             self->ir_values[ai]->context = ast_ctx(self);
+            self->ir_values[ai]->unique_life = true;
         }
     }
     else
@@ -1421,14 +1422,14 @@ bool ast_generate_accessors(ast_value *self, ir_builder *ir)
         }
     }
 
-    options_set(opts_warn, WARN_USED_UNINITIALIZED, false);
+    opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
     if (self->setter) {
         if (!ast_global_codegen  (self->setter, ir, false) ||
             !ast_function_codegen(self->setter->constval.vfunc, ir) ||
             !ir_function_finalize(self->setter->constval.vfunc->ir_func))
         {
             compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
-            options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
+            opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
             return false;
         }
     }
@@ -1438,14 +1439,14 @@ bool ast_generate_accessors(ast_value *self, ir_builder *ir)
             !ir_function_finalize(self->getter->constval.vfunc->ir_func))
         {
             compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
-            options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
+            opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
             return false;
         }
     }
     for (i = 0; i < self->expression.count; ++i) {
         vec_free(self->ir_values[i]->life);
     }
-    options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
+    opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
     return true;
 }
 
@@ -1559,7 +1560,7 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu
     for (i = 0; i < vec_size(self->locals); ++i)
     {
         if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
-            if (opts_debug)
+            if (opts.debug)
                 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
             return false;
         }
@@ -1639,7 +1640,7 @@ bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_valu
         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
             return false;
 
-        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
+        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
         if (!call)
             return false;
         ir_call_param(call, iridx);
@@ -1877,7 +1878,7 @@ bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, i
         if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
             return false;
 
-        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
+        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
         if (!call)
             return false;
         ir_call_param(call, iridx);
@@ -2010,7 +2011,9 @@ bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, i
     } else {
         *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
                                              ent, field, self->expression.vtype);
+        /* Done AFTER error checking: 
         codegen_output_type(self, *out);
+        */
     }
     if (!*out) {
         compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
@@ -2018,6 +2021,8 @@ bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, i
                  type_name[self->expression.vtype]);
         return false;
     }
+    if (!lvalue)
+        codegen_output_type(self, *out);
 
     if (lvalue)
         self->expression.outl = *out;
@@ -2105,7 +2110,7 @@ bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lva
         if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
             return false;
 
-        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
+        call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
         if (!call)
             return false;
         ir_call_param(call, iridx);
@@ -2147,7 +2152,7 @@ bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_va
     ir_value *condval;
     ir_value *dummy;
 
-    ir_block *cond = func->curblock;
+    ir_block *cond;
     ir_block *ontrue;
     ir_block *onfalse;
     ir_block *ontrue_endblock = NULL;
@@ -2346,6 +2351,8 @@ bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_
     self->expression.outr = ir_phi_value(phi);
     *out = self->expression.outr;
 
+    codegen_output_type(self, *out);
+
     return true;
 }
 
@@ -2619,8 +2626,10 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
 {
     ast_expression_codegen *cgen;
 
-    ast_switch_case *def_case  = NULL;
-    ir_block        *def_bfall = NULL;
+    ast_switch_case *def_case     = NULL;
+    ir_block        *def_bfall    = NULL;
+    ir_block        *def_bfall_to = NULL;
+    bool set_def_bfall_to = false;
 
     ir_value *dummy     = NULL;
     ir_value *irop      = NULL;
@@ -2694,6 +2703,10 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
             bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
             if (!bcase || !bnot)
                 return false;
+            if (set_def_bfall_to) {
+                set_def_bfall_to = false;
+                def_bfall_to = bcase;
+            }
             if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
                 return false;
 
@@ -2723,6 +2736,8 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
             bfall     = NULL;
             /* remember which case it was */
             def_case  = swcase;
+            /* And the next case will be remembered */
+            set_def_bfall_to = true;
         }
     }
 
@@ -2751,6 +2766,13 @@ bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_va
         cgen = def_case->code->expression.codegen;
         if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
             return false;
+
+        /* see if we need to fall through */
+        if (def_bfall_to && !func->curblock->final)
+        {
+            if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
+                return false;
+        }
     }
 
     /* Jump from the last bnot to bout */
@@ -2883,7 +2905,9 @@ bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value
         vec_push(params, param);
     }
 
-    callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
+    callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
+                                     ast_function_label(func, "call"),
+                                     funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
     if (!callinstr)
         goto error;