Skip to content

About

Optimizing compiler for Latte (Java-like language) to LLVM IR, written in Haskell: type checker, SSA, copy propagation, GCSE

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Latte → LLVM compiler

CI

An optimizing compiler for Latte, a small statically typed, Java-like language with classes, inheritance, virtual methods and arrays. Written in Haskell, it compiles Latte programs to LLVM IR through its own intermediate representation in SSA form, with copy propagation and global common-subexpression elimination.

I built it as a solo project for the Compiler Construction course (MRJP) at the University of Warsaw. The parser is generated from a grammar with BNFC. Everything after the AST is hand-written: type checking, IR generation, SSA construction, optimizations and LLVM code generation.

Pipeline

 source.lat
     │  BNFC-generated lexer + parser (src/Latte.cf)
     ▼
 AST ──► type checker ──────────────── FrontedLatte.hs
     │   scopes, classes and inheritance, method overriding, return-path analysis
     ▼
 AST ──► simplifier ────────────────── SimplifyLatte.hs
     │   constant folding of arithmetic, comparisons and boolean logic
     ▼
 Espresso IR (three-address code + CFG) ── GenerateEspresso.hs
     │   lazy && / ||, arrays, objects, method pointers
     ▼
 SSA form ──────────────────────────── EspressoToSSA.hs
     │   dominators → dominance frontiers → φ insertion → variable renaming
     │   copy propagation, global common-subexpression elimination
     ▼
 LLVM IR ───────────────────────────── GenerateLLVM.hs
     │   llvm-as + llvm-link with the C runtime (src/runtime.c)
     ▼
 source.bc  (run with `lli`, or compile to a native binary with `clang`)

The --graph flag also writes each function's control-flow graph after SSA construction to Graphviz (.dot / .png). This is handy for checking φ placement.

Language

class Shape {
  void describe() { printString("I'm a shape"); }
}

class Square extends Shape {
  void describe() { printString("I'm really a square"); }   // overrides Shape.describe
}

int main() {
  Shape s = new Square;
  s.describe();                       // dynamic dispatch → "I'm really a square"

  int[] a = new int[10];
  int i = 0;
  while (i < a.length) { a[i] = i * i; i++; }
  for (int x : a) printInt(x);
  return 0;
}

Supported features:

  • int, boolean and string types, string concatenation with +
  • if/else, while, for (T x : array), ++/--
  • functions and recursion; printInt, printString, readInt, readString, error are built in
  • arrays with .length
  • classes with fields and methods, single inheritance (extends), all methods virtual, null casts like (Node)null

The type checker rejects ill-typed programs with a position-annotated error. Examples include undeclared variables, wrong argument types, redefinitions, a missing return on some path, and void variables. See examples/bad/.

Implementation notes

  • SSA construction follows the classic Cytron et al. algorithm from the SSA Book: iterative dominator computation, immediate dominators, dominance frontiers, φ-function placement for every variable defined in more than one block, and renaming along the dominator tree.
  • Copy propagation and global CSE run on the SSA form. Every value has a single definition, so a value can safely replace another anywhere it is dominated.
  • Objects are heap-allocated (malloc + zeroing). Method pointers are stored in object slots and filled in at new by walking the inheritance chain, which gives overriding and dynamic dispatch.
  • Booleans in conditions compile to short-circuit control flow, not to eager evaluation.

Building

Tested on Ubuntu 24.04, natively and under WSL2, with GHC 9.4.7 and 9.14.1, BNFC 2.9.5, Alex 3.3, Happy 1.20 and Clang/LLVM 18:

sudo apt-get install ghc bnfc alex happy clang llvm make python3
make

This produces ./latc_llvm and lib/runtime.bc.

Usage

Run the compiler from the repository root, since it links against lib/runtime.bc:

./latc_llvm examples/good/core001.lat     # writes core001.ll and core001.bc
lli examples/good/core001.bc               # run the program
clang examples/good/core001.bc -o core001  # or build a native executable
./latc_llvm --graph file.lat               # also dump CFGs (needs Graphviz)

On success the compiler prints OK on stderr. On a parse or type error it prints ERROR followed by the message and exits with a non-zero status.

Tests

python3 test.py

The test runner compiles every program in examples/good and examples/extensions, links it, runs it (feeding .input if one exists) and compares stdout with the expected .output. It also checks that every program in examples/bad is rejected. That is 99 programs in total: 57 valid and 42 invalid. CI runs the full suite on every push.

Repository layout

Path Contents
src/Latte.cf Grammar (BNFC)
src/FrontedLatte.hs Type checker
src/SimplifyLatte.hs Constant folding / AST simplification
src/AbsEspresso.hs, src/GenerateEspresso.hs IR definition and AST → IR translation
src/EspressoToSSA.hs CFG, dominators, SSA construction, optimizations
src/GenerateLLVM.hs IR → LLVM IR
src/GenerateDot.hs, src/PrettyPrintEspresso.hs CFG visualisation / IR pretty-printing
src/RunLatte.hs Compiler driver
src/runtime.c Runtime library (I/O, string concatenation, error)
examples/ Test programs: good/, bad/, extensions/

About

Optimizing compiler for Latte (Java-like language) to LLVM IR, written in Haskell: type checker, SSA, copy propagation, GCSE

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages