From a4617d0e610380f9048499114b3fc086876bb7eb Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Sun, 22 Jul 2012 12:15:48 +0200 Subject: [PATCH] ast_function generates parameter locals, ir_function_create_local now allows adding parameters as long as no local variables have been added yet --- ast.c | 18 +++++++++++++++--- ast.h | 2 +- ir.c | 11 +++++++++-- ir.h | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ast.c b/ast.c index 767dde9..f0fba0f 100644 --- a/ast.c +++ b/ast.c @@ -557,7 +557,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) @@ -568,7 +568,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; @@ -617,11 +617,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; @@ -682,7 +694,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 ecc2498..26edec9 100644 --- a/ast.h +++ b/ast.h @@ -125,7 +125,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 diff --git a/ir.c b/ir.c index 2211949..0c0517a 100644 --- a/ir.c +++ b/ir.c @@ -311,14 +311,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; diff --git a/ir.h b/ir.h index 7aa889b..fb0f699 100644 --- a/ir.h +++ b/ir.h @@ -265,7 +265,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*); /* -- 2.39.2