Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!mcsun!ukc!edcastle!aiai!richard From: richard@aiai.ed.ac.uk (Richard Tobin) Newsgroups: comp.unix.wizards Subject: Re: Should optimizing compilers automatically assign registers? Message-ID: <2453@skye.ed.ac.uk> Date: 9 May 90 13:20:59 GMT References: <512@hhb.UUCP> <596@atcmpe.atcmp.nl> Reply-To: richard@aiai.UUCP (Richard Tobin) Organization: AIAI, University of Edinburgh, Scotland Lines: 53 In article <596@atcmpe.atcmp.nl> leo@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@uk.ac.ed AI Applications Institute, ARPA: R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!R.Tobin