Evaluation of if's

Frank P. Bresz fpb at ittc.wec.com
Wed Jun 5 07:44:00 AEST 1991


This is a post for a friend:

Direct replies to fpb at ittc.wec.com because I am interested and
perkins at cgi.com, he can't post/read news but he can get mail.

I will capture the thread and mail to him if you think this seems to have
enough interest to post back instead of mailing.

------------------------------------------------------

So what do you think of the following:

main()
{
	int i;

	if ( (i=1) == (i=2) )
	{
		printf( "i'm an ultrix compiler\n" );
	}
	else
	{
		printf( "i'm a sun compiler\n" );
	}
}

Is somebody wrong or is the behavior undefined?  I didn't look at the
sun assembly code, but on the ultrix the comparison seems to have been
optimized away (even with the optimizer off).  Ultrix seems to say
"hey, contents of i equal contents of i so why bother?"  Sun seems happy
to compare the results of 2 different expressions even though the results
are stored in the same place.

					don.

------------------------------------------------------

Here is my personal opinion along with some more information about various
vendors C Compilers.

Personal opinion: I think the behaviour is undefined by K&R anyway I can't
say for ANSI.

VAX C version 3.1 behaves as though it were ultrix.

Here are some results on the machines I have access to, just a few flavors
of C compilers, I won't dignify the question by trying a PC :

On Ultrix 4.1 running on a DECStation 5000/200

$ cd src
$ cc -o dontest dontest.c
$ dontest
i'm an ultrix compiler
$ cc -O4 -o dontest dontest.c
$ dontest
i'm an ultrix compiler
$

Now a Sun3 runnning StunOS 4.1 :

fpb at ip2_ins1:src 3==>make dontest
cc -g   -target sun3 -o dontest dontest.c
fpb at ip2_ins1:src 4==>dontest
i'm a sun compiler

OK It knows it's a Sun how about if we optimize

fpb at ip2_ins1:src 5==>cc -O  -target sun3 -o dontest dontest.c
fpb at ip2_ins1:src 6==>dontest
i'm an ultrix compiler

Ahh now it thinks its ultrix.

Now a Sun4  runnning StunOS 4.1 :

fpb at ittc:src 112==>cc   -target sun4 -o dontest dontest.c
fpb at ittc:src 113==>dontest
i'm a sun compiler
fpb at ittc:src 114==>cc -O   -target sun4 -o dontest dontest.c
fpb at ittc:src 115==>dontest
i'm an ultrix compiler

Seems to work just like the sun3 no surprise there.

Now lets try the Sun386i running StunOS 4.0.2 :

fpb at doccent:src 12==>cc -g   -sun386 -o dontest dontest.c
fpb at doccent:src 13==>dontest
i'm a sun compiler

OK optimize

fpb at doccent:src 14==>cc -O  -sun386 -o dontest dontest.c
fpb at doccent:src 15==>dontest
i'm a sun compiler

Hmmm still Sun, Optimize some more

fpb at doccent:src 16==>cc -O3  -sun386 -o dontest dontest.c
fpb at doccent:src 17==>dontest
i'm a sun compiler

Still Sun, Go all the way

fpb at doccent:src 18==>cc -O9  -sun386 -o dontest dontest.c
cc: Illegal optimization option "-O9"

Shoot! OK how about 5
fpb at doccent:src 19==>cc -O5  -sun386 -o dontest dontest.c
cc: Illegal optimization option "-O5"

Allright 4
fpb at doccent:src 20==>cc -O4  -sun386 -o dontest dontest.c
fpb at doccent:src 21==>dontest
i'm a sun compiler

Still no dice (Oh well noone ever said the 386i had intelligence)

Now lets see how the mighty VAX 6530 running VMS 5.4 and using VAX C 3.1
does on this. 

fpb at kklvx1==>cc dontest.c
fpb at kklvx1==>link dontest,sys$library:vaxcrtl/lib
fpb at kklvx1==>run dontest
i'm an ultrix compiler

Hmmmm, perhaps that message should read I am on some sort of DEC machine that
likes to optimize easy stuff no matter what.

Anyway what do the gods think?
--
| ()  ()  () | Frank P. Bresz   | Westinghouse Electric Corporation
|  \  /\  /  | fpb at ittc.wec.com | ITTC Simulators Department
|   \/  \/   | uunet!ittc!fpb   | Those who can, do. Those who can't, simulate.
| ---------- | +1 412 733 6749  | My opinions are mine, WEC doesn't want 'em.



More information about the Comp.lang.c mailing list