use std::mem; use std::ops; use std::u32; use std::fmt::Display; use elements::{Opcode, BlockType, FunctionType}; use interpreter::Error; use interpreter::module::{ModuleInstance, ItemIndex}; use interpreter::stack::StackWithLimit; use interpreter::value::{RuntimeValue, TryInto, WrapInto, TryTruncateInto, ExtendInto, TransmuteInto, ArithmeticOps, Integer, Float, LittleEndianConvert}; use interpreter::variable::VariableInstance; const DEFAULT_MEMORY_INDEX: u32 = 0; const DEFAULT_TABLE_INDEX: u32 = 0; pub struct Interpreter; /// Function execution context. pub struct FunctionContext<'a> { /// Module instance. module: &'a ModuleInstance, /// Function return type. return_type: BlockType, /// Local variables. locals: Vec, /// Values stack. value_stack: StackWithLimit, /// Blocks frames stack. frame_stack: StackWithLimit, /// Current instruction position. position: usize, } #[derive(Debug, Clone)] pub enum InstructionOutcome { /// Continue with current instruction. RunInstruction, /// Continue with next instruction. RunNextInstruction, /// Branch to given frame. Branch(usize), /// End current frame. End, /// Return from current function block. Return, } #[derive(Debug, Clone)] pub struct BlockFrame { // A label for reference from branch instructions. branch_position: usize, // A label for reference from end instructions. end_position: usize, // A limit integer value, which is an index into the value stack indicating where to reset it to on a branch to that label. value_limit: usize, // A signature, which is a block signature type indicating the number and types of result values of the region. signature: BlockType, } impl Interpreter { pub fn run_function(context: &mut FunctionContext, body: &[Opcode]) -> Result, Error> { Interpreter::execute_block(context, body)?; match context.return_type { BlockType::Value(_) => Ok(Some(context.value_stack_mut().pop()?)), BlockType::NoResult => Ok(None), } } fn run_instruction(context: &mut FunctionContext, opcode: &Opcode) -> Result { match opcode { &Opcode::Unreachable => Interpreter::run_unreachable(context), &Opcode::Nop => Interpreter::run_nop(context), &Opcode::Block(block_type, ref ops) => Interpreter::run_block(context, block_type, ops.elements()), &Opcode::Loop(block_type, ref ops) => Interpreter::run_loop(context, block_type, ops.elements()), &Opcode::If(block_type, ref ops) => Interpreter::run_if(context, block_type, ops.elements()), &Opcode::Else => Interpreter::run_else(context), &Opcode::End => Interpreter::run_end(context), &Opcode::Br(idx) => Interpreter::run_br(context, idx), &Opcode::BrIf(idx) => Interpreter::run_br_if(context, idx), &Opcode::BrTable(ref table, default) => Interpreter::run_br_table(context, table, default), &Opcode::Return => Interpreter::run_return(context), &Opcode::Call(index) => Interpreter::run_call(context, index), &Opcode::CallIndirect(index, _reserved) => Interpreter::run_call_indirect(context, index), &Opcode::Drop => Interpreter::run_drop(context), &Opcode::Select => Interpreter::run_select(context), &Opcode::GetLocal(index) => Interpreter::run_get_local(context, index), &Opcode::SetLocal(index) => Interpreter::run_set_local(context, index), &Opcode::TeeLocal(index) => Interpreter::run_tee_local(context, index), &Opcode::GetGlobal(index) => Interpreter::run_get_global(context, index), &Opcode::SetGlobal(index) => Interpreter::run_set_global(context, index), &Opcode::I32Load(offset, align) => Interpreter::run_load::(context, offset, align), &Opcode::I64Load(offset, align) => Interpreter::run_load::(context, offset, align), &Opcode::F32Load(offset, align) => Interpreter::run_load::(context, offset, align), &Opcode::F64Load(offset, align) => Interpreter::run_load::(context, offset, align), &Opcode::I32Load8S(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I32Load8U(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I32Load16S(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I32Load16U(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load8S(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load8U(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load16S(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load16U(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load32S(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I64Load32U(offset, align) => Interpreter::run_load_extend::(context, offset, align), &Opcode::I32Store(offset, align) => Interpreter::run_store::(context, offset, align), &Opcode::I64Store(offset, align) => Interpreter::run_store::(context, offset, align), &Opcode::F32Store(offset, align) => Interpreter::run_store::(context, offset, align), &Opcode::F64Store(offset, align) => Interpreter::run_store::(context, offset, align), &Opcode::I32Store8(offset, align) => Interpreter::run_store_wrap::(context, offset, align), &Opcode::I32Store16(offset, align) => Interpreter::run_store_wrap::(context, offset, align), &Opcode::I64Store8(offset, align) => Interpreter::run_store_wrap::(context, offset, align), &Opcode::I64Store16(offset, align) => Interpreter::run_store_wrap::(context, offset, align), &Opcode::I64Store32(offset, align) => Interpreter::run_store_wrap::(context, offset, align), &Opcode::CurrentMemory(_) => Interpreter::run_current_memory(context), &Opcode::GrowMemory(_) => Interpreter::run_grow_memory(context), &Opcode::I32Const(val) => Interpreter::run_const(context, val.into()), &Opcode::I64Const(val) => Interpreter::run_const(context, val.into()), &Opcode::F32Const(val) => Interpreter::run_const(context, RuntimeValue::decode_f32(val)), &Opcode::F64Const(val) => Interpreter::run_const(context, RuntimeValue::decode_f64(val)), &Opcode::I32Eqz => Interpreter::run_eqz::(context), &Opcode::I32Eq => Interpreter::run_eq::(context), &Opcode::I32Ne => Interpreter::run_ne::(context), &Opcode::I32LtS => Interpreter::run_lt::(context), &Opcode::I32LtU => Interpreter::run_lt::(context), &Opcode::I32GtS => Interpreter::run_gt::(context), &Opcode::I32GtU => Interpreter::run_gt::(context), &Opcode::I32LeS => Interpreter::run_lte::(context), &Opcode::I32LeU => Interpreter::run_lte::(context), &Opcode::I32GeS => Interpreter::run_gte::(context), &Opcode::I32GeU => Interpreter::run_gte::(context), &Opcode::I64Eqz => Interpreter::run_eqz::(context), &Opcode::I64Eq => Interpreter::run_eq::(context), &Opcode::I64Ne => Interpreter::run_ne::(context), &Opcode::I64LtS => Interpreter::run_lt::(context), &Opcode::I64LtU => Interpreter::run_lt::(context), &Opcode::I64GtS => Interpreter::run_gt::(context), &Opcode::I64GtU => Interpreter::run_gt::(context), &Opcode::I64LeS => Interpreter::run_lte::(context), &Opcode::I64LeU => Interpreter::run_lte::(context), &Opcode::I64GeS => Interpreter::run_gte::(context), &Opcode::I64GeU => Interpreter::run_gte::(context), &Opcode::F32Eq => Interpreter::run_eq::(context), &Opcode::F32Ne => Interpreter::run_ne::(context), &Opcode::F32Lt => Interpreter::run_lt::(context), &Opcode::F32Gt => Interpreter::run_gt::(context), &Opcode::F32Le => Interpreter::run_lte::(context), &Opcode::F32Ge => Interpreter::run_gte::(context), &Opcode::F64Eq => Interpreter::run_eq::(context), &Opcode::F64Ne => Interpreter::run_ne::(context), &Opcode::F64Lt => Interpreter::run_lt::(context), &Opcode::F64Gt => Interpreter::run_gt::(context), &Opcode::F64Le => Interpreter::run_lte::(context), &Opcode::F64Ge => Interpreter::run_gte::(context), &Opcode::I32Clz => Interpreter::run_clz::(context), &Opcode::I32Ctz => Interpreter::run_ctz::(context), &Opcode::I32Popcnt => Interpreter::run_popcnt::(context), &Opcode::I32Add => Interpreter::run_add::(context), &Opcode::I32Sub => Interpreter::run_sub::(context), &Opcode::I32Mul => Interpreter::run_mul::(context), &Opcode::I32DivS => Interpreter::run_div::(context), &Opcode::I32DivU => Interpreter::run_div::(context), &Opcode::I32RemS => Interpreter::run_rem::(context), &Opcode::I32RemU => Interpreter::run_rem::(context), &Opcode::I32And => Interpreter::run_and::(context), &Opcode::I32Or => Interpreter::run_or::(context), &Opcode::I32Xor => Interpreter::run_xor::(context), &Opcode::I32Shl => Interpreter::run_shl::(context), &Opcode::I32ShrS => Interpreter::run_shr::(context), &Opcode::I32ShrU => Interpreter::run_shr::(context), &Opcode::I32Rotl => Interpreter::run_rotl::(context), &Opcode::I32Rotr => Interpreter::run_rotr::(context), &Opcode::I64Clz => Interpreter::run_clz::(context), &Opcode::I64Ctz => Interpreter::run_ctz::(context), &Opcode::I64Popcnt => Interpreter::run_popcnt::(context), &Opcode::I64Add => Interpreter::run_add::(context), &Opcode::I64Sub => Interpreter::run_sub::(context), &Opcode::I64Mul => Interpreter::run_mul::(context), &Opcode::I64DivS => Interpreter::run_div::(context), &Opcode::I64DivU => Interpreter::run_div::(context), &Opcode::I64RemS => Interpreter::run_rem::(context), &Opcode::I64RemU => Interpreter::run_rem::(context), &Opcode::I64And => Interpreter::run_and::(context), &Opcode::I64Or => Interpreter::run_or::(context), &Opcode::I64Xor => Interpreter::run_xor::(context), &Opcode::I64Shl => Interpreter::run_shl::(context), &Opcode::I64ShrS => Interpreter::run_shr::(context), &Opcode::I64ShrU => Interpreter::run_shr::(context), &Opcode::I64Rotl => Interpreter::run_rotl::(context), &Opcode::I64Rotr => Interpreter::run_rotr::(context), &Opcode::F32Abs => Interpreter::run_abs::(context), &Opcode::F32Neg => Interpreter::run_neg::(context), &Opcode::F32Ceil => Interpreter::run_ceil::(context), &Opcode::F32Floor => Interpreter::run_floor::(context), &Opcode::F32Trunc => Interpreter::run_trunc::(context), &Opcode::F32Nearest => Interpreter::run_nearest::(context), &Opcode::F32Sqrt => Interpreter::run_sqrt::(context), &Opcode::F32Add => Interpreter::run_add::(context), &Opcode::F32Sub => Interpreter::run_sub::(context), &Opcode::F32Mul => Interpreter::run_mul::(context), &Opcode::F32Div => Interpreter::run_div::(context), &Opcode::F32Min => Interpreter::run_min::(context), &Opcode::F32Max => Interpreter::run_max::(context), &Opcode::F32Copysign => Interpreter::run_copysign::(context), &Opcode::F64Abs => Interpreter::run_abs::(context), &Opcode::F64Neg => Interpreter::run_neg::(context), &Opcode::F64Ceil => Interpreter::run_ceil::(context), &Opcode::F64Floor => Interpreter::run_floor::(context), &Opcode::F64Trunc => Interpreter::run_trunc::(context), &Opcode::F64Nearest => Interpreter::run_nearest::(context), &Opcode::F64Sqrt => Interpreter::run_sqrt::(context), &Opcode::F64Add => Interpreter::run_add::(context), &Opcode::F64Sub => Interpreter::run_sub::(context), &Opcode::F64Mul => Interpreter::run_mul::(context), &Opcode::F64Div => Interpreter::run_div::(context), &Opcode::F64Min => Interpreter::run_min::(context), &Opcode::F64Max => Interpreter::run_max::(context), &Opcode::F64Copysign => Interpreter::run_copysign::(context), &Opcode::I32WarpI64 => Interpreter::run_wrap::(context), &Opcode::I32TruncSF32 => Interpreter::run_trunc_to_int::(context), &Opcode::I32TruncUF32 => Interpreter::run_trunc_to_int::(context), &Opcode::I32TruncSF64 => Interpreter::run_trunc_to_int::(context), &Opcode::I32TruncUF64 => Interpreter::run_trunc_to_int::(context), &Opcode::I64ExtendSI32 => Interpreter::run_extend::(context), &Opcode::I64ExtendUI32 => Interpreter::run_extend::(context), &Opcode::I64TruncSF32 => Interpreter::run_trunc_to_int::(context), &Opcode::I64TruncUF32 => Interpreter::run_trunc_to_int::(context), &Opcode::I64TruncSF64 => Interpreter::run_trunc_to_int::(context), &Opcode::I64TruncUF64 => Interpreter::run_trunc_to_int::(context), &Opcode::F32ConvertSI32 => Interpreter::run_extend::(context), &Opcode::F32ConvertUI32 => Interpreter::run_extend::(context), &Opcode::F32ConvertSI64 => Interpreter::run_wrap::(context), &Opcode::F32ConvertUI64 => Interpreter::run_wrap::(context), &Opcode::F32DemoteF64 => Interpreter::run_wrap::(context), &Opcode::F64ConvertSI32 => Interpreter::run_extend::(context), &Opcode::F64ConvertUI32 => Interpreter::run_extend::(context), &Opcode::F64ConvertSI64 => Interpreter::run_extend::(context), &Opcode::F64ConvertUI64 => Interpreter::run_extend::(context), &Opcode::F64PromoteF32 => Interpreter::run_extend::(context), &Opcode::I32ReinterpretF32 => Interpreter::run_reinterpret::(context), &Opcode::I64ReinterpretF64 => Interpreter::run_reinterpret::(context), &Opcode::F32ReinterpretI32 => Interpreter::run_reinterpret::(context), &Opcode::F64ReinterpretI64 => Interpreter::run_reinterpret::(context), } } fn run_unreachable(_context: &mut FunctionContext) -> Result { Err(Error::Trap("programmatic".into())) } fn run_nop(_context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::RunNextInstruction) } fn run_block(context: &mut FunctionContext, block_type: BlockType, body: &[Opcode]) -> Result { let frame_position = context.position + 1; context.push_frame(frame_position, frame_position, block_type.clone())?; Interpreter::execute_block(context, body) } fn run_loop(context: &mut FunctionContext, block_type: BlockType, body: &[Opcode]) -> Result { let frame_position = context.position; context.push_frame(frame_position, frame_position + 1, block_type.clone())?; Interpreter::execute_block(context, body) } fn run_if(context: &mut FunctionContext, block_type: BlockType, body: &[Opcode]) -> Result { let body_len = body.len(); let else_index = body.iter().position(|op| *op == Opcode::Else).unwrap_or(body_len - 1); let (begin_index, end_index) = if context.value_stack_mut().pop_as()? { (0, else_index + 1) } else { (else_index + 1, body_len) }; if begin_index != end_index { let frame_position = context.position + 1; context.push_frame(frame_position, frame_position, block_type.clone())?; Interpreter::execute_block(context, &body[begin_index..end_index]) } else { Ok(InstructionOutcome::RunNextInstruction) } } fn run_else(_context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::End) } fn run_end(_context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::End) } fn run_br(_context: &mut FunctionContext, label_idx: u32) -> Result { Ok(InstructionOutcome::Branch(label_idx as usize)) } fn run_br_if(context: &mut FunctionContext, label_idx: u32) -> Result { if context.value_stack_mut().pop_as()? { Ok(InstructionOutcome::Branch(label_idx as usize)) } else { Ok(InstructionOutcome::RunNextInstruction) } } fn run_br_table(context: &mut FunctionContext, table: &Vec, default: u32) -> Result { let index: u32 = context.value_stack_mut().pop_as()?; Ok(InstructionOutcome::Branch(table.get(index as usize).cloned().unwrap_or(default) as usize)) } fn run_return(_context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::Return) } fn run_call(context: &mut FunctionContext, func_idx: u32) -> Result { context.call_function(func_idx) .and_then(|r| r.map(|r| context.value_stack_mut().push(r)).unwrap_or(Ok(()))) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_call_indirect(context: &mut FunctionContext, type_idx: u32) -> Result { let table_func_idx: u32 = context.value_stack_mut().pop_as()?; context.call_function_indirect(DEFAULT_TABLE_INDEX, type_idx, table_func_idx) .and_then(|r| r.map(|r| context.value_stack_mut().push(r)).unwrap_or(Ok(()))) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_drop(context: &mut FunctionContext) -> Result { context .value_stack_mut() .pop() .map(|_| InstructionOutcome::RunNextInstruction) } fn run_select(context: &mut FunctionContext) -> Result { context .value_stack_mut() .pop_triple() .and_then(|(left, mid, right)| match (left, mid, right.try_into()) { (left, mid, Ok(condition)) => Ok((left, mid, condition)), _ => Err(Error::Stack("expected to get int value from stack".into())) } ) .map(|(left, mid, condition)| if condition { left } else { mid }) .map(|val| context.value_stack_mut().push(val)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_get_local(context: &mut FunctionContext, index: u32) -> Result { context.get_local(index as usize) .map(|value| context.value_stack_mut().push(value)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_set_local(context: &mut FunctionContext, index: u32) -> Result { let arg = context.value_stack_mut().pop()?; context.set_local(index as usize, arg) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_tee_local(context: &mut FunctionContext, index: u32) -> Result { let arg = context.value_stack().top()?.clone(); context.set_local(index as usize, arg) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_get_global(context: &mut FunctionContext, index: u32) -> Result { context.module() .global(ItemIndex::IndexSpace(index)) .and_then(|g| context.value_stack_mut().push(g.get())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_set_global(context: &mut FunctionContext, index: u32) -> Result { context .value_stack_mut() .pop() .and_then(|v| context.module().global(ItemIndex::IndexSpace(index)).and_then(|g| g.set(v))) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_load(context: &mut FunctionContext, offset: u32, align: u32) -> Result where RuntimeValue: From, T: LittleEndianConvert { context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .and_then(|m| m.get(effective_address(offset, align)?, 4)) .and_then(|b| T::from_little_endian(b)) .and_then(|n| context.value_stack_mut().push(n.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_load_extend(context: &mut FunctionContext, offset: u32, align: u32) -> Result where T: ExtendInto, RuntimeValue: From, T: LittleEndianConvert { let stack_value: U = context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .and_then(|m| m.get(effective_address(offset, align)?, mem::size_of::())) .and_then(|b| T::from_little_endian(b)) .map(|v| v.extend_into())?; context .value_stack_mut() .push(stack_value.into()) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_store(context: &mut FunctionContext, offset: u32, align: u32) -> Result where RuntimeValue: TryInto, T: LittleEndianConvert { let stack_value = context .value_stack_mut() .pop_as::() .map(|n| n.into_little_endian())?; context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .and_then(|m| m.set(effective_address(offset, align)?, &stack_value)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_store_wrap(context: &mut FunctionContext, offset: u32, align: u32) -> Result where RuntimeValue: TryInto, T: WrapInto, U: LittleEndianConvert { let stack_value: T = context.value_stack_mut().pop().and_then(|v| v.try_into())?; let stack_value = stack_value.wrap_into().into_little_endian(); context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .and_then(|m| m.set(effective_address(offset, align)?, &stack_value)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_current_memory(context: &mut FunctionContext) -> Result { context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .map(|m| m.size()) .and_then(|s| context.value_stack_mut().push(RuntimeValue::I64(s as i64))) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_grow_memory(context: &mut FunctionContext) -> Result { let pages: u32 = context.value_stack_mut().pop_as()?; context.module() .memory(ItemIndex::IndexSpace(DEFAULT_MEMORY_INDEX)) .and_then(|m| m.grow(pages)) .and_then(|m| context.value_stack_mut().push(RuntimeValue::I32(m as i32))) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_const(context: &mut FunctionContext, val: RuntimeValue) -> Result { context .value_stack_mut() .push(val) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_eqz(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq + Default { context .value_stack_mut() .pop_as::() .map(|v| RuntimeValue::I32(if v == Default::default() { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_eq(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left == right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_ne(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left != right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_lt(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd + Display { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left < right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_gt(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left > right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_lte(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left <= right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_gte(context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| RuntimeValue::I32(if left >= right { 1 } else { 0 })) .and_then(|v| context.value_stack_mut().push(v)) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_clz(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { context .value_stack_mut() .pop_as::() .map(|v| v.leading_zeros()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_ctz(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { context .value_stack_mut() .pop_as::() .map(|v| v.trailing_zeros()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_popcnt(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { context .value_stack_mut() .pop_as::() .map(|v| v.count_ones()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_add(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.add(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_sub(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.sub(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_mul(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.mul(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_div(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto, U: ArithmeticOps + TransmuteInto { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| (left.transmute_into(), right.transmute_into())) .map(|(left, right)| left.div(right)) .map(|v| v.transmute_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_rem(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto, U: Integer + TransmuteInto { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| (left.transmute_into(), right.transmute_into())) .map(|(left, right)| left.rem(right)) .map(|v| v.transmute_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_and(context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitAnd { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.bitand(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_or(context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitOr { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.bitor(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_xor(context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitXor { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.bitxor(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_shl(context: &mut FunctionContext) -> Result where RuntimeValue: From<>::Output> + TryInto, T: ops::Shl { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.shl(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_shr(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto, U: ops::Shr, >::Output: TransmuteInto { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| (left.transmute_into(), right.transmute_into())) .map(|(left, right)| left.shr(right)) .map(|v| v.transmute_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_rotl(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.rotl(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_rotr(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.rotr(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_abs(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.abs()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_neg(context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::Neg { context .value_stack_mut() .pop_as::() .map(|v| v.neg()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_ceil(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.ceil()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_floor(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.floor()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_trunc(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.trunc()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_nearest(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.round()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_sqrt(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_as::() .map(|v| v.sqrt()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_min(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.min(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_max(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { context .value_stack_mut() .pop_pair_as::() .map(|(left, right)| left.max(right)) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_copysign(_context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { Err(Error::NotImplemented) // TODO } fn run_wrap(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: WrapInto { context .value_stack_mut() .pop_as::() .map(|v| v.wrap_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_trunc_to_int(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TryTruncateInto, U: TransmuteInto, { context .value_stack_mut() .pop_as::() .and_then(|v| v.try_truncate_into()) .map(|v| v.transmute_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_extend(context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ExtendInto, U: TransmuteInto { context .value_stack_mut() .pop_as::() .map(|v| v.extend_into()) .map(|v| v.transmute_into()) .map(|v| context.value_stack_mut().push(v.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn run_reinterpret(context: &mut FunctionContext) -> Result where RuntimeValue: From, RuntimeValue: TryInto, T: TransmuteInto { context .value_stack_mut() .pop_as::() .map(TransmuteInto::transmute_into) .and_then(|val| context.value_stack_mut().push(val.into())) .map(|_| InstructionOutcome::RunNextInstruction) } fn execute_block(context: &mut FunctionContext, body: &[Opcode]) -> Result { debug_assert!(!context.frame_stack.is_empty()); // run instructions context.position = 0; loop { let instruction = &body[context.position]; println!("=== RUNNING {:?}", instruction); // TODO: trace match Interpreter::run_instruction(context, instruction)? { InstructionOutcome::RunInstruction => (), InstructionOutcome::RunNextInstruction => context.position += 1, InstructionOutcome::Branch(index) => { if index != 0 { context.discard_frame()?; return Ok(InstructionOutcome::Branch(index - 1)); } else { context.pop_frame(true)?; return Ok(InstructionOutcome::RunInstruction); } }, InstructionOutcome::End => { context.pop_frame(false)?; return Ok(InstructionOutcome::RunInstruction); }, InstructionOutcome::Return => return Ok(InstructionOutcome::Return), } } } } impl<'a> FunctionContext<'a> { pub fn new(module: &'a ModuleInstance, value_stack_limit: usize, frame_stack_limit: usize, function: &FunctionType, body: &[Opcode], args: Vec) -> Result { let mut context = FunctionContext { module: module, return_type: function.return_type().map(|vt| BlockType::Value(vt)).unwrap_or(BlockType::NoResult), value_stack: StackWithLimit::with_limit(value_stack_limit), frame_stack: StackWithLimit::with_limit(frame_stack_limit), locals: args, position: 0, }; context.push_frame(body.len() - 1, body.len() - 1, match function.return_type() { Some(value_type) => BlockType::Value(value_type), None => BlockType::NoResult, })?; Ok(context) } pub fn module(&self) -> &ModuleInstance { self.module } pub fn call_function(&mut self, index: u32) -> Result, Error> { self.module.call_function(self, ItemIndex::IndexSpace(index)) } pub fn call_function_indirect(&mut self, table_index: u32, type_index: u32, func_index: u32) -> Result, Error> { self.module.call_function_indirect(self, ItemIndex::IndexSpace(table_index), type_index, func_index) } pub fn set_local(&mut self, index: usize, value: RuntimeValue) -> Result { self.locals.get_mut(index) .ok_or(Error::Local(format!("expected to have local with index {}", index))) .and_then(|l| l.set(value)) .map(|_| InstructionOutcome::RunNextInstruction) } pub fn get_local(&mut self, index: usize) -> Result { self.locals.get(index) .ok_or(Error::Local(format!("expected to have local with index {}", index))) .map(|l| l.get()) } pub fn value_stack(&self) -> &StackWithLimit { &self.value_stack } pub fn value_stack_mut(&mut self) -> &mut StackWithLimit { &mut self.value_stack } pub fn frame_stack(&self) -> &StackWithLimit { &self.frame_stack } pub fn push_frame(&mut self, branch_position: usize, end_position: usize, signature: BlockType) -> Result<(), Error> { self.frame_stack.push(BlockFrame { branch_position: branch_position, end_position: end_position, value_limit: self.value_stack.len(), signature: signature, }) } pub fn discard_frame(&mut self) -> Result<(), Error> { self.frame_stack.pop() .map(|_| ()) } pub fn pop_frame(&mut self, is_branch: bool) -> Result<(), Error> { let frame = self.frame_stack.pop()?; if frame.value_limit > self.value_stack.len() { return Err(Error::Stack("invalid stack len".into())); } let frame_value = match frame.signature { BlockType::Value(_) => Some(self.value_stack.pop()?), BlockType::NoResult => None, }; self.value_stack.resize(frame.value_limit, RuntimeValue::I32(0)); self.position = if is_branch { frame.branch_position } else { frame.end_position }; if let Some(frame_value) = frame_value { self.value_stack.push(frame_value)?; } Ok(()) } } impl BlockFrame { pub fn invalid() -> Self { BlockFrame { branch_position: usize::max_value(), end_position: usize::max_value(), value_limit: usize::max_value(), signature: BlockType::NoResult, } } } fn effective_address(offset: u32, align: u32) -> Result { if align == 0 { Ok(offset) } else { 1u32.checked_shl(align - 1) .and_then(|align| align.checked_add(offset)) .ok_or(Error::Interpreter("invalid memory alignment".into())) } }