This commit is contained in:
2026-03-24 13:40:36 +00:00
parent 29056ce0ad
commit 6423c6b4bc
6 changed files with 25 additions and 437 deletions
@@ -114,3 +114,21 @@ pub fn decode_next(pc: usize, bytes: &'_ [u8]) -> Result<DecodedInstr<'_>, Decod
Ok(DecodedInstr { opcode, pc, next_pc: imm_end, imm: &bytes[imm_start..imm_end] })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unknown_opcode_is_reported_deterministically() {
// 0x0060 was previously a legacy opcode; now it must be unknown.
let bytes = vec![0x60, 0x00]; // little-endian u16 = 0x0060
match decode_next(0, &bytes) {
Err(DecodeError::UnknownOpcode { pc, opcode }) => {
assert_eq!(pc, 0);
assert_eq!(opcode, 0x0060);
}
other => panic!("expected UnknownOpcode, got {:?}", other),
}
}
}
@@ -560,9 +560,7 @@ fn validate_module(module: &BytecodeModule) -> Result<(), LoadError> {
| OpCode::GetLocal
| OpCode::SetLocal
| OpCode::PopN
| OpCode::Syscall
| OpCode::GateLoad
| OpCode::GateStore => {
| OpCode::Syscall => {
pos += 4;
}
OpCode::PushI64 | OpCode::PushF64 => {
@@ -574,9 +572,6 @@ fn validate_module(module: &BytecodeModule) -> Result<(), LoadError> {
OpCode::Call => {
pos += 4;
}
OpCode::Alloc => {
pos += 8;
}
_ => {}
}
}
@@ -160,46 +160,6 @@ pub enum OpCode {
/// Ends the current local scope, discarding its local variables.
PopScope = 0x53,
// --- 6.7 HIP (Heap Interface Protocol) ---
/// Allocates `slots` slots on the heap with the given `type_id`.
/// Operands: type_id (u32), slots (u32)
/// Stack: [] -> [gate]
Alloc = 0x60,
/// Reads a value from the heap at `gate + offset`.
/// Operand: offset (u32)
/// Stack: [gate] -> [value]
GateLoad = 0x61,
/// Writes a value to the heap at `gate + offset`.
/// Operand: offset (u32)
/// Stack: [gate, value] -> []
GateStore = 0x62,
/// Marks the beginning of a Peek scope for a gate.
/// Stack: [gate] -> [gate]
GateBeginPeek = 0x63,
/// Marks the end of a Peek scope for a gate.
/// Stack: [gate] -> [gate]
GateEndPeek = 0x64,
/// Marks the beginning of a Borrow scope for a gate.
/// Stack: [gate] -> [gate]
GateBeginBorrow = 0x65,
/// Marks the end of a Borrow scope for a gate.
/// Stack: [gate] -> [gate]
GateEndBorrow = 0x66,
/// Marks the beginning of a Mutate scope for a gate.
/// Stack: [gate] -> [gate]
GateBeginMutate = 0x67,
/// Marks the end of a Mutate scope for a gate.
/// Stack: [gate] -> [gate]
GateEndMutate = 0x68,
/// Increments the reference count of a gate.
/// Stack: [gate] -> [gate]
GateRetain = 0x69,
/// Decrements the reference count of a gate.
/// Stack: [gate] -> []
GateRelease = 0x6A,
// --- 6.8 Peripherals and System ---
/// Invokes a system function (Firmware/OS).
/// Operand: syscall_id (u32)
@@ -261,17 +221,6 @@ impl TryFrom<u16> for OpCode {
0x51 => Ok(OpCode::Ret),
0x52 => Ok(OpCode::PushScope),
0x53 => Ok(OpCode::PopScope),
0x60 => Ok(OpCode::Alloc),
0x61 => Ok(OpCode::GateLoad),
0x62 => Ok(OpCode::GateStore),
0x63 => Ok(OpCode::GateBeginPeek),
0x64 => Ok(OpCode::GateEndPeek),
0x65 => Ok(OpCode::GateBeginBorrow),
0x66 => Ok(OpCode::GateEndBorrow),
0x67 => Ok(OpCode::GateBeginMutate),
0x68 => Ok(OpCode::GateEndMutate),
0x69 => Ok(OpCode::GateRetain),
0x6A => Ok(OpCode::GateRelease),
0x70 => Ok(OpCode::Syscall),
0x80 => Ok(OpCode::FrameSync),
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
@@ -330,17 +279,6 @@ impl OpCode {
OpCode::Ret => 4,
OpCode::PushScope => 3,
OpCode::PopScope => 3,
OpCode::Alloc => 10,
OpCode::GateLoad => 3,
OpCode::GateStore => 3,
OpCode::GateBeginPeek => 1,
OpCode::GateEndPeek => 1,
OpCode::GateBeginBorrow => 1,
OpCode::GateEndBorrow => 1,
OpCode::GateBeginMutate => 1,
OpCode::GateEndMutate => 1,
OpCode::GateRetain => 1,
OpCode::GateRelease => 1,
OpCode::Syscall => 1,
OpCode::FrameSync => 1,
}
@@ -434,105 +434,6 @@ impl OpCodeSpecExt for OpCode {
is_terminator: false,
may_trap: false,
},
OpCode::Alloc => OpcodeSpec {
name: "ALLOC",
imm_bytes: 8,
pops: 0,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateLoad => OpcodeSpec {
name: "GATE_LOAD",
imm_bytes: 4,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateStore => OpcodeSpec {
name: "GATE_STORE",
imm_bytes: 4,
pops: 2,
pushes: 0,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateBeginPeek => OpcodeSpec {
name: "GATE_BEGIN_PEEK",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateEndPeek => OpcodeSpec {
name: "GATE_END_PEEK",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateBeginBorrow => OpcodeSpec {
name: "GATE_BEGIN_BORROW",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateEndBorrow => OpcodeSpec {
name: "GATE_END_BORROW",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateBeginMutate => OpcodeSpec {
name: "GATE_BEGIN_MUTATE",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateEndMutate => OpcodeSpec {
name: "GATE_END_MUTATE",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateRetain => OpcodeSpec {
name: "GATE_RETAIN",
imm_bytes: 0,
pops: 1,
pushes: 1,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::GateRelease => OpcodeSpec {
name: "GATE_RELEASE",
imm_bytes: 0,
pops: 1,
pushes: 0,
is_branch: false,
is_terminator: false,
may_trap: true,
},
OpCode::Syscall => OpcodeSpec {
name: "SYSCALL",
imm_bytes: 4,