[LLVMdev] [Caml-list] Ocaml(opt) & llvm

Chris Lattner sabre at nondot.org
Wed Nov 28 12:18:46 PST 2007


On Tue, 27 Nov 2007, Gordon Henriksen wrote:
> As Daniel Berlin pointed out on IRC, the language model is trivial. It has 
> just three exception-handling primitives:
> raise expr
> try expr with matching
> exception id ( tuple-type-expr )?

ok.

> The codegen for raise is simple. It just reads a saved return address from 
> the caml_exception_pointer global and returns through several stack frames in 
> one go. The expression raise expr is compiled as such:
>
> ; Store the exception value in the return register.
> $r1 = ...
>
> ; Change the stack pointer.
> load $sp <- 0(caml_exception_pointer)
>
> ;  The stack now contains:
> ;     ... rest of stack ...
> ;     return address
> ;     old caml_exception_pointer
> ;  Restore caml_exception_pointer.
> pop 0(caml_exception_pointer)
>
> ; Jump straight to the nearest landing pad.
> ret

Nice.

> The try-with expression is where the trickery lies. The expression try body 
> with pattern1 -> catch1 | patternN -> catchN is compiled to:

This seems pretty straight-forward to normal landing pads.  The only 
significant difference is how it dynamically registers the callback and 
how it continues unwinding.

If you were interested in adding this to llvm as some new EH intrinsics, I 
wouldn't be opposed to that.  It's basically a form of sjlj exceptions. 
It only works if values are not held in registers across throws though, 
which is kinda lame ...

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list