Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!tank!shamash!nic.MR.NET!thor.acc.stolaf.edu!mike From: mike@thor.acc.stolaf.edu (Mike Haertel) Newsgroups: comp.lang.c Subject: Re: Ambiguous C? Message-ID: <1943@thor.acc.stolaf.edu> Date: 28 Apr 89 16:27:45 GMT References: <111@ssp1.idca.tds.philips.nl> <17133@mimsy.UUCP> <10136@smoke.BRL.MIL> Reply-To: mike@stolaf.edu Organization: St. Olaf College, Northfield, MN Lines: 42 In article <10136@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: :In article <17133@mimsy.UUCP> chris@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 main() ??< printf("hello, world??/n"); ??>