Files
ivy/lang/codegen/cond.c

133 lines
3.9 KiB
C
Raw Normal View History

#include "../debug.h"
#include "codegen.h"
2025-06-02 11:31:35 +01:00
#include <mie/ir/func.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cond_codegen_state {
struct code_generator_state s_base;
struct mie_value *s_cond;
struct mie_block *s_true;
struct mie_block *s_false;
struct mie_block *s_end;
};
static struct code_generator_result gen_cond(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_expr(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
/* this is either the if-condition, or the if-body for an inline
* if-statement */
if (cond->s_cond) {
cond->s_true = mie_func_create_block(func, "if.true");
cond->s_false = mie_func_create_block(func, "if.false");
mie_builder_br_if(
gen->c_builder, cond->s_cond, cond->s_true, cond->s_false);
mie_func_insert_block(func, cond->s_true, NULL);
mie_builder_set_insert_point(gen->c_builder, cond->s_true);
}
codegen_push_generator(gen, CODE_GENERATOR_EXPR, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_block(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
if (cond->s_cond) {
cond->s_true = mie_func_create_block(func, "if.true");
cond->s_false = mie_func_create_block(func, "if.false");
mie_builder_br_if(
gen->c_builder, cond->s_cond, cond->s_true, cond->s_false);
mie_func_insert_block(func, cond->s_true, NULL);
mie_builder_set_insert_point(gen->c_builder, cond->s_true);
}
codegen_push_generator(gen, CODE_GENERATOR_BLOCK, 0, NULL);
return CODEGEN_RESULT_OK(0);
}
static enum ivy_status state_init(
struct ivy_codegen *gen, struct code_generator_state *state,
uintptr_t argv, void *argp)
{
debug_printf("codegen: start of cond\n");
struct cond_codegen_state *cond = (struct cond_codegen_state *)state;
cond->s_end = argp;
return IVY_OK;
}
static enum ivy_status state_fini(
struct ivy_codegen *gen, struct code_generator_state *state,
struct code_generator_value *result)
{
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);
mie_builder_br(gen->c_builder, cond->s_end);
if (cond->s_false) {
mie_func_insert_block(func, cond->s_false, NULL);
mie_builder_set_insert_point(gen->c_builder, cond->s_false);
}
return IVY_OK;
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
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) {
/* this is already a boolean */
cond->s_cond = value;
} else {
struct mie_value *zero = mie_ctx_get_int(gen->c_ctx, 0, 32);
struct mie_value *cmp
= mie_builder_cmp_neq(gen->c_builder, value, zero, NULL);
cond->s_cond = cmp;
}
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),
}};