feat(vm): add typed array and struct guest operations
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
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.
This commit is contained in:
@@ -360,6 +360,29 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||
}
|
||||
|
||||
"STRUCT_GET" | "STRUCT_SET" => {
|
||||
let (a, b) = parse_keyvals(ops)?;
|
||||
let (ty, field) = parse_pair(a, "type", b, "field")?;
|
||||
let op =
|
||||
if mn == "STRUCT_GET" { CoreOpCode::StructGet } else { CoreOpCode::StructSet };
|
||||
emit_u16(op as u16, &mut out);
|
||||
emit_u32(ty, &mut out);
|
||||
emit_u32(field, &mut out);
|
||||
}
|
||||
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" => {
|
||||
if ops.is_empty() {
|
||||
return Err(AsmError::MissingOperand(line.into()));
|
||||
}
|
||||
let op = match mn {
|
||||
"STRUCT_NEW" => CoreOpCode::StructNew,
|
||||
"ARRAY_NEW" => CoreOpCode::ArrayNew,
|
||||
"ARRAY_LEN" => CoreOpCode::ArrayLen,
|
||||
"ARRAY_GET" => CoreOpCode::ArrayGet,
|
||||
_ => CoreOpCode::ArraySet,
|
||||
};
|
||||
emit_u16(op as u16, &mut out);
|
||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||
}
|
||||
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
|
||||
if ops.is_empty() {
|
||||
return Err(AsmError::MissingOperand(line.into()));
|
||||
|
||||
@@ -63,10 +63,20 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
|
||||
| CoreOpCode::Hostcall
|
||||
| CoreOpCode::DynPack
|
||||
| CoreOpCode::DynUnpack
|
||||
| CoreOpCode::DynCanUnpack => {
|
||||
| CoreOpCode::DynCanUnpack
|
||||
| CoreOpCode::StructNew
|
||||
| CoreOpCode::ArrayNew
|
||||
| CoreOpCode::ArrayLen
|
||||
| CoreOpCode::ArrayGet
|
||||
| CoreOpCode::ArraySet => {
|
||||
let v = u32::from_le_bytes(imm.try_into().unwrap());
|
||||
format!("{}", v)
|
||||
}
|
||||
CoreOpCode::StructGet | CoreOpCode::StructSet => {
|
||||
let ty = u32::from_le_bytes(imm[..4].try_into().unwrap());
|
||||
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
|
||||
format!("type={}, field={}", ty, field)
|
||||
}
|
||||
CoreOpCode::RefIsExact
|
||||
| CoreOpCode::RefImplements
|
||||
| CoreOpCode::RefToTrait
|
||||
|
||||
@@ -218,6 +218,15 @@ pub enum OpCode {
|
||||
RefToTrait = 0x95,
|
||||
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||
RefCast = 0x96,
|
||||
|
||||
// Registry-backed collection operations. cycles() is their base component.
|
||||
StructNew = 0xA0,
|
||||
StructGet = 0xA1,
|
||||
StructSet = 0xA2,
|
||||
ArrayNew = 0xA3,
|
||||
ArrayLen = 0xA4,
|
||||
ArrayGet = 0xA5,
|
||||
ArraySet = 0xA6,
|
||||
}
|
||||
|
||||
impl TryFrom<u16> for OpCode {
|
||||
@@ -282,6 +291,13 @@ impl TryFrom<u16> for OpCode {
|
||||
0x94 => Ok(OpCode::RefImplements),
|
||||
0x95 => Ok(OpCode::RefToTrait),
|
||||
0x96 => Ok(OpCode::RefCast),
|
||||
0xA0 => Ok(OpCode::StructNew),
|
||||
0xA1 => Ok(OpCode::StructGet),
|
||||
0xA2 => Ok(OpCode::StructSet),
|
||||
0xA3 => Ok(OpCode::ArrayNew),
|
||||
0xA4 => Ok(OpCode::ArrayLen),
|
||||
0xA5 => Ok(OpCode::ArrayGet),
|
||||
0xA6 => Ok(OpCode::ArraySet),
|
||||
|
||||
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
||||
}
|
||||
@@ -289,8 +305,9 @@ impl TryFrom<u16> for OpCode {
|
||||
}
|
||||
|
||||
impl OpCode {
|
||||
/// Returns the cost of the instruction in VM cycles.
|
||||
/// This is used for performance monitoring and resource limiting (Certification).
|
||||
/// Returns fixed VM cycles, or the base component for collection operations.
|
||||
/// Collection execution adds Registry widths and valid construction length;
|
||||
/// callers must not use this base alone as the complete attempt cost.
|
||||
pub fn cycles(&self) -> u64 {
|
||||
match self {
|
||||
OpCode::Nop => 1,
|
||||
@@ -350,6 +367,13 @@ impl OpCode {
|
||||
OpCode::RefImplements => 4,
|
||||
OpCode::RefToTrait => 5,
|
||||
OpCode::RefCast => 4,
|
||||
OpCode::StructNew => 8,
|
||||
OpCode::StructGet => 3,
|
||||
OpCode::StructSet => 4,
|
||||
OpCode::ArrayNew => 8,
|
||||
OpCode::ArrayLen => 3,
|
||||
OpCode::ArrayGet => 3,
|
||||
OpCode::ArraySet => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -599,6 +599,76 @@ impl OpCodeSpecExt for OpCode {
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::StructNew => OpcodeSpec {
|
||||
name: "STRUCT_NEW",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::StructGet => OpcodeSpec {
|
||||
name: "STRUCT_GET",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::StructSet => OpcodeSpec {
|
||||
name: "STRUCT_SET",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::ArrayNew => OpcodeSpec {
|
||||
name: "ARRAY_NEW",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::ArrayLen => OpcodeSpec {
|
||||
name: "ARRAY_LEN",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::ArrayGet => OpcodeSpec {
|
||||
name: "ARRAY_GET",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::ArraySet => OpcodeSpec {
|
||||
name: "ARRAY_SET",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::FrameSync => OpcodeSpec {
|
||||
name: "FRAME_SYNC",
|
||||
imm_bytes: 0,
|
||||
|
||||
@@ -178,6 +178,8 @@ pub struct OwnedField {
|
||||
}
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct StructLayout {
|
||||
/// Sum of execution-slot widths, resolved once for deterministic NEW charging.
|
||||
pub initialization_slots: u64,
|
||||
pub size: usize,
|
||||
pub alignment: usize,
|
||||
pub fields: Vec<FieldLayout>,
|
||||
@@ -277,9 +279,13 @@ impl Registry {
|
||||
.count();
|
||||
let mut owned = b.vector(owned_count, Q::RegistryBytes)?;
|
||||
let (mut size, mut alignment) = (0, 1);
|
||||
let mut initialization_slots = 0u64;
|
||||
for field in fields {
|
||||
b.work(1)?;
|
||||
let layout = layouts[field.type_id.0 as usize].storage;
|
||||
initialization_slots = initialization_slots
|
||||
.checked_add(u64::from(layout.slots))
|
||||
.ok_or_else(overflow)?;
|
||||
size = align(size, layout.alignment)?;
|
||||
offsets.push(FieldLayout { type_id: field.type_id, offset: size });
|
||||
if layout.ownership != Ownership::Plain {
|
||||
@@ -289,6 +295,7 @@ impl Registry {
|
||||
alignment = alignment.max(layout.alignment);
|
||||
}
|
||||
layouts[i].instance = Some(StructLayout {
|
||||
initialization_slots,
|
||||
size: align(size, alignment)?,
|
||||
alignment,
|
||||
fields: offsets,
|
||||
|
||||
@@ -76,6 +76,46 @@ impl DecodedInstr<'_> {
|
||||
};
|
||||
(source_layout.storage.slots, pushes)
|
||||
}
|
||||
Op::StructNew
|
||||
| Op::StructGet
|
||||
| Op::StructSet
|
||||
| Op::ArrayNew
|
||||
| Op::ArrayLen
|
||||
| Op::ArrayGet
|
||||
| Op::ArraySet => {
|
||||
let (id, field) = if matches!(self.opcode, Op::StructGet | Op::StructSet) {
|
||||
self.imm_u32x2().map_err(|_| malformed())?
|
||||
} else {
|
||||
(self.imm_u32().map_err(|_| malformed())?, 0)
|
||||
};
|
||||
let (_, layout) = type_info(id)?;
|
||||
if matches!(self.opcode, Op::StructNew | Op::StructGet | Op::StructSet) {
|
||||
let instance = layout.instance.as_ref().ok_or_else(|| bad_category(id))?;
|
||||
match self.opcode {
|
||||
Op::StructNew => (0, 1),
|
||||
op => {
|
||||
let f = instance
|
||||
.fields
|
||||
.get(field as usize)
|
||||
.ok_or_else(|| bad_category(id).member(field))?;
|
||||
let w = registry.type_layout(f.type_id).unwrap().storage.slots;
|
||||
if op == Op::StructGet {
|
||||
(1, w)
|
||||
} else {
|
||||
(1 + w, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let array = layout.array.as_ref().ok_or_else(|| bad_category(id))?;
|
||||
let w = registry.type_layout(array.element_type_id).unwrap().storage.slots;
|
||||
match self.opcode {
|
||||
Op::ArrayNew | Op::ArrayLen => (1, 1),
|
||||
Op::ArrayGet => (2, w),
|
||||
_ => (2 + w, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => unreachable!("requires_registry is defined by the typed opcode catalogue"),
|
||||
};
|
||||
Ok(Some(RegistryStackEffect { pops, pushes }))
|
||||
|
||||
Reference in New Issue
Block a user