129 lines
4.7 KiB
Rust
129 lines
4.7 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);
|
|
}
|