Skip to content

Commit

Permalink
Add some support for unary expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiy-belyanin committed Mar 21, 2023
1 parent 52678dd commit 9842f62
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 74 deletions.
131 changes: 57 additions & 74 deletions libs/compiler/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,12 @@ ir_builder create_ir_builder(ir_module *const module, const syntax *const sx)
builder.continue_label = IR_LABEL_NULL;
builder.function_end_label = IR_LABEL_NULL;

builder.value_one = IR_VALUE_VOID;
builder.value_minus_one = IR_VALUE_VOID;
builder.value_fone = IR_VALUE_VOID;
builder.value_fminus_one = IR_VALUE_VOID;
builder.value_zero = ir_build_const_int(builder, 0);
builder.value_fzero = ir_build_const_float(builder, 0.0);
builder.value_one = ir_build_const_int(builder, 1);
builder.value_minus_one = ir_build_const_float(builder, -1);
builder.value_fone = ir_build_const_float(builder, 1.0);
builder.value_fminus_one = ir_build_const_float(builder, -1.0);

return builder;
}
Expand Down Expand Up @@ -1365,79 +1367,60 @@ static item_t ir_emit_increment_expression(ir_builder *const builder, const node
static item_t ir_emit_unary_expression(ir_builder *const builder, const node *const nd)
{
const unary_t operator = expression_unary_get_operator(nd);
assert(operator != UN_INDIRECTION);

unimplemented();
switch (operator)
{
case UN_POSTINC:
case UN_POSTDEC:
case UN_PREINC:
case UN_PREDEC:
return ir_emit_increment_expression(builder, nd);

case UN_MINUS:
case UN_NOT:
{
const node operand = expression_unary_get_operand(nd);
const item_t operand_value = ir_emit_expression(builder, &operand);
const binary_t instruction = (operator == UN_MINUS) ? BIN_MUL : BIN_XOR;
const item_t second_value = builder->value_minus_one;

return IR_VALUE_VOID;
return ir_emit_binary_operation(builder, operand_value, second_value, instruction);
}

case UN_LOGNOT:
{
const node operand = expression_unary_get_operand(nd);
const item_t value = ir_emit_expression(builder, &operand);
const item_t second_value = builder->value_zero;

unimplemented();
return IR_VALUE_VOID
}

case UN_ABS:
{
unimplemented();
return IR_VALUE_VOID;
}

// switch (operator)
// {
// case UN_POSTINC:
// case UN_POSTDEC:
// case UN_PREINC:
// case UN_PREDEC:
// return ir_emit_increment_expression(builder, nd);

// case UN_MINUS:
// case UN_NOT:
// {
// const node operand = expression_unary_get_operand(nd);
// const item_t operand_value = ir_emit_expression(builder, &operand);
// const binary_t instruction = (operator == UN_MINUS) ? BIN_MUL : BIN_XOR;

// ir_emit_binary_operation(builder, operand_value, second_value, instruction);
// return operand_rvalue;
// }

// case UN_LOGNOT:
// {
// const node operand = expression_unary_get_operand(nd);
// const rvalue value = emit_expression(enc, &operand);

// return value;
// }

// case UN_ABS:
// {
// unimplemented();
// return IR_VALUE_VOID;
// }

// case UN_ADDRESS:
// {
// const node operand = expression_unary_get_operand(nd);
// const lvalue operand_lvalue = emit_lvalue(enc, &operand);

// assert(operand_lvalue.kind != LVALUE_KIND_REGISTER);

// const rvalue result_rvalue = {
// .from_lvalue = !FROM_LVALUE,
// .kind = RVALUE_KIND_REGISTER,
// .val.reg_num = get_register(enc),
// .type = TYPE_INTEGER
// };

// to_code_2R_I(
// enc->sx->io,
// IC_MIPS_ADDI,
// result_rvalue.val.reg_num,
// operand_lvalue.base_reg,
// operand_lvalue.loc.displ
// );
// return result_rvalue;
// }

// case UN_UPB:
// {
// unimplemented();
// return IR_VALUE_VOID;
// }

// default:
// unreachable();
// return IR_VALUE_VOID;
// }
case UN_ADDRESS:
{
const node operand = expression_unary_get_operand(nd);

unimplemented();
return IR_VALUE_VOID;
}

case UN_UPB:
{
unimplemented();
return IR_VALUE_VOID;
}

default:
unreachable();
return IR_VALUE_VOID;
}
}

static item_t ir_emit_binary_operation(ir_builder *const builder, const item_t lhs, const item_t rhs, const binary_t operator)
Expand Down
2 changes: 2 additions & 0 deletions libs/compiler/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ typedef struct ir_builder
item_t continue_label;
item_t function_end_label;

item_t value_zero;
item_t value_fzero;
item_t value_one;
item_t value_minus_one;
item_t value_fone;
Expand Down

0 comments on commit 9842f62

Please sign in to comment.