[comp.lang.c] Broken compilers?

bph@buengc.BU.EDU (Blair P. Houghton) (01/15/89)

In article <922@quintus.UUCP> nair@quintus () writes:
>What should this print?
>
>	int x, y;
>	printf("%d %d %d\n", (x = 1, y = 2), x, y);

I've emailed the conforming answer.  (I fixed the obvious typo, too).
Now, on to controversy.

A while back, someone indicated that they knew of a compiler optimizer
that would reduce something such as the above (ostensibly through
constant-reduction) to

printf("%d %d %d\n", (1,2), x, y);

or maybe even

printf("%d %d %d\n", 2, x, y);

So, you compiler-writing C-programmers, is this thing broken or what?

I mean, the assignment itself is not an expression per se, but a statement,
and comma operators deal in expressions.  But then, the assignment operators
are operators, and in this case hold a place up there with functions in
that they are expressions that can become statements with just the
concatenation of a semicolon.  It's in the syntax description.

So, "broken?"  "Not Broken?"  "Not worth the display phosphorescence
energy?"  What?

				--Blair
				  "I'm broke, too, but that's not
				   in the syntax description..."

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/15/89)

In article <1904@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes:
->	printf("%d %d %d\n", (x = 1, y = 2), x, y);
>A while back, someone indicated that they knew of a compiler optimizer
>that would reduce something such as the above (ostensibly through
>constant-reduction) to
>printf("%d %d %d\n", (1,2), x, y);
>or maybe even
>printf("%d %d %d\n", 2, x, y);
>So, you compiler-writing C-programmers, is this thing broken or what?

The second reduction is of course equivalent to the first.
However, both are broken (assuming x or y is used afterwards),
because after the statement containing printf(), x is required
to contain the value 1 and y must contain the value 2.  Leaving
their contents unchanged is not permitted (unless the program
cannot detect it, e.g. if their values are afterwards unused).

>I mean, the assignment itself is not an expression per se, but a statement,

No, assignments are expressions, not statements.  Sticking a semicolon
after an expression turns it into a statement.

chris@mimsy.UUCP (Chris Torek) (01/15/89)

->	printf("%d %d %d\n", (x = 1, y = 2), x, y);

In article <1904@buengc.BU.EDU> bph@buengc.BU.EDU (Blair P. Houghton) writes:
-A while back, someone indicated that they knew of a compiler optimizer
-that would reduce something such as the above (ostensibly through
-constant-reduction) to
-
-printf("%d %d %d\n", (1,2), x, y);
-
-or maybe even
-
-printf("%d %d %d\n", 2, x, y);
-
-So, you compiler-writing C-programmers, is this thing broken or what?

Quite thoroughly, unless both x and y are `dead'.

The side effect(s), if any, of any expression that occurs anywhere
may not be arbitrarily deleted.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

bph@buengc.BU.EDU (Blair P. Houghton) (01/17/89)

>->	printf("%d %d %d\n", (x = 1, y = 2), x, y);
>
>In article <1904@buengc.BU.EDU> bph@buengc.BU.EDU (Blair P. Houghton) writes:
>-A while back, someone indicated that they knew of a compiler optimizer
>-that would reduce something such as the above (ostensibly through
>-constant-reduction) to
>-
>-printf("%d %d %d\n", (1,2), x, y);
>-
>-or maybe even
>-
>-printf("%d %d %d\n", 2, x, y);
>-
>-So, you compiler-writing C-programmers, is this thing broken or what?

Chris Torek (chris@mimsy.UUCP) commands:
[well, some of us think he found his copy of K&R I next to a burning bush...;-]
>Quite thoroughly, unless both x and y are `dead'.
>
>The side effect(s), if any, of any expression that occurs anywhere
>may not be arbitrarily deleted.

If by "dead" you mean that the compiler has made a thorough search of the
subsequently-traceable code, and the printf argument-heap is LIFO, so that
it knows explicitly that x and y are never again referenced, then I think
I follow you.  Someone else emailed that explanation (one of these days
I'll start remembering the names of the helpful people, sorry).  It seems
that we couldn't whine (nor could we tell without disassembling) if a really
smart compiler reduced those expressions.

Of course, if the side-effect of such a "dead" thing was that your PDP-8's
eleventeenth-bit-light blinked, and that bulb was removed and the electrical
contacts hooked to your Mr. Coffee controller, and your new artificially-
intelligent compiler reduced the expression and unwittingly eliminated the
blink, and you failed the CS 101 exam because of caffeine withdrawal...

That would be injustice, eh?

				--Blair
				  "I can't find 'Mr. Coffee' in the
				   Syntax Summary..."

bph@buengc.BU.EDU (Blair P. Houghton) (01/17/89)

In article <1907@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes:
>(one of these days I'll start remembering the names
>of the helpful people, sorry).

It was Tim Olson.

My trick Alzheimer's must have been acting up...

				--Blair
				  "Ob. C_hint:  your compiler is broken
				   only if it can't find a lawyer to defend
				   its 'optimizations' in court."

arrom@aplcen.apl.jhu.edu (Ken Arromdee ) (01/17/89)

>Of course, if the side-effect of such a "dead" thing was that your PDP-8's
>eleventeenth-bit-light blinked, and that bulb was removed and the electrical
>contacts hooked to your Mr. Coffee controller, and your new artificially-
>intelligent compiler reduced the expression and unwittingly eliminated the
>blink, and you failed the CS 101 exam because of caffeine withdrawal...
>
>That would be injustice, eh?

Yes.  In this case it should have been declared "volatile"...
:-) :-) :-)
--
               EARTH                  --Kenneth Arromdee
           smog  |   bricks          UUCP: ....!jhunix!ins_akaa
        AIR     mud       FIRE   INTERNET: arromdee@crabcake.cs.jhu.edu
      soda water |   tequila       BITNET: g49i0188@jhuvm
               WATER           (please, no mail to arrom@aplcen)

tim@crackle.amd.com (Tim Olson) (01/17/89)

In article <1907@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes:
| Of course, if the side-effect of such a "dead" thing was that your PDP-8's
| eleventeenth-bit-light blinked, and that bulb was removed and the electrical
| contacts hooked to your Mr. Coffee controller, and your new artificially-
| intelligent compiler reduced the expression and unwittingly eliminated the
| blink, and you failed the CS 101 exam because of caffeine withdrawal...

Nah, just remember to declare it "volatile":

	volatile int PDP_11th_light_kludged_into_Mr_Coffee_Machine;

;-)

Dead code elimination does have other useful benefits.  I was looking
over some assembly code generated by the MetaWare Am29000 C compiler
from proprietary sources that a large company had given us, and was able
to catch the error in the following code, because the compiler generated
no instructions for it other than the return!

foo(myColor, index)
RGB *myColor;
long *index;
{
	if ((myColor->red + myColor->green + myColor->blue)/3 <= (MAXRGB/2))
		*index++;
}


	-- Tim Olson
	Advanced Micro Devices
	(tim@crackle.amd.com)