This is a compiler written in Prolog using DCG for the language Latte.
Running
> ./latc path/to/source.lat
for correct programs will produce path/to/source.prelink.ll
, path/to/source.ll
and path/to/source.bc
.
Running
> ./latc_llvm path/to/source.lat
for correct programs will print the prelink LLVM code.
Running
> ./latc_check path/to/source.lat
will return "OK" for accepted programs and "ERROR" with additional errors for all others.
> ./latc_ast path/to/source.lat
> ./latc_tokens path/to/source.lat
The executables are just wrappers for swi-prolog running the scripts prolog scripts:
latc
andlatc_llvm
runsrc/compile.prolog
latc_check
runssrc/check_latte.prolog
latc_ast
runssrc/show_parse.prolog
latc_tokens
runssrc/show_tokens.prolog
The main stages of compilation:
- Tokenization (
src/tokenize.prolog
) - Building Latte AST (
src/parse.prolog
) - Type derivation and error checks (
src/typing_and_checks.prolog
) - Generating LLVM AST (
src/llvm.prolog
) - Optimization of LLVM AST (
src/llvm_opts.prolog
) - Register numbering and printing LLVM (
src/llvm_print.prolog
)
Additionally, some helper modules:
- uniform error handling for all stages of compilation (
src/errors.prolog
) - a Context structure useful for tracking types and register allocations (
src/context.prolog
) - simplification of simple boolean expressions (
src/simplify.prolog
)
Some builtin functions are compiled from C, those can be found in runtime/lib.c
, and compiled by running make
.
In order to comply with SSA, the compiler keeps track of all constants and copy instructions, automatically avoiding unnecessary assignments and copies.
Examples: examples/opts/const*
and examples/opts/copy*
Instead of allocating variables, the compiler uses phi
s to implement
if
and while
statements. It will detect which variables are changing
to avoid creating unnecessary phi
s.
It will also try to clean up ones discovered to be redundant during optimization.
Examples: examples/opts/if*
and examples/opts/while*
The compiler will avoid recalculating instructions that don't have side effects.
Examples: examples/opts/gcse*