diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index b63b560c..c47cfa73 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -25,12 +25,12 @@ fn align_up(value: u64, align: u64) -> u64 { } #[derive(Debug, PartialEq, Eq)] -struct LinReg(Reg, Rc>); +struct LinReg(Reg, Option>>); #[cfg(debug_assertions)] impl Drop for LinReg { fn drop(&mut self) { - self.1.borrow_mut().free(self.0); + self.1.take().map(|b| b.borrow_mut().free(self.0)); } } @@ -677,7 +677,7 @@ impl<'a> Codegen<'a> { } fn alloc_reg(&mut self) -> LinReg { - LinReg(self.gpa.borrow_mut().allocate(), self.gpa.clone()).into() + LinReg(self.gpa.borrow_mut().allocate(), Some(self.gpa.clone())).into() } fn alloc_stack(&mut self, size: u64) -> Rc { @@ -1230,7 +1230,15 @@ impl<'a> Codegen<'a> { } let lsize = self.size_of(left.ty); - let lhs = self.loc_to_reg(left.loc, lsize); + let ty = ctx.ty().unwrap_or(left.ty); + let lhs = match std::mem::take(&mut ctx).loc() { + Some(Loc::RegRef(reg)) if Loc::RegRef(reg) == left.loc => LinReg(reg, None), + Some(loc) => { + ctx = Ctx::Dest(Value { ty, loc }); + self.loc_to_reg(left.loc, lsize) + } + None => self.loc_to_reg(left.loc, lsize), + }; let right = self.expr_ctx(right, Ctx::Inferred(left.ty))?; let rsize = self.size_of(right.ty); let rhs = self.loc_to_reg(right.loc, rsize); diff --git a/hblang/tests/codegen_tests_fb_driver.txt b/hblang/tests/codegen_tests_fb_driver.txt index 83862c7c..fe4ed80a 100644 --- a/hblang/tests/codegen_tests_fb_driver.txt +++ b/hblang/tests/codegen_tests_fb_driver.txt @@ -1,3 +1,3 @@ -code size: 678 +code size: 666 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index 190c4f1c..fb06ea0e 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -1,3 +1,3 @@ -code size: 320 -ret: 33 +code size: 314 +ret: 44 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index cda3ca78..5bb23a01 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -1,3 +1,3 @@ -code size: 293 -ret: 55 +code size: 287 +ret: 16 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 23eb54cd..5edce62d 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -1,3 +1,3 @@ -code size: 296 +code size: 290 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_variables.txt b/hblang/tests/codegen_tests_variables.txt index eac00322..4e788d60 100644 --- a/hblang/tests/codegen_tests_variables.txt +++ b/hblang/tests/codegen_tests_variables.txt @@ -1,3 +1,3 @@ -code size: 130 +code size: 124 ret: 0 status: Ok(())