lang: codegen: implement cond-group code generation
This commit is contained in:
@@ -11,10 +11,17 @@ enum expr_item_type {
|
||||
EXPR_OPERAND,
|
||||
};
|
||||
|
||||
enum expr_operand_type {
|
||||
EXPR_OPERAND_OTHER,
|
||||
EXPR_OPERAND_VAR,
|
||||
};
|
||||
|
||||
struct expr_item {
|
||||
b_queue_entry i_entry;
|
||||
enum expr_item_type i_type;
|
||||
enum expr_operand_type i_operand_type;
|
||||
struct ivy_ast_node *i_node;
|
||||
struct codegen_var i_var;
|
||||
struct mie_value *i_value;
|
||||
};
|
||||
|
||||
@@ -41,7 +48,7 @@ static struct code_generator_result check_expr_root(
|
||||
|
||||
static enum ivy_status push_operand(
|
||||
struct expr_codegen_state *expr, struct ivy_ast_node *node,
|
||||
struct mie_value *value)
|
||||
struct mie_value *value, const struct codegen_var *var)
|
||||
{
|
||||
struct expr_item *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
@@ -54,6 +61,11 @@ static enum ivy_status push_operand(
|
||||
item->i_node = node;
|
||||
item->i_value = value;
|
||||
|
||||
if (var) {
|
||||
memcpy(&item->i_var, var, sizeof item->i_var);
|
||||
item->i_operand_type = EXPR_OPERAND_VAR;
|
||||
}
|
||||
|
||||
b_queue_push_back(&expr->s_item_queue, &item->i_entry);
|
||||
return IVY_OK;
|
||||
}
|
||||
@@ -85,7 +97,7 @@ static struct code_generator_result gen_int(
|
||||
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);
|
||||
enum ivy_status status = push_operand(expr, node, value, NULL);
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
}
|
||||
@@ -108,7 +120,7 @@ static struct code_generator_result gen_string(
|
||||
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);
|
||||
enum ivy_status status = push_operand(expr, node, var_value, NULL);
|
||||
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
@@ -199,12 +211,8 @@ static struct code_generator_result gen_var_reference(
|
||||
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);
|
||||
status = push_operand(expr, node, NULL, &var);
|
||||
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
@@ -213,6 +221,14 @@ static struct code_generator_result gen_var_reference(
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
codegen_push_generator(gen, CODE_GENERATOR_COND_GROUP, 0, NULL);
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static struct code_generator_result gen_var_ref(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
@@ -363,29 +379,70 @@ static enum ivy_status state_fini(
|
||||
struct expr_item *right
|
||||
= b_unbox(struct expr_item, right_entry, i_entry);
|
||||
|
||||
struct mie_value *left_value = left->i_value;
|
||||
struct mie_value *right_value = left->i_value;
|
||||
|
||||
if (left->i_operand_type == EXPR_OPERAND_VAR) {
|
||||
left_value = codegen_load_variable(gen, &left->i_var);
|
||||
}
|
||||
|
||||
if (right->i_operand_type == EXPR_OPERAND_VAR) {
|
||||
right_value = codegen_load_variable(gen, &right->i_var);
|
||||
}
|
||||
|
||||
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,
|
||||
gen->c_builder, left_value, right_value,
|
||||
"addtmp");
|
||||
break;
|
||||
case IVY_OP_SUBTRACT:
|
||||
op_value = mie_builder_sub(
|
||||
gen->c_builder, left->i_value, right->i_value,
|
||||
gen->c_builder, left_value, right_value,
|
||||
"subtmp");
|
||||
break;
|
||||
case IVY_OP_MULTIPLY:
|
||||
op_value = mie_builder_mul(
|
||||
gen->c_builder, left->i_value, right->i_value,
|
||||
gen->c_builder, left_value, right_value,
|
||||
"multmp");
|
||||
break;
|
||||
case IVY_OP_DIVIDE:
|
||||
op_value = mie_builder_div(
|
||||
gen->c_builder, left->i_value, right->i_value,
|
||||
gen->c_builder, left_value, right_value,
|
||||
"divtmp");
|
||||
break;
|
||||
case IVY_OP_EQUAL:
|
||||
op_value = mie_builder_cmp_eq(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
case IVY_OP_NOT_EQUAL:
|
||||
op_value = mie_builder_cmp_neq(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
case IVY_OP_LESS_THAN:
|
||||
op_value = mie_builder_cmp_lt(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
case IVY_OP_LESS_EQUAL:
|
||||
op_value = mie_builder_cmp_leq(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
case IVY_OP_GREATER_THAN:
|
||||
op_value = mie_builder_cmp_gt(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
case IVY_OP_GREATER_EQUAL:
|
||||
op_value = mie_builder_cmp_geq(
|
||||
gen->c_builder, left_value, right_value,
|
||||
"cmptmp");
|
||||
break;
|
||||
default:
|
||||
return IVY_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@@ -407,7 +464,11 @@ static enum ivy_status state_fini(
|
||||
}
|
||||
|
||||
struct expr_item *result_item = b_unbox(struct expr_item, cur, i_entry);
|
||||
*result = result_item->i_value;
|
||||
if (result_item->i_operand_type == EXPR_OPERAND_VAR) {
|
||||
*result = codegen_load_variable(gen, &result_item->i_var);
|
||||
} else {
|
||||
*result = result_item->i_value;
|
||||
}
|
||||
free(result_item);
|
||||
|
||||
return IVY_OK;
|
||||
@@ -420,7 +481,7 @@ static struct code_generator_result value_received(
|
||||
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);
|
||||
enum ivy_status status = push_operand(expr, NULL, value, NULL);
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
}
|
||||
@@ -442,5 +503,6 @@ struct code_generator expr_generator = {
|
||||
NODE_CODEGEN(STRING, gen_string),
|
||||
NODE_CODEGEN(FSTRING, gen_fstring),
|
||||
NODE_CODEGEN(IDENT, gen_var_reference),
|
||||
NODE_CODEGEN(COND_GROUP, gen_cond_group),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user