views:

924

answers:

3

Let's assume for the moment that C++ is not a functional programming language. If you want to write a compiler using LLVM for the back-end, and you want to use a functional programming language and its bindings to LLVM to do your work, you have two choices as far as I know: Objective Caml and Haskell. If there are others, then I'd like to know about those too.

I'm not asking for subjective opinions, so please don't give this the subjective tag. I want to make up my own mind about this, but I'm not sure I know what are all the trade-offs. So, StackOverflow to the rescue. What are the trade-offs?

+4  A: 

Either OCaml or Haskell would be a good choice. Why not check out the LLVM tutorials for each language? The LLVM tutorial for OCaml is here: http://llvm.org/docs/tutorial/OCamlLangImpl1.html

Haskell has more momentum these days, but there are plenty of good parsing libraries for OCaml as well including the PEG parser generator Aurochs, Menhir, and the GLR parser generator Dypgen. Also check out this presentation on pcl a monadic parser combinator library for OCaml (like Parsec for Haskell) there's some good info in there comparing Haskell's and OCaml's approach: http://osp.janestreet.com/files/pcl.pdf

Some will say that laziness gives Haskell the edge in parsing, but you can get laziness in OCaml as well.

aneccodeal
Is there a Haskell/LLVM tutorial somewhere?
james woodyatt
Oh, and the monadic parser combinator in my OCNAE Cf library is my preferred parsing solution in OCaml. I looked at Parsec and was disappointed. If I go with Haskell, I'll probably port my Cf library over to it before I do anything else.
james woodyatt
@james http://augustss.blogspot.com/ has a long series of posts on the LLVM bindings, including examples. There's several other users of the LLVM bindings (a prototype GHC backend, and a lambda calculus compiler, http://blog.finiteimprobability.com/2009/11/17/a-compiler-for-lambda-calculus-to-llvm-part-1/
Don Stewart
@dons thanks! that's the kind of information i was hoping to elicit!
james woodyatt
+2  A: 

Haskell has higher level bindings to LLVM than OCaml (the Haskell ones provide some interesting type safety guarantees) and Haskell has by far more libraries to use (1700 packages on http://hackage.haskell.org) making it easier to glue together components.

Don Stewart
"...some interesting type safety guarantees..."I'd like to know more about that.
james woodyatt
http://augustss.blogspot.com/2009/01/llvm-llvm-low-level-virtual-machine-is.html has an overview.
Don Stewart
+3  A: 

OCaml is the only functional language with bindings in the LLVM distro itself and documentation on llvm.org such as the Kaleidoscope tutorial. If you have OCaml installed when you build and install LLVM then it will automatically build and install the LLVM bindings for OCaml as well. Moreover, these OCaml bindings have been in use for years so they are mature and reliable.

I have been developing HLVM in OCaml using the standard LLVM bindings and found OCaml+LLVM to be an extremely powerful combination. HLVM provides tuples, arrays, unions, TCO of all tail calls, generic printing, FFI to C, JIT compilation and parallel garbage collection with a VM weighing in at under 2kLOC of OCaml code that took only a few man-weeks to develop from scratch. HLVM's numerical performance already far exceeds that of today's fastest open source FPLs including OCaml itself. I have published articles in the OCaml Journal describing how LLVM can be used from OCaml for everything from basic expression evaluation to advanced topics such as parallelism and garbage collection. You may also like this mini example.

Jon Harrop
But OCaml is not the only functional language with bindings to LLVM. Haskell can be compiled to LLVM (http://www.cs.uu.nl/wiki/bin/view/Stc/CompilingHaskellToLLVM). OCaml bindings just happen to be shipped as an example.
jetxee
@jetxee: No, the OCaml bindings are not just shipped as an example. They are used in industrial LLVM-based projects to solve real problems. AFAIK, the Haskell bindings are incomplete, immature and nothing of any significance has ever been done with them. The Haskell->LLVM compiler you cite actually uses a textual interface instead of binary bindings to generate LLVM IR.
Jon Harrop