[gnu.gcc.bug] bug in switch

piet@ruuinf (Piet van Oostrum) (02/17/89)

GCC version 1.32 generates wrong code for a switch with a constant
expression.
I have tested it on the Sun3 OS3 and on HCX/UX, so it may be
machine-independent. The behaviour is that it takes the default action
rather than the correct case. This happens independent of -O.
------------------------------------------------------------------------
main()
{ switch(5)
    { case 5 : printf("five\n") ;
	       break ;
      default : printf("default\n") ;
	       break ;
    }
}
------------------------------------------------------------------------
>4> gcc -v test2.c 
gcc version 1.32 
 /usr/staff/lib/gcc-cpp -v -undef -D__GNUC__ -Dmc68000 -Dsun -Dunix -D__mc68000__ -D__sun__ -D__unix__ -D__HAVE_68881__ -Dmc68020 test2.c /tmp/cca26671.cpp 
GNU CPP version 1.32 
 /usr/staff/lib/gcc-cc1 /tmp/cca26671.cpp -quiet -dumpbase test2.c -version -o /tmp/cca26671.s 
GNU C version 1.32 (68k, MIT syntax) compiled by GNU C version 1.32. 
 as -mc68020 /tmp/cca26671.s -o test2.o 
 ld /lib/crt0.o /lib/Mcrt1.o test2.o /usr/staff/lib/gcc-gnulib -lc 
>5> a.out 
default 
>6> gcc -O test2.c 
>7> a.out 
default 
>8> gcc -S -g test2.c 
>9> cat test2.s 
#NO_APP 
gcc_compiled.: 
        .stabs "test2.c",100,0,0,Ltext 
Ltext: 
.stabs "int:t1=r1;-2147483648;2147483647;",128,0,0,0 
.stabs "char:t2=r2;0;127;",128,0,0,0 
.stabs "long int:t3=r1;-2147483648;2147483647;",128,0,0,0 
.stabs "unsigned int:t4=r1;0;-1;",128,0,0,0 
.stabs "long unsigned int:t5=r1;0;-1;",128,0,0,0 
.stabs "short int:t6=r1;-32768;32767;",128,0,0,0 
.stabs "long long int:t7=r1;0;-1;",128,0,0,0 
.stabs "short unsigned int:t8=r1;0;65535;",128,0,0,0 
.stabs "long long unsigned int:t9=r1;0;-1;",128,0,0,0 
.stabs "signed char:t10=r1;-128;127;",128,0,0,0 
.stabs "unsigned char:t11=r1;0;255;",128,0,0,0 
.stabs "float:t12=r1;4;0;",128,0,0,0 
.stabs "double:t13=r1;8;0;",128,0,0,0 
.stabs "long double:t14=r1;8;0;",128,0,0,0 
.stabs "void:t15=15",128,0,0,0 
.text 
LC0: 
        .ascii "five\12\0" 
LC1: 
        .ascii "default\12\0" 
        .even 
.globl _main 
_main: 
        .stabd 68,0,3 
        link a6,#0 
        jra L4              <<<<<<<< should be jra L3
L3: 
        .stabd 68,0,4 
        pea LC0 
        jbsr _printf 
        .stabd 68,0,5 
        addqw #4,sp 
        jra L2 
L4: 
        .stabd 68,0,6 
        pea LC1 
        jbsr _printf 
        .stabd 68,0,7 
        addqw #4,sp 
        jra L2 
L2: 
        .stabd 68,0,9 
L1: 
        unlk a6 
        rts 
.stabs "main:F1",36,0,0,_main 
>10> 
------------------------------------------------------------------------
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)

piet@ruuinf (Piet van Oostrum) (02/17/89)

In article <1141@ruuinf.UUCP>, piet@ruuinf (Piet van Oostrum) writes:
 `GCC version 1.32 generates wrong code for a switch with a constant
 `expression.

The error is a reversed test in stmt.c:
------------------------------------------------------------------------
*** stmt.c.~1~	Tue Jan 17 13:52:25 1989
--- stmt.c	Thu Feb 16 18:19:47 1989
***************
*** 2296,2302
  		      && ! tree_int_cst_lt (n->high, index_expr))
  		    break;
  		}
! 	      if (n)
  		emit_jump (default_label);
  	      else
  		emit_jump (label_rtx (n->code_label));

--- 2296,2302 -----
  		      && ! tree_int_cst_lt (n->high, index_expr))
  		    break;
  		}
! 	      if (!n)
  		emit_jump (default_label);
  	      else
  		emit_jump (label_rtx (n->code_label));
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)