From 9d2f4191400960c24654f0b283291737670ee788 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Thu, 7 Nov 2024 16:39:15 +0100 Subject: [PATCH] fixing messed up flag calculation --- lang/README.md | 19 +++++++++++++++++++ lang/src/son.rs | 6 ++++-- lang/tests/son_tests_nullable_structure.txt | 21 +++++++++++++++++++++ lang/tests/son_tests_nullable_types.txt | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lang/tests/son_tests_nullable_structure.txt diff --git a/lang/README.md b/lang/README.md index 458615b9..1264e8d6 100644 --- a/lang/README.md +++ b/lang/README.md @@ -624,6 +624,25 @@ main := fn(): uint { ### Purely Testing Examples +#### nullable_structure +```hb +Structure := struct {} + +returner_fn := fn(): ?Structure { + structure := Structure.() + return structure +} + +main := fn(): int { + ret := returner_fn() + if ret != null { + return 1 + } + + return 0 +} +``` + #### needless_unwrap ```hb main := fn(): uint { diff --git a/lang/src/son.rs b/lang/src/son.rs index 5147bb47..3b867a43 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -4267,10 +4267,11 @@ impl<'a> Codegen<'a> { match oty.loc(self.tys) { Loc::Reg => { + std::println!("{} {} {}", self.ty_display(oty), flag_offset, self.tys.size_of(oty)); self.strip_ptr(val); // registers have inverted offsets so that accessing the inner type is a noop - let flag_offset = self.tys.size_of(oty) - flag_offset - 1; - let fill = self.ci.nodes.new_const(oty, 1i64 << (flag_offset * 8 - 1)); + let flag_offset = self.tys.size_of(oty) * 8 - flag_offset * 8 - 1; + let fill = self.ci.nodes.new_const(oty, 1i64 << flag_offset); val.id = self .ci .nodes @@ -4595,6 +4596,7 @@ mod tests { fb_driver; // Purely Testing Examples; + nullable_structure; needless_unwrap; inlining_issues; null_check_test; diff --git a/lang/tests/son_tests_nullable_structure.txt b/lang/tests/son_tests_nullable_structure.txt new file mode 100644 index 00000000..20779dce --- /dev/null +++ b/lang/tests/son_tests_nullable_structure.txt @@ -0,0 +1,21 @@ +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + JAL r31, r0, :returner_fn + LI8 r32, 0b + ANDI r1, r1, 255d + ANDI r32, r32, 255d + JEQ r1, r32, :0 + LI64 r1, 1d + JMP :1 + 0: LI64 r1, 0d + 1: LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a +returner_fn: + LD r1, r254, 0a, 0h + ORI r1, r1, 128d + JALA r0, r31, 0a +code size: 164 +ret: 1 +status: Ok(()) diff --git a/lang/tests/son_tests_nullable_types.txt b/lang/tests/son_tests_nullable_types.txt index ab626496..c5044256 100644 --- a/lang/tests/son_tests_nullable_types.txt +++ b/lang/tests/son_tests_nullable_types.txt @@ -38,7 +38,7 @@ main: JNE r1, r0, :7 CP r39, r38 JMP :8 - 7: LI32 r39, 8388609w + 7: LI32 r39, 2147483649w 8: ANDI r39, r39, 4294967295d ANDI r38, r38, 4294967295d JNE r39, r38, :9