Re: PreScheme exception handling

Thomas Hintz <[email protected]> Tue, 24 Nov 2015 00:53:09 -0800
Newsgroups gmane.lisp.scheme.scheme48
Message-ID <[email protected]>
On Sun, Nov 22, 2015, at 08:38 AM, Michael Sperber wrote:
> 
> Thomas Hintz <[email protected]> writes:
> 
> >> I'd start by trying to declare the setjmp be a simple C primop.  Or did
> >> you already do that?
> 
> How did you make setjmp be available to PreScheme code?

(external setjmp "setjmp" (=> (address) integer))
(external longjmp "longjmp" (=> (address integer) integer)

Used like:

(if (= (setjmp *buf*) 0) ; setjmp initially returns 0
  (begin
    (do-stuff)
    (if invalid-input
        (longjmp *buf* 1))
    (do-more-stuff))
  (handle-error)) ; longjmp caused the setjmp call to now return nonzero

The 'address' is the address of a buffer. setjmp is an assembly function
that stores the values of the registers in said buffer, including the
stack pointer and returns 0. longjmp is an assembly function that reads
the stored values out of the buffer and fills the respective registers
with them. When longjmp is done the stack pointer points to the location
that setjmp stored so the function returns to where the original setjmp
call was made. longjmp sets a non-zero return value so that the setjmp
call can be tested whether it is being "called" from a longjmp or not.

Thanks,
Thomas Hintz