const, volatile, etc [was Re: #defines with parameters]

Peter Desnoyers desnoyer at Apple.COM
Sat Dec 3 06:41:15 AEST 1988


>In article <10919 at ulysses.homer.nj.att.com> ggs at ulysses.homer.nj.att.com (Griff Smith) writes:
>
>>....  I blew several hours discovering that a flag
>>set by a signal handler had been optimized out of existence because it
>>wasn't declared volatile.  

I've seen people blow days trying to get a pre-ANSI compiler to frob
device registers properly. Not using '-O' doesn't turn off all
optimizations in some (most?) compilers - just the ones in the
optimizing pass. 

>If I were writing new software I would be aware of the problem and use
>the proper declaration, but what am I to do about fixing all the old
>stuff that now has subtle errors caused by optimizations that used to
>be illegal? 
>
Do what you did before - turn off optimization. Same solution, same
results. 

pet peeve - It is a common thing to have to write a zero to a device
register. The obvious code, (* reg_addr) = 0, often results in:

  xor reg_addr, reg_addr

even with optimization off. This sucks - it reads the register twice,
XORs the two (possibly different) values, and then writes the possibly
non-zero value. If I wanted to read the register, I would have said
so. I probably just cleared my data_ready flag or lost other input.
The solution I have seen (pre-ANSI) was:

  extern int zero = 0;
  (* reg_addr) = zero;	/* KLUDGE */

Unfortunately I don't think specifying bus semantics is within the
purview of the ANSI committee (please correct me if I'm wrong - my
knowledge of the details of the standard is limited) and volatile is
not sufficient to force the desired activity.

				Peter Desnoyers



More information about the Comp.lang.c mailing list