diff --git a/lang/README.md b/lang/README.md index 0f0d1b3d..a41877f6 100644 --- a/lang/README.md +++ b/lang/README.md @@ -612,6 +612,39 @@ main := fn(): uint { ### Purely Testing Examples +#### null_check_returning_small_global +```hb +MAGIC := 127 +get := fn(file: ^u8): ?uint { + if *file == MAGIC { + return MAGIC + } else { + return null + } +} + +some_file := [u8].(127, 255, 255, 255, 255, 255) + +foo := fn(): ?uint { + gotten := get(&some_file[0]) + if gotten == null { + return null + } else if gotten == 4 { + return 2 + } else if gotten == MAGIC { + return 0 + } + + return null +} + +main := fn(): uint { + f := foo() + if f == null return 100 + return f +} +``` + #### null_check_in_the_loop ```hb A := struct { diff --git a/lang/src/son.rs b/lang/src/son.rs index 19581dae..2d27ba09 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -4579,7 +4579,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); @@ -4640,6 +4640,7 @@ mod tests { fb_driver; // Purely Testing Examples; + null_check_returning_small_global; null_check_in_the_loop; stack_provenance; advanced_floating_point_arithmetic; diff --git a/lang/src/son/hbvm/their_regalloc.rs b/lang/src/son/hbvm/their_regalloc.rs index b46be2e9..72d3608e 100644 --- a/lang/src/son/hbvm/their_regalloc.rs +++ b/lang/src/son/hbvm/their_regalloc.rs @@ -264,8 +264,8 @@ impl HbvmBackend { } else if let Some(against) = op.cmp_against() { let op_ty = fuc.nodes[rh].ty; - self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); - self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); + self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 1, 1)); + self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 2, 2)); let &[dst, lhs, rhs] = allocs else { unreachable!() }; if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { diff --git a/lang/tests/son_tests_null_check_returning_small_global.txt b/lang/tests/son_tests_null_check_returning_small_global.txt new file mode 100644 index 00000000..e69de29b