#594 The "default" case is not being handled

closed-fixed
Erik Petrich
5
2013-05-25
2003-09-26
Stas Sergeev
No

Hi.

I was trying to compile a small test program with
sdcc, and it fails on a very simple "switch" because
sdcc doesn't handle the "default" case.

The program is attached. Also here it is:
---
#include <at89S8252.h>

int main()
{
char a, b;
b = SBUF;
switch (b) {
case 1: a = 'a';
break;
case 2: a = 'b';
break;
default: a = 'c';
}
return a;
}
---

The command line is:
sdcc -mmcs51 --nojtbound --noinduction --noloopreverse
--xram-size 0 --code-size 8192 --debug --no-peep
switch.c

The generated (bad) asm follows:
---
;switch.c:7: switch (b) {
; genMinus
; genMinusDec
dec r2
; genJumpTab
mov a,r2
add a,acc
add a,r2
mov dptr,#00107$
jmp @a+dptr
00107$:
ljmp 00101$
ljmp 00102$
C$switch.c$8$2$2 ==.
;switch.c:8: case 1: a = 'a';
00101$:
; genAssign
mov r2,#0x61
C$switch.c$9$2$2 ==.
;switch.c:9: break;
ljmp 00104$
C$switch.c$10$2$2 ==.
;switch.c:10: case 2: a = 'b';
00102$:
; genAssign
mov r2,#0x62
C$switch.c$13$1$1 ==.
;switch.c:13: }
00104$:
C$switch.c$14$1$1 ==.
;switch.c:14: return a;
---

There is no code for the "default" case.
As "b" contains some garbage, the code just jumps
to an undefined location with the dire consequinces.

This bug seems to be very severe. Our programs
behaves very strangely and unreliably due to it.

sdcc is from CVS.

Discussion

  • Stas Sergeev
    Stas Sergeev
    2003-09-26

    test case

     
    Attachments
  • Stas Sergeev
    Stas Sergeev
    2003-09-26

    • labels: --> Icode generator
     
  • Erik Petrich
    Erik Petrich
    2003-09-27

    Logged In: YES
    user_id=635249

    Fixed in src/SDCCicode 1.167 (--nojtbound option is now
    ignored for switches with a default case)

     
  • Erik Petrich
    Erik Petrich
    2003-09-27

    • milestone: --> fixed
    • assigned_to: nobody --> epetrich
    • status: open --> closed-fixed
     
  • Stas Sergeev
    Stas Sergeev
    2003-09-27

    Logged In: YES
    user_id=501371

    Thanks for the fix, but it fixes only the part of the
    problem.
    I have opened another report (#813572) for the rest of
    that bug.