#1199 Casts to bool don't work.

closed-fixed
5
2013-05-25
2006-09-21
No

Casts to bool don't work.

I tried both implicit and explicit casts.
sdcc -v
gives
SDCC :
mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08
2.6.0 #4309 (Aug 16 2006) (UNIX)
It is the sdcc package from Debian unstable.

In the example below the function does return 0. This
can be seen in the generated assembly code:

;test.c:6: return(i);
; genRet
; Dump of IC_LEFT: type AOP_LIT size 1
ld l,#0x00
; genLabel
00101$:
; genEndFunction
ret

According to the C standard it should return 1:
"When any scalar value is converted to _Bool, the
result is 0 if the value compares equal to 0; otherwise
the result is 1."
It seems sdcc compares only the least significant byte
to 0.

I compiled the example below with sdcc -c -mz80 --std-c99

#include <stdbool.h>

bool test(void)
{
unsigned int i = 256;
return(i);
}

Discussion

  • Robert Larice

    Robert Larice - 2007-07-09

    Logged In: YES
    user_id=1840151
    Originator: NO

    sdcc's <stdbool.h> file,
    just defines bool to be a "char"
    (for your target architecture z80)

    so sdcc itself behaves correctly,
    but <stdbool.h> doesn't make much sense here.

    i'd suggest to simply insert an
    #error "bool not supported for this architecture"
    in <stdbool.h>

    Robert Larice

     
  • Maarten Brock

    Maarten Brock - 2007-07-09

    Logged In: YES
    user_id=888171
    Originator: NO

    The solution I propose is this: revert to how I implemented stdbool.h in the first place.
    For the Z80 that means that only BOOL is defined as char to something resembling a boolean.
    And bool is NOT defined.

     
  • Robert Larice

    Robert Larice - 2007-07-09

    Logged In: YES
    user_id=1840151
    Originator: NO

    just fine,
    i haven't digged the ChangeLog for changes of
    this file.

    Robert Larice

     
  • Maarten Brock

    Maarten Brock - 2007-07-10

    Logged In: YES
    user_id=888171
    Originator: NO

    Maybe this is not what Philipp had in mind, but bool is now no longer supported for the z80, hc08 and PICs. You can use BOOL which is still defined as char, shows your intention, but can generate unexpected behaviour like above problem.

    SDCC 2.7.2 #4877.

     
  • Maarten Brock

    Maarten Brock - 2007-07-10
    • labels: 101552 --> Run Time Library
    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks