[LLVMdev] OCaml

Gordon Henriksen gordonhenriksen at mac.com
Sun Nov 25 04:23:42 PST 2007


On 2007-11-24, at 21:58, Jon Harrop wrote:

> I just took another look at the LLVM project and it has come along  
> in leaps and bounds since I last looked. I've been working through  
> the (awesome!) tutorial and am now really hyped about the project.


> I am particularly interested in using LLVM to write compilers for  
> OCaml-like languages in OCaml-like languages. This requires some  
> core functionality that would be generically useful:

Your first two points haven't gotten much treatment yet, so…

> - Garbage collection tuned for functional programming


I've been doing some interesting work on this front. Getting Lattner- 
cycles to have it reviewed and integrated is probably the biggest  
challenge; LLVM is a joy to work with even on major surgery like  
this. :)

The goal is that LLVM should be able to take care of the code  
generation aspects of GC while leaving the runtime open-ended. It  
would be nice to provide a GC runtime along with LLVM, but I'm not  
entirely certain how realistic that is given how intertwined GC is  
with the object model. All of this is thoroughly treated in the above  

Can you elaborate on what tuning you're looking for?

> - Exceptions


LLVM's exception support is tuned toward DWARF "zero-cost exceptions,"  
i.e. C++ exception handling. Anton Korobeynikov and Duncan Sands (who  
is working on Ada) are probably the experts in this area.

> - Some interface to LLVM from OCaml

> What is the easiest way to interface a front-end written in OCaml  
> with an LLVM backend?

The C and Ocaml bindings in the source tree are intended to cover  
precisely this scenario, and I would recommend them over .ll emission.  
Jan's remark is a bit out of date; the bindings are sufficient for  
code generation now. A few corners of the IR are still not fully  
covered, but extending the bindings to new methods is quite  

If ocamlc is on your path, then 'configure; make; make install' should  
install the bindings in your ocaml lib. To link with them, compile  
your program with:

     ocamlopt -cc g++

The LLVM libraries currently bound are:

     llvm.cmxa / .cma
     llvm_bitwriter.cmxa / .cma
     llvm_analysis.cmxa / .cma

Their .mli files and the corresponding llvm-c headers (coupled with an  
understanding of the C++ API) are presently the best reference.

> I just rediscovered the OCaml bindings in bindings/ocaml (...). They  
> do indeed look quite complete but I can't find any examples using  
> them.

See an example here, which an Ocaml program to emit the bitcode for a  
"hello world" program:


> I think a translation of the tutorial would be most welcome and  
> about 10x shorter. ;-)

Ah, maybe. Patches are welcome. :)

— Gordon


> the OCaml bindings in bindings/ocaml (rather than the ones in test/ 
> Bindings/OCaml!).

The latter directory contains tests of the former!

More information about the llvm-dev mailing list