This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC warnings for unused global variables
Joe Buck wrote:
>
> On Fri, May 02, 2003 at 06:00:30PM +0200, Gabriel Dos Reis wrote:
> > | Because, since the object is volatile, it may affect the program in
> > | ways unknown to the compiler.
> >
> > This is my favorite peeve against the definition of "volatile".
> > Does an alpha ray fired from Centauri count as a "way unknown to the
> > compiler"?
>
> This is a much better example than you thought it was. The answer is
> "yes". Alpha particles are a major source of soft errors; they can
> spontaneously change the value that is stored in RAM. Duplicated
> reads can't be eliminated because they might miss the change caused
> by the alpha particle. A memory test might be written in C with
> a big volatile array, and one of the things it would be looking for
> is effects caused by alpha particles.
>
> (Of course, an alpha particle can't make it through the atmosphere,
> so you'll need to pick some other particle or photon type).
GCC is used for space based applications so the argument and example
still applies.
Another simple case is an octet wide HW register in which the least
significant bit mirrors the open/closed state of a piece of hardware
like a door. It could reflect someone breaking a light beam when
walking into a store. Either way, the compiler knows nothing about
when the value will change.
I am fully in favor of gcc (or any other compiler) optimizing memory
references away when a variable is not volatile. But if a variable
is declared volatile, references to it MUST be honored by the compiler.
Anyone who has written a device driver in C expects the same behavior.
Hopefully Dr. Dewar is out there listening because he could comment
on the requirements of the Ada LRM for pragma volatile. My
understanding
is that the requirements for the feature and expected behavior is
very much the same.
--
Joel Sherrill, Ph.D. Director of Research & Development
joel@OARcorp.com On-Line Applications Research
Ask me about RTEMS: a free RTOS Huntsville AL 35805
Support Available (256) 722-9985