pr 34
This commit is contained in:
@@ -41,7 +41,7 @@ pub fn disasm(rom: &[u8]) -> Result<Vec<Instr>, String> {
|
||||
match opcode {
|
||||
OpCode::PushConst | OpCode::PushI32 | OpCode::Jmp | OpCode::JmpIfFalse | OpCode::JmpIfTrue
|
||||
| OpCode::GetGlobal | OpCode::SetGlobal | OpCode::GetLocal | OpCode::SetLocal
|
||||
| OpCode::PopN | OpCode::Syscall | OpCode::LoadRef | OpCode::StoreRef => {
|
||||
| OpCode::PopN | OpCode::Syscall | OpCode::GateLoad | OpCode::GateStore => {
|
||||
let v = read_u32_le(&mut cursor).map_err(|e| e.to_string())?;
|
||||
operands.push(DisasmOperand::U32(v));
|
||||
}
|
||||
@@ -58,11 +58,11 @@ pub fn disasm(rom: &[u8]) -> Result<Vec<Instr>, String> {
|
||||
cursor.read_exact(&mut b_buf).map_err(|e| e.to_string())?;
|
||||
operands.push(DisasmOperand::Bool(b_buf[0] != 0));
|
||||
}
|
||||
OpCode::Call => {
|
||||
let addr = read_u32_le(&mut cursor).map_err(|e| e.to_string())?;
|
||||
let args = read_u32_le(&mut cursor).map_err(|e| e.to_string())?;
|
||||
operands.push(DisasmOperand::U32(addr));
|
||||
operands.push(DisasmOperand::U32(args));
|
||||
OpCode::Call | OpCode::Alloc => {
|
||||
let v1 = read_u32_le(&mut cursor).map_err(|e| e.to_string())?;
|
||||
let v2 = read_u32_le(&mut cursor).map_err(|e| e.to_string())?;
|
||||
operands.push(DisasmOperand::U32(v1));
|
||||
operands.push(DisasmOperand::U32(v2));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -153,19 +153,46 @@ pub enum OpCode {
|
||||
/// Ends the current local scope, discarding its local variables.
|
||||
PopScope = 0x53,
|
||||
|
||||
// --- 6.7 Heap ---
|
||||
// --- 6.7 HIP (Heap Interface Protocol) ---
|
||||
|
||||
/// Allocates `size` slots on the heap.
|
||||
/// Stack: [size] -> [reference]
|
||||
/// 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 `reference + offset`.
|
||||
/// Reads a value from the heap at `gate + offset`.
|
||||
/// Operand: offset (u32)
|
||||
/// Stack: [reference] -> [value]
|
||||
LoadRef = 0x61,
|
||||
/// Writes a value to the heap at `reference + offset`.
|
||||
/// Stack: [gate] -> [value]
|
||||
GateLoad = 0x61,
|
||||
/// Writes a value to the heap at `gate + offset`.
|
||||
/// Operand: offset (u32)
|
||||
/// Stack: [reference, value] -> []
|
||||
StoreRef = 0x62,
|
||||
/// 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 ---
|
||||
|
||||
@@ -226,8 +253,16 @@ impl TryFrom<u16> for OpCode {
|
||||
0x52 => Ok(OpCode::PushScope),
|
||||
0x53 => Ok(OpCode::PopScope),
|
||||
0x60 => Ok(OpCode::Alloc),
|
||||
0x61 => Ok(OpCode::LoadRef),
|
||||
0x62 => Ok(OpCode::StoreRef),
|
||||
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)),
|
||||
@@ -283,10 +318,76 @@ impl OpCode {
|
||||
OpCode::PushScope => 3,
|
||||
OpCode::PopScope => 3,
|
||||
OpCode::Alloc => 10,
|
||||
OpCode::LoadRef => 3,
|
||||
OpCode::StoreRef => 3,
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::asm::{assemble, Asm, Operand};
|
||||
|
||||
#[test]
|
||||
fn test_opcode_stability() {
|
||||
// Normative test: ensures opcode numeric values are frozen.
|
||||
assert_eq!(OpCode::Nop as u16, 0x00);
|
||||
assert_eq!(OpCode::PushConst as u16, 0x10);
|
||||
assert_eq!(OpCode::Alloc as u16, 0x60);
|
||||
assert_eq!(OpCode::GateLoad as u16, 0x61);
|
||||
assert_eq!(OpCode::GateStore as u16, 0x62);
|
||||
assert_eq!(OpCode::GateBeginPeek as u16, 0x63);
|
||||
assert_eq!(OpCode::GateEndPeek as u16, 0x64);
|
||||
assert_eq!(OpCode::GateBeginBorrow as u16, 0x65);
|
||||
assert_eq!(OpCode::GateEndBorrow as u16, 0x66);
|
||||
assert_eq!(OpCode::GateBeginMutate as u16, 0x67);
|
||||
assert_eq!(OpCode::GateEndMutate as u16, 0x68);
|
||||
assert_eq!(OpCode::GateRetain as u16, 0x69);
|
||||
assert_eq!(OpCode::GateRelease as u16, 0x6A);
|
||||
assert_eq!(OpCode::FrameSync as u16, 0x80);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hip_bytecode_golden() {
|
||||
// Golden test for HIP opcodes and their encodings.
|
||||
// Rule: All multi-byte operands are little-endian.
|
||||
|
||||
let instructions = vec![
|
||||
Asm::Op(OpCode::Alloc, vec![Operand::U32(0x11223344), Operand::U32(0x55667788)]),
|
||||
Asm::Op(OpCode::GateLoad, vec![Operand::U32(0xAABBCCDD)]),
|
||||
Asm::Op(OpCode::GateStore, vec![Operand::U32(0x11223344)]),
|
||||
Asm::Op(OpCode::GateBeginPeek, vec![]),
|
||||
Asm::Op(OpCode::GateRetain, vec![]),
|
||||
Asm::Op(OpCode::GateRelease, vec![]),
|
||||
];
|
||||
|
||||
let bytes = assemble(&instructions).unwrap();
|
||||
|
||||
let mut expected = Vec::new();
|
||||
// Alloc (0x60, 0x00) + type_id (44 33 22 11) + slots (88 77 66 55)
|
||||
expected.extend_from_slice(&[0x60, 0x00, 0x44, 0x33, 0x22, 0x11, 0x88, 0x77, 0x66, 0x55]);
|
||||
// GateLoad (0x61, 0x00) + offset (DD CC BB AA)
|
||||
expected.extend_from_slice(&[0x61, 0x00, 0xDD, 0xCC, 0xBB, 0xAA]);
|
||||
// GateStore (0x62, 0x00) + offset (44 33 22 11)
|
||||
expected.extend_from_slice(&[0x62, 0x00, 0x44, 0x33, 0x22, 0x11]);
|
||||
// GateBeginPeek (0x63, 0x00)
|
||||
expected.extend_from_slice(&[0x63, 0x00]);
|
||||
// GateRetain (0x69, 0x00)
|
||||
expected.extend_from_slice(&[0x69, 0x00]);
|
||||
// GateRelease (0x6A, 0x00)
|
||||
expected.extend_from_slice(&[0x6A, 0x00]);
|
||||
|
||||
assert_eq!(bytes, expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user