[Bug c/17336] AVRGCC ignores "volatile" keyword for "register" variables

m_klokov at mail dot ru gcc-bugzilla@gcc.gnu.org
Tue Sep 7 08:54:00 GMT 2004


------- Additional Comments From m_klokov at mail dot ru  2004-09-07 08:53 -------
> You didn't say what was wrong with the code.

Yes. It's my mistake.
The compiler moves the access to volatile variable from the loop
(i.e. optimizes it).

in fact the compiler optimizes the code:
  while(1) { if(flag) do_something(); }
to code:
  register char temp=flag; 
  while(1) { if(temp) do_something(); }

where flag global declared as:
  volatile register char flag;

(By the way such "optimization" is neither shorter nor faster for AVR MCU)

> Maybe you were expecting all references to the volatile register 
> to be atomic? They aren't. That isn't what volatile means.

I didn't expect it should be uninterrupted (if you mean this...)
I just expected that compiler will know that this variables may be changed
by external process (hardware, interrupt handler, another thread).

So if I write to check this variable 1000 times in loop,
compiler should not always use the result from first check outside the loop.
It should produce REAL checking every time as the value can be altered.

> There is also a more general problem here in that gcc knows what a 
> volatile memory location is, but does not know what a volatile register 
> is.  As a result, volatile register variables won't work the way you expect.
> By the way, current gcc sources emit a warning for this testcase:
> tmp.c:1: warning: volatile register variables don't work as you might wish

Do you mean that volatile keyword can not be used (will not work)
with register variables? So I cannot use the register variables that
can be changed from outside the code?



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17336



More information about the Gcc-bugs mailing list