From 9dfb2eb606e9049bc5d372e674a75e0be2c57ff0 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 24 Nov 2024 11:26:38 +0100 Subject: [PATCH] fg --- lang/README.md | 14 ++++ lang/src/son.rs | 16 ++++- lang/tests/son_tests_pointer_comparison.txt | 7 ++ lang/tests/son_tests_pointers.txt | 4 +- smh.hb | 77 +++------------------ 5 files changed, 44 insertions(+), 74 deletions(-) create mode 100644 lang/tests/son_tests_pointer_comparison.txt diff --git a/lang/README.md b/lang/README.md index 579a774..1c41806 100644 --- a/lang/README.md +++ b/lang/README.md @@ -658,6 +658,20 @@ main := fn(): uint { ### Purely Testing Examples +#### pointer_comparison +```hb +main := fn(): int { + a := 10 + b := 20 + + if &a == &a { + return 10 + } else { + return 20 + } +} +``` + #### wrong_dead_code_elimination ```hb Color := struct {b: u8} diff --git a/lang/src/son.rs b/lang/src/son.rs index a7c02aa..1ecb0f9 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -871,7 +871,8 @@ impl Nodes { if lhs == rhs { match op { - T::Sub => return Some(self.new_const(ty, 0)), + T::Ne | T::Gt | T::Lt | T::Sub => return Some(self.new_const(ty, 0)), + T::Eq | T::Ge | T::Le => return Some(self.new_const(ty, 1)), T::Add => { let rhs = self.new_const(ty, 2); return Some(self.new_node( @@ -4548,9 +4549,17 @@ impl<'a> Codegen<'a> { }) } - fn assign_pattern(&mut self, pat: &Expr, right: Value) { + fn assign_pattern(&mut self, pat: &Expr, mut right: Value) { match *pat { - Expr::Ident { id, .. } => { + Expr::Ident { id, pos, .. } => { + if parser::find_symbol(&self.file().symbols, id).flags & idfl::REFERENCED != 0 + && !right.ptr + { + let stack = self.new_stack(pos, right.ty); + self.store_mem(stack, right.ty, right.id); + right.id = stack; + right.ptr = true; + } self.ci.scope.vars.push(Variable::new( id, right.ty, @@ -5514,6 +5523,7 @@ mod tests { fb_driver; // Purely Testing Examples; + pointer_comparison; different_function_destinations; triggering_store_in_divergent_branch; wrong_dead_code_elimination; diff --git a/lang/tests/son_tests_pointer_comparison.txt b/lang/tests/son_tests_pointer_comparison.txt new file mode 100644 index 0000000..2d6710e --- /dev/null +++ b/lang/tests/son_tests_pointer_comparison.txt @@ -0,0 +1,7 @@ +main: + LI64 r13, 10d + CP r1, r13 + JALA r0, r31, 0a +code size: 32 +ret: 10 +status: Ok(()) diff --git a/lang/tests/son_tests_pointers.txt b/lang/tests/son_tests_pointers.txt index 0c5b1e1..7518c6a 100644 --- a/lang/tests/son_tests_pointers.txt +++ b/lang/tests/son_tests_pointers.txt @@ -8,9 +8,9 @@ main: ST r33, r254, 0a, 8h CP r2, r32 JAL r31, r0, :modify - CP r2, r33 - JAL r31, r0, :drop LD r32, r254, 0a, 8h + CP r2, r32 + JAL r31, r0, :drop ADDI64 r32, r32, -2d CP r1, r32 LD r31, r254, 8a, 24h diff --git a/smh.hb b/smh.hb index 95e4940..86275bc 100644 --- a/smh.hb +++ b/smh.hb @@ -1,71 +1,10 @@ -log := fn(msg: ^u8): void return @eca(0, msg) - -absf := fn(x: f32): f32 { - if (x < 0) { - return -x - } - return x -} - -sqrt := fn(x: f32, low: f32, high: f32): f32 { - mid := (high - low)/2.0 - if (absf(mid * mid - x) < 0.01) { - return mid - } - if (mid * mid > x) { - return sqrt(x, low, mid) - } - if (mid * mid < x) { - return sqrt(x, mid, high) - } - return -9999.0 -} - -is_filled := fn(x: uint, y: uint, center_x: f32, center_y: f32, inner_radius: f32, outer_radius: f32): bool { - x_f := @as(f32, @floatcast(@itf(@bitcast(x)))) - y_f := @as(f32, @floatcast(@itf(@bitcast(y)))) - x_diff := x_f - center_x - y_diff := x_f - center_y - dist := sqrt(x_diff * x_diff + y_diff * y_diff) - if (dist < outer_radius) { - if (dist > inner_radius) { - return true - } - } - return false -} - -buffer := @as([u8; 61], idk) - -render_row := fn(y: uint): void { - x := 0 - loop { - if (x > 60) { - break - } - buffer[x] = 32 - } - x = 0 - loop { - if (x > 60) { - break - } - if (is_filled(x, y, 30.0, 30.0, 10.0, 20.0)) { - buffer[x] = 35 - } - x = x + 1 - } - log(buffer[0]) -} - main := fn(): int { - y := 0 - loop { - if (y > 60) { - break - } - render_row(y) - y = y + 1 - } - return 0 + a := 10 + b := 20 + + if &a == &a { + return 10 + } else { + return 20 + } }