rui314/chibicc

an error in operation with the same priority

SweetPepers opened this issue · 1 comments

int i = 2;
return  i + ++i * i++;

this code return different value with different compiler:

clang : 11 = 2 + 3 * 3
gcc : 16 = 4 + 4 * 3
chibicc : 12 = 4 + 4 * 2

caused by here:
at line 1096 :

  gen_expr(node->rhs);
  push();
  gen_expr(node->lhs);
  pop("%rdi");

It will first calculate the rhs then the lhs.

maybe should be this:

  gen_expr(node->lhs);
  push();
  gen_expr(node->rhs);
  push();
  pop("%rdi");
  pop("%rax");
rui314 commented

In C, the evaluation order for such expression is not defined, so you shouldn't write code that assumes some specific evaluation order.