]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
-Oglobal-temps
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 90134134b255204e28baca3baa11e2e583ac59e0..b6a8a786c7d76b55a1fb7af884e82958848255af 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -71,9 +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)
@@ -108,8 +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) {
@@ -134,8 +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)
@@ -186,8 +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) {
@@ -209,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;
@@ -397,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;
@@ -471,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);
 }
@@ -591,6 +596,14 @@ void ast_member_delete(ast_member *self)
     mem_d(self);
 }
 
+bool ast_member_set_name(ast_member *self, const char *name)
+{
+    if (self->name)
+        mem_d((void*)self->name);
+    self->name = util_strdup(name);
+    return !!self->name;
+}
+
 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
 {
     ast_expression *outtype;
@@ -704,8 +717,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)
 {
@@ -718,6 +731,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)
@@ -1044,7 +1060,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 && !opts.debug)
         return NULL;
 
     id  = (self->labelcount++);
@@ -1053,10 +1069,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;
 }
@@ -1077,7 +1093,6 @@ void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
 }
 
 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
-#define codegen_output_type_expr(a,o) (_ast_codegen_output_type(a,(o)))
 
 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
 {
@@ -1091,9 +1106,9 @@ bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_valu
      * on all the globals.
      */
     if (!self->ir_v) {
-        char typename[1024];
-        ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
-        compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
+        char tname[1024]; /* typename is reserved in C++ */
+        ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
+        compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
         return false;
     }
     *out = self->ir_v;
@@ -1154,6 +1169,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             }
             v->context = ast_ctx(self);
             v->unique_life = true;
+            v->locked      = true;
             array->ir_v = self->ir_v = v;
 
             namelen = strlen(self->name);
@@ -1172,6 +1188,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
                 }
                 array->ir_values[ai]->context = ast_ctx(self);
                 array->ir_values[ai]->unique_life = true;
+                array->ir_values[ai]->locked      = true;
             }
             mem_d(name);
         }
@@ -1205,6 +1222,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
         }
         v->context = ast_ctx(self);
         v->unique_life = true;
+        v->locked      = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1222,6 +1240,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
             }
             self->ir_values[ai]->context = ast_ctx(self);
             self->ir_values[ai]->unique_life = true;
+            self->ir_values[ai]->locked      = true;
         }
         mem_d(name);
     }
@@ -1310,6 +1329,8 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         ast_expression_common *elemtype = &self->expression.next->expression;
         int vtype = elemtype->vtype;
 
+        func->flags |= IR_FLAG_HAS_ARRAYS;
+
         if (param) {
             compile_error(ast_ctx(self), "array-parameters are not supported");
             return false;
@@ -1333,6 +1354,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         }
         v->context = ast_ctx(self);
         v->unique_life = true;
+        v->locked      = true;
 
         namelen = strlen(self->name);
         name    = (char*)mem_a(namelen + 16);
@@ -1348,6 +1370,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
             }
             self->ir_values[ai]->context = ast_ctx(self);
             self->ir_values[ai]->unique_life = true;
+            self->ir_values[ai]->locked      = true;
         }
     }
     else
@@ -1417,14 +1440,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;
         }
     }
@@ -1434,14 +1457,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;
 }
 
@@ -1497,7 +1520,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)
@@ -1563,11 +1586,13 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu
 
     for (i = 0; i < vec_size(self->exprs); ++i)
     {
-        ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
+        ast_expression_codegen *gen;
         if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
-            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))
             return false;
     }
@@ -1635,7 +1660,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);
@@ -1690,7 +1715,7 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va
         return true;
     }
 
