+ self->constval.vfunc->ir_func = func;
+ self->ir_v = func->value;
+ /* The function is filled later on ast_function_codegen... */
+ return true;
+ }
+
+ if (isfield && self->expression.vtype == TYPE_FIELD) {
+ ast_expression *fieldtype = self->expression.next;
+
+ if (self->isconst) {
+ asterror(ast_ctx(self), "TODO: constant field pointers with value");
+ goto error;
+ }
+
+ if (fieldtype->expression.vtype == TYPE_ARRAY) {
+ size_t ai;
+ char *name;
+ size_t namelen;
+
+ ast_expression_common *elemtype;
+ int vtype;
+ ast_value *array = (ast_value*)fieldtype;
+
+ if (!ast_istype(fieldtype, ast_value)) {
+ asterror(ast_ctx(self), "internal error: ast_value required");
+ return false;
+ }
+
+ /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
+ if (!array->expression.count || array->expression.count > opts_max_array_size)
+ asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
+
+ elemtype = &array->expression.next->expression;
+ vtype = elemtype->vtype;
+
+ v = ir_builder_create_field(ir, self->name, vtype);
+ if (!v) {
+ asterror(ast_ctx(self), "ir_builder_create_global failed");
+ return false;
+ }
+ if (vtype == TYPE_FIELD)
+ v->fieldtype = elemtype->next->expression.vtype;
+ v->context = ast_ctx(self);
+ array->ir_v = self->ir_v = v;
+
+ namelen = strlen(self->name);
+ name = (char*)mem_a(namelen + 16);
+ strcpy(name, self->name);
+
+ array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
+ array->ir_values[0] = v;
+ for (ai = 1; ai < array->expression.count; ++ai) {
+ snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
+ array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
+ if (!array->ir_values[ai]) {
+ mem_d(name);
+ asterror(ast_ctx(self), "ir_builder_create_global failed");
+ return false;
+ }
+ if (vtype == TYPE_FIELD)
+ array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
+ array->ir_values[ai]->context = ast_ctx(self);
+ }
+ mem_d(name);
+ }
+ else
+ {
+ v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
+ if (!v)
+ return false;
+ v->context = ast_ctx(self);
+ self->ir_v = v;
+ }
+ return true;
+ }
+
+ 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;
+
+ /* same as with field arrays */
+ 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);
+
+ v = ir_builder_create_global(ir, self->name, vtype);
+ if (!v) {
+ asterror(ast_ctx(self), "ir_builder_create_global 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 = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
+ 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_builder_create_global(ir, name, vtype);
+ if (!self->ir_values[ai]) {
+ mem_d(name);
+ 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);
+ }
+ mem_d(name);
+ }
+ else
+ {
+ /* Arrays don't do this since there's no "array" value which spans across the
+ * whole thing.
+ */
+ v = ir_builder_create_global(ir, self->name, self->expression.vtype);
+ if (!v) {
+ asterror(ast_ctx(self), "ir_builder_create_global failed");
+ return false;
+ }
+ if (self->expression.vtype == TYPE_FIELD)
+ v->fieldtype = self->expression.next->expression.vtype;
+ v->context = ast_ctx(self);
+ }
+
+ if (self->isconst) {
+ switch (self->expression.vtype)
+ {
+ case TYPE_FLOAT:
+ if (!ir_value_set_float(v, self->constval.vfloat))
+ goto error;
+ break;
+ case TYPE_VECTOR:
+ if (!ir_value_set_vector(v, self->constval.vvec))
+ goto error;
+ break;
+ case TYPE_STRING:
+ if (!ir_value_set_string(v, self->constval.vstring))
+ goto error;
+ break;
+ case TYPE_ARRAY:
+ asterror(ast_ctx(self), "TODO: global constant array");
+ break;
+ case TYPE_FUNCTION:
+ asterror(ast_ctx(self), "global of type function not properly generated");
+ goto error;
+ /* Cannot generate an IR value for a function,
+ * need a pointer pointing to a function rather.
+ */
+ default:
+ asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
+ break;
+ }
+ }
+
+ /* link us to the ir_value */
+ self->ir_v = v;
+ return true;
+
+error: /* clean up */
+ ir_value_delete(v);
+ return false;
+}
+
+bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
+{
+ ir_value *v = NULL;
+ if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
+ {
+ /* Do we allow local functions? I think not...
+ * this is NOT a function pointer atm.
+ */
+ return false;
+ }
+
+ 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
+ {
+ 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
+ */
+ if (self->isconst) {
+ switch (self->expression.vtype)
+ {
+ case TYPE_FLOAT:
+ if (!ir_value_set_float(v, self->constval.vfloat))
+ goto error;
+ break;
+ case TYPE_VECTOR:
+ if (!ir_value_set_vector(v, self->constval.vvec))
+ goto error;
+ break;
+ case TYPE_STRING:
+ if (!ir_value_set_string(v, self->constval.vstring))
+ goto error;
+ break;
+ default:
+ asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
+ break;
+ }
+ }
+
+ /* 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) ||
+ !ir_function_finalize(self->setter->constval.vfunc->ir_func))
+ return false;
+ }
+ if (self->getter) {
+ if (!ast_global_codegen(self->getter, func->owner, false) ||
+ !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
+ !ir_function_finalize(self->getter->constval.vfunc->ir_func))
+ return false;
+ }
+ return true;
+
+error: /* clean up */
+ ir_value_delete(v);
+ return false;
+}
+
+bool ast_function_codegen(ast_function *self, ir_builder *ir)
+{
+ ir_function *irf;
+ ir_value *dummy;
+ ast_expression_common *ec;
+ size_t i;
+
+ irf = self->ir_func;
+ if (!irf) {
+ asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
+ return false;
+ }
+
+ /* fill the parameter list */
+ ec = &self->vtype->expression;
+ irf->max_parameters = vec_size(ec->params);
+ for (i = 0; i < vec_size(ec->params); ++i)
+ {
+ vec_push(irf->params, ec->params[i]->expression.vtype);
+ if (!self->builtin) {
+ if (!ast_local_codegen(ec->params[i], self->ir_func, true))
+ return false;
+ }
+ }
+
+ if (self->builtin) {
+ irf->builtin = self->builtin;
+ return true;
+ }
+
+ if (!vec_size(self->blocks)) {
+ asterror(ast_ctx(self), "function `%s` has no body", self->name);
+ return false;
+ }
+
+ self->curblock = ir_function_create_block(irf, "entry");
+ if (!self->curblock) {
+ asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
+ return false;
+ }
+
+ for (i = 0; i < vec_size(self->blocks); ++i) {
+ ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
+ if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
+ return false;
+ }
+
+ /* TODO: check return types */
+ if (!self->curblock->is_return)
+ {
+ return ir_block_create_return(self->curblock, NULL);
+ /* From now on the parser has to handle this situation */
+#if 0
+ 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"); */
+ asterror(ast_ctx(self), "function `%s` missing return value", self->name);
+ return false;
+ }
+#endif
+ }
+ return true;
+}
+
+/* Note, you will not see ast_block_codegen generate ir_blocks.
+ * To the AST and the IR, blocks are 2 different things.
+ * In the AST it represents a block of code, usually enclosed in
+ * curly braces {...}.
+ * While in the IR it represents a block in terms of control-flow.
+ */
+bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ size_t i;
+
+ /* We don't use this
+ * Note: an ast-representation using the comma-operator
+ * of the form: (a, b, c) = x should not assign to c...
+ */
+ if (lvalue) {
+ asterror(ast_ctx(self), "not an l-value (code-block)");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ /* output is NULL at first, we'll have each expression
+ * assign to out output, thus, a comma-operator represention
+ * using an ast_block will return the last generated value,
+ * so: (b, c) + a executed both b and c, and returns c,
+ * which is then added to a.
+ */
+ *out = NULL;
+
+ /* generate locals */
+ for (i = 0; i < vec_size(self->locals); ++i)
+ {
+ if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
+ if (opts_debug)
+ asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
+ return false;
+ }
+ }
+
+ for (i = 0; i < vec_size(self->exprs); ++i)
+ {
+ ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
+ if (!(*gen)(self->exprs[i], func, false, out))
+ return false;
+ }
+
+ self->expression.outr = *out;
+
+ return true;
+}
+
+bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ 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;
+ }
+
+ if (!lvalue && self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ if (ast_istype(self->dest, ast_array_index))
+ {
+
+ ai = (ast_array_index*)self->dest;
+ idx = (ast_value*)ai->index;
+
+ 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;
+ ir_call_param(call, iridx);
+ ir_call_param(call, right);
+ 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
+ * the right side as an rvalue, otherwise we have to
+ * somehow know whether or not we need to dereference the pointer
+ * on the left side - that is: OP_LOAD if it was an address.
+ * Also: in original QC we cannot OP_LOADP *anyway*.
+ */
+ *out = (lvalue ? left : right);
+
+ return true;
+}
+
+bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *left, *right;
+
+ /* A binary operation cannot yield an l-value */
+ if (lvalue) {
+ asterror(ast_ctx(self), "not an l-value (binop)");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ cgen = self->left->expression.codegen;
+ /* lvalue! */
+ if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
+ return false;
+
+ cgen = self->right->expression.codegen;
+ /* rvalue! */
+ if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
+ return false;
+
+ *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
+ self->op, left, right);
+ if (!*out)
+ return false;
+ self->expression.outr = *out;
+
+ return true;
+}
+
+bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *leftl, *leftr, *right, *bin;
+
+ if (lvalue && self->expression.outl) {
+ *out = self->expression.outl;
+ return true;
+ }
+
+ if (!lvalue && self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ /* for a binstore we need both an lvalue and an rvalue for the left side */
+ /* rvalue of destination! */
+ cgen = self->dest->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
+ return false;
+
+ /* source as rvalue only */
+ cgen = self->source->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
+ return false;
+
+ /* now the binary */
+ bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
+ self->opbin, leftr, right);
+ self->expression.outr = bin;
+
+ /* now store them */
+ cgen = self->dest->expression.codegen;
+ /* lvalue of destination */
+ if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
+ return false;
+ self->expression.outl = leftl;
+
+ if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
+ return false;
+ self->expression.outr = bin;
+
+ /* Theoretically, an assinment returns its left side as an
+ * lvalue, if we don't need an lvalue though, we return
+ * the right side as an rvalue, otherwise we have to
+ * somehow know whether or not we need to dereference the pointer
+ * on the left side - that is: OP_LOAD if it was an address.
+ * Also: in original QC we cannot OP_LOADP *anyway*.
+ */
+ *out = (lvalue ? leftl : bin);
+
+ return true;
+}
+
+bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *operand;
+
+ /* An unary operation cannot yield an l-value */
+ if (lvalue) {
+ asterror(ast_ctx(self), "not an l-value (binop)");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ cgen = self->operand->expression.codegen;
+ /* lvalue! */
+ if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
+ return false;
+
+ *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
+ self->op, operand);
+ if (!*out)
+ return false;
+ self->expression.outr = *out;
+
+ return true;
+}
+
+bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *operand;
+
+ /* In the context of a return operation, we don't actually return
+ * anything...
+ */
+ if (lvalue) {
+ asterror(ast_ctx(self), "return-expression is not an l-value");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
+ return false;
+ }
+ self->expression.outr = (ir_value*)1;
+
+ if (self->operand) {
+ cgen = self->operand->expression.codegen;
+ /* lvalue! */
+ if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
+ return false;
+
+ if (!ir_block_create_return(func->curblock, operand))
+ return false;
+ } else {
+ if (!ir_block_create_return(func->curblock, NULL))
+ return false;
+ }
+
+ return true;
+}
+
+bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *ent, *field;
+
+ /* This function needs to take the 'lvalue' flag into account!
+ * As lvalue we provide a field-pointer, as rvalue we provide the
+ * value in a temp.
+ */
+
+ if (lvalue && self->expression.outl) {
+ *out = self->expression.outl;
+ return true;
+ }
+
+ if (!lvalue && self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ cgen = self->entity->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
+ return false;
+
+ cgen = self->field->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
+ return false;
+
+ if (lvalue) {
+ /* address! */
+ *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
+ ent, field);
+ } else {
+ *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
+ ent, field, self->expression.vtype);
+ }
+ if (!*out) {
+ asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
+ (lvalue ? "ADDRESS" : "FIELD"),
+ type_name[self->expression.vtype]);
+ return false;
+ }
+
+ if (lvalue)
+ self->expression.outl = *out;
+ else
+ self->expression.outr = *out;
+
+ /* Hm that should be it... */
+ return true;
+}
+
+bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value *vec;
+
+ /* in QC this is always an lvalue */
+ (void)lvalue;
+ if (self->expression.outl) {
+ *out = self->expression.outl;
+ return true;
+ }
+
+ cgen = self->owner->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
+ return false;
+
+ if (vec->vtype != TYPE_VECTOR &&
+ !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
+ {
+ return false;
+ }
+
+ *out = ir_value_vector_member(vec, self->field);
+ self->expression.outl = *out;
+
+ return (*out != NULL);
+}
+
+bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_value *arr;
+ ast_value *idx;
+
+ if (!lvalue && self->expression.outr) {
+ *out = self->expression.outr;
+ }
+ if (lvalue && self->expression.outl) {
+ *out = self->expression.outl;
+ }
+
+ if (!ast_istype(self->array, ast_value)) {
+ asterror(ast_ctx(self), "array indexing this way is not supported");
+ /* note this would actually be pointer indexing because the left side is
+ * not an actual array but (hopefully) an indexable expression.
+ * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
+ * support this path will be filled.
+ */
+ return false;
+ }
+
+ arr = (ast_value*)self->array;
+ idx = (ast_value*)self->index;
+
+ if (!ast_istype(self->index, ast_value) || !idx->isconst) {
+ /* Time to use accessor functions */
+ ast_expression_codegen *cgen;
+ ir_value *iridx, *funval;
+ ir_instr *call;
+
+ if (lvalue) {
+ asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
+ return false;
+ }
+
+ if (!arr->getter) {
+ asterror(ast_ctx(self), "value has no getter, don't know how to index it");
+ return false;
+ }
+
+ cgen = self->index->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
+ return false;
+
+ cgen = arr->getter->expression.codegen;
+ if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
+ return false;
+
+ call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
+ if (!call)
+ return false;
+ ir_call_param(call, iridx);
+
+ *out = ir_call_value(call);
+ self->expression.outr = *out;
+ return true;
+ }
+
+ if (idx->expression.vtype == TYPE_FLOAT)
+ *out = arr->ir_values[(int)idx->constval.vfloat];
+ else if (idx->expression.vtype == TYPE_INTEGER)
+ *out = arr->ir_values[idx->constval.vint];
+ else {
+ asterror(ast_ctx(self), "array indexing here needs an integer constant");
+ return false;
+ }
+ return true;
+}
+
+bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+
+ ir_value *condval;
+ ir_value *dummy;
+
+ ir_block *cond = func->curblock;
+ ir_block *ontrue;
+ ir_block *onfalse;
+ ir_block *ontrue_endblock = NULL;
+ ir_block *onfalse_endblock = NULL;
+ ir_block *merge;
+
+ /* We don't output any value, thus also don't care about r/lvalue */
+ (void)out;
+ (void)lvalue;
+
+ if (self->expression.outr) {
+ asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
+ return false;
+ }
+ self->expression.outr = (ir_value*)1;
+
+ /* generate the condition */
+ func->curblock = cond;
+ cgen = self->cond->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
+ return false;
+
+ /* on-true path */
+
+ if (self->on_true) {
+ /* create on-true block */
+ ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
+ if (!ontrue)
+ return false;
+
+ /* enter the block */
+ func->curblock = ontrue;
+
+ /* generate */
+ cgen = self->on_true->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
+ return false;
+
+ /* we now need to work from the current endpoint */
+ ontrue_endblock = func->curblock;
+ } else
+ ontrue = NULL;
+
+ /* on-false path */
+ if (self->on_false) {
+ /* create on-false block */
+ onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
+ if (!onfalse)
+ return false;
+
+ /* enter the block */
+ func->curblock = onfalse;
+
+ /* generate */
+ cgen = self->on_false->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
+ return false;
+
+ /* we now need to work from the current endpoint */
+ onfalse_endblock = func->curblock;
+ } else
+ onfalse = NULL;
+
+ /* Merge block were they all merge in to */
+ merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
+ if (!merge)
+ return false;
+
+ /* add jumps ot the merge block */
+ if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
+ return false;
+ if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
+ return false;
+
+ /* we create the if here, that way all blocks are ordered :)
+ */
+ if (!ir_block_create_if(cond, condval,
+ (ontrue ? ontrue : merge),
+ (onfalse ? onfalse : merge)))
+ {
+ return false;
+ }
+
+ /* Now enter the merge block */
+ func->curblock = merge;
+
+ return true;
+}
+
+bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+
+ ir_value *condval;
+ ir_value *trueval, *falseval;
+ ir_instr *phi;
+
+ ir_block *cond = func->curblock;
+ ir_block *ontrue;
+ ir_block *onfalse;
+ ir_block *merge;
+
+ /* Ternary can never create an lvalue... */
+ if (lvalue)
+ return false;
+
+ /* In theory it shouldn't be possible to pass through a node twice, but
+ * in case we add any kind of optimization pass for the AST itself, it
+ * may still happen, thus we remember a created ir_value and simply return one
+ * if it already exists.
+ */
+ if (self->phi_out) {
+ *out = self->phi_out;
+ return true;
+ }
+
+ /* In the following, contraty to ast_ifthen, we assume both paths exist. */
+
+ /* generate the condition */
+ func->curblock = cond;
+ cgen = self->cond->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
+ return false;
+
+ /* create on-true block */
+ ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
+ if (!ontrue)
+ return false;
+ else
+ {
+ /* enter the block */
+ func->curblock = ontrue;
+
+ /* generate */
+ cgen = self->on_true->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
+ return false;
+ }
+
+ /* create on-false block */
+ onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
+ if (!onfalse)
+ return false;
+ else
+ {
+ /* enter the block */
+ func->curblock = onfalse;
+
+ /* generate */
+ cgen = self->on_false->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
+ return false;
+ }
+
+ /* create merge block */
+ merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
+ if (!merge)
+ return false;
+ /* jump to merge block */
+ if (!ir_block_create_jump(ontrue, merge))
+ return false;
+ if (!ir_block_create_jump(onfalse, merge))
+ return false;
+
+ /* create if instruction */
+ if (!ir_block_create_if(cond, condval, ontrue, onfalse))
+ return false;
+
+ /* Now enter the merge block */
+ func->curblock = merge;
+
+ /* Here, now, we need a PHI node
+ * but first some sanity checking...
+ */
+ if (trueval->vtype != falseval->vtype) {
+ /* error("ternary with different types on the two sides"); */
+ return false;
+ }
+
+ /* create PHI */
+ phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
+ if (!phi)
+ return false;
+ ir_phi_add(phi, ontrue, trueval);
+ ir_phi_add(phi, onfalse, falseval);
+
+ self->phi_out = ir_phi_value(phi);
+ *out = self->phi_out;
+
+ return true;
+}
+
+bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+
+ ir_value *dummy = NULL;
+ ir_value *precond = NULL;
+ ir_value *postcond = NULL;
+
+ /* Since we insert some jumps "late" so we have blocks
+ * ordered "nicely", we need to keep track of the actual end-blocks
+ * of expressions to add the jumps to.
+ */
+ ir_block *bbody = NULL, *end_bbody = NULL;
+ ir_block *bprecond = NULL, *end_bprecond = NULL;
+ ir_block *bpostcond = NULL, *end_bpostcond = NULL;
+ ir_block *bincrement = NULL, *end_bincrement = NULL;
+ ir_block *bout = NULL, *bin = NULL;
+
+ /* let's at least move the outgoing block to the end */
+ size_t bout_id;
+
+ /* 'break' and 'continue' need to be able to find the right blocks */
+ ir_block *bcontinue = NULL;
+ ir_block *bbreak = NULL;
+
+ ir_block *old_bcontinue = NULL;
+ ir_block *old_bbreak = NULL;
+
+ ir_block *tmpblock = NULL;
+
+ (void)lvalue;
+ (void)out;
+
+ if (self->expression.outr) {
+ asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
+ return false;
+ }
+ self->expression.outr = (ir_value*)1;
+
+ /* NOTE:
+ * Should we ever need some kind of block ordering, better make this function
+ * move blocks around than write a block ordering algorithm later... after all
+ * the ast and ir should work together, not against each other.
+ */
+
+ /* initexpr doesn't get its own block, it's pointless, it could create more blocks
+ * anyway if for example it contains a ternary.
+ */
+ if (self->initexpr)
+ {
+ cgen = self->initexpr->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
+ return false;
+ }
+
+ /* Store the block from which we enter this chaos */
+ bin = func->curblock;
+
+ /* The pre-loop condition needs its own block since we
+ * need to be able to jump to the start of that expression.
+ */
+ if (self->precond)
+ {
+ bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
+ if (!bprecond)
+ return false;
+
+ /* the pre-loop-condition the least important place to 'continue' at */
+ bcontinue = bprecond;
+
+ /* enter */
+ func->curblock = bprecond;
+
+ /* generate */
+ cgen = self->precond->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
+ return false;
+
+ end_bprecond = func->curblock;
+ } else {
+ bprecond = end_bprecond = NULL;
+ }
+
+ /* Now the next blocks won't be ordered nicely, but we need to
+ * generate them this early for 'break' and 'continue'.
+ */
+ if (self->increment) {
+ bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
+ if (!bincrement)
+ return false;
+ bcontinue = bincrement; /* increment comes before the pre-loop-condition */
+ } else {
+ bincrement = end_bincrement = NULL;
+ }
+
+ if (self->postcond) {
+ bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
+ if (!bpostcond)
+ return false;
+ bcontinue = bpostcond; /* postcond comes before the increment */
+ } else {
+ bpostcond = end_bpostcond = NULL;
+ }
+
+ bout_id = vec_size(func->ir_func->blocks);
+ bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
+ if (!bout)
+ return false;
+ bbreak = bout;
+
+ /* The loop body... */
+ if (self->body)
+ {
+ bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
+ if (!bbody)
+ return false;
+
+ /* enter */
+ func->curblock = bbody;
+
+ old_bbreak = func->breakblock;
+ old_bcontinue = func->continueblock;
+ func->breakblock = bbreak;
+ func->continueblock = bcontinue;
+
+ /* generate */
+ cgen = self->body->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
+ return false;
+
+ end_bbody = func->curblock;
+ func->breakblock = old_bbreak;
+ func->continueblock = old_bcontinue;
+ }
+
+ /* post-loop-condition */
+ if (self->postcond)
+ {
+ /* enter */
+ func->curblock = bpostcond;
+
+ /* generate */
+ cgen = self->postcond->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
+ return false;
+
+ end_bpostcond = func->curblock;
+ }
+
+ /* The incrementor */
+ if (self->increment)
+ {
+ /* enter */
+ func->curblock = bincrement;
+
+ /* generate */
+ cgen = self->increment->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
+ return false;
+
+ end_bincrement = func->curblock;
+ }
+
+ /* In any case now, we continue from the outgoing block */
+ func->curblock = bout;
+
+ /* Now all blocks are in place */
+ /* From 'bin' we jump to whatever comes first */
+ if (bprecond) tmpblock = bprecond;
+ else if (bbody) tmpblock = bbody;
+ else if (bpostcond) tmpblock = bpostcond;
+ else tmpblock = bout;
+ if (!ir_block_create_jump(bin, tmpblock))
+ return false;
+
+ /* From precond */
+ if (bprecond)
+ {
+ ir_block *ontrue, *onfalse;
+ if (bbody) ontrue = bbody;
+ else if (bincrement) ontrue = bincrement;
+ else if (bpostcond) ontrue = bpostcond;
+ else ontrue = bprecond;
+ onfalse = bout;
+ if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
+ return false;
+ }
+
+ /* from body */
+ if (bbody)
+ {
+ if (bincrement) tmpblock = bincrement;
+ else if (bpostcond) tmpblock = bpostcond;
+ else if (bprecond) tmpblock = bprecond;
+ else tmpblock = bout;
+ if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
+ return false;
+ }
+
+ /* from increment */
+ if (bincrement)
+ {
+ if (bpostcond) tmpblock = bpostcond;
+ else if (bprecond) tmpblock = bprecond;
+ else if (bbody) tmpblock = bbody;
+ else tmpblock = bout;
+ if (!ir_block_create_jump(end_bincrement, tmpblock))
+ return false;
+ }
+
+ /* from postcond */
+ if (bpostcond)
+ {
+ ir_block *ontrue, *onfalse;
+ if (bprecond) ontrue = bprecond;
+ else if (bbody) ontrue = bbody;
+ else if (bincrement) ontrue = bincrement;
+ else ontrue = bpostcond;
+ onfalse = bout;
+ if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
+ return false;
+ }
+
+ /* Move 'bout' to the end */
+ vec_remove(func->ir_func->blocks, bout_id, 1);
+ vec_push(func->ir_func->blocks, bout);
+
+ return true;
+}
+
+bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+ ir_value **params;
+ ir_instr *callinstr;
+ size_t i;
+
+ ir_value *funval = NULL;
+
+ /* return values are never lvalues */
+ if (lvalue) {
+ asterror(ast_ctx(self), "not an l-value (function call)");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ *out = self->expression.outr;
+ return true;
+ }
+
+ cgen = self->func->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
+ return false;
+ if (!funval)
+ return false;
+
+ params = NULL;
+
+ /* parameters */
+ for (i = 0; i < vec_size(self->params); ++i)
+ {
+ ir_value *param;
+ ast_expression *expr = self->params[i];
+
+ cgen = expr->expression.codegen;
+ if (!(*cgen)(expr, func, false, ¶m))
+ goto error;
+ if (!param)
+ goto error;
+ vec_push(params, param);
+ }
+
+ callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
+ if (!callinstr)
+ goto error;
+
+ for (i = 0; i < vec_size(params); ++i) {
+ ir_call_param(callinstr, params[i]);
+ }
+
+ *out = ir_call_value(callinstr);
+ self->expression.outr = *out;
+
+ vec_free(params);
+ return true;
+error:
+ vec_free(params);