Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Add structural Fn signatures, typed capture environments, indirect calls and Trait dispatch while preserving logical parameter and result groups, ownership and budgets. Remove guest coroutine execution and legacy loading. Activate bounded sealed PBX v1 with mandatory capture metadata, preserve frame continuations and root return results, and migrate producers and shipped cartridges. Publish coordinated specs, handoffs and the completed runtime lesson. Validation: 880 workspace tests passed, 9 existing tests ignored; both cartridges completed six production-host frames.
181 lines
7.9 KiB
Rust
181 lines
7.9 KiB
Rust
//! Registry-aware immediate validation and slot effects for typed instructions.
|
|
use crate::decoder::DecodedInstr;
|
|
use crate::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
|
use crate::load_limits::{IdDomain, LoadCode, LoadFailure, LoadReason};
|
|
use crate::registry::{FunctionId, Registry, TypeId, TypeKind};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct RegistryStackEffect {
|
|
pub pops: u32,
|
|
pub pushes: u32,
|
|
}
|
|
|
|
impl DecodedInstr<'_> {
|
|
/// Capture effects require the enclosing function's resolved environment.
|
|
pub fn registry_stack_effect_in(
|
|
&self,
|
|
registry: &Registry,
|
|
function: FunctionId,
|
|
) -> Result<Option<RegistryStackEffect>, LoadFailure> {
|
|
if self.opcode == Op::GetCapture {
|
|
let index = self
|
|
.imm_u32()
|
|
.map_err(|_| LoadFailure::new(LoadCode::InvalidCode, LoadReason::Truncated))?;
|
|
let capture = registry
|
|
.function_layout(function)
|
|
.and_then(|f| f.captures.get(index as usize))
|
|
.ok_or_else(|| {
|
|
LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex)
|
|
.member(index)
|
|
})?;
|
|
return Ok(Some(RegistryStackEffect { pops: 0, pushes: capture.slots }));
|
|
}
|
|
self.registry_stack_effect(registry)
|
|
}
|
|
/// Resolve an instruction's exact group widths and check immediate domains.
|
|
/// None means this is not a Registry instruction, not a zero stack effect.
|
|
/// This does not prove that the values on the stack satisfy the signature.
|
|
pub fn registry_stack_effect(
|
|
&self,
|
|
registry: &Registry,
|
|
) -> Result<Option<RegistryStackEffect>, LoadFailure> {
|
|
if !self.opcode.requires_registry() {
|
|
return Ok(None);
|
|
}
|
|
let malformed = || {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidCode, LoadReason::Truncated);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
};
|
|
let type_info = |id: u32| {
|
|
let decl = registry.declarations().types.get(id as usize);
|
|
let layout = registry.type_layout(TypeId(id));
|
|
decl.zip(layout).ok_or_else(|| {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidReference, LoadReason::TypeId)
|
|
.reference(IdDomain::Type, id);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
})
|
|
};
|
|
let bad_category = |id: u32| {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidReference, LoadReason::Category)
|
|
.reference(IdDomain::Type, id);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
};
|
|
let (pops, pushes) = match self.opcode {
|
|
Op::MakeClosure => {
|
|
let (function, ty) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
if layout.callable.is_none() {
|
|
return Err(bad_category(ty));
|
|
}
|
|
let target = registry.function_layout(FunctionId(function)).ok_or_else(|| {
|
|
LoadFailure::new(LoadCode::InvalidReference, LoadReason::FunctionId)
|
|
.reference(IdDomain::Function, function)
|
|
})?;
|
|
(target.capture_slots, 1)
|
|
}
|
|
Op::CallClosure => {
|
|
let ty = self.imm_u32().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
let signature = layout.callable.as_ref().ok_or_else(|| bad_category(ty))?;
|
|
(
|
|
signature.parameter_slots.checked_add(1).ok_or_else(malformed)?,
|
|
signature.result_slots,
|
|
)
|
|
}
|
|
Op::GetCapture => {
|
|
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex))
|
|
}
|
|
Op::CallTrait => {
|
|
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
let signature = layout
|
|
.methods
|
|
.get(method as usize)
|
|
.ok_or_else(|| bad_category(ty).member(method))?;
|
|
(
|
|
signature.parameter_slots.checked_add(2).ok_or_else(malformed)?,
|
|
signature.result_slots,
|
|
)
|
|
}
|
|
Op::DynPack | Op::DynUnpack | Op::DynCanUnpack => {
|
|
let id = self.imm_u32().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(id)?;
|
|
match self.opcode {
|
|
Op::DynPack => (layout.storage.slots, 2),
|
|
Op::DynUnpack => (2, layout.storage.slots),
|
|
_ => (2, 1),
|
|
}
|
|
}
|
|
Op::RefIsExact | Op::RefImplements | Op::RefToTrait | Op::RefCast => {
|
|
let (source, target) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (source_decl, source_layout) = type_info(source)?;
|
|
let (target_decl, target_layout) = type_info(target)?;
|
|
if !matches!(
|
|
source_decl.kind,
|
|
TypeKind::Struct(_) | TypeKind::Array(_) | TypeKind::Trait(_)
|
|
) {
|
|
return Err(bad_category(source));
|
|
}
|
|
let trait_target = matches!(self.opcode, Op::RefImplements | Op::RefToTrait);
|
|
if (trait_target && !matches!(target_decl.kind, TypeKind::Trait(_)))
|
|
|| (!trait_target
|
|
&& !matches!(target_decl.kind, TypeKind::Struct(_) | TypeKind::Array(_)))
|
|
{
|
|
return Err(bad_category(target));
|
|
}
|
|
let pushes = if matches!(self.opcode, Op::RefIsExact | Op::RefImplements) {
|
|
1
|
|
} else {
|
|
target_layout.storage.slots
|
|
};
|
|
(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 }))
|
|
}
|
|
}
|