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

Gordon Henriksen
Mon, 26 Nov 2007 19:30:15 -0000

On Nov 26, 2007, at 13:27, Basile STARYNKEVITCH wrote:

> As some might probably know, the LLVM compiler http://llvm.org/ has
> (at least in its latest SVN snapshot) a binding for Ocaml. This
> means that one could code in Ocaml some stuff (eg a JIT-ing
> compiler) which uses (and links with) LLVM libraries.

Yep! There are no bindings for the JIT (just for codegen), but it has
a fairly narrow interface, so adding them would be fairly
straightforward.

> So is there any code to fit the Ocaml GC requirements into LLVM
> abilities, ie to use LLVM to generate (eg JIT) code which respect
> Ocaml GC requirements.

Included in the pending LLVM garbage collection code generation
changeset is an Ocaml frametable emitter. See here:

     http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-November/
011473.html

In a JIT context--which I am not presently pursing--it would also be
possible to generate these tables in memory and spool the records into
an active runtime. This is precisely what the ocaml startup code does
and it would be straightforward to invoke that machinery at runtime.

It might be exciting to have an Ocaml with "exec" (surely it would
allow new classes of programs), but static compilation seems clearly
superior for existing programs, so my focus is there for now.

> Of course, I do know that there are some typing issues and
> theoritical points which I deliberately ignore here. I'm supposing
> the guy wanting to LLVM for Ocaml is knowing that he seeks trouble.

The ocaml type system is easily represented in LLVM. The only real
mismatches I'm aware of are:

  1. The ocaml exception model is quite unique; emulating it seems
unlikely. DWARF exceptions are a suitable but incompatible replacement.
  2. ocaml's placement of symbols in global variables, described here
<http://caml.inria.fr/mantis/view.php?id=4414
 >.

The latter was easily resolved on the ocaml side, but with a trivially
incompatible change. Perhaps it will be incorporated for 3.11.

— Gordon