pr 02
This commit is contained in:
@@ -1,47 +1,8 @@
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::opcode_spec::{OpCodeSpecExt, OpcodeSpec};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum DecodeError {
|
||||
TruncatedOpcode { pc: usize },
|
||||
UnknownOpcode { pc: usize, opcode: u16 },
|
||||
TruncatedImmediate { pc: usize, opcode: OpCode, need: usize, have: usize },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DecodedInstr<'a> {
|
||||
pub opcode: OpCode,
|
||||
pub spec: OpcodeSpec,
|
||||
pub imm: &'a [u8],
|
||||
pub next_pc: usize,
|
||||
}
|
||||
// Re-export canonical decoder from prometeu-bytecode to eliminate bespoke implementation in VM.
|
||||
pub use prometeu_bytecode::decoder::{decode_next, DecodeError, DecodedInstr};
|
||||
|
||||
/// Backwards-compatible shim for legacy call sites; delegates to canonical decoder.
|
||||
#[inline]
|
||||
pub fn decode_at(rom: &[u8], pc: usize) -> Result<DecodedInstr<'_>, DecodeError> {
|
||||
if pc + 2 > rom.len() {
|
||||
return Err(DecodeError::TruncatedOpcode { pc });
|
||||
}
|
||||
let opcode_val = u16::from_le_bytes([rom[pc], rom[pc+1]]);
|
||||
let opcode = OpCode::try_from(opcode_val).map_err(|_| DecodeError::UnknownOpcode { pc, opcode: opcode_val })?;
|
||||
let spec = opcode.spec();
|
||||
|
||||
let imm_start = pc + 2;
|
||||
let imm_end = imm_start + spec.imm_bytes as usize;
|
||||
|
||||
if imm_end > rom.len() {
|
||||
return Err(DecodeError::TruncatedImmediate {
|
||||
pc,
|
||||
opcode,
|
||||
need: spec.imm_bytes as usize,
|
||||
have: rom.len().saturating_sub(imm_start)
|
||||
});
|
||||
}
|
||||
|
||||
let imm = &rom[imm_start..imm_end];
|
||||
|
||||
Ok(DecodedInstr {
|
||||
opcode,
|
||||
spec,
|
||||
imm,
|
||||
next_pc: imm_end,
|
||||
})
|
||||
decode_next(pc, rom)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use prometeu_abi::syscalls::Syscall;
|
||||
use crate::bytecode::decoder::{decode_at, DecodeError};
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::opcode_spec::OpCodeSpecExt;
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use prometeu_bytecode::layout;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
@@ -68,6 +69,8 @@ impl Verifier {
|
||||
VerifierError::TruncatedOpcode { pc: func_start + pc },
|
||||
DecodeError::TruncatedImmediate { pc: _, opcode, need, have } =>
|
||||
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need, have },
|
||||
DecodeError::ImmediateSizeMismatch { pc: _, opcode, expected, actual } =>
|
||||
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need: expected, have: actual },
|
||||
})?;
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
@@ -87,16 +90,16 @@ impl Verifier {
|
||||
while let Some(pc) = worklist.pop_front() {
|
||||
let in_height = *stack_height_in.get(&pc).unwrap();
|
||||
let instr = decode_at(func_code, pc).unwrap(); // Guaranteed to succeed due to first pass
|
||||
let spec = instr.spec;
|
||||
let spec = instr.opcode.spec();
|
||||
|
||||
// Resolve dynamic pops/pushes
|
||||
let (pops, pushes) = match instr.opcode {
|
||||
OpCode::PopN => {
|
||||
let n = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as u16;
|
||||
let n = instr.imm_u32().unwrap() as u16;
|
||||
(n, 0)
|
||||
}
|
||||
OpCode::Call => {
|
||||
let func_id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let func_id = instr.imm_u32().unwrap();
|
||||
let callee = all_functions.get(func_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: func_id }
|
||||
})?;
|
||||
@@ -106,7 +109,7 @@ impl Verifier {
|
||||
(func.return_slots, 0)
|
||||
}
|
||||
OpCode::Syscall => {
|
||||
let id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| {
|
||||
VerifierError::InvalidSyscallId { pc: func_start + pc, id }
|
||||
})?;
|
||||
@@ -131,7 +134,7 @@ impl Verifier {
|
||||
// Propagate to successors
|
||||
if spec.is_branch {
|
||||
// Canonical contract: branch immediate is RELATIVE to function start.
|
||||
let target_rel = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let target_rel = instr.imm_u32().unwrap() as usize;
|
||||
let func_end_abs = layout::function_end_from_next(all_functions, func_idx, code.len());
|
||||
let func_len = func_end_abs - func_start;
|
||||
|
||||
|
||||
@@ -379,12 +379,12 @@ impl VirtualMachine {
|
||||
self.halted = true;
|
||||
}
|
||||
OpCode::Jmp => {
|
||||
let target = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let target = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let func_start = self.call_stack.last().map(|f| self.program.functions[f.func_idx].code_offset as usize).unwrap_or(0);
|
||||
self.pc = func_start + target;
|
||||
}
|
||||
OpCode::JmpIfFalse => {
|
||||
let target = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let target = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
match val {
|
||||
Value::Boolean(false) => {
|
||||
@@ -398,7 +398,7 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
OpCode::JmpIfTrue => {
|
||||
let target = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let target = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
match val {
|
||||
Value::Boolean(true) => {
|
||||
@@ -415,38 +415,38 @@ impl VirtualMachine {
|
||||
// Handled in run_budget for interruption
|
||||
}
|
||||
OpCode::PushConst => {
|
||||
let idx = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let idx = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.program.constant_pool.get(idx).cloned().ok_or_else(|| LogicalFrameEndingReason::Panic("Invalid constant index".into()))?;
|
||||
self.push(val);
|
||||
}
|
||||
OpCode::PushI64 => {
|
||||
let val = i64::from_le_bytes(instr.imm[0..8].try_into().unwrap());
|
||||
let val = instr.imm_i64().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
self.push(Value::Int64(val));
|
||||
}
|
||||
OpCode::PushI32 => {
|
||||
let val = i32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let val = instr.imm_i32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
self.push(Value::Int32(val));
|
||||
}
|
||||
OpCode::PushBounded => {
|
||||
let val = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let val = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
if val > 0xFFFF {
|
||||
return Err(self.trap(TRAP_OOB, opcode as u16, format!("Bounded value overflow: {} > 0xFFFF", val), start_pc as u32));
|
||||
}
|
||||
self.push(Value::Bounded(val));
|
||||
}
|
||||
OpCode::PushF64 => {
|
||||
let val = f64::from_le_bytes(instr.imm[0..8].try_into().unwrap());
|
||||
let val = instr.imm_f64().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
self.push(Value::Float(val));
|
||||
}
|
||||
OpCode::PushBool => {
|
||||
let val = instr.imm[0];
|
||||
let val = instr.imm_u8().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
self.push(Value::Boolean(val != 0));
|
||||
}
|
||||
OpCode::Pop => {
|
||||
self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
}
|
||||
OpCode::PopN => {
|
||||
let n = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let n = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
for _ in 0..n {
|
||||
self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
}
|
||||
@@ -706,12 +706,12 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
OpCode::GetGlobal => {
|
||||
let idx = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let idx = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.globals.get(idx).cloned().ok_or_else(|| LogicalFrameEndingReason::Panic("Invalid global index".into()))?;
|
||||
self.push(val);
|
||||
}
|
||||
OpCode::SetGlobal => {
|
||||
let idx = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let idx = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
if idx >= self.globals.len() {
|
||||
self.globals.resize(idx + 1, Value::Null);
|
||||
@@ -719,7 +719,7 @@ impl VirtualMachine {
|
||||
self.globals[idx] = val;
|
||||
}
|
||||
OpCode::GetLocal => {
|
||||
let slot = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let slot = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
let frame = self.call_stack.last().ok_or_else(|| LogicalFrameEndingReason::Panic("No active call frame".into()))?;
|
||||
let func = &self.program.functions[frame.func_idx];
|
||||
|
||||
@@ -731,7 +731,7 @@ impl VirtualMachine {
|
||||
self.push(val);
|
||||
}
|
||||
OpCode::SetLocal => {
|
||||
let slot = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let slot = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
let frame = self.call_stack.last().ok_or_else(|| LogicalFrameEndingReason::Panic("No active call frame".into()))?;
|
||||
let func = &self.program.functions[frame.func_idx];
|
||||
@@ -743,7 +743,7 @@ impl VirtualMachine {
|
||||
self.operand_stack[stack_idx] = val;
|
||||
}
|
||||
OpCode::Call => {
|
||||
let func_id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let func_id = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let callee = self.program.functions.get(func_id).ok_or_else(|| {
|
||||
self.trap(TRAP_INVALID_FUNC, opcode as u16, format!("Invalid func_id {}", func_id), start_pc as u32)
|
||||
})?;
|
||||
@@ -808,8 +808,7 @@ impl VirtualMachine {
|
||||
}
|
||||
OpCode::Alloc => {
|
||||
// Allocate a new gate with given type and number of slots.
|
||||
let type_id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let slots_u32 = u32::from_le_bytes(instr.imm[4..8].try_into().unwrap());
|
||||
let (type_id, slots_u32) = instr.imm_u32x2().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
let slots = slots_u32 as usize;
|
||||
|
||||
// Bump-allocate on the heap and zero-initialize with Null.
|
||||
@@ -832,7 +831,7 @@ impl VirtualMachine {
|
||||
self.push(Value::Gate(gate_id as usize));
|
||||
}
|
||||
OpCode::GateLoad => {
|
||||
let offset = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let offset = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let ref_val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
if let Value::Gate(gid_usize) = ref_val {
|
||||
let gid = gid_usize as GateId;
|
||||
@@ -855,7 +854,7 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
OpCode::GateStore => {
|
||||
let offset = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
|
||||
let offset = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize;
|
||||
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
let ref_val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
|
||||
if let Value::Gate(gid_usize) = ref_val {
|
||||
@@ -886,8 +885,7 @@ impl VirtualMachine {
|
||||
}
|
||||
OpCode::Syscall => {
|
||||
let pc_at_syscall = start_pc as u32;
|
||||
|
||||
let id = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap());
|
||||
let id = instr.imm_u32().map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
|
||||
let syscall = prometeu_abi::syscalls::Syscall::from_u32(id).ok_or_else(|| {
|
||||
self.trap(prometeu_bytecode::abi::TRAP_INVALID_SYSCALL, OpCode::Syscall as u16, format!("Unknown syscall: 0x{:08X}", id), pc_at_syscall)
|
||||
})?;
|
||||
|
||||
Reference in New Issue
Block a user