mie: implement sub/mul/div instruction generation
This commit is contained in:
@@ -203,6 +203,16 @@ void mie_builder_destroy(struct mie_builder *builder)
|
||||
free(builder);
|
||||
}
|
||||
|
||||
struct mie_func *mie_builder_get_current_func(struct mie_builder *builder)
|
||||
{
|
||||
struct mie_block *block = builder->b_current_block;
|
||||
if (!block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return block->b_parent;
|
||||
}
|
||||
|
||||
void mie_builder_put_record(
|
||||
struct mie_builder *builder, struct mie_const *val, const char *name)
|
||||
{
|
||||
@@ -335,21 +345,108 @@ struct mie_value *mie_builder_sub(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *sub = malloc(sizeof *sub);
|
||||
if (!sub) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(sub, 0x0, sizeof *sub);
|
||||
|
||||
mie_instr_init(&sub->op_base, MIE_INSTR_SUB);
|
||||
|
||||
sub->op_left = left;
|
||||
sub->op_right = right;
|
||||
sub->op_type = mie_value_get_type(left);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &sub->op_base)) {
|
||||
free(sub);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(sub), name);
|
||||
|
||||
return MIE_VALUE(sub);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_mul(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *mul = malloc(sizeof *mul);
|
||||
if (!mul) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(mul, 0x0, sizeof *mul);
|
||||
|
||||
mie_instr_init(&mul->op_base, MIE_INSTR_MUL);
|
||||
|
||||
mul->op_left = left;
|
||||
mul->op_right = right;
|
||||
mul->op_type = mie_value_get_type(left);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &mul->op_base)) {
|
||||
free(mul);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(mul), name);
|
||||
|
||||
return MIE_VALUE(mul);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_div(
|
||||
struct mie_builder *builder, struct mie_value *left,
|
||||
struct mie_value *right, const char *name)
|
||||
{
|
||||
if (!builder->b_current_block) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (builder->b_current_block->b_terminator) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct mie_binary_op *div = malloc(sizeof *div);
|
||||
if (!div) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(div, 0x0, sizeof *div);
|
||||
|
||||
mie_instr_init(&div->op_base, MIE_INSTR_DIV);
|
||||
|
||||
div->op_left = left;
|
||||
div->op_right = right;
|
||||
div->op_type = mie_value_get_type(left);
|
||||
|
||||
if (!mie_block_add_instr(builder->b_current_block, &div->op_base)) {
|
||||
free(div);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mie_func_generate_value_name(
|
||||
builder->b_current_block->b_parent, MIE_VALUE(div), name);
|
||||
|
||||
return MIE_VALUE(div);
|
||||
}
|
||||
|
||||
struct mie_value *mie_builder_load(
|
||||
|
||||
@@ -382,15 +382,33 @@ static b_status write_instr(
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_SUB:
|
||||
write_string(converter, "sub");
|
||||
case MIE_INSTR_SUB: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "sub %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
case MIE_INSTR_MUL:
|
||||
write_string(converter, "mul");
|
||||
}
|
||||
case MIE_INSTR_MUL: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "mul %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
case MIE_INSTR_DIV:
|
||||
write_string(converter, "div");
|
||||
}
|
||||
case MIE_INSTR_DIV: {
|
||||
struct mie_binary_op *op = (struct mie_binary_op *)instr;
|
||||
mie_type_to_string(op->op_type, type, sizeof type);
|
||||
write_string_f(converter, "div %s ", type);
|
||||
write_operand(converter, op->op_left, 0);
|
||||
write_string(converter, ", ");
|
||||
write_operand(converter, op->op_right, 0);
|
||||
break;
|
||||
}
|
||||
case MIE_INSTR_LOAD: {
|
||||
struct mie_load *load = (struct mie_load *)instr;
|
||||
mie_type_to_string(load->l_type, type, sizeof type);
|
||||
|
||||
Reference in New Issue
Block a user