]> 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 17c63413cc7de19fa8cc5123150b02a9c9f08afb..582cda6a7af4dc6805a9dd563360dfd3a2a67cfb 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -170,6 +170,18 @@ ast_binary* ast_binary_new(lex_ctx ctx, int op,
     self->left = left;
     self->right = right;
 
+    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)
+        self->expression.vtype = TYPE_FLOAT;
+    else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
+        self->expression.vtype = TYPE_VECTOR;
+    else if (op == INSTR_MUL_V)
+        self->expression.vtype = TYPE_FLOAT;
+    else
+        self->expression.vtype = left->expression.vtype;
+
     return self;
 }
 
@@ -316,8 +328,11 @@ ast_call* ast_call_new(lex_ctx ctx,
 
     MEM_VECTOR_INIT(self, params);
 
+    self->func = funcexpr;
+
     return self;
 }
+MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
 
 void ast_call_delete(ast_call *self)
 {
@@ -433,6 +448,9 @@ 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);
@@ -488,8 +506,10 @@ bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_valu
      * and the ast-user should take care of ast_global_codegen to be used
      * on all the globals.
      */
-    if (!self->ir_v)
+    if (!self->ir_v) {
+        printf("ast_value used before generated (%s)\n", self->name);
         return false;
+    }
     *out = self->ir_v;
     return true;
 }
@@ -504,13 +524,16 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir)
             return false;
 
         self->constval.vfunc->ir_func = func;
+        self->ir_v = func->value;
         /* The function is filled later on ast_function_codegen... */
         return true;
     }
 
     v = ir_builder_create_global(ir, self->name, self->expression.vtype);
-    if (!v)
+    if (!v) {
+        printf("ir_builder_create_global failed\n");
         return false;
+    }
 
     if (self->isconst) {
         switch (self->expression.vtype)
@@ -548,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)
@@ -559,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;
 
@@ -608,9 +631,21 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
         return false;
     }
 
-    for (i = 0; i < self->params_count; ++i)
+    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", (int)self->vtype->params_count, (int)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->params[i]->expression.vtype))
+        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;
     }
 
@@ -634,7 +669,9 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     {
         if (!self->vtype->expression.next ||
             self->vtype->expression.next->expression.vtype == TYPE_VOID)
+        {
             return ir_block_create_return(self->curblock, NULL);
+        }
         else
         {
             /* error("missing return"); */
@@ -671,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;
     }
 
@@ -1194,7 +1231,6 @@ bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value
 
 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
 {
-    /* TODO: call ir codegen */
     ast_expression_codegen *cgen;
     ir_value_vector         params;
     ir_instr               *callinstr;