]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
Generating function-local arrays
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index ac204d31808841135b060a23650ed4b4b77c50c9..89da314f114b03b28d442c4fae1b685425b5ef8d 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -968,6 +968,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
 
         ast_expression_common *elemtype = &self->expression.next->expression;
         int vtype = elemtype->vtype;
+
         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
         if (!self->expression.count || self->expression.count > opts_max_array_size) {
             asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
@@ -1070,19 +1071,66 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
         return false;
     }
 
-    if (self->expression.vtype == TYPE_ARRAY)
+    if (self->expression.vtype == TYPE_ARRAY) {
+        size_t ai;
+        char   *name;
+        size_t  namelen;
+
+        ast_expression_common *elemtype = &self->expression.next->expression;
+        int vtype = elemtype->vtype;
+
+        if (param) {
+            asterror(ast_ctx(self), "array-parameters are not supported");
+            return false;
+        }
+
+        /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
+        if (!self->expression.count || self->expression.count > opts_max_array_size) {
+            asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
+        }
+
+        self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
+        if (!self->ir_values) {
+            asterror(ast_ctx(self), "failed to allocate array values");
+            return false;
+        }
+
+        v = ir_function_create_local(func, self->name, vtype, param);
+        if (!v) {
+            asterror(ast_ctx(self), "ir_function_create_local failed");
+            return false;
+        }
+        if (vtype == TYPE_FIELD)
+            v->fieldtype = elemtype->next->expression.vtype;
+        v->context = ast_ctx(self);
+
+        namelen = strlen(self->name);
+        name    = (char*)mem_a(namelen + 16);
+        strcpy(name, self->name);
+
+        self->ir_values[0] = v;
+        for (ai = 1; ai < self->expression.count; ++ai) {
+            snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
+            self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
+            if (!self->ir_values[ai]) {
+                asterror(ast_ctx(self), "ir_builder_create_global failed");
+                return false;
+            }
+            if (vtype == TYPE_FIELD)
+                self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
+            self->ir_values[ai]->context = ast_ctx(self);
+        }
+    }
+    else
     {
-        asterror(ast_ctx(self), "TODO: ast_local_codgen for TYPE_ARRAY");
-        return false;
+        v = ir_function_create_local(func, self->name, self->expression.vtype, param);
+        if (!v)
+            return false;
+        if (self->expression.vtype == TYPE_FIELD)
+            v->fieldtype = self->expression.next->expression.vtype;
+        v->context = ast_ctx(self);
     }
 
-    v = ir_function_create_local(func, self->name, self->expression.vtype, param);
-    if (!v)
-        return false;
-    if (self->expression.vtype == TYPE_FIELD)
-        v->fieldtype = self->expression.next->expression.vtype;
-    v->context = ast_ctx(self);
-
     /* A constant local... hmmm...
      * I suppose the IR will have to deal with this
      */
@@ -1109,6 +1157,17 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
 
     /* link us to the ir_value */
     self->ir_v = v;
+
+    if (self->setter) {
+        if (!ast_global_codegen(self->setter, func->owner, false) ||
+            !ast_function_codegen(self->setter->constval.vfunc, func->owner))
+            return false;
+    }
+    if (self->getter) {
+        if (!ast_global_codegen(self->getter, func->owner, false) ||
+            !ast_function_codegen(self->getter->constval.vfunc, func->owner))
+            return false;
+    }
     return true;
 
 error: /* clean up */
@@ -1244,6 +1303,10 @@ bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_valu
     ast_expression_codegen *cgen;
     ir_value *left, *right;
 
+    ast_value       *arr;
+    ast_value       *idx;
+    ast_array_index *ai = NULL;
+
     if (lvalue && self->expression.outl) {
         *out = self->expression.outl;
         return true;
@@ -1254,20 +1317,72 @@ bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_valu
         return true;
     }
 
-    cgen = self->dest->expression.codegen;
-    /* lvalue! */
-    if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
-        return false;
-    self->expression.outl = left;
+    if (ast_istype(self->dest, ast_array_index))
+    {
 
-    cgen = self->source->expression.codegen;
-    /* rvalue! */
-    if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
-        return false;
+        ai = (ast_array_index*)self->dest;
+        idx = (ast_value*)ai->index;
 
-    if (!ir_block_create_store_op(func->curblock, self->op, left, right))
-        return false;
-    self->expression.outr = right;
+        if (ast_istype(ai->index, ast_value) && idx->isconst)
+            ai = NULL;
+    }
+
+    if (ai) {
+        /* we need to call the setter */
+        ir_value  *iridx, *funval;
+        ir_instr  *call;
+
+        if (lvalue) {
+            asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
+            return false;
+        }
+
+        arr = (ast_value*)ai->array;
+        if (!ast_istype(ai->array, ast_value) || !arr->setter) {
+            asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
+            return false;
+        }
+
+        cgen = idx->expression.codegen;
+        if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
+            return false;
+
+        cgen = arr->setter->expression.codegen;
+        if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
+            return false;
+
+        cgen = self->source->expression.codegen;
+        if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
+            return false;
+
+        call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
+        if (!call)
+            return false;
+        if (!ir_call_param(call, iridx))
+            return false;
+        if (!ir_call_param(call, right))
+            return false;
+        self->expression.outr = right;
+    }
+    else
+    {
+        /* regular code */
+
+        cgen = self->dest->expression.codegen;
+        /* lvalue! */
+        if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
+            return false;
+        self->expression.outl = left;
+
+        cgen = self->source->expression.codegen;
+        /* rvalue! */
+        if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
+            return false;
+
+        if (!ir_block_create_store_op(func->curblock, self->op, left, right))
+            return false;
+        self->expression.outr = right;
+    }
 
     /* Theoretically, an assinment returns its left side as an
      * lvalue, if we don't need an lvalue though, we return