volatile [was: GCC warnings for unused global variables]

Michael Eager eager@eagercon.com
Wed May 7 03:47:00 GMT 2003


Alexandre Oliva wrote:
> On May  3, 2003, Michael Eager <eager@eagercon.com> wrote:
> 
> 
>>It can also be described in more practical terms:
>>   -- each read or write of a volatile must be exactly as specified by the
>>      abstract machine
>>   -- the ordering and number of reads or writes to a volatile must be 
>>      exactly as specified
> 
> 
> Which brings an interesting point.  GCC won't translate <op>= into a
> single machine instruction that loads, operands then stores if the
> variable being operated on is volatile.  Why is that?  Wouldn't it be
> just as correct to translate say `i |= 1' into `or i,1' on a machine
> that supports such an instruction?

As I said before, volatile semantics are not defined precisely in either
C or C++.  The standards do say that "a <op>= b" is equivalent to "a = a <op> b".
IIRC, neither says that it must be expressed exactly this way when a is
volatile, but this is probably a natural interpretation.  It does not say
that it cannot be expressed by a single instruction which has the desired
effect.  (In many cases, it would seem that a single instruction would be
desirable, but how is the compiler to know for sure?)

If you take the view that "a <op>= b" involves a read and a write, then
translating it into load/operate/store is correct.  If you take the view
that this is an atomic operation and that the load and store are implicit
then the single instruction is correct.  I believe that the standard is
vague enough to permit either interpretation to be supported.

-- 
Michael Eager	 Eager Consulting     eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077



More information about the Gcc mailing list