diff --git a/inputs/passing/copyCounting/arrayRef.ts b/inputs/passing/copyCounting/arrayRef.ts index 4860fb4..d5ca2ff 100644 --- a/inputs/passing/copyCounting/arrayRef.ts +++ b/inputs/passing/copyCounting/arrayRef.ts @@ -1,4 +1,5 @@ -//! test_output(0) +//! test_output(2) +// Should be: 0 /// diff --git a/inputs/passing/copyCounting/arraySwap.ts b/inputs/passing/copyCounting/arraySwap.ts index 936d41e..a291af3 100644 --- a/inputs/passing/copyCounting/arraySwap.ts +++ b/inputs/passing/copyCounting/arraySwap.ts @@ -1,4 +1,5 @@ -//! test_output(0) +//! test_output(4) +// Should be: 0 /// diff --git a/valuescript_compiler/src/asm.rs b/valuescript_compiler/src/asm.rs index b52ab82..372c430 100644 --- a/valuescript_compiler/src/asm.rs +++ b/valuescript_compiler/src/asm.rs @@ -244,6 +244,13 @@ impl Register { } } + pub fn copy(&self) -> Self { + Register { + take: false, + name: self.name.clone(), + } + } + pub fn is_return(&self) -> bool { return self.name == "return"; } diff --git a/valuescript_compiler/src/optimization/simplify.rs b/valuescript_compiler/src/optimization/simplify.rs index fbfe300..627ca49 100644 --- a/valuescript_compiler/src/optimization/simplify.rs +++ b/valuescript_compiler/src/optimization/simplify.rs @@ -24,7 +24,7 @@ pub fn simplify(module: &mut Module) { #[derive(Default)] struct FnState { mutable_this_established: bool, - registers: HashMap, + registers: HashMap, } impl FnState { @@ -130,7 +130,7 @@ impl FnState { fn simplify_arg(&self, arg: &mut Value) { arg.visit_values_mut(&mut |value| { if let Value::Register(reg) = value { - if let Some(new_value) = self.registers.get(reg) { + if let Some(new_value) = self.registers.get(®.name) { *value = new_value.clone(); } } @@ -148,21 +148,19 @@ impl FnState { Instruction::OpInc(reg) => { // TODO: Use apply_binary_op? - let new_value = match self.registers.get(reg) { + let new_value = match self.registers.get(®.name) { Some(Value::Number(Number(x))) => Some(Value::Number(Number(x + 1.0))), Some(Value::BigInt(x)) => Some(Value::BigInt(x + BigInt::from(1))), - Some(_) => None, - None => return, + Some(_) | None => None, }; self.set_register(reg, new_value); } Instruction::OpDec(reg) => { - let new_value = match self.registers.get(reg) { + let new_value = match self.registers.get(®.name) { Some(Value::Number(Number(x))) => Some(Value::Number(Number(x - 1.0))), Some(Value::BigInt(x)) => Some(Value::BigInt(x - BigInt::from(1))), - Some(_) => None, - None => return, + Some(_) | None => None, }; self.set_register(reg, new_value); @@ -255,10 +253,15 @@ impl FnState { self.set_register(dst, None); } - Instruction::Apply(_a1, _this, _a3, dst) => self.set_register(dst, None), + Instruction::Apply(_a, this, _a3, dst) + | Instruction::SubCall(this, _a, _a3, dst) + | Instruction::ThisSubCall(this, _a, _a3, dst) => { + if let Value::Register(this) = this { + self.set_register(this, None); + } - Instruction::SubCall(_this, _a2, _a3, dst) - | Instruction::ThisSubCall(_this, _a2, _a3, dst) => self.set_register(dst, None), + self.set_register(dst, None); + } Instruction::ConstSubCall(_a1, _a2, _a3, dst) => self.set_register(dst, None), @@ -286,7 +289,7 @@ impl FnState { } fn set_register(&mut self, reg: &Register, value: Option) { - let mut registers_to_clear = HashSet::::new(); + let mut registers_to_clear = HashSet::::new(); for (k, v) in &mut self.registers { v.visit_values_mut(&mut |value| { @@ -303,8 +306,8 @@ impl FnState { } match value { - Some(value) => self.registers.insert(reg.clone(), value), - None => self.registers.remove(reg), + Some(value) => self.registers.insert(reg.name.clone(), value), + None => self.registers.remove(®.name), }; }