lang: codegen: cond: implement implicitly returning values from an if-else expression

This commit is contained in:
2025-09-08 16:24:29 +01:00
parent 42dd3ffffc
commit 68ca789e43
2 changed files with 130 additions and 19 deletions

View File

@@ -2,13 +2,16 @@
#include "codegen.h"
#include <mie/ir/func.h>
#include <mie/ir/phi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cond_codegen_state {
struct code_generator_state s_base;
bool s_is_else;
struct mie_value *s_cond;
struct mie_value *s_result;
struct mie_block *s_true;
struct mie_block *s_false;
struct mie_block *s_end;
@@ -19,6 +22,8 @@ static struct code_generator_result gen_cond(
struct ivy_ast_node *node, size_t depth)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
struct ivy_ast_cond_node *cond_node = (struct ivy_ast_cond_node *)node;
cond->s_is_else = (cond_node->n_cond == NULL);
return CODEGEN_RESULT_OK(0);
}
@@ -42,7 +47,6 @@ static struct code_generator_result gen_expr(
}
codegen_push_generator(gen, CODE_GENERATOR_EXPR, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
@@ -84,7 +88,11 @@ static enum ivy_status state_fini(
debug_printf("codegen: end of cond\n");
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
struct mie_block *block = mie_builder_get_current_block(gen->c_builder);
struct mie_phi_edge *edge = mie_phi_edge_create(block, cond->s_result);
code_generator_value_set_phi_edge(result, edge);
mie_builder_br(gen->c_builder, cond->s_end);
if (cond->s_false) {
@@ -95,15 +103,10 @@ static enum ivy_status state_fini(
return IVY_OK;
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
static void set_cond(
struct ivy_codegen *gen, struct cond_codegen_state *cond,
struct mie_value *value)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
if (!value) {
return CODEGEN_RESULT_OK(0);
}
struct mie_type *cond_type = mie_value_get_type(value, gen->c_ctx);
if (cond_type->t_id == MIE_TYPE_INT && cond_type->t_width == 1) {
@@ -115,18 +118,40 @@ static struct code_generator_result value_received(
= mie_builder_cmp_neq(gen->c_builder, value, zero, NULL);
cond->s_cond = cmp;
}
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
struct code_generator_value *value)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
if (!value) {
return CODEGEN_RESULT_OK(0);
}
if (!cond->s_is_else && !cond->s_cond) {
set_cond(gen, cond, code_generator_value_get_mie_value(value));
return CODEGEN_RESULT_OK(0);
}
if (!cond->s_result) {
cond->s_result = code_generator_value_get_mie_value(value);
}
return CODEGEN_RESULT_OK(0);
}
struct code_generator cond_generator
= {.g_type = CODE_GENERATOR_COND,
.g_state_size = sizeof(struct cond_codegen_state),
.g_state_init = state_init,
.g_state_fini = state_fini,
.g_value_received = value_received,
.g_expr_generator = gen_expr,
.g_node_generators = {
NODE_CODEGEN(COND, gen_cond),
NODE_CODEGEN(BLOCK, gen_block),
}};
struct code_generator cond_generator = {
.g_type = CODE_GENERATOR_COND,
.g_state_size = sizeof(struct cond_codegen_state),
.g_state_init = state_init,
.g_state_fini = state_fini,
.g_value_received = value_received,
.g_expr_generator = gen_expr,
.g_node_generators = {
NODE_CODEGEN(COND, gen_cond),
NODE_CODEGEN(BLOCK, gen_block),
NODE_CODEGEN(LOOP_BREAK, gen_block),
NODE_CODEGEN(LOOP_REPEAT, gen_block),
},
};