Skip to content

Commit

Permalink
fix: apply proper offset for pointers in unary operations (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
amiremohamadi authored Nov 8, 2024
1 parent 30e785e commit ccd84ec
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 254 deletions.
3 changes: 3 additions & 0 deletions examples/inp_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ int main()
printf("*a: %d\n", *a++);
}

printf("*--a %d\n", *--a);
printf("*--a %d\n", *--a);

char *s = (char *)malloc(10 * sizeof(char));
s[0] = '1';
s[1] = 'h';
Expand Down
11 changes: 9 additions & 2 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ apply_result *unary_op_apply(parser_node *node, context *ctx)

int op = unary_op->op;

int unit = 1;
if (operand->type->kind == TYPE_POINTER)
{
general_type *type = ((node_type *)operand->type->data)->type;
unit = type->size(type, ctx);
}

if (op == TKN_MIN)
{
add_text(ctx, "neg rax");
Expand All @@ -122,12 +129,12 @@ apply_result *unary_op_apply(parser_node *node, context *ctx)
}
else if (op == TKN_MINMIN)
{
add_text(ctx, "sub rax, 1");
add_text(ctx, "sub rax, %d", unit);
move_reg_to_var(ctx, operand, "rax");
}
else if (op == TKN_PLUSPLUS)
{
add_text(ctx, "add rax, 1");
add_text(ctx, "add rax, %d", unit);
move_reg_to_var(ctx, operand, "rax");
}

Expand Down
Loading

0 comments on commit ccd84ec

Please sign in to comment.