Behaviour of setjmp/longjmp and registers

Charles Bryant ch at maths.tcd.ie
Fri Jan 27 07:09:11 AEST 1989


In article <5771 at phoenix.Princeton.EDU> haahr at princeton.edu (Paul Gluckauf Haahr) writes:
>a setjmp/longjmp implementation that restores all variables (including
>those in registers) falls out of a caller saves function call protocol.
>no muss, no fuss, just restore the stack pointer and pc.  this will
>work in the presence of inter-procedural optimization, if setjmp() is
>marked as a function that trashes all registers.
>

Could you explain this further please. Consider the example:

	foo()
	{
		register a,b,c;
		/* useful work with a,b,c so all are live */
		if (setjmp(jb)==1) { something using a, b, and c }
		a += b+c;
		fna();		/* only a is live */
		return a;
	}

	fna()
	{
		longjmp(jb,1);
	}

Then the stack will look like this:

call to setjmp: <addr in foo> a b c <addr foo will return to>
call to longjmp: <addr in fna> <addr in foo> a <addr foo will return to>

setjmp can't know what registers were saved (i.e. a, b, c) so it must save
the sp as the address where a is saved.
if longjmp restores the sp to this value, 'foo' will assign
a = <addr in fna>
b = <addr in foo>
c = a at time of longjmp

Is this reasoning correct? Does the compiler have to save ALL the registers for
every function call in 'foo' which potentially could call longjmp or is in
a different file?
-- 

		Charles Bryant.
Working at Datacode Electronics Ltd.



More information about the Comp.lang.c mailing list