+ if (self->expression.outr) {
+ *out = self->expression.outr;
+ 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->expression.outr = ir_phi_value(phi);
+ *out = self->expression.outr;
+
+ 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_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ir_block *target;
+
+ *out = NULL;
+
+ if (lvalue) {
+ asterror(ast_ctx(self), "break/continue expression is not an l-value");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
+ return false;
+ }
+ self->expression.outr = (ir_value*)1;
+
+ if (self->is_continue)
+ target = func->continueblock;
+ else
+ target = func->breakblock;
+
+ if (!ir_block_create_jump(func->curblock, target))
+ return false;
+ return true;
+}
+
+bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ ast_expression_codegen *cgen;
+
+ ast_switch_case *def_case = NULL;
+ ir_block *def_bfall = NULL;
+
+ ir_value *dummy = NULL;
+ ir_value *irop = NULL;
+ ir_block *old_break = NULL;
+ ir_block *bout = NULL;
+ ir_block *bfall = NULL;
+ size_t bout_id;
+ size_t c;
+
+ char typestr[1024];
+ uint16_t cmpinstr;
+
+ if (lvalue) {
+ asterror(ast_ctx(self), "switch expression is not an l-value");
+ return false;
+ }
+
+ if (self->expression.outr) {
+ asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
+ return false;
+ }
+ self->expression.outr = (ir_value*)1;
+
+ (void)lvalue;
+ (void)out;
+
+ cgen = self->operand->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
+ return false;
+
+ if (!vec_size(self->cases))
+ return true;
+
+ cmpinstr = type_eq_instr[irop->vtype];
+ if (cmpinstr >= AINSTR_END) {
+ ast_type_to_string(self->operand, typestr, sizeof(typestr));
+ asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
+ return false;
+ }
+
+ bout_id = vec_size(func->ir_func->blocks);
+ bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
+ if (!bout)
+ return false;
+
+ /* setup the break block */
+ old_break = func->breakblock;
+ func->breakblock = bout;
+
+ /* Now create all cases */
+ for (c = 0; c < vec_size(self->cases); ++c) {
+ ir_value *cond, *val;
+ ir_block *bcase, *bnot;
+ size_t bnot_id;
+
+ ast_switch_case *swcase = &self->cases[c];
+
+ if (swcase->value) {
+ /* A regular case */
+ /* generate the condition operand */
+ cgen = swcase->value->expression.codegen;
+ if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
+ return false;
+ /* generate the condition */
+ cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
+ if (!cond)
+ return false;
+
+ bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
+ bnot_id = vec_size(func->ir_func->blocks);
+ bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
+ if (!bcase || !bnot)
+ return false;
+ if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
+ return false;
+
+ /* Make the previous case-end fall through */
+ if (bfall && !bfall->final) {
+ if (!ir_block_create_jump(bfall, bcase))
+ return false;
+ }
+
+ /* enter the case */
+ func->curblock = bcase;
+ cgen = swcase->code->expression.codegen;
+ if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
+ return false;
+
+ /* remember this block to fall through from */
+ bfall = func->curblock;
+
+ /* enter the else and move it down */
+ func->curblock = bnot;
+ vec_remove(func->ir_func->blocks, bnot_id, 1);
+ vec_push(func->ir_func->blocks, bnot);
+ } else {
+ /* The default case */
+ /* Remember where to fall through from: */
+ def_bfall = bfall;
+ bfall = NULL;
+ /* remember which case it was */
+ def_case = swcase;
+ }
+ }
+
+ /* Jump from the last bnot to bout */
+ if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
+ /*
+ astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
+ */
+ return false;
+ }
+
+ /* If there was a default case, put it down here */
+ if (def_case) {
+ ir_block *bcase;
+
+ /* No need to create an extra block */
+ bcase = func->curblock;
+
+ /* Insert the fallthrough jump */
+ if (def_bfall && !def_bfall->final) {
+ if (!ir_block_create_jump(def_bfall, bcase))
+ return false;
+ }
+
+ /* Now generate the default code */
+ cgen = def_case->code->expression.codegen;
+ if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
+ return false;
+ }
+
+ /* Jump from the last bnot to bout */
+ if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
+ return false;
+ /* enter the outgoing block */
+ func->curblock = bout;
+
+ /* restore the break block */
+ func->breakblock = old_break;
+
+ /* Move 'bout' to the end, it's nicer */
+ 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;