diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 9bf7462..b63b560 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -371,12 +371,18 @@ impl Func { } } -#[derive(Default, PartialEq, Eq, Debug)] +#[derive(Default, PartialEq, Eq)] pub struct RegAlloc { free: Vec, max_used: Reg, } +impl std::fmt::Debug for RegAlloc { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RegAlloc").finish() + } +} + impl RegAlloc { fn init_callee(&mut self) { self.free.clear(); @@ -564,18 +570,19 @@ impl<'a> Codegen<'a> { } let fn_label = self.labels[frame.label as usize].clone(); + self.gpa.borrow_mut().init_callee(); log::dbg!("fn-args"); let mut parama = 3..12; for (arg, &ty) in args.iter().zip(fn_label.args.iter()) { - let loc = self.load_arg(ty, &mut parama); + let refed = arg.last.is_some_and(|l| l.get() & parser::REFERENCED != 0); + let loc = self.load_arg(refed, ty, &mut parama); self.vars.push(Variable { id: arg.id, value: Value { ty, loc }, }); } - self.gpa.borrow_mut().init_callee(); self.ret = fn_label.ret; log::dbg!("fn-body"); @@ -1630,10 +1637,15 @@ impl<'a> Codegen<'a> { } } - fn load_arg(&mut self, ty: Type, parama: &mut Range) -> Loc { + fn load_arg(&mut self, referenced: bool, ty: Type, parama: &mut Range) -> Loc { let size = self.size_of(ty); match size { 0 => Loc::Imm(0), + ..=8 if !referenced => { + let reg = self.alloc_reg(); + self.code.encode(instrs::cp(reg.0, parama.next().unwrap())); + Loc::Reg(reg) + } ..=8 => { let stack = self.alloc_stack(size as _); self.store_stack(parama.next().unwrap(), stack.offset, size as _); diff --git a/hblang/tests/codegen_tests_fb_driver.txt b/hblang/tests/codegen_tests_fb_driver.txt index f79bef2..83862c7 100644 --- a/hblang/tests/codegen_tests_fb_driver.txt +++ b/hblang/tests/codegen_tests_fb_driver.txt @@ -1,3 +1,3 @@ -code size: 758 +code size: 678 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index 9f1d250..190c4f1 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -1,3 +1,3 @@ -code size: 388 +code size: 320 ret: 33 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index 5fadd38..cda3ca7 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -1,3 +1,3 @@ -code size: 347 +code size: 293 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 5fadd38..23eb54c 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -1,3 +1,3 @@ -code size: 347 +code size: 296 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_pointers.txt b/hblang/tests/codegen_tests_pointers.txt index 5216a64..9417ba6 100644 --- a/hblang/tests/codegen_tests_pointers.txt +++ b/hblang/tests/codegen_tests_pointers.txt @@ -1,3 +1,3 @@ -code size: 437 +code size: 382 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_structs.txt b/hblang/tests/codegen_tests_structs.txt index db6996d..e81084a 100644 --- a/hblang/tests/codegen_tests_structs.txt +++ b/hblang/tests/codegen_tests_structs.txt @@ -1,3 +1,3 @@ -code size: 530 +code size: 483 ret: 3 status: Ok(())