Mini-C Compiler
In this project I designed and implemented a compiler for a subset of the C programming language.
I utilised the ANTLR parsing tool to write a g4 file to parse the input source file into an AST and perform syntactic checking, built an AST traversal program to perform semantic checking and create symbol tables, and used the LLVM C++ bindings to produce correct and fast LLVM IR.
On top of this compiler, I added further optimizations such as Constant Folding, Constant Propagation, Variable Folding, Global Variable Cleaning, Loop Unrolling and Mem2Reg promotion.
These optimization methods clean up all possible constant values in a module, including global scope values. This was done using an additional function pass and a module pass. These passes perform folding on binary and unary operations and replace values, while deleting stores and loads where unnecessary. To ensure no undesirable side effects are caused, instructions that store values into global variables are not removed as those could theoretically be used elsewhere in the module. Thus came the purpose of the module pass, which checked on the usage of the global variables, and if they were constant then forwarded their values in place of where they were used. Additionally, if they were never loaded again but still had values being stored into them, these were removed.