diff --git a/ast.c b/ast.c index 9779f8f..6675244 100644 --- a/ast.c +++ b/ast.c @@ -411,6 +411,7 @@ ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype) self->vtype = vtype; self->name = name ? util_strdup(name) : NULL; MEM_VECTOR_INIT(self, blocks); + MEM_VECTOR_INIT(self, params); self->labelcount = 0; self->builtin = 0; @@ -428,6 +429,7 @@ ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype) } MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks) +MEM_VEC_FUNCTIONS(ast_function, ast_value*, params) void ast_function_delete(ast_function *self) { @@ -446,6 +448,12 @@ void ast_function_delete(ast_function *self) for (i = 0; i < self->blocks_count; ++i) ast_delete(self->blocks[i]); MEM_VECTOR_CLEAR(self, blocks); + /* ast_delete, not unref, there must only have been references + * to the parameter values inside the blocks deleted above. + */ + for (i = 0; i < self->params_count; ++i) + ast_delete(self->params[i]); + MEM_VECTOR_CLEAR(self, params); mem_d(self); } @@ -563,7 +571,7 @@ error: /* clean up */ return false; } -bool ast_local_codegen(ast_value *self, ir_function *func) +bool ast_local_codegen(ast_value *self, ir_function *func, bool param) { ir_value *v = NULL; if (self->isconst && self->expression.vtype == TYPE_FUNCTION) @@ -574,7 +582,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func) return false; } - v = ir_function_create_local(func, self->name, self->expression.vtype); + v = ir_function_create_local(func, self->name, self->expression.vtype, param); if (!v) return false; @@ -623,11 +631,23 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir) return false; } + if (!self->builtin && self->vtype->params_count != self->params_count) { + printf("ast_function's parameter variables doesn't match the declared parameter count\n"); + printf("%i != %i\n", self->vtype->params_count, self->params_count); + return false; + } + + /* fill the parameter list */ for (i = 0; i < self->vtype->params_count; ++i) { if (!ir_function_params_add(irf, self->vtype->params[i]->expression.vtype)) return false; } + /* generate the parameter locals */ + for (i = 0; i < self->params_count; ++i) { + if (!ast_local_codegen(self->params[i], self->ir_func, true)) + return false; + } if (self->builtin) { irf->builtin = self->builtin; @@ -688,7 +708,7 @@ bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_valu /* generate locals */ for (i = 0; i < self->locals_count; ++i) { - if (!ast_local_codegen(self->locals[i], func->ir_func)) + if (!ast_local_codegen(self->locals[i], func->ir_func, false)) return false; } diff --git a/ast.h b/ast.h index 2c6c4d8..1f448bc 100644 --- a/ast.h +++ b/ast.h @@ -127,7 +127,7 @@ void ast_value_delete(ast_value*); bool ast_value_set_name(ast_value*, const char *name); bool ast_value_codegen(ast_value*, ast_function*, bool lvalue, ir_value**); -bool ast_local_codegen(ast_value *self, ir_function *func); +bool ast_local_codegen(ast_value *self, ir_function *func, bool isparam); bool ast_global_codegen(ast_value *self, ir_builder *ir); /* Binary @@ -362,6 +362,14 @@ struct ast_function_s char labelbuf[64]; MEM_VECTOR_MAKE(ast_block*, blocks); + + /* contrary to the params in ast_value, these are the parameter variables + * which are to be used in expressions. + * The ast_value for the function contains only the parameter types used + * to generate ast_calls, and ast_call contains the parameter values + * used in that call. + */ + MEM_VECTOR_MAKE(ast_value*, params); }; ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype); /* This will NOT delete the underlying ast_value */ @@ -372,6 +380,7 @@ void ast_function_delete(ast_function*); const char* ast_function_label(ast_function*, const char *prefix); MEM_VECTOR_PROTO(ast_function, ast_block*, blocks); +MEM_VECTOR_PROTO(ast_function, ast_value*, params); bool ast_function_codegen(ast_function *self, ir_builder *builder); diff --git a/gmqcc.h b/gmqcc.h index 59c8f47..58e12cf 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -741,6 +741,7 @@ _MEM_VEC_FUN_FIND(Tself, Twhat, mem) enum store_types { store_global, store_local, /* local, assignable for now, should get promoted later */ + store_param, /* parameters, they are locals with a fixed position */ store_value, /* unassignable */ store_return /* unassignable, at OFS_RETURN */ }; diff --git a/ir.c b/ir.c index 1457dee..7f9cdb1 100644 --- a/ir.c +++ b/ir.c @@ -341,14 +341,21 @@ ir_value* ir_function_get_local(ir_function *self, const char *name) return NULL; } -ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype) +ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param) { ir_value *ve = ir_function_get_local(self, name); if (ve) { return NULL; } - ve = ir_value_var(name, store_local, vtype); + if (param && + self->locals_count && + self->locals[self->locals_count-1]->store != store_param) { + printf("cannot add parameters after adding locals\n"); + return NULL; + } + + ve = ir_value_var(name, (param ? store_param : store_local), vtype); if (!ir_function_locals_add(self, ve)) { ir_value_delete(ve); return NULL; @@ -1516,7 +1523,7 @@ static bool ir_block_naive_phi(ir_block *self) if (v->writes[w]->_ops[0] == v) v->writes[w]->_ops[0] = instr->_ops[0]; - if (old->store != store_value && old->store != store_local) + if (old->store != store_value && old->store != store_local && old->store != store_param) { /* If it originally wrote to a global we need to store the value * there as welli @@ -1896,8 +1903,11 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change value = instr->_ops[o]; /* We only care about locals */ + /* we also calculate parameter liferanges so that locals + * can take up parameter slots */ if (value->store != store_value && - value->store != store_local) + value->store != store_local && + value->store != store_param) continue; /* read operands */ @@ -2420,6 +2430,7 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) static bool ir_builder_gen_global(ir_builder *self, ir_value *global) { + size_t i; int32_t *iptr; prog_section_def def; @@ -2501,8 +2512,8 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global) case TYPE_VARIANT: /* assume biggest type */ global->code.globaladdr = code_globals_add(0); - code_globals_add(0); - code_globals_add(0); + for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i) + code_globals_add(0); return true; default: /* refuse to create 'void' type or any other fancy business. */ diff --git a/ir.h b/ir.h index 721799b..6fdeb89 100644 --- a/ir.h +++ b/ir.h @@ -269,7 +269,7 @@ MEM_VECTOR_PROTO(ir_function, int, params); MEM_VECTOR_PROTO(ir_function, ir_block*, blocks); ir_value* ir_function_get_local(ir_function *self, const char *name); -ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype); +ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param); bool GMQCC_WARN ir_function_finalize(ir_function*); /*