With type checking done, it’s time to generate code from the parse tree. Code generation typically consists of the following steps:
1. Generate intermediate code.
2. Generate the machine or byte code from the intermediate code.
The following are options for doing the above two tasks:
- Generate intermediate and machine code for x86 or RISC using our own methods (tedious and error prone).
- Generate the intermediate code and then byte code for either the JVM or CLI.
- Outsource the problem to LLVM.
I’m a lazy fellow so I pick choice #3 of outsourcing the code generation to LLVM. This is a particularly apt choice given the exciting intersection of Haskell with LLVM recently via David Terei’s honors thesis on using LLVM as the backend for the GHC compiler. This work is ongoing at
http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/Backends/LLVM
and for an explanation of why LLVM is used as the backend see
http://blog.llvm.org/2010/05/glasgow-haskell-compiler-and-llvm.html
.
Bryan O’Sullivan put together bindings to LLVM at
http://code.haskell.org/llvm/
. Bryan and the other author, Lennart Augustsson, also posted examples of using the bindings at
http://www.serpentine.com/blog/2008/01/03/llvm-bindings-for-haskell/
,
http://augustss.blogspot.com/2009/01/llvm-llvm-low-level-virtual-machine-is.html
, and
http://augustss.blogspot.com/2009/01/llvm-arithmetic-so-we-want-to-compute-x.html
.
I’m reading up on LLVM and will post more as I learn more about LLVM in general and the Haskell LLVM bindings in particular.
LLVM is awesome and I love using it. Please keep posting updated on you use of LLVM.
Thanks Nadav. I intend to keep the series going on LLVM. It’s fascinating material.