CSC
is a simple and easy extensible C Subset Compiler. It's not realy for production, it was made JUST FOR FUN.
It contains lexer, parser, llvm-ir-codegenerator and SSA-form-builder.
CSC
uses a number of open source projects to work properly:
- LLVM - Framework for building compilers and codegenerators.
And of course CSC
itself is open source with a public repository
on GitHub.
CSC
requires LLVM v5.0+ to run.
Install the dependencies and build compiler.
$ sudo apt-get install llvm
$ sudo apt-get install clang
$ git clone https://github.com/masyagin1998/CSC.git
$ cd CSC
$ make all # to make compiler
$ make test # to check compiler's work
CSC
supports simplified version of C89
with:
int
variables definitions;while
loops;if-then
andif-then-else
statements;- inner
if
andwhile
statements (multiple inner statements) - all arithmetical and logical operations;
- and many other;
Linear Object-Oriented lexer with error recovery and comments ignoring.
Recursive top-down parser (I know it is shitty, but OK for small projects) for simplified to LL(1)
C grammar, which turns lexemas to Abstract Syntax Tree (AST
). AST can be debugged to file using in human-readable format.
Codegenerator to LLVM IR BitCode
from abstract syntax tree. Yon can link generated object files (*.o
files) with main.c
Classical code-generation algorithm, which firstly creates Control Flow Graph (CFG
) from AST
, and then creates SSA
-form using Dominance Frontier Algorithm (DFA
).
Anatoly Sinyavin - BMSTU Optimal Code Generation teacher