Remove redundant require_mutable_this

This commit is contained in:
Andrew Morris
2023-06-29 16:01:19 +10:00
parent 76995eaf2d
commit 4d7e9a2e5c
3 changed files with 45 additions and 0 deletions

View File

@@ -2,5 +2,6 @@ mod collapse_pointers_of_pointers;
mod extract_constants;
mod optimize;
mod shake_tree;
mod simplify;
pub use optimize::optimize;

View File

@@ -4,9 +4,11 @@ use crate::name_allocator::NameAllocator;
use super::collapse_pointers_of_pointers::collapse_pointers_of_pointers;
use super::extract_constants::extract_constants;
use super::shake_tree::shake_tree;
use super::simplify::simplify;
pub fn optimize(module: &mut Module, pointer_allocator: &mut NameAllocator) {
collapse_pointers_of_pointers(module);
extract_constants(module, pointer_allocator);
shake_tree(module);
simplify(module);
}

View File

@@ -0,0 +1,42 @@
use crate::asm::{DefinitionContent, FnLine, Function, Instruction, Module};
pub fn simplify(module: &mut Module) {
for defn in &mut module.definitions {
match &mut defn.content {
DefinitionContent::Function(fn_) => simplify_fn(FnState::default(), fn_),
DefinitionContent::Class(_) => {}
DefinitionContent::Value(_) => {}
DefinitionContent::Lazy(_) => {}
}
}
}
#[derive(Default)]
struct FnState {
mutable_this_established: bool,
// registers: HashMap<Register, Value>,
}
impl FnState {
fn clear(&mut self) {
*self = Self::default();
}
}
fn simplify_fn(mut state: FnState, fn_: &mut Function) {
for line in &mut fn_.body {
match line {
FnLine::Instruction(instr) => {
if let Instruction::RequireMutableThis = instr {
if state.mutable_this_established {
*line = FnLine::Empty;
} else {
state.mutable_this_established = true;
}
}
}
FnLine::Label(..) => state.clear(),
FnLine::Empty | FnLine::Comment(..) => {}
}
}
}