From 33eea026709c68d44905053e7469dd954a7cdf6d Mon Sep 17 00:00:00 2001 From: mlokr Date: Mon, 24 Jun 2024 17:45:58 +0200 Subject: [PATCH] fixed a significant bugus --- hblang/README.md | 11 ++++++++--- hblang/src/codegen.rs | 18 +++++++++++------- hblang/tests/codegen_tests_generic_types.txt | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/hblang/README.md b/hblang/README.md index 17137980..e26371e0 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -261,8 +261,9 @@ Vec := fn($Elem: type): type { new := fn($Elem: type): Vec(Elem) return Vec(Elem).{ data: @bitcast(0), len: 0, cap: 0 }; deinit := fn($Elem: type, vec: ^Vec(Elem)): void { - free(@bitcast(vec.data), vec.len * @sizeof(Elem), @alignof(Elem)); + free(@bitcast(vec.data), vec.cap * @sizeof(Elem), @alignof(Elem)); *vec = new(Elem); + return; } push := fn($Elem: type, vec: ^Vec(Elem), value: Elem): ^Elem { @@ -286,7 +287,9 @@ push := fn($Elem: type, vec: ^Vec(Elem), value: Elem): ^Elem { dst_cursor += 1; } - free(@bitcast(vec.data), vec.len * @sizeof(Elem), @alignof(Elem)); + if vec.len != 0 { + free(@bitcast(vec.data), vec.len * @sizeof(Elem), @alignof(Elem)); + } vec.data = new_alloc; } @@ -299,7 +302,9 @@ push := fn($Elem: type, vec: ^Vec(Elem), value: Elem): ^Elem { main := fn(): int { vec := new(int); push(int, &vec, 69); - return *vec.data; + res := *vec.data; + deinit(int, &vec); + return res; } ``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index fd1104c6..285fdd6d 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -2292,19 +2292,23 @@ impl Codegen { } fn alloc_ret(&mut self, ret: ty::Id, ctx: Ctx) -> Loc { - if let Some(loc) = ctx.loc { - return loc; - } - let size = self.tys.size_of(ret); match size { 0 => Loc::default(), 1..=8 => Loc::reg(1), 9..=16 => Loc::stack(self.ci.stack.allocate(size)), _ => { - let stack = self.ci.stack.allocate(size); - self.stack_offset(1, STACK_PTR, Some(&stack), 0); - Loc::stack(stack) + let loc = ctx + .loc + .unwrap_or_else(|| Loc::stack(self.ci.stack.allocate(size))); + let Loc::Rt { + reg, stack, offset, .. + } = &loc + else { + todo!("old man with the beard looks at the sky scared"); + }; + self.stack_offset(1, reg.get(), stack.as_ref(), *offset); + loc } } } diff --git a/hblang/tests/codegen_tests_generic_types.txt b/hblang/tests/codegen_tests_generic_types.txt index 49a3f5ce..33331145 100644 --- a/hblang/tests/codegen_tests_generic_types.txt +++ b/hblang/tests/codegen_tests_generic_types.txt @@ -1,3 +1,3 @@ -code size: 1302 +code size: 1503 ret: 69 status: Ok(())