From 9f67b22aa2583bbcb332f4a45fc7f3ef23b8ef89 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 22 Dec 2024 17:01:36 +0100 Subject: [PATCH] maybe now the bug is fixed Signed-off-by: Jakub Doka --- lang/src/son.rs | 40 +++++++++++++--------- lang/tests/son_tests_slices.txt | 46 +++++++++----------------- lang/tests/son_tests_subsclice_bug.txt | 12 +++++++ 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/lang/src/son.rs b/lang/src/son.rs index 754f7dff..e1f431d3 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -704,6 +704,14 @@ impl<'a> Codegen<'a> { ); debug_assert!(self.ci.nodes[region].kind != Kind::Stre); + debug_assert!( + self.ci.nodes[region].kind != Kind::Stck + || self.tys.size_of(self.ci.nodes[region].ty) >= self.tys.size_of(ty), + "{} {}", + self.ty_display(self.ci.nodes[region].ty), + self.ty_display(ty) + ); + let (value_index, value_region) = self.ci.nodes.aclass_index(value); if value_index != 0 { self.ci.nodes[value_region].aclass = 0; @@ -1260,7 +1268,7 @@ impl<'a> Codegen<'a> { } } Expr::Index { base, index: &Expr::Range { start, end, pos } } => { - let mut bs = self.ptr_expr(base)?; + let bs = self.ptr_expr(base)?; let start = match start { Some(s) => self.checked_expr(s, ty::Id::UINT, "range start")?.id, @@ -1298,14 +1306,7 @@ impl<'a> Codegen<'a> { let elem = match bs.ty.expand() { ty::Kind::Slice(s) => self.tys.ins.slices[s].elem, - ty::Kind::Ptr(_) => { - if let Some(base) = self.tys.base_of(bs.ty) { - bs.ptr = true; - bs.ty = base; - } - - bs.ty - } + ty::Kind::Ptr(_) => self.tys.base_of(bs.ty).unwrap_or(bs.ty), _ => { return self.error( base.pos(), @@ -1317,10 +1318,9 @@ impl<'a> Codegen<'a> { } }; - let ptr = match bs.ty.expand() { + let mut ptr = match bs.ty.expand() { ty::Kind::Slice(s) if let Some(_) = self.tys.ins.slices[s].len() => bs, ty::Kind::Slice(_) => { - // Might change let off = self.offset(bs.id, SLICE_PTR_OFF); let base = self.tys.make_ptr(elem); Value::new(self.load_mem(off, base)).ty(base) @@ -1328,15 +1328,23 @@ impl<'a> Codegen<'a> { ty::Kind::Ptr(_) => bs, _ => unreachable!(), }; + ptr.id = self.offset_ptr(ptr.id, elem, start).id; + ptr.ty = self.tys.make_ptr(elem); + self.ci.nodes.lock(ptr.id); let ty = self.tys.make_array(elem, ArrayLen::MAX); let mem = self.new_stack(pos, ty); - for (off, value) in [(SLICE_PTR_OFF, ptr), (SLICE_LEN_OFF, len)] { - let region = self.offset(mem, off); - self.store_mem(region, value.ty, value.id); - } + + let (off, value) = (SLICE_PTR_OFF, ptr); + let region = self.offset(mem, off); + self.store_mem(region, value.ty, value.id); + + let (off, value) = (SLICE_LEN_OFF, len); + let region = self.offset(mem, off); + self.store_mem(region, value.ty, value.id); + self.ci.nodes.unlock(start); self.ci.nodes.unlock(len.id); self.ci.nodes.unlock(end); @@ -4384,7 +4392,7 @@ mod tests { fn generate(ident: &'static str, input: &'static str, output: &mut String) { _ = log::set_logger(&crate::fs::Logger); log::set_max_level(log::LevelFilter::Info); - log::set_max_level(log::LevelFilter::Trace); + //log::set_max_level(log::LevelFilter::Trace); let mut ctx = CodegenCtx::default(); let (ref files, embeds) = crate::test_parse_files(ident, input, &mut ctx.parser); diff --git a/lang/tests/son_tests_slices.txt b/lang/tests/son_tests_slices.txt index bcf0c1ab..569e386c 100644 --- a/lang/tests/son_tests_slices.txt +++ b/lang/tests/son_tests_slices.txt @@ -1,41 +1,27 @@ main: - ADDI64 r254, r254, -120d - ADDI64 r13, r254, 32d - ST r0, r254, 32a, 8h - ADDI64 r14, r13, 8d - LI64 r15, 1d - ST r15, r14, 0a, 8h - ADDI64 r15, r13, 16d - LI64 r16, 2d - ST r16, r15, 0a, 8h - LI64 r16, 3d - ADDI64 r17, r254, 112d - LI64 r18, 10d - ST r13, r254, 0a, 8h - ST r16, r254, 56a, 8h - ST r18, r254, 112a, 8h - ST r14, r254, 64a, 8h - LD r14, r254, 0a, 8h - LD r16, r254, 64a, 8h - ST r15, r254, 96a, 8h + ADDI64 r254, r254, -40d + ST r0, r254, 0a, 8h + LI64 r13, 1d + ST r13, r254, 8a, 8h + LI64 r13, 2d ST r13, r254, 16a, 8h - LD r13, r14, 0a, 8h - LD r14, r16, 0a, 8h - LD r15, r254, 96a, 8h - LD r16, r254, 16a, 8h + LI64 r13, 3d + LI64 r14, 10d + ST r13, r254, 24a, 8h + ST r14, r254, 32a, 8h + LD r13, r254, 0a, 8h + LD r14, r254, 8a, 8h ADD64 r13, r14, r13 - LD r14, r15, 0a, 8h - ST r17, r254, 80a, 8h + LD r14, r254, 16a, 8h ADD64 r13, r14, r13 - LD r14, r16, 24a, 8h - LD r15, r254, 80a, 8h + LD r14, r254, 24a, 8h ADD64 r13, r14, r13 - LD r14, r15, 0a, 8h + LD r14, r254, 32a, 8h ADDI64 r13, r13, 4d SUB64 r13, r13, r14 CP r1, r13 - ADDI64 r254, r254, 120d + ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 415 +code size: 241 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_subsclice_bug.txt b/lang/tests/son_tests_subsclice_bug.txt index e69de29b..91455eef 100644 --- a/lang/tests/son_tests_subsclice_bug.txt +++ b/lang/tests/son_tests_subsclice_bug.txt @@ -0,0 +1,12 @@ +main: + LRA r13, r0, :"abcd\0" + ADDI64 r13, r13, 1d + LI64 r14, 37d + CP r2, r14 + CP r3, r13 + ECA + JALA r0, r31, 0a +bcd +code size: 59 +ret: 0 +status: Ok(())