Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Add seven Registry-backed collection opcodes with bounded verification, complete value groups, transactional publication and exact attempt costs. Publish ISA, memory and cycle contracts, producer handoff and the completed lesson. Validate with 925 workspace tests, optimized collection suites and all eight coverage domain gates. Make headless fixture directory names unique for parallel tests.
205 lines
7.6 KiB
Rust
205 lines
7.6 KiB
Rust
use prometeu_bytecode::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
|
use prometeu_bytecode::load_limits::{LoadBudget, LoadCode, LoadLimits, LoadReason};
|
|
use prometeu_bytecode::registry::*;
|
|
use prometeu_bytecode::{assemble, decode_next, disassemble, DecodeError, RegistryStackEffect};
|
|
|
|
fn registry() -> Registry {
|
|
let types = vec![
|
|
TypeKind::Builtin(BuiltinId::INT32),
|
|
TypeKind::Builtin(BuiltinId::VEC2),
|
|
TypeKind::Builtin(BuiltinId::PIXEL),
|
|
TypeKind::Builtin(BuiltinId::DYNAMIC),
|
|
TypeKind::Struct(vec![]),
|
|
TypeKind::Array(TypeId(0)),
|
|
TypeKind::Trait(vec![]),
|
|
]
|
|
.into_iter()
|
|
.map(|kind| TypeDecl { name: None, kind })
|
|
.collect();
|
|
Registry::resolve(
|
|
RegistryDeclarations { types, ..Default::default() },
|
|
0,
|
|
&mut LoadBudget::new(LoadLimits::default()),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn all_typed_instructions_match_independent_bytes_and_costs() {
|
|
let cases: &[(&str, &[u8], u64)] = &[
|
|
("DYN_PACK 16909060", &[0x90, 0, 4, 3, 2, 1], 8),
|
|
("DYN_UNPACK 16909060", &[0x91, 0, 4, 3, 2, 1], 4),
|
|
("DYN_CAN_UNPACK 16909060", &[0x92, 0, 4, 3, 2, 1], 3),
|
|
("REF_IS_EXACT source=16909060, target=84281096", &[0x93, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
|
|
("REF_IMPLEMENTS source=16909060, target=84281096", &[0x94, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
|
|
("REF_TO_TRAIT source=16909060, target=84281096", &[0x95, 0, 4, 3, 2, 1, 8, 7, 6, 5], 5),
|
|
("REF_CAST source=16909060, target=84281096", &[0x96, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
|
|
];
|
|
for &(text, bytes, cycles) in cases {
|
|
assert_eq!(assemble(text).unwrap(), bytes);
|
|
assert_eq!(disassemble(bytes).unwrap(), text);
|
|
let decoded = decode_next(0, bytes).unwrap();
|
|
assert_eq!(decoded.next_pc, bytes.len());
|
|
assert_eq!(decoded.opcode.cycles(), cycles);
|
|
assert_eq!(decoded.opcode.spec().pops, None);
|
|
assert_eq!(decoded.opcode.spec().pushes, None);
|
|
assert!(decoded.opcode.requires_registry());
|
|
for n in 0..bytes.len() {
|
|
assert!(decode_next(0, &bytes[..n]).is_err(), "{text}, prefix {n}");
|
|
}
|
|
}
|
|
assert!(matches!(
|
|
decode_next(0, &[0x97, 0]),
|
|
Err(DecodeError::UnknownOpcode { opcode: 0x97, .. })
|
|
));
|
|
}
|
|
#[test]
|
|
fn slot_effects_resolve_semantic_widths_and_reference_categories() {
|
|
let registry = registry();
|
|
for (text, pops, pushes) in [
|
|
("DYN_PACK 0", 1, 2),
|
|
("DYN_PACK 1", 2, 2),
|
|
("DYN_PACK 2", 3, 2),
|
|
("DYN_PACK 3", 2, 2),
|
|
("DYN_UNPACK 2", 2, 3),
|
|
("DYN_CAN_UNPACK 2", 2, 1),
|
|
("REF_IS_EXACT source=6, target=4", 2, 1),
|
|
("REF_IMPLEMENTS source=5, target=6", 1, 1),
|
|
("REF_TO_TRAIT source=4, target=6", 1, 2),
|
|
("REF_TO_TRAIT source=6, target=6", 2, 2),
|
|
("REF_CAST source=6, target=4", 2, 1),
|
|
] {
|
|
let bytes = assemble(text).unwrap();
|
|
assert_eq!(
|
|
decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap(),
|
|
Some(RegistryStackEffect { pops, pushes }),
|
|
"{text}"
|
|
);
|
|
}
|
|
assert_eq!(decode_next(0, &[0, 0]).unwrap().registry_stack_effect(®istry).unwrap(), None);
|
|
}
|
|
#[test]
|
|
fn invalid_type_immediates_are_contextual_load_errors() {
|
|
let registry = registry();
|
|
for text in ["DYN_PACK 7", "DYN_UNPACK 4294967295", "REF_CAST source=7, target=4"] {
|
|
let bytes = assemble(text).unwrap();
|
|
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
|
assert_eq!(
|
|
(e.code, e.reason, e.pc),
|
|
(LoadCode::InvalidReference, LoadReason::TypeId, Some(0))
|
|
);
|
|
}
|
|
for text in [
|
|
"REF_CAST source=3, target=4",
|
|
"REF_IS_EXACT source=4, target=6",
|
|
"REF_TO_TRAIT source=4, target=0",
|
|
"REF_IMPLEMENTS source=0, target=6",
|
|
] {
|
|
let bytes = assemble(text).unwrap();
|
|
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
|
assert_eq!(
|
|
(e.code, e.reason),
|
|
(LoadCode::InvalidReference, LoadReason::Category),
|
|
"{text}"
|
|
);
|
|
}
|
|
}
|
|
#[test]
|
|
fn assembler_rejects_missing_overflowing_or_ambiguous_type_ids() {
|
|
for text in [
|
|
"DYN_PACK",
|
|
"DYN_PACK -1",
|
|
"DYN_PACK 4294967296",
|
|
"DYN_PACK 1 2",
|
|
"REF_CAST source=4",
|
|
"REF_CAST source=4, target=5, target=6",
|
|
"REF_CAST source=4, trait=6",
|
|
] {
|
|
assert!(assemble(text).is_err(), "{text}");
|
|
}
|
|
}
|
|
#[test]
|
|
fn decoder_extreme_pc_does_not_overflow() {
|
|
assert!(matches!(
|
|
decode_next(usize::MAX, &[]),
|
|
Err(DecodeError::TruncatedOpcode { pc: usize::MAX })
|
|
));
|
|
assert_eq!(Op::DynPack as u16, 0x0090);
|
|
}
|
|
|
|
#[test]
|
|
fn collection_literal_encodings_roundtrip_and_reject_every_truncation() {
|
|
for (text, bytes, base) in [
|
|
("STRUCT_NEW 16909060", vec![0xa0, 0, 4, 3, 2, 1], 8),
|
|
("STRUCT_GET type=16909060, field=84281096", vec![0xa1, 0, 4, 3, 2, 1, 8, 7, 6, 5], 3),
|
|
("STRUCT_SET type=16909060, field=84281096", vec![0xa2, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
|
|
("ARRAY_NEW 16909060", vec![0xa3, 0, 4, 3, 2, 1], 8),
|
|
("ARRAY_LEN 16909060", vec![0xa4, 0, 4, 3, 2, 1], 3),
|
|
("ARRAY_GET 16909060", vec![0xa5, 0, 4, 3, 2, 1], 3),
|
|
("ARRAY_SET 16909060", vec![0xa6, 0, 4, 3, 2, 1], 4),
|
|
] {
|
|
assert_eq!(assemble(text).unwrap(), bytes);
|
|
assert_eq!(disassemble(&bytes).unwrap(), text);
|
|
let op = decode_next(0, &bytes).unwrap().opcode;
|
|
assert_eq!(op.cycles(), base);
|
|
assert!(op.requires_registry() && op.spec().may_trap);
|
|
assert!(!op.spec().is_safepoint && !op.spec().is_branch && !op.spec().is_terminator);
|
|
for n in 0..bytes.len() {
|
|
assert!(decode_next(0, &bytes[..n]).is_err());
|
|
}
|
|
}
|
|
for bad in [
|
|
"STRUCT_NEW",
|
|
"ARRAY_NEW -1",
|
|
"ARRAY_GET 4294967296",
|
|
"ARRAY_LEN 1 2",
|
|
"STRUCT_GET type=1",
|
|
"STRUCT_SET type=1, field=2, field=3",
|
|
"STRUCT_GET type=1, field=-1",
|
|
] {
|
|
assert!(assemble(bad).is_err(), "{bad}");
|
|
}
|
|
}
|
|
#[test]
|
|
fn collection_effects_and_cached_initialization_width_are_exact() {
|
|
let mut d = RegistryDeclarations::default();
|
|
d.types = vec![
|
|
TypeDecl { name: None, kind: TypeKind::Builtin(BuiltinId::PIXEL) },
|
|
TypeDecl {
|
|
name: None,
|
|
kind: TypeKind::Struct(vec![
|
|
NamedType { type_id: TypeId(0), name: None },
|
|
NamedType { type_id: TypeId(1), name: None },
|
|
]),
|
|
},
|
|
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
|
];
|
|
let r = Registry::resolve(d, 0, &mut LoadBudget::new(Default::default())).unwrap();
|
|
assert_eq!(
|
|
r.type_layout(TypeId(1)).unwrap().instance.as_ref().unwrap().initialization_slots,
|
|
4
|
|
);
|
|
for (s, pops, pushes) in [
|
|
("STRUCT_NEW 1", 0, 1),
|
|
("STRUCT_GET type=1, field=0", 1, 3),
|
|
("STRUCT_SET type=1, field=0", 4, 0),
|
|
("ARRAY_NEW 2", 1, 1),
|
|
("ARRAY_LEN 2", 1, 1),
|
|
("ARRAY_GET 2", 2, 3),
|
|
("ARRAY_SET 2", 5, 0),
|
|
] {
|
|
let code = assemble(s).unwrap();
|
|
assert_eq!(
|
|
decode_next(0, &code).unwrap().registry_stack_effect(&r).unwrap(),
|
|
Some(RegistryStackEffect { pops, pushes })
|
|
);
|
|
}
|
|
for s in ["STRUCT_NEW 0", "ARRAY_NEW 1", "STRUCT_GET type=1, field=2", "ARRAY_GET 99"] {
|
|
let code = assemble(s).unwrap();
|
|
assert_eq!(
|
|
decode_next(0, &code).unwrap().registry_stack_effect(&r).unwrap_err().code,
|
|
LoadCode::InvalidReference
|
|
);
|
|
}
|
|
}
|