Fang - A compiler for Tiger
This is version 1.3.0, 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
Fang
- Start here! High-level functionality for compiling Tiger into assembly codeFang_x64
- All Fang functionality specialized to the x86-64 architecture
The Tiger language
Fang_tiger
- All things Tiger: parsing, type-checking, static analysis, and pretty-printing
The intermediate representation (IR)
Fang_ir
- The IR language definition, pretty-printing, rewriting, and in-memory storage
Translation from Tiger to IR
Fang_translate
- Platform-agnostic translation from Tiger to IRFang_frame
- Architecture abstraction layer for translation
Flow graphs and register allocation
Fang_flow
- Create arbitrary flow graphs and perform analysis on themFang_alloc
- Register allocation (assigning processor registers to boxes)
Support libraries
Fang_asm
- Assembly instructions, including printing and inspectionFang_vm
- Abstractions for program labels and temporary storage locations ("boxes")Fang_iter
- Fast iterators for data structuresFang_monads
- Monads that we know and loveFang_cmake
- Tools for interacting with CMake (used byfangu
)
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.