[gnu.gcc.bug] Unsigned optimization problem in function argument

satz@CISCO.COM (01/04/89)

gcc 1.32 on a Sun 3/280 running SunOS 3.5 manages to botch the extraction
of an unsigned short from an unsigned integer that is passed into a
function. this occurs whether you use the new style function prototype
declartion syntax or not.

long test (arg)
    unsigned int arg;
{

    unsigned short code;

    code = arg;
    switch (code) {
	case 0x0800: return(1);
	case 0x0806: return(2);
	case 0x0200: return(3);
	case 0x0201: return(4);
	case 0x8035: return(5);
	case 0x0804: return(6);
	case 0x6003: return(7);
	case 0x9000: return(8);
	case 0x0600: return(9);
	case 0x8038: return(10);
	default: return(-1);
	case 0:	     return(-2);
    }
}

main()
{
	printf("result = %d\n",test(0x9000));
}

The resulting output should be 8 but is -2. Note that the compiler
generated longword tests when it wasn't told to cast the variable in the
switch statement. Generated code:

#NO_APP
gcc_compiled.:
.text
	.even
.globl _test
_test:
	link a6,#0
	clrl d0
	movew a6@(8),d0			#shouldn't this be a6@(10)?
	cmpl #2052,d0
	jeq L8
	jgt L16
	cmpl #513,d0
	jeq L6
	jgt L17
	tstl d0
	jeq L14
	cmpl #512,d0
	jeq L5
	jra L13
L17:
	cmpl #1536,d0
	jeq L11
	cmpl #2048,d0
	jeq L3
	jra L13
L16:
	cmpl #32821,d0
	jeq L7
	jgt L18
	cmpl #2054,d0
	jeq L4
	cmpl #24579,d0
	jeq L9
	jra L13
L18:
	cmpl #32824,d0
	jeq L12
	cmpl #36864,d0
	jeq L10
	jra L13
L3:
	moveq #1,d0
	jra L1
L4:
	moveq #2,d0
	jra L1
L5:
	moveq #3,d0
	jra L1
L6:
	moveq #4,d0
	jra L1
L7:
	moveq #5,d0
	jra L1
L8:
	moveq #6,d0
	jra L1
L9:
	moveq #7,d0
	jra L1
L10:
	moveq #8,d0
	jra L1
L11:
	moveq #9,d0
	jra L1
L12:
	moveq #10,d0
	jra L1
L13:
	moveq #-1,d0
	jra L1
L14:
	moveq #-2,d0
L1:
	unlk a6
	rts
LC0:
	.ascii "result = %d\12\0"
	.even
.globl _main
_main:
	link a6,#0
	movel #36864,sp@-
	jbsr _test
	movel d0,sp@-
	pea LC0
	jbsr _printf
	unlk a6
	rts