ALL compilers?
COTTRELL, JAMES
cottrell at nbs-vms.ARPA
Fri Oct 4 08:58:51 AEST 1985
> > ...even trivial compilers will generate identical code for
> > if(p) /* BAD */
> > if(p != (anytype *)0) /* GOOD */
>
> ALL C COMPILERS GENERATE IDENTICAL CODE FOR THOSE TWO CASES. Anything that
> does *not* generate identical code for them - even if that means comparing
> the bits stored in "p" with the bit pattern of 0x555555 - is not a C
> compiler. It is a compiler for a language which resembles C in some ways
> but isn't C.
> Guy Harris
This is probably quibbling. It all depends on the degree of optimization
in the compiler. Lets take the two statements (on a PDP-11):
`if (p)' vs `if (p != 0)'
asm: tst p cmp p,#0
beq false beq false
Now a smart optimizer knows how to optimize the compare with zero into
a test, but a simple, literal-minded do-what-I-say not what-I-mean
one semester three credit course compiler might not optimize.
It get hairier with something like:
`if (a = b)' vs `if ((a = b) != 0)'
asm: mov a,b mov a,b
cmp a,#0 (or tst a)
beq false beq false
Here we can optimize away the compare instruxion entirely.
The point is that both would be C compilers, one would just be better.
Guy is right, however, in that programmers should not get hooked
on details like this. Both are *conceptually* equivalent & I would
bet that all *real* C compilers do optimize them to *be* equivalent.
I used to worry about multiplying & dividing by powers of two &
wrote them as shifts until I saw that the optimizer did that for me.
jim cottrell at nbs
*/
------
More information about the Comp.lang.c
mailing list