Fang - A compiler for Tiger

This is version 1.2.1, which is released under the terms of the GPL-3.0 license.

Fang is copyright 2021 by Jesse Haber-Kucharsky.

Summary

Fang is a self-contained compiler for Tiger (a Pascal-like language with some additional features) which targets x86-64.

let program = {| for i := 1 to 10 do (print_int(i); print_line()) |} in
let lexbuf = Lexing.from_string program in
(* Assume no compilation errors! *)
let pp = Fang.compile_to_asm lexbuf |> Result.get_ok in
Fmt.pr "%a@." pp ()
    .intel_syntax noprefix
    .global fang

fang:
    push r12
    push rbx
    mov rbx, 1
    mov r12, 10
    cmp rbx, r12
    jg .L2
.L0:
    mov rdi, rbx
    call fang_io_print_int
    call fang_io_print_line
    cmp rbx, r12
    jge .L2
    inc rbx
    jmp .L0
.L2:
    mov rax, 0
    pop rbx
    pop r12
    ret

You may wish to consult Fang's homepage for more discussion of the project's goals, scope, and implementation.

The Tiger language and the Fang compilation process are from Appel's book [1].

Libraries

Fang's implementation is divided among several libraries, but in most cases only the top-level library will be of interest.

High-level libraries

The Tiger language

The intermediate representation (IR)

Translation from Tiger to IR

Flow graphs and register allocation

Support libraries

References

Other Fang libraries note references where they're applicable.

[1] A. Appel, Modern compiler implementation in ML, 2nd ed. Cambridge: Cambridge University Press, 1999.