https://github.com/williamfedele/crucible
🦀 Simple compiler written to learn about IR and optimization techniques
https://github.com/williamfedele/crucible
compiler compiler-design compiler-optimization rust rust-lang
Last synced: 6 months ago
JSON representation
🦀 Simple compiler written to learn about IR and optimization techniques
- Host: GitHub
- URL: https://github.com/williamfedele/crucible
- Owner: williamfedele
- License: mit
- Created: 2024-11-03T01:40:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-21T04:03:59.000Z (about 1 year ago)
- Last Synced: 2025-04-08T16:11:14.058Z (10 months ago)
- Topics: compiler, compiler-design, compiler-optimization, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
🦀 Crucible
A simple compiler written to learn about intermediate representation and optimization techniques.
This project is purely for experimenting while I was researching compiler optimization techniques.
```shell
cargo run
```
1. The source is lexed to get tokens.
2. Next the parser creates an abstract syntax tree.
3. The AST is lowered into a Static Single Assignment format (SSA) as intermediate representation (IR).
4. The IR is analyzed for optimizations. Optimizations implemented: dead code elimination, constant folding.
Example source:
```ts
let x: int = 3;
let unused: int = 0;
let y: int = x + 1;
let z: int = x * y / 2;
z = z + 1;
```
Initial intermediate representation:
```
[
Constant { result: "x.1", value: 3 },
Constant { result: "unused.1", value: 0 },
Binary { result: "y.1", op: Add, left: "x.1", right: "1" },
Binary { result: "bin.1", op: Multiply, left: "x.1", right: "y.1" },
Binary { result: "z.1", op: Divide, left: "bin.1", right: "2" },
Binary { result: "z.2", op: Add, left: "z.1", right: "1" }
]
```
Dead code eliminated (remove code that can't affect the program):
```
[
Constant { result: "x.1", value: 3 },
Binary { result: "y.1", op: Add, left: "x.1", right: "1" },
Binary { result: "bin.1", op: Multiply, left: "x.1", right: "y.1" },
Binary { result: "z.1", op: Divide, left: "bin.1", right: "2" }
]
```
Constant folding (identify and evaluate constant expressions at compile time):
```
[
Constant { result: "x.1", value: 3 },
Constant { result: "y.1", value: 4 },
Constant { result: "bin.1", value: 12 },
Constant { result: "z.1", value: 6 }
]
```