[comp.sys.mac.programmer] That amazing MPW C compiler.

larryh@tekgvs.TEK.COM (Larry Hutchinson) (05/11/88)

I recently happened upon an amusing little feaure/bug of the MPW C
compiler.  In the little routine below, the compiler actually did
what I wanted and NOT what I told it to do!  The relevant code is
the 'if' statment.  You will note that if the first part of the 'if'
is true then cTop will be loaded with the undefined value in tmp.
What is actually used is the result from the (xxx*8/10) which was
calculated in register d0.  This is exactly what I intended and the
code worked just fine.  The code had been working for months and
I found it only because I wanted to change something and couldn't
figure out how it worked!

Since cTop IS loaded with tmp if something is assigned to tmp before
the 'if' statment, I assume that this action is the result of a
conscious decision on the part of the compiler writer.



/* 
 *	Autosize thewin in the lower portion of useable.
 *	Window top is not moved if it is in the lower 20% of
 *	useable.
 */
void
SetCWinPos(thewin,useable)
	WindowPtr thewin;
	Rect *useable;		/* screen space for windows */
{

	int cTop;		/* top of the c window */
	int tmp;

	cTop= -thewin->portBits.bounds.top;

	if( (cTop < (useable->bottom*8)/10) || 
			(cTop > (tmp= (useable->bottom-MINCMD_VSIZE))) )
		cTop= tmp;

	MoveWindow(thewin,useable->left,cTop,0);
	SizeWindow(thewin, useable->right-useable->left,useable->bottom-cTop,
			-1);
}

Larry Hutchinson, Tektronix, Inc. PO Box 500, MS 50-383, Beaverton, OR 97077
UUCP:   [uunet|ucbvax|decvax|ihnp4|hplabs]!tektronix!tekgvs!larryh
ARPA:   larryh%tekgvs.TEK.COM@RELAY.CS.NET
CSNet:  larryh@tekgvs.TEK.COM

jimm@amiga.UUCP (06/03/88)

In article <11531@apple.Apple.Com> tecot@apple.UUCP (Ed Tecot) writes:
)In article <3428@tekgvs.TEK.COM> larryh@tekgvs.TEK.COM (Larry Hutchinson) writes:
)>
)>In the little routine below, the compiler actually did
)>what I wanted and NOT what I told it to do!
)
)You've stumbled upon the -dwim option of the MPW 2.0.2 C compiler.  This can
)also be invoked by the compiler directive:
)#read my mind
)						_emt

I think this option should not be the default.  I released an example
program to wide distribution (Greenhills complier, actually, on an*ther
computer, but close enough) in which I failed to pass an argument to a
function.  Worked fine.  Needless to say, normal compilers couldn't
reproduce the intended effect.

When I set the "verbose" option much later, it said:
    "Pretty sloppy, man, but I got you covered."


Just so there is some non-zero content to this posting, I offer the folowing:

I maintain a module about 52K big that has to fit in a crowded ROM.  Based
on experience from an earlier life, I came in one night and went through
the whole thing judiciously declaring register variables.  Recompiled,
and the resulting object got BIGGER.

Greenhills (MPW-C) must have a Master Degree in Science: It's smarter
than you are (or at least me).

	jimm
-- 
	Jim Mackraz, I and I Computing	  
	amiga!jimm	BIX:jmackraz
Opinions are my own.  Comments regarding the Amiga operating system, and
all others, are not to be taken as Commodore official policy.