Skip to content

A minimal compiler written in OCaml that compiles Tiger to LLVM IR then to assembly code

Notifications You must be signed in to change notification settings

anhnguyen1618/Tiger-ocaml-llvm-compiler

Repository files navigation

LLVM Compiler for Tiger programming language

Build Status

A minimal compiler written in OCaml that compiles Tiger to LLVM IR and subsequently to target-dependent assembly code

Tiger programming language

Link to manual

Sample Tiger code:

let
  function fib (n: int): int =
    if n = 0 then 0
    else if n = 1 then 1
    else fib(n-1) + fib(n-2)

  type node = {data: my_int, next: node}
  type my_int = int
  var a := node {data = 3, next = nil}
  var b := node {data = 2, next = a}

  type intFun = int -> int
  type intIntFun = int -> intFun
  var f := (a: int): intIntFun =>
              (b: int): intFun =>
                (c: int): int =>  a + b + c 
in
  assert_int(fib (0), 0);
  assert_int(fib (30), 832040);
  assert_int(b.next.data, 3);
  assert_int(f(1)(2)(3), 6)
end

Technologies

Required software

  • llvm-6.0-dev
  • opam-1.2.2
  • ocaml-4.05.0
  • python-2.7
  • ocamlbuild
  • ocamlfind
  • llvm (opam package)

See .travis.yml for more details

Usage

After cloning this repo, please create new output directory /llvm_byte_code/test/ with the command:

mkdir llvm_byte_code/test

Tiger test file <test_file_name>.tig must be placed directly under directory ./test

Compile file test/<test_file_name>.tig without executing the output binary run_prog:

make parse -f=<test_file_name>.tig

This command generates the binary executable file run_prog in the current directory. This file can be executed with the command:

./run_prog

To compile and run test on Tiger file test/<test_file_name>.tig in one command:

python test.py -s <name>

Compile and run all Tiger test files in directory ./test:

python test.py

or

./compile

The LLVM IR results and assembly code emitted by compiling file test/<test_file_name>.tig can be found in directory /llvm_byte_code/test/:

  * <test_file_name>.tig.ll               # LLVM IR code
  * <test_file_name>.tig-opt.ll           # Optimized IR codes
  * <test_file_name>.tig-opt.s            # Assembly code

About

A minimal compiler written in OCaml that compiles Tiger to LLVM IR then to assembly code

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published