]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Factoring out variable parsing so it can be used for locals too
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index 065dfd88dbf2d4fb80dd09c8c70caaa66475cfa7..d519919ac10b50504b4c0e7d846be66c6cb5fe90 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -316,8 +316,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)
 {
@@ -398,6 +401,7 @@ ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
     MEM_VECTOR_INIT(self, blocks);
 
     self->labelcount = 0;
+    self->builtin = 0;
 
     self->ir_func = NULL;
     self->curblock = NULL;
@@ -482,8 +486,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;
 }
@@ -498,6 +504,7 @@ 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;
     }
@@ -522,10 +529,11 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir)
                     goto error;
                 break;
             case TYPE_FUNCTION:
+                printf("global of type function not properly generated\n");
+                goto error;
                 /* Cannot generate an IR value for a function,
                  * need a pointer pointing to a function rather.
                  */
-                goto error;
             default:
                 printf("TODO: global constant type %i\n", self->expression.vtype);
                 break;
@@ -601,6 +609,17 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
         return false;
     }
 
+    for (i = 0; i < self->vtype->params_count; ++i)
+    {
+        if (!ir_function_params_add(irf, self->vtype->params[i]->expression.vtype))
+            return false;
+    }
+
+    if (self->builtin) {
+        irf->builtin = self->builtin;
+        return true;
+    }
+
     self->curblock = ir_function_create_block(irf, "entry");
     if (!self->curblock)
         return false;
@@ -616,7 +635,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"); */
@@ -1176,7 +1197,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;