Should optimizing compilers automatically assign registers?

Richard Tobin richard at aiai.ed.ac.uk
Wed May 9 23:20:59 AEST 1990


In article <596 at atcmpe.atcmp.nl> leo at atcmp.nl (Leo  Willems) writes:
>Should an optimizer put autovar's in a register anyway? If you use
>setjmp/longjmp, on the return from setjmp via longjmp, automatic
>variables can not be trusted any more! 

This is often true.

>This is in conflict with the manual page of setjmp/longjmp.

This depends on your manual.

>Is there an answer to this problem? 

Yes, there are several.

(1) In Ansi C, auto variables that have changed since the setjmp()
    have unpredictable values (ie, the registers may be restored to
    the values saved by setjmp()).  You can avoid this by declaring the
    variable as "volatile", which will prevent it from being stored in
    a register.

(2) In some implementations, longjmp() "unwinds" the stack so that all
    variables in registers are restored to the correct values.

(3) In some implementations, only variables declared as "register" will
    be assigned to registers.

(4) In some implementations, any routine that calls setjmp() will not
    be optimised - all auto variables will go on the stack :-(

(5) In some implementations, in a routine that calls setjmp() only
    variables declared as "register" will be assigned to registers.

I suppose it would also be possible to use registers, but always store
them on the stack before calling a routine which might call longjmp(),
but I don't know of any implementation that does this.

So what do you do?  Choose from the following:

 - if your compiler supports it, use "volatile"
 - if your compiler respects it, use (or rather don't use) "register"
 - don't optimise the relvant routines.

In some cases, it may be possible to move the setjmp() down into a
separate procedure with fewer auto variables.  In such cases it must
be that you don't modify the variables in the outer procedure between
setjmp() and longjmp(), so this will only help in case (4).

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.unix.wizards mailing list