This project is an experiment in compiling a subset of the ML language (or more properly OCaml). The project follows the structure of the mincaml compiler. However, instead of directly emiting assembler, we use the OCaml LLVM bindings to emit LLVM IR. These bindings don't need to have the code in SSA form, meaning that our intermediate language used inside the compiler does not need to be in kNormal form.
On Ubuntu or Debian, if you don't have OCaml and OPAM yet, you can do
sudo add-apt-repository ppa:avsm/ppa
sudo apt-get update
sudo apt-get install curl build-essential m4 ocaml opam
or use your system's package manager and install its version of OCaml and OPAM.
Install OPAM
opam init
eval `opam config env`
Install Menhir
opam install menhir
On Ubuntu or Debian:
sudo apt-get install llvm-3.7 llvm-3.7-dev llvm-3.7-runtime
opam install llvm
The most complicated part of the compiler is the typed closure conversion.
The compiler is structured as follows:
lexer.mll
- The lexer (use ocamllex to generatelexer.ml
)paser.mly
- The parser (use menhir to generateparser.ml
)type.ml
- Defines the types supported in minimlid.ml
- Functions for working with identifierssyntax.ml
- Defines the AST for minimlenv.ml
- Defines the environment data structures.ml
- Defines a set data structuretyping.ml
- Performs type inferenceinter.ml
- Defines an intermediate language, and converts from ASTclosure.ml
- Performs closure conversionprettyprint.ml
- Pretty printing routinescompile.ml
- Emit's LLVM IRminiml.ml
- The main program driver
There is a small collection of sample programs written in miniml:
You can compile these programs by doing make progname.exe
. For
example, to compile fib.mml
into an exectuable do:
make fib.exe
You can then run fib.exe
via
./fib.exe