+static bool gen_blocks_recursive(ir_function *func, ir_block *block)
+{
+ prog_section_statement stmt;
+ ir_instr *instr;
+ ir_block *target;
+ ir_block *ontrue;
+ ir_block *onfalse;
+ size_t stidx;
+ size_t i;
+
+tailcall:
+ block->generated = true;
+ block->code_start = code_statements_elements;
+ for (i = 0; i < block->instr_count; ++i)
+ {
+ instr = block->instr[i];
+
+ if (instr->opcode == VINSTR_PHI) {
+ printf("cannot generate virtual instruction (phi)\n");
+ return false;
+ }
+
+ if (instr->opcode == VINSTR_JUMP) {
+ target = instr->bops[0];
+ /* for uncoditional jumps, if the target hasn't been generated
+ * yet, we generate them right here.
+ */
+ if (!target->generated) {
+ block = target;
+ goto tailcall;
+ }
+
+ /* otherwise we generate a jump instruction */
+ stmt.opcode = INSTR_GOTO;
+ stmt.o1.s1 = (target->code_start) - code_statements_elements;
+ stmt.o2.s1 = 0;
+ stmt.o3.s1 = 0;
+ if (code_statements_add(stmt) < 0)
+ return false;
+
+ /* no further instructions can be in this block */
+ return true;
+ }
+
+ if (instr->opcode == VINSTR_COND) {
+ ontrue = instr->bops[0];
+ onfalse = instr->bops[1];
+ /* TODO: have the AST signal which block should
+ * come first: eg. optimize IFs without ELSE...
+ */
+
+ stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
+ stmt.o2.u1 = 0;
+ stmt.o3.s1 = 0;
+
+ if (ontrue->generated) {
+ stmt.opcode = INSTR_IF;
+ stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
+ if (code_statements_add(stmt) < 0)
+ return false;
+ }
+ if (onfalse->generated) {
+ stmt.opcode = INSTR_IFNOT;
+ stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
+ if (code_statements_add(stmt) < 0)
+ return false;
+ }
+ if (!ontrue->generated) {
+ if (onfalse->generated) {
+ block = ontrue;
+ goto tailcall;
+ }
+ }
+ if (!onfalse->generated) {
+ if (ontrue->generated) {
+ block = onfalse;
+ goto tailcall;
+ }
+ }
+ /* neither ontrue nor onfalse exist */
+ stmt.opcode = INSTR_IFNOT;
+ stidx = code_statements_elements;
+ if (code_statements_add(stmt) < 0)
+ return false;
+ /* on false we jump, so add ontrue-path */
+ if (!gen_blocks_recursive(func, ontrue))
+ return false;
+ /* fixup the jump address */
+ code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
+ /* generate onfalse path */
+ if (onfalse->generated) {
+ /* fixup the jump address */
+ code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
+ /* may have been generated in the previous recursive call */
+ stmt.opcode = INSTR_GOTO;
+ stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
+ stmt.o2.s1 = 0;
+ stmt.o3.s1 = 0;
+ return (code_statements_add(stmt) >= 0);
+ }
+ /* if not, generate now */
+ block = onfalse;
+ goto tailcall;
+ }
+
+ if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
+ /* Trivial call translation:
+ * copy all params to OFS_PARM*
+ * if the output's storetype is not store_return,
+ * add append a STORE instruction!
+ *
+ * NOTES on how to do it better without much trouble:
+ * -) The liferanges!
+ * Simply check the liferange of all parameters for
+ * other CALLs. For each param with no CALL in its
+ * liferange, we can store it in an OFS_PARM at
+ * generation already. This would even include later
+ * reuse.... probably... :)
+ */
+ size_t p;
+ ir_value *retvalue;
+
+ for (p = 0; p < instr->params_count; ++p)
+ {
+ ir_value *param = instr->params[p];
+
+ stmt.opcode = INSTR_STORE_F;
+ stmt.o3.u1 = 0;
+
+ stmt.opcode = type_store_instr[param->vtype];
+ stmt.o1.u1 = param->code.globaladdr;
+ stmt.o2.u1 = OFS_PARM0 + 3 * p;
+ if (code_statements_add(stmt) < 0)
+ return false;
+ }
+ stmt.opcode = INSTR_CALL0 + instr->params_count;
+ if (stmt.opcode > INSTR_CALL8)
+ stmt.opcode = INSTR_CALL8;
+ stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
+ stmt.o2.u1 = 0;
+ stmt.o3.u1 = 0;
+ if (code_statements_add(stmt) < 0)
+ return false;
+
+ retvalue = instr->_ops[0];
+ if (retvalue && retvalue->store != store_return && retvalue->life_count)
+ {
+ /* not to be kept in OFS_RETURN */
+ stmt.opcode = type_store_instr[retvalue->vtype];
+ stmt.o1.u1 = OFS_RETURN;
+ stmt.o2.u1 = retvalue->code.globaladdr;
+ stmt.o3.u1 = 0;
+ if (code_statements_add(stmt) < 0)
+ return false;
+ }
+ continue;
+ }
+
+ if (instr->opcode == INSTR_STATE) {
+ printf("TODO: state instruction\n");
+ return false;
+ }
+
+ stmt.opcode = instr->opcode;
+ stmt.o1.u1 = 0;
+ stmt.o2.u1 = 0;
+ stmt.o3.u1 = 0;
+
+ /* This is the general order of operands */
+ if (instr->_ops[0])
+ stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
+
+ if (instr->_ops[1])
+ stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
+
+ if (instr->_ops[2])
+ stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
+
+ if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
+ {
+ stmt.o1.u1 = stmt.o3.u1;
+ stmt.o3.u1 = 0;
+ }
+ else if (stmt.opcode >= INSTR_STORE_F &&
+ stmt.opcode <= INSTR_STORE_FNC)
+ {
+ /* 2-operand instructions with A -> B */
+ stmt.o2.u1 = stmt.o3.u1;
+ stmt.o3.u1 = 0;
+ }
+
+ if (code_statements_add(stmt) < 0)
+ return false;
+ }
+ return true;
+}
+