pr6.2
This commit is contained in:
@@ -155,6 +155,12 @@ pub enum OpCode {
|
||||
/// Returns from the current function.
|
||||
/// Stack: [return_val] -> [return_val]
|
||||
Ret = 0x51,
|
||||
/// Creates a closure capturing values from the operand stack (Model B).
|
||||
/// Operands: fn_id (u32), capture_count (u32)
|
||||
/// Stack before: [..., captured_N, ..., captured_1]
|
||||
/// Pops capture_count values (top-first), preserves order as [captured_1..captured_N]
|
||||
/// and stores them inside the closure environment. Pushes a HeapRef to the closure.
|
||||
MakeClosure = 0x52,
|
||||
|
||||
// --- 6.8 Peripherals and System ---
|
||||
/// Invokes a system function (Firmware/OS).
|
||||
@@ -215,6 +221,7 @@ impl TryFrom<u16> for OpCode {
|
||||
0x43 => Ok(OpCode::SetLocal),
|
||||
0x50 => Ok(OpCode::Call),
|
||||
0x51 => Ok(OpCode::Ret),
|
||||
0x52 => Ok(OpCode::MakeClosure),
|
||||
0x70 => Ok(OpCode::Syscall),
|
||||
0x80 => Ok(OpCode::FrameSync),
|
||||
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
||||
@@ -271,6 +278,7 @@ impl OpCode {
|
||||
OpCode::SetLocal => 2,
|
||||
OpCode::Call => 5,
|
||||
OpCode::Ret => 4,
|
||||
OpCode::MakeClosure => 8,
|
||||
OpCode::Syscall => 1,
|
||||
OpCode::FrameSync => 1,
|
||||
}
|
||||
|
||||
@@ -462,6 +462,18 @@ impl OpCodeSpecExt for OpCode {
|
||||
may_trap: false,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::MakeClosure => OpcodeSpec {
|
||||
name: "MAKE_CLOSURE",
|
||||
// Two u32 immediates: fn_id and capture_count
|
||||
imm_bytes: 8,
|
||||
// Dynamic, depends on capture_count; keep 0 here for verifier-free spec
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::Syscall => OpcodeSpec {
|
||||
name: "SYSCALL",
|
||||
imm_bytes: 4,
|
||||
|
||||
@@ -407,6 +407,26 @@ impl VirtualMachine {
|
||||
self.cycles += OpCode::Trap.cycles();
|
||||
return Err(LogicalFrameEndingReason::Breakpoint);
|
||||
}
|
||||
OpCode::MakeClosure => {
|
||||
// Immediate carries (fn_id, capture_count)
|
||||
let (fn_id, cap_count) = instr
|
||||
.imm_u32x2()
|
||||
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
|
||||
// Pop cap_count values from the operand stack, top-first.
|
||||
let mut temp: Vec<Value> = Vec::with_capacity(cap_count as usize);
|
||||
for _ in 0..cap_count {
|
||||
let v = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
temp.push(v);
|
||||
}
|
||||
// Preserve order so that env[0] corresponds to captured_1 (the bottom-most
|
||||
// among the popped values): reverse the temp vector.
|
||||
temp.reverse();
|
||||
|
||||
// Allocate closure on heap and push its reference.
|
||||
let href = self.heap.alloc_closure(fn_id, &temp);
|
||||
self.push(Value::HeapRef(href));
|
||||
}
|
||||
OpCode::PushConst => {
|
||||
let idx = instr
|
||||
.imm_u32()
|
||||
@@ -2723,4 +2743,84 @@ mod tests {
|
||||
|
||||
assert_eq!(vm.heap.len(), 0, "All short-lived objects must be reclaimed deterministically");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_make_closure_zero_captures() {
|
||||
use prometeu_bytecode::{FunctionMeta, Value};
|
||||
|
||||
// ROM: MAKE_CLOSURE fn_id=7, cap=0; HALT
|
||||
let mut rom = Vec::new();
|
||||
rom.extend_from_slice(&(OpCode::MakeClosure as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&7u32.to_le_bytes()); // fn_id
|
||||
rom.extend_from_slice(&0u32.to_le_bytes()); // capture_count
|
||||
rom.extend_from_slice(&(OpCode::Halt as u16).to_le_bytes());
|
||||
|
||||
let mut vm = new_test_vm(rom.clone(), vec![]);
|
||||
vm.program.functions = std::sync::Arc::from(vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: rom.len() as u32,
|
||||
..Default::default()
|
||||
}]);
|
||||
|
||||
let mut native = MockNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
// step MAKE_CLOSURE
|
||||
vm.step(&mut native, &mut ctx).unwrap();
|
||||
// step HALT
|
||||
vm.step(&mut native, &mut ctx).unwrap();
|
||||
|
||||
assert!(vm.halted);
|
||||
assert_eq!(vm.operand_stack.len(), 1);
|
||||
let top = vm.peek().unwrap().clone();
|
||||
let href = match top { Value::HeapRef(h) => h, _ => panic!("Expected HeapRef on stack") };
|
||||
assert!(vm.heap.is_valid(href));
|
||||
assert_eq!(vm.heap.closure_fn_id(href), Some(7));
|
||||
let env = vm.heap.closure_env_slice(href).expect("env slice");
|
||||
assert_eq!(env.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_make_closure_multiple_captures_and_order() {
|
||||
use prometeu_bytecode::{FunctionMeta, Value};
|
||||
|
||||
// Build ROM:
|
||||
// PUSH_I32 1; PUSH_I32 2; PUSH_I32 3; // Stack: [1,2,3]
|
||||
// MAKE_CLOSURE fn_id=9, cap=3; // Pops 3 (3,2,1), env = [1,2,3]
|
||||
// HALT
|
||||
let mut rom = Vec::new();
|
||||
rom.extend_from_slice(&(OpCode::PushI32 as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&1i32.to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::PushI32 as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&2i32.to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::PushI32 as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&3i32.to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::MakeClosure as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&9u32.to_le_bytes()); // fn_id
|
||||
rom.extend_from_slice(&3u32.to_le_bytes()); // capture_count
|
||||
rom.extend_from_slice(&(OpCode::Halt as u16).to_le_bytes());
|
||||
|
||||
let mut vm = new_test_vm(rom.clone(), vec![]);
|
||||
vm.program.functions = std::sync::Arc::from(vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: rom.len() as u32,
|
||||
..Default::default()
|
||||
}]);
|
||||
|
||||
let mut native = MockNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
// Execute instructions until HALT
|
||||
while !vm.halted {
|
||||
vm.step(&mut native, &mut ctx).unwrap();
|
||||
}
|
||||
|
||||
// After HALT, stack must contain only the closure ref
|
||||
assert_eq!(vm.operand_stack.len(), 1);
|
||||
let href = match vm.pop().unwrap() { Value::HeapRef(h) => h, _ => panic!("Expected HeapRef") };
|
||||
assert_eq!(vm.heap.closure_fn_id(href), Some(9));
|
||||
let env = vm.heap.closure_env_slice(href).expect("env slice");
|
||||
assert_eq!(env.len(), 3);
|
||||
assert_eq!(env[0], Value::Int32(1));
|
||||
assert_eq!(env[1], Value::Int32(2));
|
||||
assert_eq!(env[2], Value::Int32(3));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user