perf(vm): eliminate recurring stack and return allocations

This commit is contained in:
2026-10-14 10:54:11 +01:00
parent e09f1197bc
commit 46bbad19c2
8 changed files with 308 additions and 30 deletions
@@ -216,3 +216,47 @@ fn integer_division_and_remainder_overflow_wrap_without_host_panic() {
assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(expected)]);
}
}
#[test]
fn maximum_width_mixed_return_preserves_order_and_caller_locals() {
// TestProgram catalog: Int64, Vec2, Float = 0, 6, 2.
let mut caller = String::from("PUSH_I64 73\nSET_LOCAL 0\nCALL 1\n");
caller.push_str(&"POP\n".repeat(1024));
caller.push_str("GET_LOCAL 0\nRET");
let mut callee = String::new();
let mut results = vec![];
let mut expected = vec![];
for i in 0..256 {
callee.push_str(&format!("PUSH_I64 {i}\nPUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nPUSH_F64 f64:0x4008000000000000\n"));
results.extend([TypeId(0), TypeId(6), TypeId(2)]);
expected.extend([Value::Int64(i), Value::Float(1.), Value::Float(2.), Value::Float(3.)]);
}
callee.push_str("RET");
let caller_code = assemble(&caller).unwrap();
let body = assemble(&callee).unwrap();
let mut program = TestProgram::single(caller_code.clone(), int64_signature(0, 1), 1, 1024);
program.functions.push(FunctionDecl {
signature: Signature { parameters: vec![], results },
code_offset: caller_code.len() as u32,
code_len: body.len() as u32,
local_slots: 3,
max_stack_slots: 1024,
..Default::default()
});
program.code.extend(body);
let image = ValidatedProgramImage::load(
&program.serialize(),
&mut LoadBudget::new(Default::default()),
caps::NONE,
)
.unwrap();
let mut machine = VirtualMachine::from_image(image, Default::default());
machine.prepare_boot_call().unwrap();
for _ in 0..3 + 1024 + 1 {
machine.step(&mut Native, &mut HostContext::new(None)).unwrap();
}
expected.reverse();
assert_eq!(machine.operand_stack_top(1024), expected);
assert_eq!(run(&mut machine, 10000).reason, LogicalFrameEndingReason::EndOfRom);
assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(73)]);
}