X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=ast.c;h=68a8eaccf0f6b7ef6c7303b56bbb2e70218aaba1;hb=dd6f415a0456b7f09afebe6b008060bdb7fecb91;hp=31df700d2b0e0e1e21235bc9f5380786270af9b7;hpb=1ca3e724176898c5610d943d98915acef43214bb;p=xonotic%2Fgmqcc.git diff --git a/ast.c b/ast.c index 31df700..68a8eac 100644 --- a/ast.c +++ b/ast.c @@ -73,6 +73,7 @@ static void ast_expression_init(ast_expression *self, self->expression.outr = NULL; self->expression.variadic = false; self->expression.params = NULL; + self->expression.count = 0; } static void ast_expression_delete(ast_expression *self) @@ -108,6 +109,7 @@ ast_value* ast_value_copy(const ast_value *self) fromex = &self->expression; selfex = &cp->expression; selfex->variadic = fromex->variadic; + selfex->count = fromex->count; for (i = 0; i < vec_size(fromex->params); ++i) { ast_value *v = ast_value_copy(fromex->params[i]); if (!v) { @@ -133,6 +135,7 @@ 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; for (i = 0; i < vec_size(fromex->params); ++i) { ast_value *v = ast_value_copy(fromex->params[i]); if (!v) @@ -184,6 +187,7 @@ ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex) selfex->next = NULL; selfex->variadic = fromex->variadic; + selfex->count = fromex->count; for (i = 0; i < vec_size(fromex->params); ++i) { ast_value *v = ast_value_copy(fromex->params[i]); if (!v) { @@ -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; } @@ -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; @@ -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 */