[net.bugs.4bsd] 4.2bsd C compiler generates incorrect code

pag@hao.UUCP (Peter Gross) (01/11/85)

Description:
	4.2bsd C compiler generates incorrect code for float/int compare.

Repeat-By:
	Compile and run the following program:
main()
{
	int i=0, y=10;
	float x[2];

	x[i] = 6.;
	if( x[i] <= y )
	    printf("No bug here.\n");
	else
	    printf("This should never print\n");
}

	Obvious wrong code is produced by the compiler, as indicated
	by this fragment of the assembly language produced:

L16:
	.double	0d6.00000000000000000000e+00
	.text
	movl	-4(fp),r0
	cvtdf	L16,-16(fp)[r0]
	movl	-4(fp),r0
	cvtld	-8(fp),r2
	cvtfd	-16(fp)[r0],r1
------>>cmpd	r1,r2<<--------- this is a no no!
	jgtr	L17
	.data	1

	The cmpd instruction should not be using adjacent registers!
	If the "float" declaration is changed to "double" in the C program,
	then the compiler produces correct code.
Fix:
	Unknown.  FYI, We have installed the bug fix (from rlgvax!guy) to
	src/lib/c2/c21.c dealing with improper conversions.

--peter gross
hao!pag

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (01/12/85)

> 	4.2bsd C compiler generates incorrect code for float/int compare.

Yet another in the long list of PCC bugs that AT&T fixed long ago.

jim@mcvax.UUCP (Jim McKie) (01/14/85)

This bug was not in the original 4.2BSD compiler, but it appeared
in an intermediate version; I have been told it is fixed in the
next release.

As an aside, the bug can be used to demonstrate something we all know:

main()
{
	int i=0, system5=5;
	float fourpointtwo[2];

	fourpointtwo[i] = 4.2;
	if( fourpointtwo[i] > system5 )
		printf("%1.1f > %d\n", fourpointtwo[i], system5);
	else
	    printf("No bug here.\n");
}

Jim McKie
mcvax!jim

jim@mcvax.UUCP (Jim McKie) (01/16/85)

I am no compiler expert, but I looked at the difference in the
code template tables (pcc/table.c) between a version of the 4.2
compiler which works and one which doesn't. The broken compiler
did not have the NASL (share left register) for the 'convert float
to double for comparison' table entry below:

OPLOG,	FORCC,
	SAREG|AWD,	TFLOAT,
	SAREG|AWD,	TDOUBLE,
		NAREG|NASL,	RESCC,
		"	cvtfd	AL,A1\n	cmpd	A1,AR\nZP",

With the 'NASL', it seems to generate the correct code. Anyone care
to confirm who has more experience with the compiler?

--jim