Ambiguity in definition of setjmp/longjmp makes them much less useful

Tom Horsley tom at ssd.csd.harris.com
Mon Oct 8 21:18:02 AEST 1990


>>>>> Regarding Re: Ambiguity in definition of setjmp/longjmp makes them much less useful; henry at zoo.toronto.edu (Henry Spencer) adds:

henry> Some implementations restore all the registers to the way they were
henry> when the *setjmp* was called, but this is often unsatisfactory in
henry> general and can be very unsatisfactory when compilers really start
henry> playing games with register usage.

Wrong! With compilers that play register games restoring the registers as
they were at the time of the setjmp is the ONLY implementation that works
at all (unless setjmp is recognized as a special construct by the compiler,
which I agree is the best way). In any optimizing compiler which is likely
to do things like keep common sub-expressions in registers, the following
simple example shows the requirement for restoring the registers as of the
setjmp() call:

{
   ...
   /* compiler computes a CSE and keeps it in register 47 */
   if (setjmp(...) != 0) {
      /* compiler references the CSE in register 47 */
   }
   /* compiler makes last reference to CSE in register 47 */
   ...
   /* compiler now has something totally different in register 47 */
   longjmp(...)
}

(In the above example register 47 is assumed to be a register that is not
normally destroyed by a function call).

If you were to unwind the stack and restore the registers as of the
longjmp() call, you would get back to the setjmp() with random gibberish in
the register the code generator expected to contain a CSE value.

Personally, I believe that compilers should support setjmp() as a special
construct - simply making might-goto arcs from every other function call to
a point immediately following any setjmp() calls would add enough
information to the flow graph for an optimizing compiler to recognize the
funny lifetimes that registers might have and volatile would only be needed
for variables that interact with signal handling code (since a signal
can happen anywhere in the program, not just at a function call).

Until the day that compilers properly support setjmp() however, the only
implementation of setjmp() that stands a chance of interacting correctly
with an optimizing compiler is one that restores the registers as of the
setjmp() call. Unfortunately, this also means that the only user code that
stands a chance of interacting correctly with an optimizing compiler is code
that correctly declares all variables volatile where necessary.  Since the
phrase "where necessary" is difficult (if not impossible) for an ordinary
mortal to figure out, the obviously best solution is to fix compilers to
special case setjmp().
--
======================================================================
domain: tahorsley at csd.harris.com       USMail: Tom Horsley
  uucp: ...!uunet!hcx1!tahorsley               511 Kingbird Circle
                                               Delray Beach, FL  33444
+==== Censorship is the only form of Obscenity ======================+
|     (Wait, I forgot government tobacco subsidies...)               |
+====================================================================+



More information about the Comp.lang.c mailing list