Ambiguous C?

Mike Haertel mike at thor.acc.stolaf.edu
Sat Apr 29 02:27:45 AEST 1989


In article <10136 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
:In article <17133 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
:>If your compiler does not understand `volatile', and has no way to
:>disable optimisation, you are out of luck.  (You can resort to assembly
:>language subroutines.)
:
:Back, back!  (Making the sign of the cross.)  No need to resort to
:assembly language for something so simple.
:
: [ ... portable trick for faking volatile ... ]
:
:The one thing you can't really accomplish via such a trick is an
:atomic read-modify-write operation, such as ORing a bit into a memory-
:mapped device register.  I can't recall ever needing to ensure that,
:but it is theoretically possible that one might.  In such a case we
:might be inspired by the trick and code the operation something like:
:	if ( (*(long *)DEVADDR |= BIT) == BIT )
:		something_innocuous();
:which will also force the compiler to examine the whole longword.
:Odds are good that it will generate the desired code for the OR
:operation.  (If not, additional effort along these general lines
:should eventually outwit the compiler's optimization.)

I think this would be a really bad idea, and that it would be *much*
better to use the assembly language.  You're never going to port code
like this to a different machine anyway, so there is no loss for using
the assembly language.  But, suppose you wanted to switch to a different
compiler?  And suppose that compiler used the same calling conventions
(they usually do) but generated different code for this particular
reference.  Then the assembly language approach would work, but possibly
not the tweaked C approach.  The insidious thing is, this code looks
portable, so when things stopped working you would have a large puzzle
on your hands.

In general I believe programs should say what they mean, and if they
mean something obviously machine dependent they should say it in an
obviously machine dependent way.  It is not hard to port machine
dependent code, as long as the dependencies are clearly labeled and
isolated from the portable code.
-- 
Mike Haertel <mike at stolaf.edu>
main() ??< printf("hello, world??/n"); ??>



More information about the Comp.lang.c mailing list