An open API service indexing awesome lists of open source software.

https://github.com/clflushopt/compiler-examples

An implementation oriented cookbook for compiler writers.
https://github.com/clflushopt/compiler-examples

Last synced: 8 months ago
JSON representation

An implementation oriented cookbook for compiler writers.

Awesome Lists containing this project

README

          

# Compiler Examples

This repo contains various reference implementations of compiler related data
structures and algorithms.

Most of the following was initially done while working through Cornell's CS6120
[course](https://www.cs.cornell.edu/courses/cs6120/2023fa/self-guided/) from
the course assignments, reading papers, LLVM's codebase and various books.

The objective is to create a sort of cookbook of reference implementations that
focus on clarity, simplicity and hopefully correctness.

The repository contains a complete implementation of the Bril intermediate
language with several analyses, optimizations and various compiler related
data structures e.g control flow graphs, SSA representation, dominance graphs...

I hope you enjoy hacking on it as much as I enjoyed writing.

## Usage

The repository is designed to allow you to hack on things as much as possible.

The core implementation revolves around [`bril.ir`](bril/core/ir.py) where most
of the core intermediate language is implemented, the [tools](bril/tools/) directory
contains small utilities that can be used to parse and manipulate Bril programs.

For testing, a custom built tool inspired by `llvm-lit` called [`turnstile`](turnstile/turnstile.py)
is provided. `turnstile` runs snapshot testing of input vs outputs, it's both a
testing framework and a library that can be used to write `expect` style tests.

`turnstile` is how we test all program transformations especially when it comes
to optimization passes and so on.

If you don't like `turnstile` which is just a lighter, basic version of `turnt`
then you can use `turnt` instead which supports configuration via TOML and better
CLI integration.

## Recipes

This is a non-exhaustive list of things that are currently implemented :

- Various mini tools to work with the IR in [tools](bril/tools/) folder.
- Instructions and Basic Blocks see [ir.py](bril/core/ir.py)
- Control Flow Graph construction, predecessors and successors computation.. see [cfg.py](bril/core/cfg.py)
- Scalar optimizations like dead code elimination, redundant store elimination, local value numbering
with constant propagation and constant folding see [transform.py](bril/core/transform.py).
- Program analyses (without the data flow framework) like liveness analysis and available expressions see [analyses.py](bril/core/analyses.py) .
- Data-flow analysis, with a data-flow framework see [df.py](bril/core/df.py)

## References

* [CS6120: Advanced Compilers](https://www.cs.cornell.edu/courses/cs6120/2023fa/self-guided/)
* [LLVM Programmer's Manual](https://llvm.org/docs/ProgrammersManual.html)
* [Engineering a Compiler 3rd Edition](https://www.sciencedirect.com/book/9780128154120/engineering-a-compiler)
* [CSC255/455 Software Analysis and Improvement](https://www.cs.rochester.edu/~sree/courses/csc-255-455/spring-2020/schedule.html)
* [Data flow analysis: an informal introduction](https://clang.llvm.org/docs/DataFlowAnalysisIntro.html)
* [Notes on Graph Algorithms in Optimizing Compilers](https://www.cs.umb.edu/~offner/files/flow_graph.pdf)
* [SSA-based Compiler Design](https://link.springer.com/book/10.1007/978-3-030-80515-9)