lynn/chibicc

`char` return type doesn't work correctly

Closed this issue · 2 comments

These two functions should behave the same, but don't:

char foo(void)
{
    return -1;
}
char bar(void)
{
    return 0xff;
}
void main(void)
{
    int f = foo();
    int b = bar();
}

Somewhere a truncation and extension should happen, but doesn't…

|0000 @rbp $2
|0100 #ff00 .rbp STZ2 main_ BRK
( bss )
( data )
( text )
@foo_
  #0000
  #0001
  SUB2
  !.L.return.foo
  #0000
@.L.return.foo
  JMP2r
@bar_
  #00ff
  !.L.return.bar
  #0000
@.L.return.bar
  JMP2r
@main_
  .rbp LDZ2 #0004 SUB2 .rbp STZ2
  #0002 .rbp LDZ2 ADD2
  foo_
  DUP2 ROT2 STA2
  POP2
  .rbp LDZ2
  bar_
  DUP2 ROT2 STA2
  POP2
  #0000
@.L.return.main
  .rbp LDZ2 #0004 ADD2 .rbp STZ2
  JMP2r

Looks like the original chibicc might have the same problem. It does however try to handle this for _Bool, which is broken in our version.

@lynn can confirm your commit fixed this. now this

return SIN_TABLE[(a & 0x40 ? ~a : a) & 0x3f] * (a & 0x80 ? 0xff : 1);

behaves the same as

return SIN_TABLE[(a & 0x40 ? ~a : a) & 0x3f] * (a & 0x80 ? -1 : 1);

if i modify examples/star.c, whereas it didn't before (which was how I discovered the bug). thanks~!