Files
ivy/lang/codegen/expr.c
Max Wash 7483b32c25 lang: codegen: redesign variable definition/resolution system to support capturing
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.
2025-04-22 15:23:42 +01:00

447 lines
12 KiB
C

#include "../debug.h"
#include "codegen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum expr_item_type {
EXPR_NONE = 0,
EXPR_OPERATOR,
EXPR_OPERAND,
};
struct expr_item {
b_queue_entry i_entry;
enum expr_item_type i_type;
struct ivy_ast_node *i_node;
struct mie_value *i_value;
};
struct expr_codegen_state {
struct code_generator_state s_base;
struct ivy_ast_node *s_expr_root;
uintptr_t s_flags;
b_queue s_item_queue;
};
#if 0
static struct code_generator_result check_expr_root(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
if (!expr->s_expr_root) {
expr->s_expr_root = node;
}
return CODEGEN_RESULT_OK(0);
}
#endif
static enum ivy_status push_operand(
struct expr_codegen_state *expr, struct ivy_ast_node *node,
struct mie_value *value)
{
struct expr_item *item = malloc(sizeof *item);
if (!item) {
return IVY_ERR_NO_MEMORY;
}
memset(item, 0x0, sizeof *item);
item->i_type = EXPR_OPERAND;
item->i_node = node;
item->i_value = value;
b_queue_push_back(&expr->s_item_queue, &item->i_entry);
return IVY_OK;
}
static enum ivy_status push_operator(
struct expr_codegen_state *expr, struct ivy_ast_node *node)
{
struct expr_item *item = malloc(sizeof *item);
if (!item) {
return IVY_ERR_NO_MEMORY;
}
memset(item, 0x0, sizeof *item);
item->i_type = EXPR_OPERATOR;
item->i_node = node;
b_queue_push_back(&expr->s_item_queue, &item->i_entry);
return IVY_OK;
}
static struct code_generator_result gen_int(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
debug_printf("codegen: got int\n");
struct ivy_ast_int_node *int_node = (struct ivy_ast_int_node *)node;
struct mie_value *value
= mie_ctx_get_int(gen->c_ctx, int_node->n_value->t_int, 32);
enum ivy_status status = push_operand(expr, node, value);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_string(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
debug_printf("codegen: got string\n");
struct ivy_ast_string_node *string_node
= (struct ivy_ast_string_node *)node;
struct mie_value *var_ptr = mie_builder_get_string_ptr(
gen->c_builder, string_node->n_value->t_str);
struct mie_type *str = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_STR);
struct mie_value *var_value
= mie_builder_load(gen->c_builder, str, var_ptr, NULL);
enum ivy_status status = push_operand(expr, node, var_value);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_fstring(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
debug_printf("codegen: got fstring\n");
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
codegen_push_generator(gen, CODE_GENERATOR_FSTRING, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_op(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
debug_printf("codegen: got op\n");
enum ivy_status status = push_operator(expr, node);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_msg(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
debug_printf("codegen: got msg\n");
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
codegen_push_generator(gen, CODE_GENERATOR_MSG, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_lambda(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
debug_printf("codegen: got lambda\n");
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
codegen_push_generator(gen, CODE_GENERATOR_LAMBDA, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_var_reference(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
debug_printf("codegen: got var reference\n");
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
struct mie_value *var_ptr = NULL;
struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node;
struct codegen_var var = {};
enum ivy_status status
= codegen_resolve_variable(gen, ident->n_content->t_str, &var);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
#if 0
if (var) {
/* this is a local variable */
var_ptr = var->v_ptr;
} else {
/* this is a global variable, and needs to be loaded via a data ptr */
var_ptr = mie_builder_get_data_ptr(
gen->c_builder, ident->n_content->t_str);
}
struct mie_type *id = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_ID);
struct mie_value *var_value
= mie_builder_load(gen->c_builder, id, var_ptr, NULL);
#endif
struct mie_value *var_value = codegen_load_variable(gen, &var);
if (!var_value) {
return CODEGEN_RESULT_ERR(IVY_ERR_INTERNAL_FAILURE);
}
status = push_operand(expr, node, var_value);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
return CODEGEN_RESULT_OK(0);
}
#if 0
static struct code_generator_result gen_var_ref(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node)
{
struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node;
struct codegen_var *var
= codegen_resolve_variable(gen, ident->n_content->t_str);
if (var) {
/* this variable has been defined previously, and we have a
* mie_value representing its location on the stack */
struct codegen_value *var_value
= codegen_value_create(node, var->v_ptr);
codegen_push_value(gen, var_value);
return CODEGEN_RESULT_OK(0);
}
/* this variable either doesn't exist, or is a global variable */
struct codegen_value *var_value = codegen_value_create(node, NULL);
codegen_push_value(gen, var_value);
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_op(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
debug_printf("codegen: got operator\n");
struct codegen_value *left = codegen_pop_value(gen);
struct codegen_value *right = codegen_pop_value(gen);
if (!left || !right) {
return CODEGEN_RESULT_ERR(IVY_ERR_INVALID_VALUE);
}
struct mie_value *left_value = left->v_value;
struct mie_value *right_value = right->v_value;
struct mie_type *left_type = mie_value_get_type(left_value);
struct mie_type *right_type = mie_value_get_type(right_value);
struct mie_type *i32 = mie_ctx_get_int_type(gen->c_ctx, 32);
if (left_type && left_type->t_id == MIE_TYPE_PTR) {
left_value
= mie_builder_load(gen->c_builder, i32, left_value, NULL);
}
if (right_type && right_type->t_id == MIE_TYPE_PTR) {
right_value = mie_builder_load(
gen->c_builder, i32, right_value, NULL);
}
struct ivy_ast_op_node *op_node = (struct ivy_ast_op_node *)node;
const struct ivy_operator *op = op_node->n_op;
struct mie_value *op_instr = NULL;
switch (op->op_id) {
case IVY_OP_ADD:
op_instr = mie_builder_add(
gen->c_builder, left_value, right_value, "addtmp");
break;
case IVY_OP_SUBTRACT:
op_instr = mie_builder_sub(
gen->c_builder, left_value, right_value, "subtmp");
break;
case IVY_OP_MULTIPLY:
op_instr = mie_builder_mul(
gen->c_builder, left_value, right_value, "multmp");
break;
case IVY_OP_DIVIDE:
op_instr = mie_builder_div(
gen->c_builder, left_value, right_value, "divtmp");
break;
default:
mie_value_destroy(left->v_value);
mie_value_destroy(right->v_value);
codegen_value_destroy(left);
codegen_value_destroy(right);
return CODEGEN_RESULT_ERR(IVY_ERR_NOT_SUPPORTED);
}
if (!op_instr) {
mie_value_destroy(left->v_value);
mie_value_destroy(right->v_value);
codegen_value_destroy(left);
codegen_value_destroy(right);
return CODEGEN_RESULT_ERR(IVY_ERR_NO_MEMORY);
}
struct codegen_value *op_value = codegen_value_create(node, op_instr);
codegen_push_value(gen, op_value);
if (node == expr->s_expr_root) {
codegen_pop_generator(gen);
}
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result cancel_expr(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node)
{
debug_printf(
"codegen: expression stopped by %s\n",
ivy_ast_node_type_to_string(node->n_type));
codegen_pop_generator(gen);
return CODEGEN_RESULT_OK(CODEGEN_REPEAT_NODE);
}
#endif
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 expression\n");
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 expression\n");
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
b_queue stack = B_QUEUE_INIT;
b_queue_entry *cur = NULL;
while ((cur = b_queue_pop_back(&expr->s_item_queue))) {
struct expr_item *item = b_unbox(struct expr_item, cur, i_entry);
if (item->i_type == EXPR_OPERAND) {
b_queue_push_back(&stack, &item->i_entry);
continue;
}
struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)item->i_node;
b_queue_entry *left_entry = b_queue_pop_back(&stack);
b_queue_entry *right_entry = b_queue_pop_back(&stack);
struct expr_item *left
= b_unbox(struct expr_item, left_entry, i_entry);
struct expr_item *right
= b_unbox(struct expr_item, right_entry, i_entry);
struct mie_value *op_value = NULL;
switch (op->n_op->op_id) {
case IVY_OP_ADD:
op_value = mie_builder_add(
gen->c_builder, left->i_value, right->i_value,
"addtmp");
break;
case IVY_OP_SUBTRACT:
op_value = mie_builder_sub(
gen->c_builder, left->i_value, right->i_value,
"subtmp");
break;
case IVY_OP_MULTIPLY:
op_value = mie_builder_mul(
gen->c_builder, left->i_value, right->i_value,
"multmp");
break;
case IVY_OP_DIVIDE:
op_value = mie_builder_div(
gen->c_builder, left->i_value, right->i_value,
"divtmp");
break;
default:
return IVY_ERR_NOT_SUPPORTED;
}
if (!op_value) {
return IVY_ERR_NO_MEMORY;
}
free(left);
free(right);
item->i_value = op_value;
b_queue_push_back(&stack, &item->i_entry);
}
cur = b_queue_pop_back(&stack);
if (!cur) {
return IVY_ERR_INTERNAL_FAILURE;
}
struct expr_item *result_item = b_unbox(struct expr_item, cur, i_entry);
*result = result_item->i_value;
free(result_item);
return IVY_OK;
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
struct mie_value *value)
{
struct expr_codegen_state *expr = (struct expr_codegen_state *)state;
debug_printf("codegen: got sub-expr\n");
enum ivy_status status = push_operand(expr, NULL, value);
if (status != IVY_OK) {
return CODEGEN_RESULT_ERR(status);
}
return CODEGEN_RESULT_OK(0);
}
struct code_generator expr_generator = {
.g_type = CODE_GENERATOR_EXPR,
.g_state_size = sizeof(struct expr_codegen_state),
.g_state_init = state_init,
.g_state_fini = state_fini,
.g_value_received = value_received,
.g_node_generators = {
NODE_CODEGEN(INT, gen_int),
NODE_CODEGEN(OP, gen_op),
NODE_CODEGEN(MSG, gen_msg),
NODE_CODEGEN(LAMBDA, gen_lambda),
NODE_CODEGEN(STRING, gen_string),
NODE_CODEGEN(FSTRING, gen_fstring),
NODE_CODEGEN(IDENT, gen_var_reference),
},
};