-    if (OPTS_FLAG(SHORT_LOGIC) &&
+    if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
         (self->op == INSTR_AND || self->op == INSTR_OR))
     {
         /* short circuit evaluation */
@@ -1698,64 +1723,42 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va
         ir_block *from_left, *from_right;
         ir_instr *phi;
         size_t    merge_id;
-        uint16_t  notop;
-
-        /* Note about casting to true boolean values:
-         * We use a single NOT for sub expressions, and an
-         * overall NOT at the end, and for that purpose swap
-         * all the jump conditions in order for the NOT to get
-         * doubled.
-         * ie: (a && b) usually becomes (!!a ? !!b : !!a)
-         * but we translate this to (!(!a ? !a : !b))
-         */
 
+        /* prepare end-block */
         merge_id = vec_size(func->ir_func->blocks);
-        merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
+        merge    = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
 
+        /* generate the left expression */
         cgen = self->left->expression.codegen;
         if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
             return false;
-        if (!OPTS_FLAG(PERL_LOGIC)) {
-            notop = type_not_instr[left->vtype];
-            if (notop == AINSTR_END) {
-                compile_error(ast_ctx(self), "don't know how to cast to bool...");
-                return false;
-            }
-            left = ir_block_create_unary(func->curblock, ast_ctx(self),
-                                         ast_function_label(func, "sce_not"),
-                                         notop,
-                                         left);
-        }
+        /* remember the block */
         from_left = func->curblock;
 
+        /* create a new block for the right expression */
         other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
-        if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
+        if (self->op == INSTR_AND) {
+            /* on AND: left==true -> other */
             if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
                 return false;
         } else {
+            /* on OR: left==false -> other */
             if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
                 return false;
         }
         /* use the likely flag */
         vec_last(func->curblock->instr)->likely = true;
 
+        /* enter the right-expression's block */
         func->curblock = other;
+        /* generate */
         cgen = self->right->expression.codegen;
         if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
             return false;
-        if (!OPTS_FLAG(PERL_LOGIC)) {
-            notop = type_not_instr[right->vtype];
-            if (notop == AINSTR_END) {
-                compile_error(ast_ctx(self), "don't know how to cast to bool...");
-                return false;
-            }
-            right = ir_block_create_unary(func->curblock, ast_ctx(self),
-                                          ast_function_label(func, "sce_not"),
-                                          notop,
-                                          right);
-        }
+        /* remember block */
         from_right = func->curblock;
 
+        /* jump to the merge block */
         if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
             return false;
 
@@ -1763,23 +1766,50 @@ bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_va
         vec_push(func->ir_func->blocks, merge);
 
         func->curblock = merge;
-        phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
+        phi = ir_block_create_phi(func->curblock, ast_ctx(self),
+                                  ast_function_label(func, "sce_value"),
+                                  self->expression.vtype);
         ir_phi_add(phi, from_left, left);
         ir_phi_add(phi, from_right, right);
         *out = ir_phi_value(phi);
+        if (!*out)
+            return false;
+
         if (!OPTS_FLAG(PERL_LOGIC)) {
-            notop = type_not_instr[(*out)->vtype];
-            if (notop == AINSTR_END) {
-                compile_error(ast_ctx(self), "don't know how to cast to bool...");
-                return false;
+            /* cast-to-bool */
+            if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
+                *out = ir_block_create_unary(func->curblock, ast_ctx(self),
+                                             ast_function_label(func, "sce_bool_v"),
+                                             INSTR_NOT_V, *out);
+                if (!*out)
+                    return false;
+                *out = ir_block_create_unary(func->curblock, ast_ctx(self),
+                                             ast_function_label(func, "sce_bool"),
+                                             INSTR_NOT_F, *out);
+                if (!*out)
+                    return false;
+            }
+            else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
+                *out = ir_block_create_unary(func->curblock, ast_ctx(self),
+                                             ast_function_label(func, "sce_bool_s"),
+                                             INSTR_NOT_S, *out);
+                if (!*out)
+                    return false;
+                *out = ir_block_create_unary(func->curblock, ast_ctx(self),
+                                             ast_function_label(func, "sce_bool"),
+                                             INSTR_NOT_F, *out);
+                if (!*out)
+                    return false;
+            }
+            else {
+                *out = ir_block_create_binop(func->curblock, ast_ctx(self),
+                                             ast_function_label(func, "sce_bool"),
+                                             INSTR_AND, *out, *out);
+                if (!*out)
+                    return false;
             }
-            *out = ir_block_create_unary(func->curblock, ast_ctx(self),
-                                         ast_function_label(func, "sce_final_not"),
-                                         notop,
-                                         *out);
         }
-        if (!*out)
-            return false;
+
         self->expression.outr = *out;
         return true;
     }
@@ -1873,7 +1903,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);
@@ -2105,7 +2135,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 +2177,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;
@@ -2539,6 +2569,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;
     }
@@ -2574,6 +2609,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;
     }
@@ -2621,8 +2661,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;
@@ -2696,6 +2738,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;
 
@@ -2725,6 +2771,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;
         }
     }
 
@@ -2753,6 +2801,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 */
@@ -2817,14 +2872,14 @@ bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value
         if (self->irblock_from) {
             /* we already tried once, this is the callback */
             self->irblock_from->final = false;
-            if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
+            if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
                 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
                 return false;
             }
         }
         else
         {
-            if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
+            if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
                 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
                 return false;
             }
@@ -2885,7 +2940,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;