Main Routine

Next, look at MinCaml's main routine main.ml. Execution of the compiler begins from its bottom let () = ....

First, we process options by using OCaml's standard library Arg. Option -inline specifies the maximal size of functions to be inlined, and -iter defines the maximal number of optimizations to be iterated. (Details will be explained later.)

The other command line arguments are considered as the names of programs to be compiled. Function Main.file generates SPARC assembly "program-name.s" from ML source "program-name.ml".

For debugging and experiments, function Main.string is also provided which compiles a string argument and displays the result on standard output.

The core of main.ml is function Main.lexbuf. Given a buffer argument, it applies in this order: lexical analysis (Lexer.token), parsing (Parser.exp), type inference (Typing.f), K-normalization (KNormal.f), α-conversion (Alpha.f), optimizations (iter), closure conversion (Closure.f), virtual machine code generation (Virtual.f), SPARC's 13-bit immediate optimization (Simm13.f), register allocation (RegAlloc.f), and assembly generation (Emit.f).

The optimization function iter repeats the following five optimizations until its result stops changing or the number of iterations reaches the upper bound specified by -iter: β-reduction (Beta.f), reduction of nested let (Assoc.f), inline expansion (Inline.f), constant folding (ConstFold.f), and elimination of unnecessary definitions (Elim.f).

We will soon explain details of these translations and optimizations.

Next