From 4502a645140354b365b39f38d3f56249ee776ae3 Mon Sep 17 00:00:00 2001 From: mlokr Date: Thu, 16 May 2024 13:32:04 +0200 Subject: [PATCH] some cleanup and bug fix --- hblang/src/codegen.rs | 23 +++++++++---------- hblang/tests/codegen_tests_arithmetic.txt | 2 +- .../tests/codegen_tests_different_types.txt | 2 +- hblang/tests/codegen_tests_fb_driver.txt | 2 +- hblang/tests/codegen_tests_functions.txt | 2 +- hblang/tests/codegen_tests_if_statements.txt | 2 +- hblang/tests/codegen_tests_loops.txt | 2 +- hblang/tests/codegen_tests_pointers.txt | 2 +- hblang/tests/codegen_tests_variables.txt | 2 +- 9 files changed, 19 insertions(+), 20 deletions(-) diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index e86f2cc..cd7c463 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -1248,13 +1248,13 @@ impl<'a> Codegen<'a> { let signed = bt::is_signed(ty); if let Loc::Imm(mut imm) = right.loc - && let Some(oper) = Self::imm_math_op(op, signed, size, false) + && let Some(oper) = Self::imm_math_op(op, signed, size) { - if matches!(op, T::Add | T::Sub) { - if bt::is_pointer(left.ty) { - let size = self.size_of(self.pointers[ty as usize]); - imm *= size; - } + if matches!(op, T::Add | T::Sub) + && let TypeKind::Pointer(ty) = TypeKind::from_ty(ty) + { + let size = self.size_of(self.pointers[ty as usize]); + imm *= size; } self.code.encode(oper(lhs.0, lhs.0, imm)); @@ -1381,7 +1381,6 @@ impl<'a> Codegen<'a> { op: T, signed: bool, size: u64, - first_imm: bool, ) -> Option (usize, [u8; instrs::MAX_SIZE])> { use instrs as i; @@ -1402,14 +1401,14 @@ impl<'a> Codegen<'a> { let ops = match op { T::Add => basic_op!(addi8, addi16, addi32, addi64), - T::Sub if !first_imm => sub_op!(addi8, addi16, addi32, addi64), + T::Sub => sub_op!(addi8, addi16, addi32, addi64), T::Mul => basic_op!(muli8, muli16, muli32, muli64), T::Band => return Some(i::andi), T::Bor => return Some(i::ori), T::Xor => return Some(i::xori), - T::Shr if signed && !first_imm => basic_op!(srui8, srui16, srui32, srui64), - T::Shr if !first_imm => basic_op!(srui8, srui16, srui32, srui64), - T::Shl if !first_imm => basic_op!(slui8, slui16, slui32, slui64), + T::Shr if signed => basic_op!(srui8, srui16, srui32, srui64), + T::Shr => basic_op!(srui8, srui16, srui32, srui64), + T::Shl => basic_op!(slui8, slui16, slui32, slui64), _ => return None, }; @@ -1442,7 +1441,7 @@ impl<'a> Codegen<'a> { let (lhs, owned) = self.loc_to_reg_ref(&left, size); if let Loc::Imm(imm) = right - && let Some(op) = Self::imm_math_op(op, signed, size, false) + && let Some(op) = Self::imm_math_op(op, signed, size) { self.code.encode(op(lhs, lhs, imm)); return if let Ctx::Dest(dest) = ctx { diff --git a/hblang/tests/codegen_tests_arithmetic.txt b/hblang/tests/codegen_tests_arithmetic.txt index 8906125..45bf88a 100644 --- a/hblang/tests/codegen_tests_arithmetic.txt +++ b/hblang/tests/codegen_tests_arithmetic.txt @@ -1,3 +1,3 @@ -code size: 209 +code size: 200 ret: 1 status: Ok(()) diff --git a/hblang/tests/codegen_tests_different_types.txt b/hblang/tests/codegen_tests_different_types.txt index 0c9ea22..c6c7348 100644 --- a/hblang/tests/codegen_tests_different_types.txt +++ b/hblang/tests/codegen_tests_different_types.txt @@ -1,3 +1,3 @@ -code size: 527 +code size: 499 ret: 512 status: Ok(()) diff --git a/hblang/tests/codegen_tests_fb_driver.txt b/hblang/tests/codegen_tests_fb_driver.txt index fe4ed80..1837065 100644 --- a/hblang/tests/codegen_tests_fb_driver.txt +++ b/hblang/tests/codegen_tests_fb_driver.txt @@ -1,3 +1,3 @@ -code size: 666 +code size: 657 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index fb06ea0..3138559 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -1,3 +1,3 @@ -code size: 314 +code size: 308 ret: 44 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index 5bb23a0..d33af68 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -1,3 +1,3 @@ -code size: 287 +code size: 281 ret: 16 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 5edce62..a26ad5e 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -1,3 +1,3 @@ -code size: 290 +code size: 287 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_pointers.txt b/hblang/tests/codegen_tests_pointers.txt index 9417ba6..b2c6418 100644 --- a/hblang/tests/codegen_tests_pointers.txt +++ b/hblang/tests/codegen_tests_pointers.txt @@ -1,3 +1,3 @@ -code size: 382 +code size: 379 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_variables.txt b/hblang/tests/codegen_tests_variables.txt index 4e788d6..e9bb4e8 100644 --- a/hblang/tests/codegen_tests_variables.txt +++ b/hblang/tests/codegen_tests_variables.txt @@ -1,3 +1,3 @@ -code size: 124 +code size: 121 ret: 0 status: Ok(())