Search code examples
exceptionstackforthgforth

What differentiates exception frames from other data on the return stack?


I'm trying to understand how exception frames are located on the return stack during a THROW.

The comments in "jonesforth.f.txt" assert that "When called, THROW walks up the return stack (the process is called 'unwinding') until it finds the exception stack frame." I'm unclear how the (EXCEPTION-MARKER) is differentiated from another other data on the return stack (return addresses, user values using >R, and indicies for do-loops).

In general, how do the various Forth's distinguish between exception frames and other data on the return stack?


Solution

  • It seems Gforth doesn't use this 'unwinding' method.

    Instead it stores the location of the active exception frame in a global variable, while saving the previously active frame's location within a new frame on the return stack. When an exception thrown, Gforth reads the last frame (most inner catch) location directly from the global variable.

    Actually, across multiple other forths I've checked, I didn't see this implementation of the 'unwind' method. All those forths used the same idea of chaining frames in a linked list, with the head pointer stored in a global variable. This looks typical now for forths: http://lars.nocrew.org/dpans/dpansa9.htm

    Maybe Jones Forth is relying on an assumption that the return stack should contain only return addresses at the moment of throwing. The marker address is unique, as it's a dictionary word. And typical loop indices don't reach that high to be confused with a return address.