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:
latcandlatc_llvmrunsrc/compile.prologlatc_checkrunssrc/check_latte.prologlatc_astrunssrc/show_parse.prologlatc_tokensrunssrc/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 phis to implement
if and while statements. It will detect which variables are changing
to avoid creating unnecessary phis.
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*