https://github.com/aabanakhtar/mini-c-compiler
LLVM based compiler for a subset of C
https://github.com/aabanakhtar/mini-c-compiler
c compilers ir lexer llvm parsing
Last synced: 2 months ago
JSON representation
LLVM based compiler for a subset of C
- Host: GitHub
- URL: https://github.com/aabanakhtar/mini-c-compiler
- Owner: aabanakhtar
- Created: 2025-06-25T15:10:25.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-09-21T02:14:09.000Z (9 months ago)
- Last Synced: 2025-10-04T07:23:17.354Z (9 months ago)
- Topics: c, compilers, ir, lexer, llvm, parsing
- Language: C++
- Homepage:
- Size: 219 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mini-c
### Watch an explanation by clicking down here
[](https://www.youtube.com/watch?v=rlQPR69gjhY)
Mini-C is a compiler for a restricted subset of the C programming language, written in C++ using LLVM's backend. It is primarily built to learn how the LLVM backend cooperates with a frontend.
Example Input:
```c
int add(int a, int b)
{
return a + b;
}
int main() {
return add(2, 3);
}
```
Corresponding Output:
```llvm
; ModuleID = 'main'
source_filename = "main"
define i32 @add(i32 %a, i32 %b) {
entry:
%a.addr = alloca i32, align 4
store i32 %a, ptr %a.addr, align 4
%b.addr = alloca i32, align 4
store i32 %b, ptr %b.addr, align 4
%loadedVal = load i32, ptr %a.addr, align 4
%loadedVal1 = load i32, ptr %b.addr, align 4
%0 = add i32 %loadedVal, %loadedVal1
ret i32 %0
}
define i32 @main() {
entry:
%callresult = call i32 @add(i32 2, i32 3)
ret i32 %callresult
}
```
## Features
- Simple lexer based on a large switch
- Recursive Descent Parsing using `std::variant`
- Semantic Analysis using recursive descent
- Supports core C language features:
- `int` type
- `while`, `if`, and `else` statements
- Function declarations of type `int` and `void`
## Requirements
- C++20 compiler (tested with GCC and Clang)
- LLVM (version 15 or newer recommended)
- CMake
## Building
First, ensure LLVM is installed and available via `find_package`.
On Linux, you may need to install LLVM development packages, for example:
```sh
sudo apt-get install llvm-dev clang
```
Then, build the CMake Project
```sh
mkdir build
cd build
cmake -G ..
cmake --build .
```
Then, run the project:
```sh
./mini-c-compiler main.c
```
The project will spit out an llvm IR file, which you can compile using clang.