break monster VM into small pieces :D

This commit is contained in:
2026-03-24 13:40:48 +00:00
parent ea12bd3fd5
commit 9a60754e5a
24 changed files with 2894 additions and 3514 deletions
@@ -1,3 +1,8 @@
mod gc;
mod loader;
mod runtime;
mod stack;
use crate::call_frame::CallFrame;
use crate::heap::{CoroutineState, Heap};
use crate::lookup_intrinsic_by_id;
@@ -20,73 +25,6 @@ use prometeu_bytecode::{
use prometeu_hal::syscalls::caps::NONE;
use prometeu_hal::vm_fault::VmFault;
fn patch_module_hostcalls(
module: &mut BytecodeModule,
capabilities: prometeu_hal::syscalls::CapFlags,
) -> Result<(), LoaderPatchError> {
let resolved =
prometeu_hal::syscalls::resolve_declared_program_syscalls(&module.syscalls, capabilities)
.map_err(LoaderPatchError::ResolveFailed)?;
let mut used = vec![false; module.syscalls.len()];
let mut pc = 0usize;
while pc < module.code.len() {
let instr = decode_next(pc, &module.code).map_err(LoaderPatchError::DecodeFailed)?;
let next_pc = instr.next_pc;
match instr.opcode {
OpCode::Hostcall => {
let sysc_index = instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?;
let Some(resolved_syscall) = resolved.get(sysc_index as usize) else {
return Err(LoaderPatchError::HostcallIndexOutOfBounds {
pc,
sysc_index,
syscalls_len: resolved.len(),
});
};
used[sysc_index as usize] = true;
module.code[pc..pc + 2].copy_from_slice(&(OpCode::Syscall as u16).to_le_bytes());
module.code[pc + 2..pc + 6].copy_from_slice(&resolved_syscall.id.to_le_bytes());
}
OpCode::Syscall => {
return Err(LoaderPatchError::RawSyscallInPreloadArtifact {
pc,
syscall_id: instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?,
});
}
_ => {}
}
pc = next_pc;
}
for (index, (was_used, decl)) in used.iter().zip(&module.syscalls).enumerate() {
if !was_used {
return Err(LoaderPatchError::UnusedSyscallDecl {
sysc_index: index as u32,
module: decl.module.clone(),
name: decl.name.clone(),
version: decl.version,
});
}
}
let mut pc = 0usize;
while pc < module.code.len() {
let instr = decode_next(pc, &module.code).map_err(LoaderPatchError::DecodeFailed)?;
if instr.opcode == OpCode::Hostcall {
return Err(LoaderPatchError::HostcallRemaining {
pc,
sysc_index: instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?,
});
}
pc = instr.next_pc;
}
Ok(())
}
/// Reason why the Virtual Machine stopped execution during a specific run.
/// This allows the system to decide if it should continue execution in the next tick
/// or if the frame is finalized.
@@ -256,310 +194,6 @@ impl VirtualMachine {
}
}
/// Resets the VM state and loads a new program.
/// This is typically called by the Firmware when starting a new App/Cartridge.
pub fn initialize(
&mut self,
program_bytes: Vec<u8>,
entrypoint: &str,
) -> Result<(), VmInitError> {
// Fail fast: reset state upfront. If we return early with an error,
// the VM is left in a "halted and empty" state.
self.program = ProgramImage::default();
self.pc = 0;
self.operand_stack.clear();
self.call_stack.clear();
self.globals.clear();
self.heap = Heap::new();
self.cycles = 0;
self.halted = true; // execution is impossible until a successful load
self.last_gc_live_count = 0;
self.current_tick = 0;
self.sleep_requested_until = None;
self.scheduler = Scheduler::new();
self.current_coro = None;
// Preserve capabilities across loads; firmware may set them per cart.
// Only recognized format is loadable: PBS v0 industrial format
let program = if program_bytes.starts_with(b"PBS\0") {
match prometeu_bytecode::BytecodeLoader::load(&program_bytes) {
Ok(mut module) => {
patch_module_hostcalls(&mut module, self.capabilities)
.map_err(VmInitError::LoaderPatchFailed)?;
// Run verifier on the module
let max_stacks = Verifier::verify(&module.code, &module.functions)
.map_err(|e| VmInitError::VerificationFailed(format!("{:?}", e)))?;
let mut program = ProgramImage::from(module);
let mut functions = program.functions.as_ref().to_vec();
for (func, max_stack) in functions.iter_mut().zip(max_stacks) {
func.max_stack_slots = max_stack;
}
program.functions = std::sync::Arc::from(functions);
program
}
Err(prometeu_bytecode::LoadError::InvalidVersion) => {
return Err(VmInitError::UnsupportedFormat);
}
Err(e) => {
return Err(VmInitError::ImageLoadFailed(e));
}
}
} else {
return Err(VmInitError::InvalidFormat);
};
// Resolve the entrypoint: empty (defaults to func 0), numeric func_idx, or symbol name.
let pc = if entrypoint.is_empty() {
program.functions.first().map(|f| f.code_offset as usize).unwrap_or(0)
} else if let Ok(func_idx) = entrypoint.parse::<usize>() {
program
.functions
.get(func_idx)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else {
// Try to resolve as a symbol name from the exports map
if let Some(&func_idx) = program.exports.get(entrypoint) {
program
.functions
.get(func_idx as usize)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else {
return Err(VmInitError::EntrypointNotFound);
}
};
// Finalize initialization by applying the new program and PC.
self.program = program;
self.pc = pc;
self.halted = false; // Successfully loaded, execution is now possible
Ok(())
}
/// Sets the capability flags for the current program.
pub fn set_capabilities(&mut self, caps: prometeu_hal::syscalls::CapFlags) {
self.capabilities = caps;
}
/// Prepares the VM to execute a specific entrypoint by setting the PC and
/// pushing an initial call frame.
pub fn prepare_call(&mut self, entrypoint: &str) {
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
idx
} else {
// Try to resolve as a symbol name
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
// Default to 0 if not found
};
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
let addr = callee.code_offset as usize;
self.pc = addr;
self.halted = false;
// Pushing a sentinel frame so RET works at the top level.
// The return address is set to the end of ROM, which will naturally
// cause the VM to stop after returning from the entrypoint.
self.operand_stack.clear();
self.call_stack.clear();
// Entrypoint also needs locals allocated.
// For the sentinel frame, stack_base is always 0.
if let Some(func) = self.program.functions.get(func_idx) {
let total_slots = func.param_slots as u32 + func.local_slots as u32;
for _ in 0..total_slots {
self.operand_stack.push(Value::Null);
}
}
self.call_stack.push(CallFrame {
return_pc: self.program.rom.len() as u32,
stack_base: 0,
func_idx,
});
// Initialize the main coroutine object.
// IMPORTANT INVARIANT:
// - The RUNNING coroutine's authoritative execution state lives in the VM fields
// (pc, operand_stack, call_stack).
// - The heap-side coroutine object is authoritative ONLY when the coroutine is suspended
// (Ready/Sleeping/Finished). While running, its `stack`/`frames` should be empty.
//
// Therefore we do NOT clone the VM stacks into the heap here. We create the main
// coroutine object with empty stack/frames and mark it as Running, and the VM already
// holds the live execution context initialized above.
let main_href = self.heap.allocate_coroutine(
self.pc,
CoroutineState::Running,
0,
Vec::new(),
Vec::new(),
);
self.current_coro = Some(main_href);
self.scheduler.set_current(self.current_coro);
}
/// Executes the VM for a limited number of cycles (budget).
///
/// This is the heart of the deterministic execution model. Instead of running
/// indefinitely, the VM runs until it consumes its allocated budget or reaches
/// a synchronization point (`FRAME_SYNC`).
///
/// # Arguments
/// * `budget` - Maximum number of cycles allowed for this execution slice.
/// * `native` - Interface for handling syscalls (Firmware/OS).
/// * `hw` - Access to virtual hardware peripherals.
pub fn run_budget(
&mut self,
budget: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<BudgetReport, String> {
let start_cycles = self.cycles;
let mut steps_executed = 0;
let mut ending_reason: Option<LogicalFrameEndingReason> = None;
while (self.cycles - start_cycles) < budget
&& !self.halted
&& self.pc < self.program.rom.len()
{
// Debugger support: stop before executing an instruction if there's a breakpoint.
// Note: we skip the check for the very first step of a slice to avoid
// getting stuck on the same breakpoint repeatedly.
if steps_executed > 0 && self.breakpoints.contains(&self.pc) {
ending_reason = Some(LogicalFrameEndingReason::Breakpoint);
break;
}
let pc_before = self.pc;
let cycles_before = self.cycles;
// Execute a single step (Fetch-Decode-Execute)
if let Err(reason) = self.step(native, ctx) {
ending_reason = Some(reason);
break;
}
steps_executed += 1;
// Integrity check: ensure real progress is being made to avoid infinite loops
// caused by zero-cycle instructions or stuck PC.
if self.pc == pc_before && self.cycles == cycles_before && !self.halted {
ending_reason = Some(LogicalFrameEndingReason::Panic(format!(
"VM stuck at PC 0x{:08X}",
self.pc
)));
break;
}
}
// Determine why we stopped if no explicit reason (FrameSync/Breakpoint) was set.
if ending_reason.is_none() {
if self.halted {
ending_reason = Some(LogicalFrameEndingReason::Halted);
} else if self.pc >= self.program.rom.len() {
ending_reason = Some(LogicalFrameEndingReason::EndOfRom);
} else {
ending_reason = Some(LogicalFrameEndingReason::BudgetExhausted);
}
}
Ok(BudgetReport {
cycles_used: self.cycles - start_cycles,
steps_executed,
reason: ending_reason.unwrap(),
})
}
/// Harness: run exactly `frames` logical frames deterministically.
///
/// This repeatedly calls `run_budget` with the provided `budget_per_slice` until
/// a full logical frame is completed (i.e., a `FrameSync` is observed). If a
/// terminal condition is reached earlier (Halt/EndOfRom/Panic/Trap/Breakpoint),
/// the function returns early with all collected slice reports so far.
pub fn run_frames(
&mut self,
frames: u64,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
assert!(budget_per_slice > 0, "budget_per_slice must be > 0");
let mut out = Vec::new();
let mut frames_done = 0u64;
while frames_done < frames {
let rep = self.run_budget(budget_per_slice, native, ctx)?;
let terminal = matches!(
rep.reason,
LogicalFrameEndingReason::Halted
| LogicalFrameEndingReason::EndOfRom
| LogicalFrameEndingReason::Panic(_)
| LogicalFrameEndingReason::Trap(_)
| LogicalFrameEndingReason::Breakpoint
);
let is_frame_end = matches!(rep.reason, LogicalFrameEndingReason::FrameSync);
out.push(rep);
if terminal {
break;
}
if is_frame_end {
frames_done += 1;
}
}
Ok(out)
}
/// Harness: alias for `run_frames(frames, ...)`.
pub fn run_ticks(
&mut self,
ticks: u64,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
self.run_frames(ticks, budget_per_slice, native, ctx)
}
/// Harness: run until HALT/EndOfRom/Panic/Trap/Breakpoint deterministically.
///
/// Repeatedly invokes `run_budget` with a fixed `budget_per_slice`, collecting
/// each slice's report until a terminal condition is reached.
pub fn run_until_halt(
&mut self,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
assert!(budget_per_slice > 0, "budget_per_slice must be > 0");
let mut out = Vec::new();
loop {
let rep = self.run_budget(budget_per_slice, native, ctx)?;
let terminal = matches!(
rep.reason,
LogicalFrameEndingReason::Halted
| LogicalFrameEndingReason::EndOfRom
| LogicalFrameEndingReason::Panic(_)
| LogicalFrameEndingReason::Trap(_)
| LogicalFrameEndingReason::Breakpoint
);
out.push(rep);
if terminal {
break;
}
}
Ok(out)
}
/// Executes a single instruction at the current Program Counter (PC).
///
/// This follows the classic CPU cycle:
@@ -1535,249 +1169,6 @@ impl VirtualMachine {
self.cycles += opcode.cycles();
Ok(())
}
/// Perform safepoint duties that occur at logical frame boundaries.
/// Runs GC if thresholds are reached, clears cooperative yield flag,
/// and advances the logical tick counter.
fn handle_safepoint(&mut self) {
// 1) GC Safepoint: only at FRAME_SYNC-like boundaries
if self.gc_alloc_threshold > 0 {
let live_now = self.heap.len();
let since_last = live_now.saturating_sub(self.last_gc_live_count);
if since_last >= self.gc_alloc_threshold {
// Collect GC roots from VM state
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
impl crate::roots::RootVisitor for CollectRoots {
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
self.0.push(r);
}
}
let mut collector = CollectRoots(Vec::new());
self.visit_roots(&mut collector);
// Add current coroutine and all suspended (ready/sleeping) coroutines as GC roots
if let Some(cur) = self.current_coro {
collector.0.push(cur);
}
let mut coro_roots = self.heap.suspended_coroutine_handles();
collector.0.append(&mut coro_roots);
// Run mark-sweep
self.heap.mark_from_roots(collector.0);
self.heap.sweep();
// Update baseline for next cycles
self.last_gc_live_count = self.heap.len();
}
}
// 2) Advance logical tick and wake sleepers
self.current_tick = self.current_tick.wrapping_add(1);
self.scheduler.wake_ready(self.current_tick);
// 3) Apply pending transitions for the current coroutine (yield/sleep/finished)
let mut switched_out = false;
if let Some(cur) = self.current_coro {
// Handle sleep request
if let Some(wake) = self.sleep_requested_until.take() {
if let Some(co) = self.heap.coroutine_data_mut(cur) {
// Save execution context into the coroutine object
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Sleeping;
co.wake_tick = wake;
}
self.scheduler.sleep_until(cur, wake);
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
} else if self.yield_requested {
if let Some(co) = self.heap.coroutine_data_mut(cur) {
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Ready;
}
self.scheduler.enqueue_ready(cur);
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
} else if self.halted || self.pc >= self.program.rom.len() {
// Current finished; save final context and mark Finished
if let Some(co) = self.heap.coroutine_data_mut(cur) {
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Finished;
}
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
} else {
// Stays running; nothing to do
}
}
// 4) Select next coroutine if needed
if self.current_coro.is_none() {
if let Some(next) = self.scheduler.dequeue_next() {
// Load next context into the VM
if let Some(co) = self.heap.coroutine_data_mut(next) {
self.pc = co.pc;
self.operand_stack = std::mem::take(&mut co.stack);
self.call_stack = std::mem::take(&mut co.frames);
co.state = CoroutineState::Running;
}
self.current_coro = Some(next);
self.scheduler.set_current(self.current_coro);
} else {
// Nothing ready now. If there are sleeping coroutines, we keep VM idle until next frame tick.
// If there are no sleeping coroutines either (i.e., all finished), we can halt deterministically.
if switched_out && !self.scheduler.has_sleeping() {
self.halted = true;
}
}
} else {
// Keep current as scheduler current for observability
self.scheduler.set_current(self.current_coro);
}
// 5) Clear cooperative yield request at the safepoint boundary.
self.yield_requested = false;
}
// /// Save the currently running VM execution context back into its coroutine object.
// /// Must be called only at safepoints.
// fn save_current_context_into_coroutine(&mut self) {
// if let Some(cur) = self.current_coro {
// if let Some(co) = self.heap.coroutine_data_mut(cur) {
// co.pc = self.pc;
// co.stack = std::mem::take(&mut self.operand_stack);
// co.frames = std::mem::take(&mut self.call_stack);
// }
// }
// }
// /// Load a coroutine context from heap into the VM runtime state.
// /// Must be called only at safepoints.
// fn load_coroutine_context_into_vm(&mut self, coro: HeapRef) {
// if let Some(co) = self.heap.coroutine_data_mut(coro) {
// self.pc = co.pc;
// self.operand_stack = std::mem::take(&mut co.stack);
// self.call_stack = std::mem::take(&mut co.frames);
// co.state = CoroutineState::Running;
// }
// self.current_coro = Some(coro);
// self.scheduler.set_current(self.current_coro);
// }
pub fn trap(
&self,
code: u32,
opcode: u16,
message: String,
pc: u32,
) -> LogicalFrameEndingReason {
LogicalFrameEndingReason::Trap(self.program.create_trap(code, opcode, message, pc))
}
pub fn push(&mut self, val: Value) {
self.operand_stack.push(val);
}
pub fn pop(&mut self) -> Result<Value, String> {
self.operand_stack.pop().ok_or("Stack underflow".into())
}
pub fn pop_number(&mut self) -> Result<f64, String> {
let val = self.pop()?;
val.as_float().ok_or_else(|| "Expected number".into())
}
pub fn pop_integer(&mut self) -> Result<i64, String> {
let val = self.pop()?;
if let Value::Boolean(b) = val {
return Ok(if b { 1 } else { 0 });
}
val.as_integer().ok_or_else(|| "Expected integer".into())
}
pub fn peek(&self) -> Result<&Value, String> {
self.operand_stack.last().ok_or("Stack underflow".into())
}
fn pop_trap<S: Into<String>>(
&mut self,
opcode: OpCode,
pc: u32,
message: S,
) -> Result<Value, LogicalFrameEndingReason> {
self.pop().map_err(|_| self.trap(TRAP_STACK_UNDERFLOW, opcode as u16, message.into(), pc))
}
fn peek_trap<S: Into<String>>(
&self,
opcode: OpCode,
pc: u32,
message: S,
) -> Result<&Value, LogicalFrameEndingReason> {
self.peek().map_err(|_| self.trap(TRAP_STACK_UNDERFLOW, opcode as u16, message.into(), pc))
}
fn binary_op<F>(
&mut self,
opcode: OpCode,
start_pc: u32,
f: F,
) -> Result<(), LogicalFrameEndingReason>
where
F: FnOnce(Value, Value) -> Result<Value, OpError>,
{
let b = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
match f(a, b) {
Ok(res) => {
self.push(res);
Ok(())
}
Err(OpError::Trap(code, msg)) => Err(self.trap(code, opcode as u16, msg, start_pc)),
}
}
/// Visit all GC roots reachable from the VM state.
/// This includes:
/// - Entire operand stack values
/// - Locals/args in each call frame (derived from `stack_base` and function layout)
/// - Global variables
pub fn visit_roots<V: RootVisitor + ?Sized>(&self, visitor: &mut V) {
// 1) Operand stack (all values are roots)
for v in &self.operand_stack {
visit_value_for_roots(v, visitor);
}
// 2) Call frames: iterate locals/args range for each frame
for frame in &self.call_stack {
if let Some(func_meta) = self.program.functions.get(frame.func_idx) {
let start = frame.stack_base;
let frame_slots =
(func_meta.param_slots as usize) + (func_meta.local_slots as usize);
let mut end = start.saturating_add(frame_slots);
// Clamp to current stack height just in case
if end > self.operand_stack.len() {
end = self.operand_stack.len();
}
for i in start..end {
if let Some(v) = self.operand_stack.get(i) {
visit_value_for_roots(v, visitor);
}
}
}
}
// 3) Globals
for g in &self.globals {
visit_value_for_roots(g, visitor);
}
}
}
#[cfg(test)]
@@ -0,0 +1,115 @@
use super::*;
impl VirtualMachine {
pub(super) fn handle_safepoint(&mut self) {
if self.gc_alloc_threshold > 0 {
let live_now = self.heap.len();
let since_last = live_now.saturating_sub(self.last_gc_live_count);
if since_last >= self.gc_alloc_threshold {
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
impl crate::roots::RootVisitor for CollectRoots {
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
self.0.push(r);
}
}
let mut collector = CollectRoots(Vec::new());
self.visit_roots(&mut collector);
if let Some(cur) = self.current_coro {
collector.0.push(cur);
}
let mut coro_roots = self.heap.suspended_coroutine_handles();
collector.0.append(&mut coro_roots);
self.heap.mark_from_roots(collector.0);
self.heap.sweep();
self.last_gc_live_count = self.heap.len();
}
}
self.current_tick = self.current_tick.wrapping_add(1);
self.scheduler.wake_ready(self.current_tick);
let mut switched_out = false;
if let Some(cur) = self.current_coro {
if let Some(wake) = self.sleep_requested_until.take() {
if let Some(co) = self.heap.coroutine_data_mut(cur) {
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Sleeping;
co.wake_tick = wake;
}
self.scheduler.sleep_until(cur, wake);
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
} else if self.yield_requested {
if let Some(co) = self.heap.coroutine_data_mut(cur) {
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Ready;
}
self.scheduler.enqueue_ready(cur);
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
} else if self.halted || self.pc >= self.program.rom.len() {
if let Some(co) = self.heap.coroutine_data_mut(cur) {
co.pc = self.pc;
co.stack = std::mem::take(&mut self.operand_stack);
co.frames = std::mem::take(&mut self.call_stack);
co.state = CoroutineState::Finished;
}
self.current_coro = None;
self.scheduler.clear_current();
switched_out = true;
}
}
if self.current_coro.is_none() {
if let Some(next) = self.scheduler.dequeue_next() {
if let Some(co) = self.heap.coroutine_data_mut(next) {
self.pc = co.pc;
self.operand_stack = std::mem::take(&mut co.stack);
self.call_stack = std::mem::take(&mut co.frames);
co.state = CoroutineState::Running;
}
self.current_coro = Some(next);
self.scheduler.set_current(self.current_coro);
} else if switched_out && !self.scheduler.has_sleeping() {
self.halted = true;
}
} else {
self.scheduler.set_current(self.current_coro);
}
self.yield_requested = false;
}
pub fn visit_roots<V: RootVisitor + ?Sized>(&self, visitor: &mut V) {
for value in &self.operand_stack {
visit_value_for_roots(value, visitor);
}
for frame in &self.call_stack {
if let Some(func_meta) = self.program.functions.get(frame.func_idx) {
let start = frame.stack_base;
let frame_slots =
(func_meta.param_slots as usize) + (func_meta.local_slots as usize);
let mut end = start.saturating_add(frame_slots);
if end > self.operand_stack.len() {
end = self.operand_stack.len();
}
for index in start..end {
if let Some(value) = self.operand_stack.get(index) {
visit_value_for_roots(value, visitor);
}
}
}
}
for global in &self.globals {
visit_value_for_roots(global, visitor);
}
}
}
@@ -0,0 +1,182 @@
use super::*;
fn patch_module_hostcalls(
module: &mut BytecodeModule,
capabilities: prometeu_hal::syscalls::CapFlags,
) -> Result<(), LoaderPatchError> {
let resolved =
prometeu_hal::syscalls::resolve_declared_program_syscalls(&module.syscalls, capabilities)
.map_err(LoaderPatchError::ResolveFailed)?;
let mut used = vec![false; module.syscalls.len()];
let mut pc = 0usize;
while pc < module.code.len() {
let instr = decode_next(pc, &module.code).map_err(LoaderPatchError::DecodeFailed)?;
let next_pc = instr.next_pc;
match instr.opcode {
OpCode::Hostcall => {
let sysc_index = instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?;
let Some(resolved_syscall) = resolved.get(sysc_index as usize) else {
return Err(LoaderPatchError::HostcallIndexOutOfBounds {
pc,
sysc_index,
syscalls_len: resolved.len(),
});
};
used[sysc_index as usize] = true;
module.code[pc..pc + 2].copy_from_slice(&(OpCode::Syscall as u16).to_le_bytes());
module.code[pc + 2..pc + 6].copy_from_slice(&resolved_syscall.id.to_le_bytes());
}
OpCode::Syscall => {
return Err(LoaderPatchError::RawSyscallInPreloadArtifact {
pc,
syscall_id: instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?,
});
}
_ => {}
}
pc = next_pc;
}
for (index, (was_used, decl)) in used.iter().zip(&module.syscalls).enumerate() {
if !was_used {
return Err(LoaderPatchError::UnusedSyscallDecl {
sysc_index: index as u32,
module: decl.module.clone(),
name: decl.name.clone(),
version: decl.version,
});
}
}
let mut pc = 0usize;
while pc < module.code.len() {
let instr = decode_next(pc, &module.code).map_err(LoaderPatchError::DecodeFailed)?;
if instr.opcode == OpCode::Hostcall {
return Err(LoaderPatchError::HostcallRemaining {
pc,
sysc_index: instr.imm_u32().map_err(LoaderPatchError::DecodeFailed)?,
});
}
pc = instr.next_pc;
}
Ok(())
}
impl VirtualMachine {
pub fn initialize(
&mut self,
program_bytes: Vec<u8>,
entrypoint: &str,
) -> Result<(), VmInitError> {
self.program = ProgramImage::default();
self.pc = 0;
self.operand_stack.clear();
self.call_stack.clear();
self.globals.clear();
self.heap = Heap::new();
self.cycles = 0;
self.halted = true;
self.last_gc_live_count = 0;
self.current_tick = 0;
self.sleep_requested_until = None;
self.scheduler = Scheduler::new();
self.current_coro = None;
let program = if program_bytes.starts_with(b"PBS\0") {
match prometeu_bytecode::BytecodeLoader::load(&program_bytes) {
Ok(mut module) => {
patch_module_hostcalls(&mut module, self.capabilities)
.map_err(VmInitError::LoaderPatchFailed)?;
let max_stacks = Verifier::verify(&module.code, &module.functions)
.map_err(|e| VmInitError::VerificationFailed(format!("{:?}", e)))?;
let mut program = ProgramImage::from(module);
let mut functions = program.functions.as_ref().to_vec();
for (func, max_stack) in functions.iter_mut().zip(max_stacks) {
func.max_stack_slots = max_stack;
}
program.functions = std::sync::Arc::from(functions);
program
}
Err(prometeu_bytecode::LoadError::InvalidVersion) => {
return Err(VmInitError::UnsupportedFormat);
}
Err(e) => return Err(VmInitError::ImageLoadFailed(e)),
}
} else {
return Err(VmInitError::InvalidFormat);
};
let pc = if entrypoint.is_empty() {
program.functions.first().map(|f| f.code_offset as usize).unwrap_or(0)
} else if let Ok(func_idx) = entrypoint.parse::<usize>() {
program
.functions
.get(func_idx)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else if let Some(&func_idx) = program.exports.get(entrypoint) {
program
.functions
.get(func_idx as usize)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else {
return Err(VmInitError::EntrypointNotFound);
};
self.program = program;
self.pc = pc;
self.halted = false;
Ok(())
}
pub fn set_capabilities(&mut self, caps: prometeu_hal::syscalls::CapFlags) {
self.capabilities = caps;
}
pub fn prepare_call(&mut self, entrypoint: &str) {
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
idx
} else {
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
};
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
self.pc = callee.code_offset as usize;
self.halted = false;
self.operand_stack.clear();
self.call_stack.clear();
if let Some(func) = self.program.functions.get(func_idx) {
let total_slots = func.param_slots as u32 + func.local_slots as u32;
for _ in 0..total_slots {
self.operand_stack.push(Value::Null);
}
}
self.call_stack.push(CallFrame {
return_pc: self.program.rom.len() as u32,
stack_base: 0,
func_idx,
});
let main_href = self.heap.allocate_coroutine(
self.pc,
CoroutineState::Running,
0,
Vec::new(),
Vec::new(),
);
self.current_coro = Some(main_href);
self.scheduler.set_current(self.current_coro);
}
}
@@ -0,0 +1,129 @@
use super::*;
impl VirtualMachine {
pub fn run_budget(
&mut self,
budget: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<BudgetReport, String> {
let start_cycles = self.cycles;
let mut steps_executed = 0;
let mut ending_reason: Option<LogicalFrameEndingReason> = None;
while (self.cycles - start_cycles) < budget
&& !self.halted
&& self.pc < self.program.rom.len()
{
if steps_executed > 0 && self.breakpoints.contains(&self.pc) {
ending_reason = Some(LogicalFrameEndingReason::Breakpoint);
break;
}
let pc_before = self.pc;
let cycles_before = self.cycles;
if let Err(reason) = self.step(native, ctx) {
ending_reason = Some(reason);
break;
}
steps_executed += 1;
if self.pc == pc_before && self.cycles == cycles_before && !self.halted {
ending_reason = Some(LogicalFrameEndingReason::Panic(format!(
"VM stuck at PC 0x{:08X}",
self.pc
)));
break;
}
}
if ending_reason.is_none() {
ending_reason = Some(if self.halted {
LogicalFrameEndingReason::Halted
} else if self.pc >= self.program.rom.len() {
LogicalFrameEndingReason::EndOfRom
} else {
LogicalFrameEndingReason::BudgetExhausted
});
}
Ok(BudgetReport {
cycles_used: self.cycles - start_cycles,
steps_executed,
reason: ending_reason.unwrap(),
})
}
pub fn run_frames(
&mut self,
frames: u64,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
assert!(budget_per_slice > 0, "budget_per_slice must be > 0");
let mut out = Vec::new();
let mut frames_done = 0u64;
while frames_done < frames {
let rep = self.run_budget(budget_per_slice, native, ctx)?;
let terminal = matches!(
rep.reason,
LogicalFrameEndingReason::Halted
| LogicalFrameEndingReason::EndOfRom
| LogicalFrameEndingReason::Panic(_)
| LogicalFrameEndingReason::Trap(_)
| LogicalFrameEndingReason::Breakpoint
);
let is_frame_end = matches!(rep.reason, LogicalFrameEndingReason::FrameSync);
out.push(rep);
if terminal {
break;
}
if is_frame_end {
frames_done += 1;
}
}
Ok(out)
}
pub fn run_ticks(
&mut self,
ticks: u64,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
self.run_frames(ticks, budget_per_slice, native, ctx)
}
pub fn run_until_halt(
&mut self,
budget_per_slice: u64,
native: &mut dyn NativeInterface,
ctx: &mut HostContext,
) -> Result<Vec<BudgetReport>, String> {
assert!(budget_per_slice > 0, "budget_per_slice must be > 0");
let mut out = Vec::new();
loop {
let rep = self.run_budget(budget_per_slice, native, ctx)?;
let terminal = matches!(
rep.reason,
LogicalFrameEndingReason::Halted
| LogicalFrameEndingReason::EndOfRom
| LogicalFrameEndingReason::Panic(_)
| LogicalFrameEndingReason::Trap(_)
| LogicalFrameEndingReason::Breakpoint
);
out.push(rep);
if terminal {
break;
}
}
Ok(out)
}
}
@@ -0,0 +1,76 @@
use super::*;
impl VirtualMachine {
pub fn trap(
&self,
code: u32,
opcode: u16,
message: String,
pc: u32,
) -> LogicalFrameEndingReason {
LogicalFrameEndingReason::Trap(self.program.create_trap(code, opcode, message, pc))
}
pub fn push(&mut self, val: Value) {
self.operand_stack.push(val);
}
pub fn pop(&mut self) -> Result<Value, String> {
self.operand_stack.pop().ok_or("Stack underflow".into())
}
pub fn pop_number(&mut self) -> Result<f64, String> {
let val = self.pop()?;
val.as_float().ok_or_else(|| "Expected number".into())
}
pub fn pop_integer(&mut self) -> Result<i64, String> {
let val = self.pop()?;
if let Value::Boolean(b) = val {
return Ok(if b { 1 } else { 0 });
}
val.as_integer().ok_or_else(|| "Expected integer".into())
}
pub fn peek(&self) -> Result<&Value, String> {
self.operand_stack.last().ok_or("Stack underflow".into())
}
pub(super) fn pop_trap<S: Into<String>>(
&mut self,
opcode: OpCode,
pc: u32,
message: S,
) -> Result<Value, LogicalFrameEndingReason> {
self.pop().map_err(|_| self.trap(TRAP_STACK_UNDERFLOW, opcode as u16, message.into(), pc))
}
pub(super) fn peek_trap<S: Into<String>>(
&self,
opcode: OpCode,
pc: u32,
message: S,
) -> Result<&Value, LogicalFrameEndingReason> {
self.peek().map_err(|_| self.trap(TRAP_STACK_UNDERFLOW, opcode as u16, message.into(), pc))
}
pub(super) fn binary_op<F>(
&mut self,
opcode: OpCode,
start_pc: u32,
f: F,
) -> Result<(), LogicalFrameEndingReason>
where
F: FnOnce(Value, Value) -> Result<Value, OpError>,
{
let b = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
match f(a, b) {
Ok(res) => {
self.push(res);
Ok(())
}
Err(OpError::Trap(code, msg)) => Err(self.trap(code, opcode as u16, msg, start_pc)),
}
}
}