Menu ▾ ▴

#3167 constant expression -0 ... -128 produce char

closed-works-for-me
None
Front-end
5
2023-08-26
2021-01-07
No

seems like sdcc (z80 port) makes a constant expression in the range -0 ... -128 to be type char. sizeof(-0 ... -128) => 1

$ sdcc -mz80 -c -o main.rel main.c

int main(void) {

  volatile static int a = sizeof(-128), b = sizeof(-0), c = sizeof(-1);

  if (sizeof(-128) == 1 && sizeof(-0) == 1 && sizeof(-1) == 1) {

    return 0;
  }

  return 1;
}

ld (_main_a_65536_2), hl
ld l,#0x01
ld (_main_b_65536_2),hl
ld (_main_c_65536_2), hl

tried on:
3.8.0 #10562 linux x64
3.9.0 #11195 linux x64
4.0.0 #11528 linux x64

Related

Wiki: NGI0-Entrust-SDCC

Discussion

  • Benedikt Freisen

    Does SDCC behave the same when used in standard-complaint mode, i.e. when you explicitly specify a certain C standard?

     
    • 10 characters in the password

      i tried it with no specify any (like i wrote command above), but found it with c89.

       
  • Philipp Klaus Krause

    Looks like the AST generation for unary minus uses valUnaryPM(), which narrows anything that is neither float, nor long, no long long, nor a fixed point type, into a char, as long as it fits.
    Since there are no other uses of valUnaryPM(), we could just omit he narrowing there. I'll check if that results in any (code size) regressions.

    P.S.: Looks like cheapestVal might even make -0 have type bool.

     

    Last edit: Philipp Klaus Krause 2021-01-21
    • Philipp Klaus Krause

      Just omitting the call to cheapestVal() in valUnaryPM() breaks comparisons of signed bit-fields with negative integer constant expressions, so fixing this is a bit more involved.

       
  • Philipp Klaus Krause

    • Category: Z80 --> Front-end
     
  • Philipp Klaus Krause

    • status: open --> open-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    This is currently working, and has been working for a while. I guess it was fixed sometime in summer 2022.

     
  • Philipp Klaus Krause

    • status: open-fixed --> closed-works-for-me
     
  • Philipp Klaus Krause

    This is currently working, and has been working for a while. I guess it was fixed sometime in summer 2022, at which time a lot of fixes were made in the type system.

     

    Last edit: Philipp Klaus Krause 2023-03-10
  • Maarten Brock

    Maarten Brock - 2023-08-26

    How is regression test bug-3167.c related to this bug report?
    I don't see it.

    Further, that test is disabled for almost all targets, and generates a warning for ucgbz80.
    If it is for a specific target, why not enable it only for that target instead of disabling it for all others?

     
    • Philipp Klaus Krause

      Looks like the bug number in the regression test filename was a typo. I fixed it in [r14319].

       

      Related

      Commit: [r14319]


Log in to post a comment.