mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Remove redundant require_mutable_this
This commit is contained in:
@@ -2,5 +2,6 @@ mod collapse_pointers_of_pointers;
|
||||
mod extract_constants;
|
||||
mod optimize;
|
||||
mod shake_tree;
|
||||
mod simplify;
|
||||
|
||||
pub use optimize::optimize;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
42
valuescript_compiler/src/optimization/simplify.rs
Normal file
42
valuescript_compiler/src/optimization/simplify.rs
Normal 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(..) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user