From dc96c8b10ae85af7a11bd1e8ca2cd29e2c42a033 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sat, 14 Dec 2024 18:50:13 +0100 Subject: [PATCH] the items accesed outside the nested scope no longer get duplicated Signed-off-by: Jakub Doka --- lang/README.md | 2 +- lang/src/backend/hbvm.rs | 2 +- lang/src/son.rs | 13 ++-- lang/tests/son_tests_generic_types.txt | 26 +------- .../son_tests_method_receiver_by_value.txt | 61 +++++++++++++++++++ 5 files changed, 73 insertions(+), 31 deletions(-) diff --git a/lang/README.md b/lang/README.md index 1ed58ee5..8d1ced38 100644 --- a/lang/README.md +++ b/lang/README.md @@ -734,7 +734,7 @@ main := fn(): uint { #### method_receiver_by_value ```hb -$log := fn(ptr: ^u8): void return @eca(0, ptr) +$log := fn(ptr: ^u8): void return @eca(37, ptr) Struct := struct { ptr: ^u8, print := fn(self: Self, rhs: Self): void { diff --git a/lang/src/backend/hbvm.rs b/lang/src/backend/hbvm.rs index b559b417..5c989f58 100644 --- a/lang/src/backend/hbvm.rs +++ b/lang/src/backend/hbvm.rs @@ -1138,7 +1138,7 @@ pub fn test_run_vm(out: &[u8], output: &mut String) { match vm.run() { Ok(hbvm::VmRunOk::End) => break Ok(()), Ok(hbvm::VmRunOk::Ecall) => match vm.read_reg(2).0 { - 0 => writeln!( + 37 => writeln!( output, "{}", unsafe { CStr::from_ptr(vm.read_reg(3).0 as _) }.to_str().unwrap() diff --git a/lang/src/son.rs b/lang/src/son.rs index cd73d06d..093de051 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -5641,7 +5641,7 @@ impl<'a> Codegen<'a> { }; }; - let ty = if let Some(&ty) = self.tys.syms.get(SymKey::Decl(parent, name), &self.tys.ins) + let ty = if let Some(&ty) = self.tys.syms.get(SymKey::Decl(piter, name), &self.tys.ins) { ty } else { @@ -5651,13 +5651,18 @@ impl<'a> Codegen<'a> { self.tys .ins .consts - .push(ConstData { ast: ExprRef::new(expr), name, file, parent }) + .push(ConstData { + ast: ExprRef::new(expr), + name, + file, + parent: piter, + }) .into() } else { self.parse_ty( TyScope { file, - parent, + parent: piter, name: Some(name), alloc_const: true, is_ct, @@ -5667,7 +5672,7 @@ impl<'a> Codegen<'a> { } }) .unwrap_or_else(|_| unreachable!()); - self.tys.syms.insert(SymKey::Decl(parent, name), ty, &self.tys.ins); + self.tys.syms.insert(SymKey::Decl(piter, name), ty, &self.tys.ins); ty }; diff --git a/lang/tests/son_tests_generic_types.txt b/lang/tests/son_tests_generic_types.txt index 6ff4f3b9..e8939554 100644 --- a/lang/tests/son_tests_generic_types.txt +++ b/lang/tests/son_tests_generic_types.txt @@ -43,18 +43,6 @@ free: CP r5, r15 ECA JALA r0, r31, 0a -free: - CP r13, r2 - CP r14, r3 - CP r15, r4 - LRA r16, r0, :free_sys_call - LD r16, r16, 0a, 8h - CP r2, r16 - CP r3, r13 - CP r4, r14 - CP r5, r15 - ECA - JALA r0, r31, 0a main: ADDI64 r254, r254, -96d ST r31, r254, 48a, 48h @@ -99,18 +87,6 @@ malloc: CP r13, r1 CP r1, r13 JALA r0, r31, 0a -malloc: - CP r13, r2 - CP r14, r3 - LRA r15, r0, :malloc_sys_call - LD r15, r15, 0a, 8h - CP r2, r15 - CP r3, r13 - CP r4, r14 - ECA - CP r13, r1 - CP r1, r13 - JALA r0, r31, 0a new: ADDI64 r254, r254, -24d CP r15, r1 @@ -242,6 +218,6 @@ push: 4: LD r31, r254, 0a, 88h ADDI64 r254, r254, 88d JALA r0, r31, 0a -code size: 1757 +code size: 1635 ret: 69 status: Ok(()) diff --git a/lang/tests/son_tests_method_receiver_by_value.txt b/lang/tests/son_tests_method_receiver_by_value.txt index e69de29b..01bbd7f3 100644 --- a/lang/tests/son_tests_method_receiver_by_value.txt +++ b/lang/tests/son_tests_method_receiver_by_value.txt @@ -0,0 +1,61 @@ +main: + ADDI64 r254, r254, -72d + ST r31, r254, 32a, 40h + ADDI64 r32, r254, 8d + LRA r33, r0, :"Goodbye, World!\0" + ADDI64 r34, r254, 24d + LRA r35, r0, :"Hello, World!\0" + ST r33, r254, 8a, 8h + ST r35, r254, 24a, 8h + LD r2, r34, 0a, 8h + LD r3, r32, 0a, 8h + JAL r31, r0, :print + ADDI64 r32, r254, 0d + ADDI64 r34, r254, 16d + ST r33, r254, 0a, 8h + ST r35, r254, 16a, 8h + CP r2, r34 + CP r3, r32 + JAL r31, r0, :print2 + LD r31, r254, 32a, 40h + ADDI64 r254, r254, 72d + JALA r0, r31, 0a +print: + ADDI64 r254, r254, -16d + ST r2, r254, 8a, 8h + ADDI64 r2, r254, 8d + CP r13, r2 + ST r3, r254, 0a, 8h + ADDI64 r3, r254, 0d + CP r14, r3 + LD r13, r13, 0a, 8h + LI64 r15, 37d + CP r2, r15 + CP r3, r13 + ECA + LD r13, r14, 0a, 8h + CP r2, r15 + CP r3, r13 + ECA + ADDI64 r254, r254, 16d + JALA r0, r31, 0a +print2: + CP r13, r2 + CP r14, r3 + LD r13, r13, 0a, 8h + LI64 r15, 37d + CP r2, r15 + CP r3, r13 + ECA + LD r13, r14, 0a, 8h + CP r2, r15 + CP r3, r13 + ECA + JALA r0, r31, 0a +Hello, World! +Goodbye, World! +Hello, World! +Goodbye, World! +code size: 457 +ret: 0 +status: Ok(())