oakc
oakc copied to clipboard
WIP: add initial mvp for optimizing programs
This PR:
- implements the
Display
trait for all of theMir*
structs/enums - adds basic optimization by evaluating constant expressions at compile time
- traverses entry point to find all call expressions recursively (checks a called function for call expressions) and removes a ll uncalled functions
It still needs work on implementing the Display
trait with readable indents and to work for nested conditional statements like for
, while
and if
statements.
I decided to get more familiar with rust and the codebase so I started exploring a possible implementation of optimizing the IR. I noticed a lot of work is done in MIR
so i decided to try and implement an MVP here. I have no idea if this is a good way of implementing it, but I think it shows some of the possiblities.
The current approach is limited to returning a similar type/enum of whatever is being optimized. This restricts things like if (true) { /* body */ }
to be optimized to inlining the body. Tough there should also be more clarity about scopes and identifiers before that can be implemented.
It will succesfully optimize the following program:
fn square() -> num {
return 2 * 2;
}
fn main() {
if (8 >= 3) {
let _ = 2 * 8;
} else {
let _ = 4 / 2;
}
}
into the following:
fn main() -> void {
if (true) {
let _ = 16;
} else {
let _ = 2;
}
}