VAX 4.2BSD cc compiler bug

Jim Franklin jwf at vaxine.UUCP
Sat Feb 1 05:02:31 AEST 1986


----
There is a bug in the VAX 4.2BSD cc compiler related to expressions
involving "register short *" operands and the bitwise-& operator.  The
problem does not occur if you remove the "register" declaration or if you
compile without optimizing.  Program good.c below will correctly print:

6
6
1

However, if you autoincrement t in the printf statement, i.e.,
*t++ & SYM_TYPE, the optimizer generates bogus code and you get the
following output from program bad.c:

6
1664
6

The problem is that the optimizer generates an extzv with autoincrement
for bad.c, and the implied length is 4 bytes (so t gets incremented by 4
bytes rather than 2).

-------------------------- good.c --------------------------------------
#define SYM_READONLY    0x8000
#define SYM_TYPE        0x7ff
short   t_values[] = { 6 | SYM_READONLY, 6 | SYM_READONLY, 1 | SYM_READONLY };

main()
{
        register short * t;
        int i;

        t = t_values;
        for ( i = 0; i < 3; i++ ) {
                printf ("%d\n", *t & SYM_TYPE);
                t++;
        }
}
-------------------------- bad.c --------------------------------------
#define SYM_READONLY    0x8000
#define SYM_TYPE        0x7ff
short   t_values[] = { 6 | SYM_READONLY, 6 | SYM_READONLY, 1 | SYM_READONLY };

main()
{
        register short * t;
        int i;

        t = t_values;
        for ( i = 0; i < 3; i++ )
                printf ("%d\n", *t++ & SYM_TYPE);
}
-------------------------- good.s -------------------------------------
.data
.data
.align	1
.globl	_t_values
_t_values:.long	0x80068006
.long	0x8001
.text
LL0:.align	1
.globl	_main
.data	1
L21:.ascii	"%d\12\0"
.text
.set	L13,0x800
.data
.text
_main:.word	L13
subl2	$4,sp
moval	_t_values,r11
clrl	-4(fp)
L2000001:extzv	$0,$11,(r11),-(sp)
pushal	L21
calls	$2,_printf
addl2	$2,r11
aoblss	$3,-4(fp),L2000001
ret
-------------------------- bad.s --------------------------------------
.data
.data
.align	1
.globl	_t_values
_t_values:.long	0x80068006
.long	0x8001
.text
LL0:.align	1
.globl	_main
.data	1
L21:.ascii	"%d\12\0"
.text
.set	L13,0x800
.data
.text
_main:.word	L13
subl2	$4,sp
moval	_t_values,r11
clrl	-4(fp)
L2000001:extzv	$0,$11,(r11)+,-(sp)             <-- wrong !!!
pushal	L21
calls	$2,_printf
aoblss	$3,-4(fp),L2000001
ret
-----------------------------------------------------------------------



More information about the Comp.lang.c mailing list