9fans archive / 1995 / 11 / 57 /    prev next

From: haertel@ich... haertel@ich...
Subject: Plan 9 "8c" compiler bug, with fix.
Date: Wed, 8 Nov 1995 18:52:25 -0500

The "8c" compiler sometimes loses track of side effects in expressions
whose values aren't used.  For example, the following program should
print "1" but in fact prints "0".

I don't know whether the bug occurs in the other compilers.  A proposed
fix is included at the end of this message.  I'm open to alternative
suggestions from people who know the compiler better than me.

The compiler self-bootstraps with the proposed fix installed.

-----beginning of bug example-----
#include <u.h>
#include <libc.h>

int inptr, insize;
char inbuf[100];

void
skipbyte()
{
	/*
	 * Due to a bug in 8c, the side effect to inptr gets lost.
	 */
	(inptr < insize ? inbuf[inptr++] : 0);
}

void
main()
{
	inptr = 0;
	insize = 100;
	skipbyte();
	print("inptr = %d\n", inptr);
}
-----end of bug example-----

Here is a proposed fix, as a diff against the CD-ROM source:

diff /sys/src/cmd/8c/cgen.c 8c/cgen.c
24a25,29
> 		else {
> 			regalloc(&nod, n, Z);
> 			gmove(n, &nod);
> 			regfree(&nod);
> 		}