braner@batcomputer.TN.CORNELL.EDU (braner) (08/27/86)
[]
The following program will never exit:
-------------------------------------------------
#include <stdio.h>
long n;
subr()
{
register int j;
j = (97 * n) / 12345; /* integer result, long intermediate */
}
main()
{
register long i;
for (i=0; i<0x20000; i++) {
subr();
if ((i&0xFFF) == 0)
printf("t = %lx\n", i);
}
}
------------------------------------------------
The reason is that subr() is compiled into: (excerpt)
MOVE D7,-(A7) /* save the "register int j" space, */
MOVE.L #97,D7 /* but use it as "register long scratch", */
/* upper word of main's i gets clobbered! */
...
(If you declare > 1 register, MOVEM.L ,-(A7) is used, so no problem.)
This is especially surprising because in MOST other cases the compiler
moves register vars into a scratch register (D0) before doing ANYTHING
with them, which is a waste of time and space. For example:
register int i, k;
i = i + k;
compiles into (even if mmimp'roved):
MOVE D7,D0
ADD D6,D0
MOVE D0,D7
You would think that that's because while evaluating "i + k", the
stupid compiler doesn't know yet that it is not going to keep the old
value of i, i.e. it could say "j = i + k". But look:
register int i, j, k;
i = j + k;
compiles into:
MOVE D6,D7
ADD D5,D7
- i.e. in the former example the compiler is not stupid, just lazy!
Anybody at Megamax listening?
Disclaimer: Although I AM disappointed that I have to spend so much time
debugging a compiler I spent $200 on (and get no rebate...), I still think
that the Megamax compiler is the best we have right now. But, if anybody
has had experience with the brand new Mark Williams compiler/shell, please
let us know! (bug density, speed of compilation, quality of code...)
At something like $125 for compiler AND shell it MIGHT be a bargain, unless
you have already spent your money on such things. But then, the compiler
output products might work with the shell's I/O redirection...
- Moshe Braner
Corson Hall, Cornell U., Ithaca NY 14853 607-272-3487