🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #281

Re: gcc-Optimize

#281
From: Dave Gillespie
Date: Sun, 26 Jul 1992 10:37
38 lines
1947 bytes
> i discovered an bug, when gcc -g -O removed (optimized) a statement. It
> removes count-- from the example.

ANSI C does not require setjmp() to preserve local variables unless
they are "volatile".

>From the bottom of page 254 of K&R 2:

> Accessible objects have the values they had when "longjmp" was
> called, except that non-"volatile" automatic variables in the
> function calling "setjmp" become undefined if they were changed
> after the "setjmp" call.

Since your change to "count" occurs after the "setjmp" call, the
variable is undefined and so "count--" can just as well be removed.

Michael Salmon pointed out:
> While this is undoubtably frustrating I don't see how gcc can be
> expected to understand your function without including setjmp as an
> element of the language.

I believe the standard allows this precisely so that the compiler
doesn't have to know about the special properties of "setjmp", but
I wouldn't be surprised if some machines' libraries take advantage
of this fact and save some time by not restoring variables that
appear in registers.  Even if GCC did know about setjmp, it wouldn't
be able to assume the variable had a reliable value here, so again,
it might as well remove the statement.

To fix your problem, change the declaration "count" to:

> volatile int  count;

"Volatile" means, more or less, "something funny is happening here
so compile all references to this object literally even if it looks
like they can be optimized."

								-- Dave
Technical Details
Message-ID: <9207270037.AA10971@thymus.synaptics>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!thymus.synaptics.COM!daveg

Show Full Headers
Complete Article Headers
Newsgroups: gnu.gcc.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!thymus.synaptics.COM!daveg
From: daveg@thymus.synaptics.COM (Dave Gillespie)
Subject: Re: gcc-Optimize
Message-ID: <9207270037.AA10971@thymus.synaptics>
Sender: gnulists@ai.mit.edu
Reply-To: daveg@synaptics.com
Organization: GNUs Not Usenet
Distribution: gnu
Date: Sun, 26 Jul 1992 10:37:52 GMT
Approved: bug-gcc@prep.ai.mit.edu
Lines: 39