pr 00.2
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
mod value;
|
||||
mod vm_init_error;
|
||||
pub mod program;
|
||||
|
||||
pub use program::ProgramImage;
|
||||
pub use value::Value;
|
||||
pub use vm_init_error::VmInitError;
|
||||
|
||||
|
||||
@@ -15,13 +15,7 @@ include = ["../../VERSION.txt"]
|
||||
|
||||
[dependencies]
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-core = { path = "../prometeu-core" }
|
||||
oxc_parser = "0.110.0"
|
||||
oxc_allocator = "0.110.0"
|
||||
oxc_ast = "0.110.0"
|
||||
oxc_ast_visit = "0.110.0"
|
||||
oxc_syntax = "0.110.0"
|
||||
oxc_span = "0.110.0"
|
||||
prometeu-abi = { path = "../prometeu-abi" }
|
||||
clap = { version = "4.5.54", features = ["derive"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
|
||||
@@ -2,8 +2,8 @@ use crate::building::output::CompiledModule;
|
||||
use crate::building::plan::BuildStep;
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo};
|
||||
use prometeu_core::virtual_machine::{ProgramImage, Value};
|
||||
use std::collections::HashMap;
|
||||
use prometeu_abi::virtual_machine::{ProgramImage, Value};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum LinkError {
|
||||
|
||||
@@ -3,8 +3,8 @@ use crate::building::output::{compile_project, CompileError};
|
||||
use crate::building::plan::{BuildPlan, BuildTarget};
|
||||
use crate::common::files::FileManager;
|
||||
use crate::deps::resolver::ResolvedGraph;
|
||||
use prometeu_core::virtual_machine::ProgramImage;
|
||||
use std::collections::HashMap;
|
||||
use prometeu_abi::virtual_machine::ProgramImage;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BuildError {
|
||||
|
||||
@@ -1,83 +1,83 @@
|
||||
use prometeu_compiler::compiler::compile;
|
||||
use prometeu_core::hardware::{AssetBridge, AssetManager, Audio, AudioBridge, Gfx, GfxBridge, HardwareBridge, MemoryBanks, Pad, PadBridge, Touch, TouchBridge};
|
||||
use prometeu_core::virtual_machine::{HostContext, HostReturn, LogicalFrameEndingReason, NativeInterface, Value, VirtualMachine, VmFault};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct SimpleNative;
|
||||
impl NativeInterface for SimpleNative {
|
||||
fn syscall(&mut self, _id: u32, _args: &[Value], _ret: &mut HostReturn, _ctx: &mut HostContext) -> Result<(), VmFault> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct SimpleHardware {
|
||||
gfx: Gfx,
|
||||
audio: Audio,
|
||||
pad: Pad,
|
||||
touch: Touch,
|
||||
assets: AssetManager,
|
||||
}
|
||||
|
||||
impl SimpleHardware {
|
||||
fn new() -> Self {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
Self {
|
||||
gfx: Gfx::new(320, 240, banks.clone()),
|
||||
audio: Audio::new(banks.clone()),
|
||||
pad: Pad::default(),
|
||||
touch: Touch::default(),
|
||||
assets: AssetManager::new(vec![], vec![], banks.clone(), banks.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HardwareBridge for SimpleHardware {
|
||||
fn gfx(&self) -> &dyn GfxBridge { &self.gfx }
|
||||
fn gfx_mut(&mut self) -> &mut dyn GfxBridge { &mut self.gfx }
|
||||
fn audio(&self) -> &dyn AudioBridge { &self.audio }
|
||||
fn audio_mut(&mut self) -> &mut dyn AudioBridge { &mut self.audio }
|
||||
fn pad(&self) -> &dyn PadBridge { &self.pad }
|
||||
fn pad_mut(&mut self) -> &mut dyn PadBridge { &mut self.pad }
|
||||
fn touch(&self) -> &dyn TouchBridge { &self.touch }
|
||||
fn touch_mut(&mut self) -> &mut dyn TouchBridge { &mut self.touch }
|
||||
fn assets(&self) -> &dyn AssetBridge { &self.assets }
|
||||
fn assets_mut(&mut self) -> &mut dyn AssetBridge { &mut self.assets }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_integration_test01_link() {
|
||||
let project_dir = PathBuf::from("../../test-cartridges/test01");
|
||||
// Since the test runs from crates/prometeu-compiler, we need to adjust path if necessary.
|
||||
// Actually, usually tests run from the workspace root if using cargo test --workspace,
|
||||
// but if running from the crate dir, it's different.
|
||||
|
||||
// Let's try absolute path or relative to project root.
|
||||
let mut root_dir = std::env::current_dir().unwrap();
|
||||
while !root_dir.join("test-cartridges").exists() {
|
||||
if let Some(parent) = root_dir.parent() {
|
||||
root_dir = parent.to_path_buf();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _project_dir = root_dir.join("test-cartridges/test01");
|
||||
|
||||
let unit = compile(&project_dir).expect("Failed to compile and link");
|
||||
|
||||
let mut vm = VirtualMachine::default();
|
||||
// Use initialize to load the ROM; entrypoint must be numeric or empty (defaults to 0)
|
||||
vm.initialize(unit.rom, "frame").expect("Failed to initialize VM");
|
||||
|
||||
let mut native = SimpleNative;
|
||||
let mut hw = SimpleHardware::new();
|
||||
let mut ctx = HostContext::new(Some(&mut hw));
|
||||
|
||||
// Run for a bit
|
||||
let report = vm.run_budget(1000, &mut native, &mut ctx).expect("VM execution failed");
|
||||
|
||||
// It should not trap. test01 might loop or return.
|
||||
if let LogicalFrameEndingReason::Trap(t) = report.reason {
|
||||
panic!("VM trapped: {:?}", t);
|
||||
}
|
||||
}
|
||||
// use prometeu_compiler::compiler::compile;
|
||||
// use std::path::PathBuf;
|
||||
// use std::sync::Arc;
|
||||
// use prometeu_compiler::ir_vm::Value;
|
||||
// use prometeu_hardware_contract::{AssetBridge, AudioBridge, GfxBridge, HardwareBridge, HostContext, HostReturn, NativeInterface, PadBridge, TouchBridge};
|
||||
//
|
||||
// struct SimpleNative;
|
||||
// impl NativeInterface for SimpleNative {
|
||||
// fn syscall(&mut self, _id: u32, _args: &[Value], _ret: &mut HostReturn, _ctx: &mut HostContext) -> Result<(), VmFault> {
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// struct SimpleHardware {
|
||||
// gfx: Gfx,
|
||||
// audio: Audio,
|
||||
// pad: Pad,
|
||||
// touch: Touch,
|
||||
// assets: AssetManager,
|
||||
// }
|
||||
//
|
||||
// impl SimpleHardware {
|
||||
// fn new() -> Self {
|
||||
// let banks = Arc::new(MemoryBanks::new());
|
||||
// Self {
|
||||
// gfx: Gfx::new(320, 240, banks.clone()),
|
||||
// audio: Audio::new(banks.clone()),
|
||||
// pad: Pad::default(),
|
||||
// touch: Touch::default(),
|
||||
// assets: AssetManager::new(vec![], vec![], banks.clone(), banks.clone()),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// impl HardwareBridge for SimpleHardware {
|
||||
// fn gfx(&self) -> &dyn GfxBridge { &self.gfx }
|
||||
// fn gfx_mut(&mut self) -> &mut dyn GfxBridge { &mut self.gfx }
|
||||
// fn audio(&self) -> &dyn AudioBridge { &self.audio }
|
||||
// fn audio_mut(&mut self) -> &mut dyn AudioBridge { &mut self.audio }
|
||||
// fn pad(&self) -> &dyn PadBridge { &self.pad }
|
||||
// fn pad_mut(&mut self) -> &mut dyn PadBridge { &mut self.pad }
|
||||
// fn touch(&self) -> &dyn TouchBridge { &self.touch }
|
||||
// fn touch_mut(&mut self) -> &mut dyn TouchBridge { &mut self.touch }
|
||||
// fn assets(&self) -> &dyn AssetBridge { &self.assets }
|
||||
// fn assets_mut(&mut self) -> &mut dyn AssetBridge { &mut self.assets }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn test_integration_test01_link() {
|
||||
// let project_dir = PathBuf::from("../../test-cartridges/test01");
|
||||
// // Since the test runs from crates/prometeu-compiler, we need to adjust path if necessary.
|
||||
// // Actually, usually tests run from the workspace root if using cargo test --workspace,
|
||||
// // but if running from the crate dir, it's different.
|
||||
//
|
||||
// // Let's try absolute path or relative to project root.
|
||||
// let mut root_dir = std::env::current_dir().unwrap();
|
||||
// while !root_dir.join("test-cartridges").exists() {
|
||||
// if let Some(parent) = root_dir.parent() {
|
||||
// root_dir = parent.to_path_buf();
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// let _project_dir = root_dir.join("test-cartridges/test01");
|
||||
//
|
||||
// let unit = compile(&project_dir).expect("Failed to compile and link");
|
||||
//
|
||||
// let mut vm = VirtualMachine::default();
|
||||
// // Use initialize to load the ROM; entrypoint must be numeric or empty (defaults to 0)
|
||||
// vm.initialize(unit.rom, "frame").expect("Failed to initialize VM");
|
||||
//
|
||||
// let mut native = SimpleNative;
|
||||
// let mut hw = SimpleHardware::new();
|
||||
// let mut ctx = HostContext::new(Some(&mut hw));
|
||||
//
|
||||
// // Run for a bit
|
||||
// let report = vm.run_budget(1000, &mut native, &mut ctx).expect("VM execution failed");
|
||||
//
|
||||
// // It should not trap. test01 might loop or return.
|
||||
// if let LogicalFrameEndingReason::Trap(t) = report.reason {
|
||||
// panic!("VM trapped: {:?}", t);
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -7,6 +7,7 @@ license.workspace = true
|
||||
[dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
prometeu-vm = { path = "../prometeu-vm" }
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-abi = { path = "../prometeu-abi" }
|
||||
prometeu-hardware-contract = { path = "../prometeu-hardware-contract" }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use crate::firmware::boot_target::BootTarget;
|
||||
use crate::firmware::firmware_state::{FirmwareState, LoadCartridgeStep, ResetStep};
|
||||
use crate::firmware::prometeu_context::PrometeuContext;
|
||||
@@ -5,7 +6,6 @@ use crate::hardware::{HardwareBridge, InputSignals};
|
||||
use crate::model::Cartridge;
|
||||
use crate::prometeu_hub::PrometeuHub;
|
||||
use crate::prometeu_os::PrometeuOS;
|
||||
use crate::virtual_machine::VirtualMachine;
|
||||
|
||||
use crate::telemetry::CertificationConfig;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use crate::firmware::boot_target::BootTarget;
|
||||
use crate::hardware::{HardwareBridge, InputSignals};
|
||||
use crate::prometeu_hub::PrometeuHub;
|
||||
use crate::prometeu_os::PrometeuOS;
|
||||
use crate::virtual_machine::VirtualMachine;
|
||||
|
||||
pub struct PrometeuContext<'a> {
|
||||
pub vm: &'a mut VirtualMachine,
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
//! The "Source of Truth" for the console's behavior lives here.
|
||||
|
||||
pub mod hardware;
|
||||
pub mod virtual_machine;
|
||||
pub mod firmware;
|
||||
pub mod fs;
|
||||
pub mod prometeu_os;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
mod prometeu_os;
|
||||
|
||||
use crate::virtual_machine::NativeInterface;
|
||||
pub use prometeu_os::PrometeuOS;
|
||||
|
||||
@@ -3,12 +3,12 @@ use crate::fs::{FsBackend, FsState, VirtualFS};
|
||||
use crate::hardware::{HardwareBridge, InputSignals};
|
||||
use crate::log::{LogLevel, LogService, LogSource};
|
||||
use crate::model::{BankType, Cartridge, Color};
|
||||
use crate::prometeu_os::NativeInterface;
|
||||
use crate::telemetry::{CertificationConfig, Certifier, TelemetryFrame};
|
||||
use crate::virtual_machine::{HostContext, HostReturn, SyscallId, Value, VirtualMachine, VmFault};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Instant;
|
||||
use prometeu_hardware_contract::{expect_bool, expect_int};
|
||||
use prometeu_abi::virtual_machine::{Value, VmFault};
|
||||
use prometeu_hardware_contract::{expect_bool, expect_int, HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
use prometeu_vm::{LogicalFrameEndingReason, VirtualMachine};
|
||||
|
||||
/// PrometeuOS (POS): The central system firmware and resource manager.
|
||||
///
|
||||
@@ -251,22 +251,22 @@ impl PrometeuOS {
|
||||
self.telemetry_current.vm_steps += run.steps_executed;
|
||||
|
||||
// Handle Breakpoints
|
||||
if run.reason == crate::virtual_machine::LogicalFrameEndingReason::Breakpoint {
|
||||
if run.reason == LogicalFrameEndingReason::Breakpoint {
|
||||
self.paused = true;
|
||||
self.debug_step_request = false;
|
||||
self.log(LogLevel::Info, LogSource::Vm, 0xDEB1, format!("Breakpoint hit at PC 0x{:X}", vm.pc));
|
||||
}
|
||||
|
||||
// Handle Panics
|
||||
if let crate::virtual_machine::LogicalFrameEndingReason::Panic(err) = run.reason {
|
||||
if let LogicalFrameEndingReason::Panic(err) = run.reason {
|
||||
let err_msg = format!("PVM Fault: \"{}\"", err);
|
||||
self.log(LogLevel::Error, LogSource::Vm, 0, err_msg.clone());
|
||||
return Some(err_msg);
|
||||
}
|
||||
|
||||
// 4. Frame Finalization (FRAME_SYNC reached or Entrypoint returned)
|
||||
if run.reason == crate::virtual_machine::LogicalFrameEndingReason::FrameSync ||
|
||||
run.reason == crate::virtual_machine::LogicalFrameEndingReason::EndOfRom {
|
||||
if run.reason == LogicalFrameEndingReason::FrameSync ||
|
||||
run.reason == LogicalFrameEndingReason::EndOfRom {
|
||||
// All drawing commands for this frame are now complete.
|
||||
// Finalize the framebuffer.
|
||||
hw.gfx_mut().render_all();
|
||||
@@ -415,10 +415,11 @@ impl PrometeuOS {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prometeu_abi::virtual_machine::Value;
|
||||
use prometeu_hardware_contract::HostReturn;
|
||||
use super::*;
|
||||
use crate::hardware::InputSignals;
|
||||
use crate::model::{AppMode, Cartridge};
|
||||
use crate::virtual_machine::{Value, VirtualMachine};
|
||||
use crate::Hardware;
|
||||
|
||||
fn call_syscall(os: &mut PrometeuOS, id: u32, vm: &mut VirtualMachine, hw: &mut dyn HardwareBridge) -> Result<(), VmFault> {
|
||||
@@ -768,7 +769,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_gfx_clear565_syscall() {
|
||||
let mut hw = crate::Hardware::new();
|
||||
let mut hw = Hardware::new();
|
||||
let mut os = PrometeuOS::new(None);
|
||||
let mut stack = Vec::new();
|
||||
|
||||
@@ -795,7 +796,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_input_snapshots_syscalls() {
|
||||
let mut hw = crate::Hardware::new();
|
||||
let mut hw = Hardware::new();
|
||||
let mut os = PrometeuOS::new(None);
|
||||
|
||||
// Pad snapshot
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Represents any piece of data that can be stored on the VM stack or in globals.
|
||||
///
|
||||
/// The PVM is "dynamically typed" at the bytecode level, meaning a single
|
||||
/// `Value` enum can hold different primitive types. The VM performs
|
||||
/// automatic type promotion (e.g., adding an Int32 to a Float64 results
|
||||
/// in a Float64) to ensure mathematical correctness.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Value {
|
||||
/// 32-bit signed integer. Used for most loop counters and indices.
|
||||
Int32(i32),
|
||||
/// 64-bit signed integer. Used for large numbers and timestamps.
|
||||
Int64(i64),
|
||||
/// 64-bit double precision float. Used for physics and complex math.
|
||||
Float(f64),
|
||||
/// Boolean value (true/false).
|
||||
Boolean(bool),
|
||||
/// UTF-8 string. Strings are immutable and usually come from the Constant Pool.
|
||||
String(String),
|
||||
/// Bounded 16-bit-ish integer.
|
||||
Bounded(u32),
|
||||
/// A pointer to an object on the heap.
|
||||
Gate(usize),
|
||||
/// Represents the absence of a value (equivalent to `null` or `undefined`).
|
||||
Null,
|
||||
}
|
||||
|
||||
impl PartialEq for Value {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(Value::Int32(a), Value::Int32(b)) => a == b,
|
||||
(Value::Int64(a), Value::Int64(b)) => a == b,
|
||||
(Value::Int32(a), Value::Int64(b)) => *a as i64 == *b,
|
||||
(Value::Int64(a), Value::Int32(b)) => *a == *b as i64,
|
||||
(Value::Float(a), Value::Float(b)) => a == b,
|
||||
(Value::Int32(a), Value::Float(b)) => *a as f64 == *b,
|
||||
(Value::Float(a), Value::Int32(b)) => *a == *b as f64,
|
||||
(Value::Int64(a), Value::Float(b)) => *a as f64 == *b,
|
||||
(Value::Float(a), Value::Int64(b)) => *a == *b as f64,
|
||||
(Value::Boolean(a), Value::Boolean(b)) => a == b,
|
||||
(Value::String(a), Value::String(b)) => a == b,
|
||||
(Value::Bounded(a), Value::Bounded(b)) => a == b,
|
||||
(Value::Gate(a), Value::Gate(b)) => a == b,
|
||||
(Value::Null, Value::Null) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Value {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
match (self, other) {
|
||||
(Value::Int32(a), Value::Int32(b)) => a.partial_cmp(b),
|
||||
(Value::Int64(a), Value::Int64(b)) => a.partial_cmp(b),
|
||||
(Value::Int32(a), Value::Int64(b)) => (*a as i64).partial_cmp(b),
|
||||
(Value::Int64(a), Value::Int32(b)) => a.partial_cmp(&(*b as i64)),
|
||||
(Value::Float(a), Value::Float(b)) => a.partial_cmp(b),
|
||||
(Value::Bounded(a), Value::Bounded(b)) => a.partial_cmp(b),
|
||||
(Value::Int32(a), Value::Float(b)) => (*a as f64).partial_cmp(b),
|
||||
(Value::Float(a), Value::Int32(b)) => a.partial_cmp(&(*b as f64)),
|
||||
(Value::Int64(a), Value::Float(b)) => (*a as f64).partial_cmp(b),
|
||||
(Value::Float(a), Value::Int64(b)) => a.partial_cmp(&(*b as f64)),
|
||||
(Value::Boolean(a), Value::Boolean(b)) => a.partial_cmp(b),
|
||||
(Value::String(a), Value::String(b)) => a.partial_cmp(b),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn as_float(&self) -> Option<f64> {
|
||||
match self {
|
||||
Value::Int32(i) => Some(*i as f64),
|
||||
Value::Int64(i) => Some(*i as f64),
|
||||
Value::Float(f) => Some(*f),
|
||||
Value::Bounded(b) => Some(*b as f64),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_integer(&self) -> Option<i64> {
|
||||
match self {
|
||||
Value::Int32(i) => Some(*i as i64),
|
||||
Value::Int64(i) => Some(*i),
|
||||
Value::Float(f) => Some(*f as i64),
|
||||
Value::Bounded(b) => Some(*b as i64),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Value::Int32(i) => i.to_string(),
|
||||
Value::Int64(i) => i.to_string(),
|
||||
Value::Float(f) => f.to_string(),
|
||||
Value::Bounded(b) => format!("{}b", b),
|
||||
Value::Boolean(b) => b.to_string(),
|
||||
Value::String(s) => s.clone(),
|
||||
Value::Gate(r) => format!("[Gate {}]", r),
|
||||
Value::Null => "null".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_value_equality() {
|
||||
assert_eq!(Value::Int32(10), Value::Int32(10));
|
||||
assert_eq!(Value::Int64(10), Value::Int64(10));
|
||||
assert_eq!(Value::Int32(10), Value::Int64(10));
|
||||
assert_eq!(Value::Int64(10), Value::Int32(10));
|
||||
assert_eq!(Value::Float(10.5), Value::Float(10.5));
|
||||
assert_eq!(Value::Int32(10), Value::Float(10.0));
|
||||
assert_eq!(Value::Float(10.0), Value::Int32(10));
|
||||
assert_eq!(Value::Int64(10), Value::Float(10.0));
|
||||
assert_eq!(Value::Float(10.0), Value::Int64(10));
|
||||
assert_ne!(Value::Int32(10), Value::Int32(11));
|
||||
assert_ne!(Value::Int64(10), Value::Int64(11));
|
||||
assert_ne!(Value::Int32(10), Value::Int64(11));
|
||||
assert_ne!(Value::Int32(10), Value::Float(10.1));
|
||||
assert_eq!(Value::Boolean(true), Value::Boolean(true));
|
||||
assert_ne!(Value::Boolean(true), Value::Boolean(false));
|
||||
assert_eq!(Value::String("oi".into()), Value::String("oi".into()));
|
||||
assert_eq!(Value::Null, Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_value_conversions() {
|
||||
let v_int32 = Value::Int32(42);
|
||||
assert_eq!(v_int32.as_float(), Some(42.0));
|
||||
assert_eq!(v_int32.as_integer(), Some(42));
|
||||
|
||||
let v_int64 = Value::Int64(42);
|
||||
assert_eq!(v_int64.as_float(), Some(42.0));
|
||||
assert_eq!(v_int64.as_integer(), Some(42));
|
||||
|
||||
let v_float = Value::Float(42.7);
|
||||
assert_eq!(v_float.as_float(), Some(42.7));
|
||||
assert_eq!(v_float.as_integer(), Some(42));
|
||||
|
||||
let v_bool = Value::Boolean(true);
|
||||
assert_eq!(v_bool.as_float(), None);
|
||||
assert_eq!(v_bool.as_integer(), None);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
use prometeu_core::virtual_machine::HostReturn;
|
||||
use prometeu_core::virtual_machine::NativeInterface;
|
||||
use prometeu_core::virtual_machine::Value;
|
||||
use prometeu_core::virtual_machine::{HostContext, LogicalFrameEndingReason, VirtualMachine};
|
||||
use prometeu_core::Hardware;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use prometeu_abi::virtual_machine::{Value, VmFault};
|
||||
use prometeu_hardware_contract::{HostContext, HostReturn, NativeInterface};
|
||||
use prometeu_vm::{LogicalFrameEndingReason, VirtualMachine};
|
||||
|
||||
struct MockNative;
|
||||
impl NativeInterface for MockNative {
|
||||
fn syscall(&mut self, id: u32, _args: &[Value], ret: &mut HostReturn, _ctx: &mut HostContext) -> Result<(), prometeu_core::virtual_machine::VmFault> {
|
||||
fn syscall(&mut self, id: u32, _args: &[Value], ret: &mut HostReturn, _ctx: &mut HostContext) -> Result<(), VmFault> {
|
||||
if id == 0x2010 { // InputPadSnapshot
|
||||
for _ in 0..48 {
|
||||
ret.push_bool(false);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "prometeu-vm"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-abi = { path = "../prometeu-abi" }
|
||||
prometeu-hardware-contract = { path = "../prometeu-hardware-contract" }
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
use crate::virtual_machine::opcode_spec::{OpCodeSpecExt, OpcodeSpec};
|
||||
use crate::opcode_spec::{OpCodeSpecExt, OpcodeSpec};
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -1,13 +1,12 @@
|
||||
mod virtual_machine;
|
||||
mod call_frame;
|
||||
mod scope_frame;
|
||||
mod program;
|
||||
pub mod local_addressing;
|
||||
pub mod opcode_spec;
|
||||
pub mod bytecode;
|
||||
pub mod verifier;
|
||||
|
||||
pub use program::ProgramImage;
|
||||
pub use prometeu_abi::virtual_machine::program::ProgramImage;
|
||||
pub use prometeu_abi::virtual_machine::{Value, VmFault};
|
||||
pub use prometeu_bytecode::abi::TrapInfo;
|
||||
pub use prometeu_bytecode::opcode::OpCode;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
use crate::virtual_machine::call_frame::CallFrame;
|
||||
use crate::call_frame::CallFrame;
|
||||
use prometeu_bytecode::abi::{TrapInfo, TRAP_INVALID_LOCAL};
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
use crate::abi::syscalls::Syscall;
|
||||
use crate::virtual_machine::bytecode::decoder::{decode_at, DecodeError};
|
||||
use prometeu_abi::syscalls::Syscall;
|
||||
use crate::bytecode::decoder::{decode_at, DecodeError};
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
+17
-18
@@ -1,7 +1,7 @@
|
||||
use crate::virtual_machine::call_frame::CallFrame;
|
||||
use crate::virtual_machine::scope_frame::ScopeFrame;
|
||||
use crate::virtual_machine::Value;
|
||||
use crate::virtual_machine::{HostContext, NativeInterface, ProgramImage, VmInitError};
|
||||
use crate::call_frame::CallFrame;
|
||||
use crate::scope_frame::ScopeFrame;
|
||||
use crate::Value;
|
||||
use crate::{HostContext, NativeInterface, ProgramImage, VmInitError};
|
||||
use prometeu_bytecode::abi::{TrapInfo, TRAP_BAD_RET_SLOTS, TRAP_DIV_ZERO, TRAP_INVALID_FUNC, TRAP_OOB, TRAP_TYPE};
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
|
||||
@@ -125,7 +125,7 @@ impl VirtualMachine {
|
||||
match prometeu_bytecode::BytecodeLoader::load(&program_bytes) {
|
||||
Ok(module) => {
|
||||
// Run verifier on the module
|
||||
let max_stacks = crate::virtual_machine::verifier::Verifier::verify(&module.code, &module.functions)
|
||||
let max_stacks = crate::verifier::Verifier::verify(&module.code, &module.functions)
|
||||
.map_err(VmInitError::VerificationFailed)?;
|
||||
|
||||
let mut program = ProgramImage::from(module);
|
||||
@@ -342,7 +342,7 @@ impl VirtualMachine {
|
||||
let start_pc = self.pc;
|
||||
|
||||
// Fetch & Decode
|
||||
let instr = crate::virtual_machine::bytecode::decoder::decode_at(&self.program.rom, self.pc)
|
||||
let instr = crate::bytecode::decoder::decode_at(&self.program.rom, self.pc)
|
||||
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
|
||||
let opcode = instr.opcode;
|
||||
@@ -699,10 +699,10 @@ impl VirtualMachine {
|
||||
let frame = self.call_stack.last().ok_or_else(|| LogicalFrameEndingReason::Panic("No active call frame".into()))?;
|
||||
let func = &self.program.functions[frame.func_idx];
|
||||
|
||||
crate::virtual_machine::local_addressing::check_local_slot(func, slot, opcode as u16, start_pc as u32)
|
||||
crate::local_addressing::check_local_slot(func, slot, opcode as u16, start_pc as u32)
|
||||
.map_err(|trap_info| self.trap(trap_info.code, trap_info.opcode, trap_info.message, trap_info.pc))?;
|
||||
|
||||
let stack_idx = crate::virtual_machine::local_addressing::local_index(frame, slot);
|
||||
let stack_idx = crate::local_addressing::local_index(frame, slot);
|
||||
let val = self.operand_stack.get(stack_idx).cloned().ok_or_else(|| LogicalFrameEndingReason::Panic("Internal error: validated local slot not found in stack".into()))?;
|
||||
self.push(val);
|
||||
}
|
||||
@@ -712,10 +712,10 @@ impl VirtualMachine {
|
||||
let frame = self.call_stack.last().ok_or_else(|| LogicalFrameEndingReason::Panic("No active call frame".into()))?;
|
||||
let func = &self.program.functions[frame.func_idx];
|
||||
|
||||
crate::virtual_machine::local_addressing::check_local_slot(func, slot, opcode as u16, start_pc as u32)
|
||||
crate::local_addressing::check_local_slot(func, slot, opcode as u16, start_pc as u32)
|
||||
.map_err(|trap_info| self.trap(trap_info.code, trap_info.opcode, trap_info.message, trap_info.pc))?;
|
||||
|
||||
let stack_idx = crate::virtual_machine::local_addressing::local_index(frame, slot);
|
||||
let stack_idx = crate::local_addressing::local_index(frame, slot);
|
||||
self.operand_stack[stack_idx] = val;
|
||||
}
|
||||
OpCode::Call => {
|
||||
@@ -828,8 +828,7 @@ impl VirtualMachine {
|
||||
let pc_at_syscall = start_pc as u32;
|
||||
|
||||
let id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
|
||||
let syscall = crate::abi::syscalls::Syscall::from_u32(id).ok_or_else(|| {
|
||||
let syscall = prometeu_abi::syscalls::Syscall::from_u32(id).ok_or_else(|| {
|
||||
self.trap(prometeu_bytecode::abi::TRAP_INVALID_SYSCALL, OpCode::Syscall as u16, format!("Unknown syscall: 0x{:08X}", id), pc_at_syscall)
|
||||
})?;
|
||||
|
||||
@@ -845,11 +844,11 @@ impl VirtualMachine {
|
||||
args.reverse();
|
||||
|
||||
let stack_height_before = self.operand_stack.len();
|
||||
let mut ret = crate::virtual_machine::HostReturn::new(&mut self.operand_stack);
|
||||
let mut ret = crate::HostReturn::new(&mut self.operand_stack);
|
||||
native.syscall(id, &args, &mut ret, ctx).map_err(|fault| match fault {
|
||||
crate::virtual_machine::VmFault::Trap(code, msg) => self.trap(code, OpCode::Syscall as u16, msg, pc_at_syscall),
|
||||
crate::virtual_machine::VmFault::Panic(msg) => LogicalFrameEndingReason::Panic(msg),
|
||||
crate::virtual_machine::VmFault::Unavailable => LogicalFrameEndingReason::Panic("Host feature unavailable".into()),
|
||||
crate::VmFault::Trap(code, msg) => self.trap(code, OpCode::Syscall as u16, msg, pc_at_syscall),
|
||||
crate::VmFault::Panic(msg) => LogicalFrameEndingReason::Panic(msg),
|
||||
crate::VmFault::Unavailable => LogicalFrameEndingReason::Panic("Host feature unavailable".into()),
|
||||
})?;
|
||||
|
||||
let stack_height_after = self.operand_stack.len();
|
||||
@@ -931,7 +930,7 @@ mod tests {
|
||||
}]);
|
||||
vm
|
||||
}
|
||||
use crate::virtual_machine::{HostReturn, Value, VmFault};
|
||||
use crate::{HostReturn, Value, VmFault};
|
||||
use prometeu_bytecode::abi::SourceSpan;
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use prometeu_hardware_contract::expect_int;
|
||||
@@ -1845,7 +1844,7 @@ mod tests {
|
||||
let res = ret.push_bounded(65536);
|
||||
assert!(res.is_err());
|
||||
match res.err().unwrap() {
|
||||
crate::virtual_machine::VmFault::Trap(code, _) => {
|
||||
crate::VmFault::Trap(code, _) => {
|
||||
assert_eq!(code, prometeu_bytecode::abi::TRAP_OOB);
|
||||
}
|
||||
_ => panic!("Expected Trap"),
|
||||
Reference in New Issue
Block a user