From 254d5ed96234c8291770d84b2ac11ef7dd403b28 Mon Sep 17 00:00:00 2001 From: mlokr Date: Mon, 16 Sep 2024 21:46:02 +0200 Subject: [PATCH] fixing array bug, well actually more serious bug that somehow did not happen until now --- Cargo.lock | 29 ---------------- hblang/Cargo.toml | 1 - hblang/README.md | 6 +++- hblang/src/codegen.rs | 6 +++- hblang/tests/codegen_tests_arrays.txt | 49 ++++++++++++++++++++------- 5 files changed, 46 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9946593..59a7e4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "hashbrown" -version = "0.14.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" - [[package]] name = "hbbytecode" version = "0.1.0" @@ -21,7 +15,6 @@ name = "hblang" version = "0.1.0" dependencies = [ "hbvm", - "regalloc2", ] [[package]] @@ -54,28 +47,6 @@ dependencies = [ "libc", ] -[[package]] -name = "regalloc2" -version = "0.10.2" -source = "git+https://github.com/jakubDoka/regalloc2.git#34d8424a0d74746d231a96239fdb32e3d5ec0245" -dependencies = [ - "hashbrown", - "rustc-hash", - "smallvec", -] - -[[package]] -name = "rustc-hash" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" - -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - [[package]] name = "xtask" version = "0.1.0" diff --git a/hblang/Cargo.toml b/hblang/Cargo.toml index 3bf150b..ee487b4 100644 --- a/hblang/Cargo.toml +++ b/hblang/Cargo.toml @@ -9,4 +9,3 @@ path = "src/main.rs" [dependencies] hbvm = { path = "../hbvm", features = ["nightly"] } -regalloc2 = { git = "https://github.com/jakubDoka/regalloc2.git" } diff --git a/hblang/README.md b/hblang/README.md index fa15623..d58e63f 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -317,8 +317,12 @@ fib_iter := fn(n: int): int { #### arrays ```hb main := fn(): int { + addr := @as(u16, 0x1FF) + msg := [u8].(0, 0, @as(u8, addr & 0xFF), @as(u8, addr >> 8 & 0xFF)) + _force_stack := &msg + arr := [int].(1, 2, 4) - return pass(&arr) + return pass(&arr) + msg[3] } pass := fn(arr: ^[int; 3]): int { diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 8f75b86..669921d 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -1677,7 +1677,11 @@ impl Codegen { Some(match ctx.loc { Some(dest) => { - self.store_typed(value.loc, dest, value.ty); + self.store_sized( + value.loc, + dest, + self.tys.size_of(ctx.ty.unwrap_or(value.ty)).min(self.tys.size_of(value.ty)), + ); Value { ty: value.ty, loc: Loc::ct(0) } } None => value, diff --git a/hblang/tests/codegen_tests_arrays.txt b/hblang/tests/codegen_tests_arrays.txt index a665fc6..270c21c 100644 --- a/hblang/tests/codegen_tests_arrays.txt +++ b/hblang/tests/codegen_tests_arrays.txt @@ -1,17 +1,40 @@ main: - ADDI64 r254, r254, -40d - ST r31, r254, 24a, 16h - LI64 r32, 1d - ST r32, r254, 0a, 8h - LI64 r32, 2d - ST r32, r254, 8a, 8h - LI64 r32, 4d - ST r32, r254, 16a, 8h + ADDI64 r254, r254, -68d + ST r31, r254, 28a, 40h + LI64 r32, 511d + LI64 r33, 0d + ST r33, r254, 24a, 1h + LI64 r33, 0d + ST r33, r254, 25a, 1h + CP r33, r32 + ANDI r33, r33, 255d + ST r33, r254, 26a, 1h + SRUI16 r32, r32, 8b + ANDI r32, r32, 255d + ST r32, r254, 27a, 1h + CP r32, r0 + LD r32, r254, 24a, 4h + ST r32, r254, 0a, 4h ADDI64 r32, r254, 0d - CP r2, r32 + LI64 r33, 1d + ST r33, r254, 4a, 8h + LI64 r33, 2d + ST r33, r254, 12a, 8h + LI64 r33, 4d + ST r33, r254, 20a, 8h + ADDI64 r33, r254, 4d + CP r2, r33 JAL r31, r0, :pass - LD r31, r254, 24a, 16h - ADDI64 r254, r254, 40d + CP r33, r1 + ADDI64 r34, r254, 0d + LI64 r35, 3d + ADD64 r34, r34, r35 + CP r35, r0 + LD r35, r34, 0a, 1h + SXT8 r35, r35 + ADD64 r1, r33, r35 + LD r31, r254, 28a, 40h + ADDI64 r254, r254, 68d JALA r0, r31, 0a pass: ADDI64 r254, r254, -40d @@ -40,6 +63,6 @@ pass: LD r31, r254, 0a, 40h ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 378 -ret: 7 +code size: 580 +ret: 8 status: Ok(())