volatile

T. William Wells bill at proxftl.UUCP
Fri May 20 04:15:53 AEST 1988


In article <526 at wsccs.UUCP>, terry at wsccs.UUCP (Every system needs one) writes:
> The whole direction of the argument has been changed, now, I hope.  The
> question, bluntly, is:
>
>       Why can't the compiler figure out what is volitile and
>       THEN optimize without being hit over the head?  Is this
>       a matter of it being impossible, ...

Yes.

Let me tell you why volatile is desirable.  After that, please
can we spend net bandwidth on something more interesting?

There are two things that a user of a C compiler might like to
have happen in a given piece of code.

1) If the compiler detects that a memory access is not necessary,
   get rid of it. This makes the program smaller and faster.

2) Every time that a memory access is implied by the code, a
   memory access must be made.  This is necessary for several
   kinds of things: memory mapped I/O, shared memory, and
   signals.

If we decided that C was not going to support hardware specific
programs (and wouldn't that cause screams!) two of the three
reasons for alternative two go away, BUT THE THIRD DOES NOT.

Now, since you asked, why can't the compiler determine that a
variable is going to be used for a signal?  There are two
reasons, one practical, and one theoretical: The practical reason
is separately compiled modules.  What do you do if the variable
is used in one file and the signal handler in another?  The
second is theoretical: even if you had all the source code in one
module, you would still have problems determining which variables
are actually modifiable by a signal handler.  This is because any
question of the form: "does my program access some variable while
executing a particular part of the program" is equivalent to a
halting problem; the only general way to solve such problems is
to run the program and find out.

Now, given the problem, what should the solution be?  The
solution is one of:

1) Allow memory access optimizations.
2) Forbid memory access optimizations.
3) Tell the compiler when in can or can't do memory access
   optimizations.

The first makes signal handlers (and the other hardware things)
unreasonably difficult.  The second makes the code VERY slow.
That leaves the third.

AND THAT IS WHY VOLATILE EXISTS.

Got it?



More information about the Comp.lang.c mailing list