Path: utzoo!utgpu!watmath!clyde!att!rutgers!apple!desnoyer
From: desnoyer@Apple.COM (Peter Desnoyers)
Newsgroups: comp.lang.c
Subject: Re: const, volatile, etc [was Re: #defines with parameters]
Message-ID: <21560@apple.Apple.COM>
Date: 2 Dec 88 20:41:15 GMT
References: <674@quintus.UUCP> <117@halcdc.UUCP> <468@auspex.UUCP> <9016@smoke.BRL.MIL> <10919@ulysses.homer.nj.att.com> <377@chem.ucsd.EDU>
Distribution: eunet,world
Organization: Apple Computer Inc, Cupertino, CA
Lines: 39

>In article <10919@ulysses.homer.nj.att.com> ggs@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