feat(pbs): add service constants and runtime math integration
This commit is contained in:
@@ -36,6 +36,8 @@ impl NativeInterface for NoNative {
|
||||
|
||||
fn main() {
|
||||
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory"));
|
||||
verify_pbs_math_fixtures(&root);
|
||||
if std::env::args().any(|arg| arg == "--math-only") { return; }
|
||||
verify_catalog_fixtures(&root);
|
||||
verify_pbs_fixtures(&root);
|
||||
verify_pbs_structured_fixtures(&root);
|
||||
@@ -475,7 +477,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
|
||||
for entry in std::fs::read_dir(root).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
let name = path.file_stem().unwrap().to_str().unwrap();
|
||||
if name.starts_with("pbs-structured-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
if name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
count += 1;
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
||||
@@ -630,3 +632,97 @@ fn verify_pbs_closure_frames(root: &std::path::Path) {
|
||||
}
|
||||
println!("PBS CLOSURE FRAME LIFECYCLE/RETENTION/GC PASS");
|
||||
}
|
||||
|
||||
// The expected results/call costs are independent provider fixtures. Step costs
|
||||
// use the published opcode contract, not observed timings or compiler estimates.
|
||||
fn verify_pbs_math_fixtures(root: &std::path::Path) {
|
||||
use prometeu_vm::LogicalFrameEndingReason as End;
|
||||
use prometeu_bytecode::isa::core::CoreOpCode as OpCode;
|
||||
let corpus = std::fs::read_to_string(root.join("math-vectors.tsv")).expect("mandatory math corpus");
|
||||
let mut covered = std::collections::BTreeSet::new();
|
||||
let mut count = 0;
|
||||
let mut original_cycles = std::collections::BTreeMap::new();
|
||||
let mut names = std::collections::BTreeSet::new();
|
||||
for line in corpus.lines() {
|
||||
let parts: Vec<_> = line.split_whitespace().collect();
|
||||
let name = parts[0];
|
||||
assert!(names.insert(parts[0]), "duplicate math fixture");
|
||||
let id = u32::from_str_radix(parts[1], 16).unwrap();
|
||||
let expected = parts[parts.len()-2];
|
||||
let cost: u64 = parts.last().unwrap().parse().unwrap();
|
||||
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let code = image.code().to_vec();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let mut total = 0;
|
||||
let mut calls = 0;
|
||||
let ending = loop {
|
||||
let pc = vm.pc();
|
||||
let opcode = OpCode::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
|
||||
let cycles = if opcode == OpCode::Intrinsic {
|
||||
let actual = u32::from_le_bytes(code[pc+2..pc+6].try_into().unwrap());
|
||||
assert_eq!(actual, id, "{name}: wrong overload or unexpected conversion");
|
||||
calls += 1;
|
||||
cost
|
||||
} else { opcode.cycles() };
|
||||
let report = vm.run_budget(1, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert_eq!(report.cycles_used, cycles, "{name}: cycle charge at {pc}, {opcode:?}");
|
||||
total += cycles;
|
||||
if matches!(report.reason, End::Halted | End::EndOfRom | End::Trap(_)) { break report.reason; }
|
||||
assert!(total < 10000, "{name}: nonterminating source fixture");
|
||||
};
|
||||
assert_eq!(calls, 1, "{name}: preserve literal calls and no hidden conversions");
|
||||
if let Some(code) = expected.strip_prefix("trap:") {
|
||||
let code = u32::from_str_radix(code,16).unwrap();
|
||||
assert!(matches!(ending, End::Trap(ref trap) if trap.code == code), "{name}: {ending:?}");
|
||||
} else {
|
||||
assert!(matches!(ending, End::Halted | End::EndOfRom), "{name}: {ending:?}");
|
||||
let value = vm.operand_stack_top(1);
|
||||
let actual = match &value[0] {
|
||||
Value::Float(value) => format!("f:{:016x}",value.to_bits()),
|
||||
Value::Int32(value) => format!("i:{value}"),
|
||||
Value::Int64(value) => format!("l:{value}"),
|
||||
value => panic!("{name}: unexpected value {value:?}"),
|
||||
};
|
||||
assert_eq!(actual, expected, "{name}: exact result bits");
|
||||
}
|
||||
// A full-budget execution must charge exactly the independently checked steps.
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert_eq!(report.cycles_used, total, "{name}: aggregate cost");
|
||||
if let Some(original) = name.strip_suffix("-optimized") {
|
||||
assert_eq!(original_cycles.get(original), Some(&total), "{name}: optimization changed cycles");
|
||||
} else { original_cycles.insert(name.to_string(), total); }
|
||||
covered.insert(id);
|
||||
count += 1;
|
||||
}
|
||||
assert_eq!(covered, (0x3000..=0x3019).collect());
|
||||
assert_eq!(count, 522, "mandatory boundary corpus missing");
|
||||
for (name, expected) in [("pi", Value::Float(f64::from_bits(0x400921fb54442d18))),
|
||||
("tau", Value::Float(f64::from_bits(0x401921fb54442d18))),
|
||||
("literal", Value::Float(-0.0)), ("dependency", Value::Int32(42)), ("long", Value::Int64(2147483648)),
|
||||
("service", Value::Int32(42)), ("min", Value::Int64(42))] {
|
||||
let bytes = std::fs::read(root.join(format!("pbs-math-{name}.pbx"))).unwrap();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
||||
let actual = vm.operand_stack_top(1);
|
||||
match (&actual[0], &expected) {
|
||||
(Value::Float(a), Value::Float(b)) => assert_eq!(a.to_bits(), b.to_bits(), "{name}"),
|
||||
_ => assert_eq!(actual, vec![expected]),
|
||||
}
|
||||
if name == "pi" || name == "tau" || name == "service" { assert_eq!(report.cycles_used, 6, "constant load + root RET only"); }
|
||||
}
|
||||
let bytes = std::fs::read(root.join("pbs-math-import.pbx")).expect("real SDK imports fixture");
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "import: {report:?}");
|
||||
println!("PBS MATH PASS: {count} exact bits/faults/cycle vectors; all 26 signatures; service constants");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user