#1684 signed comparison used for jumptable on unsigned bitfield

closed-fixed
5
2013-05-25
2010-09-18
No

When SDCC (2.9.7 #5977) generates a jumptable for a switch statement on an unsigned bitfield it incorrectly uses a signed compare to check the range.

typedef struct {
uint8_t a : 3;
} TStruct;

TStruct s;
uint8_t a, b;

void test(void)
{
switch (s.a) {
case 0: a = 5; break;
case 1: a = 3; break;
case 2: a = 1; break;
case 3: a = 1; break;
case 4: a = 5; break;
case 5: a = 3; break;
case 6: a = 1; break;
//no case 7:
}
}

generates for mcs51 small:

mov r0,#_s
mov a,@r0
swap a
rl a
anl a,#0x07
mov r2,a
mov r3,a
clr c
mov a,#(0x06 ^ 0x80)
mov b,r3
xrl b,#0x80
subb a,b
jc 00115$
mov a,r2
add a,r2
add a,r2
mov dptr,#00133$
jmp @a+dptr
00133$:
ljmp 00115$
ljmp 00109$
ljmp 00110$
ljmp 00111$
ljmp 00112$
ljmp 00113$
ljmp 00114$

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-rejected
     
  • Philipp Klaus Krause

    You did not create an unsigned bitfield. Rules on signedness in bitfields are a bit different from outside:

    Compilers are required to support bitfields of types
    -unsigned int
    -bool
    -signed int
    -int

    The first two are the only reliable ways to create unsigned bitfields. The third reliably gives a signed bitfield. The signedness of bitfields of type int is implementation-defined. And so is the signedness of any other bitfield type the compiler might support, including u_int8_t.

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2010-09-18
    • status: closed-rejected --> open
     
  • Maarten Brock

    Maarten Brock - 2010-09-18

    Ok, change the uint8_t to unsigned int then and it is still broken.

     
  • Maarten Brock

    Maarten Brock - 2010-09-19
    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed
     
  • Maarten Brock

    Maarten Brock - 2010-09-19

    Fixed in SDCC 2.9.7 #5981.

     

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

Sign up for the SourceForge newsletter:





No, thanks