From 4ec88e33972b103c6b28f5add10732a64e7d4022 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Tue, 12 Nov 2024 20:42:04 +0100 Subject: [PATCH] adding pointer edgecase --- lang/src/son.rs | 12 +++++- lang/src/son/hbvm.rs | 17 +++++--- lang/tests/son_tests_inline.txt | 21 +++++----- lang/tests/son_tests_nullable_structure.txt | 39 +++++++++---------- .../son_tests_wrong_dead_code_elimination.txt | 38 +++++++++--------- 5 files changed, 68 insertions(+), 59 deletions(-) diff --git a/lang/src/son.rs b/lang/src/son.rs index edf24a1..c9de393 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2936,7 +2936,15 @@ impl<'a> Codegen<'a> { self.implicit_unwrap(right.pos(), &mut rhs); let (ty, aclass) = self.binop_ty(pos, &mut lhs, &mut rhs, op); let fty = ty.bin_ret(op); - if fty == ty::Id::BOOL { + if matches!( + op, + TokenKind::Lt + | TokenKind::Gt + | TokenKind::Ge + | TokenKind::Le + | TokenKind::Ne + | TokenKind::Eq + ) { if lhs.ty.is_float() { } else { self.ci.nodes.lock(rhs.id); @@ -4236,6 +4244,8 @@ impl<'a> Codegen<'a> { self.pool.push_ci(file, Some(sig.ret), 0, &mut self.ci); let prev_err_len = self.errors.borrow().len(); + log::info!("{}", self.ast_display(expr)); + let &Expr::Closure { body, args, pos, .. } = expr else { unreachable!("{}", self.ast_display(expr)) }; diff --git a/lang/src/son/hbvm.rs b/lang/src/son/hbvm.rs index 2a5bf5b..399b2bf 100644 --- a/lang/src/son/hbvm.rs +++ b/lang/src/son/hbvm.rs @@ -584,14 +584,19 @@ impl TokenKind { Self::Number if src.is_float() && dst.is_integer() => { [|a, b| instrs::fti32(a, b, 1), |a, b| instrs::fti64(a, b, 1)][src_idx - 2] } - Self::Number if src.is_signed() && dst.is_integer() => { + Self::Number if src.is_signed() && (dst.is_integer() || dst.is_pointer()) => { [instrs::sxt8, instrs::sxt16, instrs::sxt32][src_idx] } - Self::Number if (src.is_unsigned() || src == ty::Id::BOOL) && dst.is_integer() => [ - |a, b| instrs::andi(a, b, 0xff), - |a, b| instrs::andi(a, b, 0xffff), - |a, b| instrs::andi(a, b, 0xffffffff), - ][src_idx], + Self::Number + if (src.is_unsigned() || src == ty::Id::BOOL) + && (dst.is_integer() || dst.is_pointer()) => + { + [ + |a, b| instrs::andi(a, b, 0xff), + |a, b| instrs::andi(a, b, 0xffff), + |a, b| instrs::andi(a, b, 0xffffffff), + ][src_idx] + } Self::Float if dst.is_float() && src.is_float() => { [instrs::fc32t64, |a, b| instrs::fc64t32(a, b, 1)][src_idx - 2] } diff --git a/lang/tests/son_tests_inline.txt b/lang/tests/son_tests_inline.txt index 841e96d..f144cb0 100644 --- a/lang/tests/son_tests_inline.txt +++ b/lang/tests/son_tests_inline.txt @@ -1,21 +1,20 @@ main: LI64 r2, 8d ECA - LI64 r11, 6d + LI64 r10, 6d LRA r6, r0, :gb LI64 r9, 0d - LD r10, r6, 0a, 8h - CMPU r12, r10, r9 + LD r11, r6, 0a, 8h + CMPU r12, r11, r9 CMPUI r12, r12, 0d - ANDI r2, r12, 255d - OR r4, r2, r9 - ANDI r4, r4, 255d - JNE r4, r0, :0 - CP r9, r11 + ORI r2, r12, 0d + ANDI r2, r2, 255d + JNE r2, r0, :0 + CP r7, r10 JMP :1 - 0: LI64 r9, 1d - 1: SUB64 r1, r9, r11 + 0: LI64 r7, 1d + 1: SUB64 r1, r7, r10 JALA r0, r31, 0a -code size: 146 +code size: 142 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_nullable_structure.txt b/lang/tests/son_tests_nullable_structure.txt index 7003636..4ee4ab4 100644 --- a/lang/tests/son_tests_nullable_structure.txt +++ b/lang/tests/son_tests_nullable_structure.txt @@ -1,6 +1,6 @@ main: - ADDI64 r254, r254, -138d - ST r31, r254, 26a, 112h + ADDI64 r254, r254, -122d + ST r31, r254, 26a, 96h JAL r31, r0, :returner_fn CP r32, r1 ADDI64 r1, r254, 2d @@ -9,28 +9,25 @@ main: JAL r31, r0, :returner_cn ST r1, r254, 0a, 2h LI8 r34, 0b + LI8 r35, 0b + LD r36, r254, 2a, 1h CP r1, r32 - CMPU r35, r1, r34 - CMPUI r35, r35, 0d - LI8 r36, 0b - LD r37, r254, 2a, 1h - ANDI r38, r35, 255d - CMPU r32, r37, r36 - CMPUI r32, r32, 0d - AND r39, r32, r38 - LD r40, r254, 0a, 1h - ANDI r41, r39, 255d - CMPU r42, r40, r36 - CMPUI r42, r42, 0d - AND r43, r42, r41 - ANDI r44, r43, 255d - ANDI r44, r44, 255d - JNE r44, r0, :0 + CMPU r37, r1, r34 + CMPUI r37, r37, 0d + CMPU r38, r36, r35 + CMPUI r38, r38, 0d + LD r39, r254, 0a, 1h + AND r40, r38, r37 + CMPU r41, r39, r35 + CMPUI r41, r41, 0d + AND r42, r41, r40 + ANDI r42, r42, 255d + JNE r42, r0, :0 LI64 r1, 0d JMP :1 0: LI64 r1, 1d - 1: LD r31, r254, 26a, 112h - ADDI64 r254, r254, 138d + 1: LD r31, r254, 26a, 96h + ADDI64 r254, r254, 122d JALA r0, r31, 0a returner_bn: ADDI64 r254, r254, -24d @@ -57,6 +54,6 @@ returner_fn: LD r1, r254, 0a, 0h ORI r1, r1, 128d JALA r0, r31, 0a -code size: 513 +code size: 480 ret: 1 status: Ok(()) diff --git a/lang/tests/son_tests_wrong_dead_code_elimination.txt b/lang/tests/son_tests_wrong_dead_code_elimination.txt index be8973d..7bce152 100644 --- a/lang/tests/son_tests_wrong_dead_code_elimination.txt +++ b/lang/tests/son_tests_wrong_dead_code_elimination.txt @@ -1,31 +1,29 @@ main: ADDI64 r254, r254, -1d LI64 r7, 0d - LI64 r5, 255d - LI8 r4, 1b - LI8 r6, 0b - ST r6, r254, 0a, 1h + LI64 r6, 255d + LI8 r2, 1b + LI8 r4, 0b + ST r4, r254, 0a, 1h 2: LD r9, r254, 0a, 1h - AND r12, r9, r5 - CMPU r2, r12, r5 - CMPUI r2, r2, 0d - NOT r2, r2 - CMPU r6, r12, r7 - CMPUI r6, r6, 0d - NOT r6, r6 - AND r8, r2, r5 - AND r10, r6, r5 - OR r11, r10, r8 - ANDI r11, r11, 255d - JNE r11, r0, :0 + ANDI r11, r9, 255d + CMPU r3, r11, r6 + CMPUI r3, r3, 0d + NOT r3, r3 + CMPU r5, r11, r7 + CMPUI r5, r5, 0d + NOT r5, r5 + OR r8, r5, r3 + ANDI r8, r8, 255d + JNE r8, r0, :0 JMP :1 - 0: SUB8 r4, r0, r4 - 1: ADD8 r8, r9, r4 - ST r8, r254, 0a, 1h + 0: SUB8 r2, r0, r2 + 1: ADD8 r4, r9, r2 + ST r4, r254, 0a, 1h JMP :2 ADDI64 r254, r254, 1d JALA r0, r31, 0a timed out -code size: 192 +code size: 191 ret: 0 status: Ok(())