volatile: a summary

Lloyd Zusman ljz at fxgrp.UUCP
Fri Jun 17 06:02:57 AEST 1988


In article <580 at wsccs.UUCP> terry at wsccs.UUCP (Every system needs one) writes:
  In article <3811 at pasteur.Berkeley.Edu>, faustus at ic.Berkeley.EDU (Wayne A. Christopher) writes:
  > In article <11837 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
  > > 	Is the [volatile] keyword necessary?
  > > 
  > > A perfect compiler would know as much as all its programmers combined,
  > > and could use whatever rules those programmers would use to detect
  > > `real volatility'.
  
  Yes yes yes.
  
  > The problem is that these rules may be in the manual for the graphics
  > board the programmer is writing a driver for.  Say there are two memory
  > mapped registers, "model_number" and "time_of_day", declared as
  > 
  > int *model_number = (int *) 0xf0;	/* Shouldn't be volatile. */
  > int *time_of_day  = (int *) 0ff4;	/* Should be volatile. */
  > 
  > model_number will never change, but time_of_day will.  How is the compiler
  > going to tell the difference, without volatile?
  
  No no no.
  
  The compiler will know because, as part of the board installation, you will
  tell it... or we could make a hardware-standard (I know; too much to ask for)
  method of asking it.  The machine should "know" about the machine.  How can
  you possibly expect it to know if you don't tell it ("how can you have any
  pudding if you don't eat your meat?").  ...

But what if the value of the address isn't known at compile time?  Suppose
I am using the following code:

    extern int *model_addr();
    extern int *time_addr();

    main()
    {
    	int *model_number;
    	int *time_of_day;

    	...

    	model_number = model_addr();
    	time_of_day = time_addr();

    	...

    	while (*time_of_day != 0) {
    	    ...
    	}
    }

Perhaps the routines model_addr() and time_addr() look at some system
configuration file AT RUN TIME.  How can I tell my compiler that the
model_number and time_of_day variables should still be treated as
"volatile" entities, such as in the 'while' loop near the end of my
example?  Your suggestion that the *compiler* read a configuration
file wouldn't work in this case.  However, the programmer still knows
that the model_number and time_of_day variables are to be treated as
"volatile".

I don't understand why there is so much resistance to "volatile".
If you don't like it, just don't use it.  If you can write an
optimizer that somehow can correctly determine all volatility
in all cases, then write one ... it could then treat "volatile" as
a no-op (as, for example, do some IBM PC based C compilers with the
'register' keyword).  But why prevent the rest of us from using it
if we want?

Besides, I don't believe that anyone *can* write an optimizer that
will always correctly identify all volatility, even with the
machine configuration file that is proposed (unless, of course,
this optimizer treated *all* variables as volatile).

--
  Lloyd Zusman                          UUCP:   ...!ames!fxgrp!ljz
  Master Byte Software              Internet:   ljz%fx.com at ames.arc.nasa.gov
  Los Gatos, California               or try:   fxgrp!ljz at ames.arc.nasa.gov
  "We take things well in hand."



More information about the Comp.lang.c mailing list