the codegen scope system has been removed. instead, each generator state in the stack, from the current state backwards, is informed when a variable is defined, resolved, or captured. when a variable is defined, the state stack is traversed back-to-front (current generator first). each state has a chance to record the variable definition. once one state has signalled that it has recorded the variable definition, the traversal ends. when a variable is resolved, the state stack is traversed back-to-front (current generator first). each state is asked whether or not it recognises the variable identifier being resolved. if a state has the variable in question defined, it returns information about the variable definition, and the traversal stops. once a variable has been resolved, the state stack is traversed front-to-back (current generator last), starting from the generator /after/ the one that provided the variable definition. each generator in the iteration is given the chance to adjust the variable information, or generate IR in response to the variable being accessed. this is used to implement variable capture, where the state of a variable in the enclosing context is captured for later use.
68 lines
1.9 KiB
C
68 lines
1.9 KiB
C
#include "codegen.h"
|
|
|
|
#include <mie/block.h>
|
|
#include <mie/func.h>
|
|
#include <mie/module.h>
|
|
|
|
struct block_codegen_state {
|
|
struct code_generator_state s_base;
|
|
struct code_generator_scope *s_scope;
|
|
};
|
|
|
|
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 unit_codegen_state *unit = (struct unit_codegen_state *)state;
|
|
struct mie_builder *builder = gen->c_builder;
|
|
struct mie_func *current = mie_builder_get_current_func(builder);
|
|
|
|
codegen_push_generator(
|
|
gen, CODE_GENERATOR_EXPR, CODEGEN_F_IGNORE_RESULT, NULL);
|
|
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
|
}
|
|
|
|
static struct code_generator_result gen_var_declaration(
|
|
struct ivy_codegen *gen, struct code_generator_state *state,
|
|
struct ivy_ast_node *node, size_t depth)
|
|
{
|
|
struct block_codegen_state *block = (struct block_codegen_state *)state;
|
|
struct mie_builder *builder = gen->c_builder;
|
|
struct mie_func *current = mie_builder_get_current_func(builder);
|
|
|
|
enum ivy_status status = IVY_OK;
|
|
|
|
if (status != IVY_OK) {
|
|
return CODEGEN_RESULT_ERR(status);
|
|
}
|
|
|
|
codegen_push_generator(gen, CODE_GENERATOR_VAR, 0, NULL);
|
|
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)
|
|
{
|
|
struct block_codegen_state *block = (struct block_codegen_state *)state;
|
|
return IVY_OK;
|
|
}
|
|
|
|
static enum ivy_status state_fini(
|
|
struct ivy_codegen *gen, struct code_generator_state *state,
|
|
struct mie_value **result)
|
|
{
|
|
return IVY_OK;
|
|
}
|
|
|
|
struct code_generator block_generator = {
|
|
.g_type = CODE_GENERATOR_BLOCK,
|
|
.g_state_size = sizeof(struct block_codegen_state),
|
|
.g_state_init = state_init,
|
|
.g_state_fini = state_fini,
|
|
.g_node_generators = {
|
|
NODE_CODEGEN(VAR, gen_var_declaration),
|
|
},
|
|
.g_expr_generator = gen_expr,
|
|
};
|