]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Merge branch 'master' into blub/bc3
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 9779f8f64d9b7d73d2f04195ee877689d64da039..66752444f6fb697d014badd08489565292f7831b 100644 (file)
--- 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;
     }