Kaleidoscope

Toy Programming language & optimizing JIT Compiler built with LLVM compiler infrastructure and C++

Stars
3
Committers
1

Kaleidoscope 🔭

This is a toy programming language and its compiler made with LLVM and C++. I have implemented all of the frontend components the language needs:

  • Lexing
  • Abstract Syntax Tree (AST)
  • Parsing

Along with the code the generation to generate the LLVM IR (LLVM Intermediate Representation). Also I added a JIT Driver so I can see the IR generated, and the evaluated results.

And Finally I Added additional Optimizers to the compiler such as:

Optimization Example

Before adding optimization

This screenshot shows the corresponding LLVM IR for the function I defined, the function basically computes $(x + 3)^2$, but notice in the IR that the compiler instead of calculating $(x + 3)$ once then squaring the result, what it actually does is computing it twice, the first time it stores it in addtmp and the other in addtmp1:

After adding optimization

This screenshot shows the IR for the same function definition from earlier after I added the Common Subexpression Elimination optimization:

Note

This is just something I made to teach myself more about compilers in general and the LLVM compiler infrastructure in particular, so I didn't follow software engineering best practices and its not production ready.