jserv / amacc

Small C Compiler generating ELF executable Arm architecture, supporting JIT execution
Other
1.01k stars 161 forks source link

Incorrect result of left shift with negative number #22

Closed yodalee closed 8 years ago

yodalee commented 8 years ago

In test file tests/shift.c, the execution result of following code:

printf("4 << -1 = %x\n", 4 << -1);

is 4 << -1 = 0, while gcc output 4 << -1 = 2 It is possible that left shift of negative number is handled like unsigned number, thus wipe out all bits.

yodalee commented 8 years ago

About this bug, I can make shift as a special case when parsing. In current implementation, amacc will generate instruction: left shift a negative number. Since ARM left shift cannot recognize negative number, so left shift a negative number will cause left shift many times and the result 0.

yodalee commented 8 years ago

I think this issue can be closed. gcc output 2 since it optimize out the left shift. The following code:

scanf("%d", &v);
printf("%d", 4<<v);

with v=-1 will also get 0 with gcc. cc @jserv