Path: utzoo!mnetor!uunet!mcvax!unido!ecrcvax!johng
From: johng@ecrcvax.UUCP (John Gregor)
Newsgroups: comp.lang.c
Subject: Is this a bug, or is it just me?
Message-ID: <464@ecrcvax.UUCP>
Date: 15 Dec 87 14:25:23 GMT
Reply-To: johng@ecrcvax.UUCP (John Gregor)
Organization: ECRC, Munich 81, West Germany
Lines: 43

I think I have found a problem.  I know that operations of the same 
precedence can be reordered by the compiler, but I wouldn't think that
all three auto-increments would be done after all the additions.  This
happens on both a vax (4.3bsd) and a Sun-3.

#include 

int sum = 0;
int a[3] = {1, 4, 16};
int i = 0;

main () {
    sum = a[i++] + a[i++] + a[i++];
    printf ("sum: %d      i:%d\n", sum, i);
}

Results in:
sum: 3      i:3

Here is part of the code produced by the sun:

	movl	_i,d0
	lea	_a,a0
	movl	_i,d1
	lea	_a,a1
	movl	a1@(0,d1:l:4),d1
	addl	a0@(0,d0:l:4),d1
	movl	_i,d0
	lea	_a,a0
	addl	a0@(0,d0:l:4),d1
	movl	d1,_sum
	addql	#0x1,_i		    /* Here are the auto-increments */
	addql	#0x1,_i
	addql	#0x1,_i

Jeez, if it has to behave this way, you'd at least think it would optimize
the 3 addql's into 1.  [0.5 * :-)]

Am I missing some obscure part of K&R (I've looked).  Or is this truly a
bug?

			 John Gregor
		johng%ecrcvax.UUCP@germany.CSNET