[net.bugs.4bsd] VAX 4.2BSD cc compiler bug

jwf@vaxine.UUCP (Jim Franklin) (01/31/86)

----
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
-----------------------------------------------------------------------

chris@umcp-cs.UUCP (Chris Torek) (02/02/86)

This has been fixed for ages.  The problem is in /lib/c2, where it
folds cvt[bw]l+bicl instruction pairs into extzv instructions: it
should not do this if the left operand of the cvt instruction
contains an auto increment or decrement.  The easiest way to fix
/lib/c2 is to disable the optimisation completely.  A better fix
is no doubt already in the Mt. Xinu bug list and is already in the
4.3 c2.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu