lang: codegen: implement cond-group code generation
This commit is contained in:
88
lang/codegen/cond-group.c
Normal file
88
lang/codegen/cond-group.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "../debug.h"
|
||||
#include "codegen.h"
|
||||
|
||||
#include <mie/block.h>
|
||||
#include <mie/func.h>
|
||||
#include <mie/value.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct cond_group_codegen_state {
|
||||
struct code_generator_state s_base;
|
||||
struct mie_block *s_end;
|
||||
};
|
||||
|
||||
static struct code_generator_result gen_cond_group(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct cond_group_codegen_state *group
|
||||
= (struct cond_group_codegen_state *)state;
|
||||
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
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_group_codegen_state *group
|
||||
= (struct cond_group_codegen_state *)state;
|
||||
|
||||
codegen_push_generator(gen, CODE_GENERATOR_COND, 0, group->s_end);
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
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 group\n");
|
||||
struct cond_group_codegen_state *group
|
||||
= (struct cond_group_codegen_state *)state;
|
||||
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
|
||||
group->s_end = mie_func_create_block(func, "if.end");
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status state_fini(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct mie_value **result)
|
||||
{
|
||||
debug_printf("codegen: end of cond group\n");
|
||||
struct cond_group_codegen_state *group
|
||||
= (struct cond_group_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);
|
||||
|
||||
if (!block->b_terminator) {
|
||||
mie_builder_br(gen->c_builder, group->s_end);
|
||||
}
|
||||
|
||||
mie_func_insert_block(func, group->s_end, NULL);
|
||||
mie_builder_set_insert_point(gen->c_builder, group->s_end);
|
||||
|
||||
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_group_codegen_state *cond
|
||||
= (struct cond_group_codegen_state *)state;
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
struct code_generator cond_group_generator = {
|
||||
.g_type = CODE_GENERATOR_COND_GROUP,
|
||||
.g_state_size = sizeof(struct cond_group_codegen_state),
|
||||
.g_state_init = state_init,
|
||||
.g_state_fini = state_fini,
|
||||
.g_value_received = value_received,
|
||||
.g_node_generators = {
|
||||
NODE_CODEGEN(COND_GROUP, gen_cond_group),
|
||||
NODE_CODEGEN(COND, gen_cond),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user