From 590983701528fa1979bea907ad1a3ec8b660f0a7 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 22 Dec 2024 18:53:52 +0100 Subject: [PATCH] making sure sliced pointer is loaded Signed-off-by: Jakub Doka --- lang/README.md | 10 ++++++++++ lang/src/son.rs | 8 ++++++-- lang/tests/son_tests_slice_to_global_pointer.txt | 8 ++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 lang/tests/son_tests_slice_to_global_pointer.txt diff --git a/lang/README.md b/lang/README.md index 3db8e542..800fc92a 100644 --- a/lang/README.md +++ b/lang/README.md @@ -798,6 +798,16 @@ main := fn(): uint { ### Purely Testing Examples +#### slice_to_global_pointer +```hb +a := @as(^u8, @bitcast(0)) + +main := fn(): ^u8 { + b := a[0..100] + return b.ptr +} +``` + #### subsclice_bug ```hb main := fn(): void { diff --git a/lang/src/son.rs b/lang/src/son.rs index e1f431d3..d7c4fe26 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -1268,7 +1268,7 @@ impl<'a> Codegen<'a> { } } Expr::Index { base, index: &Expr::Range { start, end, pos } } => { - let bs = self.ptr_expr(base)?; + let mut bs = self.ptr_expr(base)?; let start = match start { Some(s) => self.checked_expr(s, ty::Id::UINT, "range start")?.id, @@ -1325,7 +1325,10 @@ impl<'a> Codegen<'a> { let base = self.tys.make_ptr(elem); Value::new(self.load_mem(off, base)).ty(base) } - ty::Kind::Ptr(_) => bs, + ty::Kind::Ptr(_) => { + self.strip_ptr(&mut bs); + bs + } _ => unreachable!(), }; ptr.id = self.offset_ptr(ptr.id, elem, start).id; @@ -4462,6 +4465,7 @@ mod tests { fb_driver; // Purely Testing Examples; + slice_to_global_pointer; subsclice_bug; string_array; proper_ident_propagation; diff --git a/lang/tests/son_tests_slice_to_global_pointer.txt b/lang/tests/son_tests_slice_to_global_pointer.txt new file mode 100644 index 00000000..5eccd165 --- /dev/null +++ b/lang/tests/son_tests_slice_to_global_pointer.txt @@ -0,0 +1,8 @@ +main: + LRA r13, r0, :a + LD r13, r13, 0a, 8h + CP r1, r13 + JALA r0, r31, 0a +code size: 50 +ret: 0 +status: Ok(())