[LLVMdev] New HLVM examples

Jon Harrop jon at ffconsultancy.com
Wed Jun 24 15:48:59 PDT 2009


HLVM is a garbage collected virtual machine built upon LLVM:

  http://forge.ocamlcore.org/projects/hlvm/

The development of HLVM was described in a series of three OCaml Journal 
articles that turned out to be among our most popular. Consequently, we have 
decided to run another series of related articles that build upon this 
foundation in order to develop complete compilers. The first article 
describes a compiler written in only 228 lines of code that can execute 
interesting programs:

  http://ocamlnews.blogspot.com/2009/06/compiler-development-part-1.html

For example, the compiler described in the article can execute the following 
OCaml-like program to print the Mandelbrot set:

# let rec pixel((n, zr, zi, cr, ci) : int * float * float * float * float) :
unit =
    if n = 65536 then print_char ' ' else
      if zr * zr + zi * zi >= 4.0 then print_char '.' else
        pixel(n+1, zr * zr - zi * zi + cr, 2.0 * zr * zi + ci, cr, ci);;
# let rec row((i, j, n) : int * int * int) : unit =
    if i>n then () else
      begin
        let cr = 2.0 * float_of_int i / float_of_int n - 1.5 in
        let ci = 2.0 * float_of_int j / float_of_int n - 1.0 in
        pixel(0, 0.0, 0.0, cr, ci);
        row(i+1, j, n)
      end;;
# let rec col((j, n) : int * int) : unit =
    if j>n then () else
      begin
        row(0, j, n);
        print_char '\n';
        col(j+1, n)
      end;;
# let rec mandelbrot(n : int) : unit =
    col(0, n);;
# mandelbrot 77;;

In particular, our compiler runs this program interactively 50x faster than
the OCaml top-level and 60% faster than native-code compiled OCaml!

The complete source code of HLVM and the example programs including this 
compiler are freely available under a BSD license and may be downloaded from 
HLVM's SVN repository as follows:

  svn checkout svn://svn.forge.ocamlcore.org/svnroot/hlvm

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e




More information about the llvm-dev mailing list