diff --git a/hblang/README.md b/hblang/README.md index edc80df..8fdd067 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -678,10 +678,23 @@ rect_line := fn(buffer: Buffer, pos: Point, tr: Transform, color: ColorBGRA, thi return } +random := @use("random.hb") + +example := fn(): void { + loop { + random_x := @inline(random.integer, 0, 1024) + random_y := random.integer(0, 768) + a := @inline(screenidx, random_x, random_y) + break + } + return +} + main := fn(): int { line(.(), .(0, 0), .(0, 0), .(0, 0), 10) rect_line(.(), .(0, 0), .(0, 0), .(0, 0), 10) - return + example() + return 0 } // in module: screen.hb @@ -690,4 +703,14 @@ screenidx := fn(orange: int): int { return orange } +// in module: random.hb + +integer := fn(min: int, max: int): int { + rng := @eca(int, 3, 4) + + if min != 0 | max != 0 { + return rng % (max - min + 1) + min + } + return rng +} ``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 2419826..22b5db6 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -8,7 +8,7 @@ use { parser::{self, find_symbol, idfl, CtorField, Expr, ExprRef, FileId, Pos}, HashMap, }, - std::{ops::Range, rc::Rc, usize}, + std::{ops::Range, rc::Rc, u32, usize}, }; type Offset = u32; @@ -1981,7 +1981,7 @@ impl Codegen { tk => Some(Value::ty(tk.compress())), }, E::Return { pos, val, .. } => { - if let Some(val) = val { + let ty = if let Some(val) = val { let size = self.ci.ret.map_or(17, |ty| self.tys.size_of(ty)); let loc = match size { _ if self.ci.inline_ret_loc != Loc::default() => { @@ -1991,12 +1991,16 @@ impl Codegen { 1..=16 => Some(Loc::reg(1)), _ => Some(Loc::reg(self.ci.ret_reg.as_ref()).into_derefed()), }; - let ty = self.expr_ctx(val, Ctx { ty: self.ci.ret, loc })?.ty; - match self.ci.ret { - None => self.ci.ret = Some(ty), - Some(ret) => _ = self.assert_ty(pos, ty, ret), - } + self.expr_ctx(val, Ctx { ty: self.ci.ret, loc })?.ty + } else { + ty::VOID.into() + }; + + match self.ci.ret { + None => self.ci.ret = Some(ty), + Some(ret) => _ = self.assert_ty(pos, ty, ret), } + self.ci.ret_relocs.push(Reloc::new(self.local_offset(), 1, 4)); self.output.emit(jmp(0)); None @@ -3050,6 +3054,7 @@ impl Codegen { if let Some(existing) = self.tys.syms.get(&SymKey { file, ident }) { if let ty::Kind::Func(id) = existing.expand() && let func = &mut self.tys.funcs[id as usize] + && func.offset != u32::MAX && let Err(idx) = task::unpack(func.offset) { func.offset = task::id(self.tasks.len()); @@ -3438,6 +3443,7 @@ mod tests { let layout = std::alloc::Layout::from_size_align(size, align).unwrap(); unsafe { std::alloc::dealloc(ptr as *mut u8, layout) }; } + 3 => vm.write_reg(1, 42), unknown => unreachable!("unknown ecall: {unknown:?}"), }, Ok(ev) => writeln!(output, "ev: {:?}", ev).unwrap(), diff --git a/hblang/tests/codegen_tests_inline_test.txt b/hblang/tests/codegen_tests_inline_test.txt index 9c8be48..c780e3c 100644 --- a/hblang/tests/codegen_tests_inline_test.txt +++ b/hblang/tests/codegen_tests_inline_test.txt @@ -1,3 +1,3 @@ -code size: 1043 +code size: 1521 ret: 0 status: Ok(())