This commit is contained in:
Nilton Constantino
2026-02-02 19:57:04 +00:00
parent b6912c4369
commit b4deaa243e
19 changed files with 66 additions and 136 deletions
@@ -434,10 +434,10 @@ mod tests {
let mut hw = Hardware::new();
let signals = InputSignals::default();
let rom = prometeu_bytecode::v0::BytecodeModule {
let rom = prometeu_bytecode::BytecodeModule {
version: 0,
const_pool: vec![],
functions: vec![prometeu_bytecode::v0::FunctionMeta {
functions: vec![prometeu_bytecode::FunctionMeta {
code_offset: 0,
code_len: 6,
param_slots: 0,
@@ -447,7 +447,7 @@ mod tests {
}],
code: vec![0x02, 0x00, 0x00, 0x00, 0x00, 0x00],
debug_info: None,
exports: vec![prometeu_bytecode::v0::Export { symbol: "main".into(), func_idx: 0 }],
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
imports: vec![],
}.serialize();
let cartridge = Cartridge {
@@ -488,10 +488,10 @@ mod tests {
// PUSH_CONST 0 (dummy)
// FrameSync (0x80)
// JMP 0
let rom = prometeu_bytecode::v0::BytecodeModule {
let rom = prometeu_bytecode::BytecodeModule {
version: 0,
const_pool: vec![],
functions: vec![prometeu_bytecode::v0::FunctionMeta {
functions: vec![prometeu_bytecode::FunctionMeta {
code_offset: 0,
code_len: 8,
param_slots: 0,
@@ -504,7 +504,7 @@ mod tests {
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // JMP 0 (2 bytes opcode + 4 bytes u32)
],
debug_info: None,
exports: vec![prometeu_bytecode::v0::Export { symbol: "main".into(), func_idx: 0 }],
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
imports: vec![],
}.serialize();
let cartridge = Cartridge {
@@ -702,10 +702,10 @@ mod tests {
let signals = InputSignals::default();
// PushI32 0 (0x17), then Ret (0x51)
let rom = prometeu_bytecode::v0::BytecodeModule {
let rom = prometeu_bytecode::BytecodeModule {
version: 0,
const_pool: vec![],
functions: vec![prometeu_bytecode::v0::FunctionMeta {
functions: vec![prometeu_bytecode::FunctionMeta {
code_offset: 0,
code_len: 10,
param_slots: 0,
@@ -720,7 +720,7 @@ mod tests {
0x51, 0x00 // Ret
],
debug_info: None,
exports: vec![prometeu_bytecode::v0::Export { symbol: "main".into(), func_idx: 0 }],
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
imports: vec![],
}.serialize();
let cartridge = Cartridge {
@@ -1,6 +1,6 @@
use crate::virtual_machine::call_frame::CallFrame;
use prometeu_bytecode::abi::{TrapInfo, TRAP_INVALID_LOCAL};
use prometeu_bytecode::v0::FunctionMeta;
use prometeu_bytecode::FunctionMeta;
/// Computes the absolute stack index for the start of the current frame's locals (including args).
pub fn local_base(frame: &CallFrame) -> usize {
@@ -28,7 +28,7 @@ pub enum VmFault {
pub enum VmInitError {
InvalidFormat,
UnsupportedFormat,
PbsV0LoadFailed(prometeu_bytecode::v0::LoadError),
PbsV0LoadFailed(prometeu_bytecode::LoadError),
EntrypointNotFound,
VerificationFailed(VerifierError),
}
@@ -1,6 +1,6 @@
use crate::virtual_machine::Value;
use prometeu_bytecode::abi::TrapInfo;
use prometeu_bytecode::v0::{BytecodeModule, ConstantPoolEntry, DebugInfo, Export, FunctionMeta};
use prometeu_bytecode::{BytecodeModule, ConstantPoolEntry, DebugInfo, Export, FunctionMeta};
use std::collections::HashMap;
use std::sync::Arc;
@@ -1,6 +1,6 @@
use crate::virtual_machine::bytecode::decoder::{decode_at, DecodeError};
use prometeu_bytecode::opcode::OpCode;
use prometeu_bytecode::v0::FunctionMeta;
use prometeu_bytecode::FunctionMeta;
use std::collections::{HashMap, HashSet, VecDeque};
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -123,7 +123,7 @@ impl VirtualMachine {
// Only recognized format is loadable: PBS v0 industrial format
let program = if program_bytes.starts_with(b"PBS\0") {
match prometeu_bytecode::v0::BytecodeLoader::load(&program_bytes) {
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)
@@ -139,7 +139,7 @@ impl VirtualMachine {
program
}
Err(prometeu_bytecode::v0::LoadError::InvalidVersion) => return Err(VmInitError::UnsupportedFormat),
Err(prometeu_bytecode::LoadError::InvalidVersion) => return Err(VmInitError::UnsupportedFormat),
Err(e) => {
return Err(VmInitError::PbsV0LoadFailed(e));
}
@@ -148,7 +148,7 @@ impl VirtualMachine {
return Err(VmInitError::InvalidFormat);
};
// Resolve the entrypoint. Currently supports numeric addresses, symbolic exports, or empty (defaults to 0).
// Resolve the entrypoint: only empty (defaults to 0) or numeric PC are allowed.
let pc = if entrypoint.is_empty() {
0
} else if let Ok(addr) = entrypoint.parse::<usize>() {
@@ -157,16 +157,10 @@ impl VirtualMachine {
}
addr
} else {
// Try to resolve symbol name via ProgramImage exports
if let Some(&func_idx) = program.exports.get(entrypoint) {
program.functions[func_idx as usize].code_offset as usize
} else if let Some(&func_idx) = program.exports.get(&format!("src/main/modules:{}", entrypoint)) {
program.functions[func_idx as usize].code_offset as usize
} else {
return Err(VmInitError::EntrypointNotFound);
}
// No symbol lookup by name in runtime. Linking is compiler-owned.
return Err(VmInitError::EntrypointNotFound);
};
// Finalize initialization by applying the new program and PC.
self.program = program;
self.pc = pc;
@@ -183,11 +177,8 @@ impl VirtualMachine {
addr >= f.code_offset as usize && addr < (f.code_offset + f.code_len) as usize
}).unwrap_or(0);
(addr, idx)
} else if let Some(&func_idx) = self.program.exports.get(entrypoint) {
(self.program.functions[func_idx as usize].code_offset as usize, func_idx as usize)
} else if let Some(&func_idx) = self.program.exports.get(&format!("src/main/modules:{}", entrypoint)) {
(self.program.functions[func_idx as usize].code_offset as usize, func_idx as usize)
} else {
// No symbol lookup by name in runtime. Default to 0 for non-numeric entrypoints.
(0, 0)
};
@@ -921,7 +912,7 @@ mod tests {
use crate::hardware::HardwareBridge;
use crate::virtual_machine::{expect_int, HostReturn, Value, VmFault};
use prometeu_bytecode::abi::SourceSpan;
use prometeu_bytecode::v0::FunctionMeta;
use prometeu_bytecode::FunctionMeta;
struct MockNative;
impl NativeInterface for MockNative {
@@ -1862,7 +1853,7 @@ mod tests {
let res = vm.initialize(header, "");
match res {
Err(VmInitError::PbsV0LoadFailed(prometeu_bytecode::v0::LoadError::UnexpectedEof)) => {},
Err(VmInitError::PbsV0LoadFailed(prometeu_bytecode::LoadError::UnexpectedEof)) => {},
_ => panic!("Expected PbsV0LoadFailed(UnexpectedEof), got {:?}", res),
}
}
@@ -2424,7 +2415,7 @@ mod tests {
pc_to_span.push((6, SourceSpan { file_id: 1, start: 16, end: 20 }));
pc_to_span.push((12, SourceSpan { file_id: 1, start: 21, end: 25 }));
let debug_info = prometeu_bytecode::v0::DebugInfo {
let debug_info = prometeu_bytecode::DebugInfo {
pc_to_span,
function_names: vec![(0, "main".to_string())],
};
@@ -2465,7 +2456,7 @@ mod tests {
let pc_to_span = vec![(12, SourceSpan { file_id: 1, start: 21, end: 25 })];
let function_names = vec![(0, "math_utils::divide".to_string())];
let debug_info = prometeu_bytecode::v0::DebugInfo {
let debug_info = prometeu_bytecode::DebugInfo {
pc_to_span,
function_names,
};
+14 -2
View File
@@ -6,6 +6,7 @@ use prometeu_core::virtual_machine::{LogicalFrameEndingReason, VirtualMachine};
use prometeu_core::Hardware;
use std::fs;
use std::path::Path;
use prometeu_bytecode::BytecodeLoader;
struct MockNative;
impl NativeInterface for MockNative {
@@ -37,10 +38,21 @@ fn test_canonical_cartridge_heartbeat() {
}
let pbc_bytes = fs::read(pbc_path).expect("Failed to read canonical PBC. Did you run the generation test?");
// Determine numeric entrypoint PC from the compiled module exports
let module = BytecodeLoader::load(&pbc_bytes).expect("Failed to parse PBC");
let func_idx = module
.exports
.iter()
.find(|e| e.symbol == "src/main/modules:frame")
.map(|e| e.func_idx as usize)
.expect("Entrypoint symbol not found in exports");
let entry_pc = module.functions[func_idx].code_offset as usize;
let entry_pc_str = entry_pc.to_string();
let mut vm = VirtualMachine::new(vec![], vec![]);
vm.initialize(pbc_bytes, "src/main/modules:frame").expect("Failed to initialize VM with canonical cartridge");
vm.prepare_call("src/main/modules:frame");
vm.initialize(pbc_bytes, &entry_pc_str).expect("Failed to initialize VM with canonical cartridge");
vm.prepare_call(&entry_pc_str);
let mut native = MockNative;
let mut hw = Hardware::new();