+ self->expression.outr = *out;
+ codegen_output_type(self, *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 = NULL, *leftr, *right, *bin;
+
+ ast_value *arr;
+ ast_value *idx = 0;
+ ast_array_index *ai = NULL;
+ ir_value *iridx = 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->hasvalue && idx->cvq == CV_CONST)
+ ai = NULL;
+ }
+
+ /* for a binstore we need both an lvalue and an rvalue for the left side */
+ /* rvalue of destination! */
+ if (ai) {
+ cgen = idx->expression.codegen;
+ if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
+ return false;
+ }
+ 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_ctx(self), ast_function_label(func, "binst"),
+ self->opbin, leftr, right);
+ self->expression.outr = bin;
+
+
+ if (ai) {
+ /* we need to call the setter */
+ ir_value *funval;
+ ir_instr *call;
+
+ if (lvalue) {
+ compile_error(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) {
+ compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
+ return false;
+ }
+
+ cgen = arr->setter->expression.codegen;
+ if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
+ return false;
+
+ call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
+ if (!call)
+ return false;
+ ir_call_param(call, iridx);
+ ir_call_param(call, bin);
+ self->expression.outr = bin;
+ } else {
+ /* 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, ast_ctx(self), 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) {
+ compile_error(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_ctx(self), 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;
+
+ *out = NULL;
+
+ /* In the context of a return operation, we don't actually return
+ * anything...
+ */
+ if (lvalue) {
+ compile_error(ast_ctx(self), "return-expression is not an l-value");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ compile_error(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, ast_ctx(self), operand))
+ return false;
+ } else {
+ if (!ir_block_create_return(func->curblock, ast_ctx(self), 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_ctx(self), ast_function_label(func, "efa"),
+ ent, field);
+ } else {
+ *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
+ ent, field, self->expression.vtype);
+ /* Done AFTER error checking:
+ codegen_output_type(self, *out);
+ */
+ }
+ if (!*out) {
+ compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
+ (lvalue ? "ADDRESS" : "FIELD"),
+ type_name[self->expression.vtype]);
+ return false;
+ }
+ if (!lvalue)
+ codegen_output_type(self, *out);
+
+ 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 */
+ if (lvalue && self->rvalue) {
+ compile_error(ast_ctx(self), "not an l-value (member access)");
+ return false;
+ }
+ if (self->expression.outl) {
+ *out = self->expression.outl;
+ return true;
+ }
+
+ cgen = self->owner->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->owner), func, false, &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;
+ return true;
+ }
+ if (lvalue && self->expression.outl) {
+ *out = self->expression.outl;
+ return true;
+ }
+
+ if (!ast_istype(self->array, ast_value)) {
+ compile_error(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;
+ }