Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!ll-xn!mit-eddie!bloom-beacon!gatech!uflorida!novavax!proxftl!bill From: bill@proxftl.UUCP (T. William Wells) Newsgroups: comp.lang.c Subject: Re: volatile Summary: you obviously do not understand the problem Message-ID: <196@proxftl.UUCP> Date: 19 May 88 18:15:53 GMT References: <20345@pyramid.pyramid.com> <502@wsccs.UUCP> <51431@sun.uucp> <526@wsccs.UUCP> Organization: Proximity Technology, Ft. Lauderdale Lines: 55 In article <526@wsccs.UUCP>, terry@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?