Files
ivy/lang/codegen/expr.c

391 lines
10 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_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;
int flags = 0;
if (b_queue_empty(&expr->s_item_queue)
&& (expr->s_flags & CODEGEN_F_IGNORE_RESULT)) {
flags |= CODEGEN_F_IGNORE_RESULT;
}
codegen_push_generator(gen, CODE_GENERATOR_MSG, flags, 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
= codegen_resolve_variable(gen, ident->n_content->t_str);
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);
enum ivy_status 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(IDENT, gen_var_reference),
},
};