/* * Copyright (c) 2021, Ali Mohammad Pur * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include using namespace AK::SIMD; namespace Wasm { #define TRAP_IF_NOT(x) \ do { \ if (trap_if_not(x, #x##sv)) { \ dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \ return; \ } \ } while (false) void BytecodeInterpreter::interpret(Configuration& configuration) { m_trap = Empty {}; auto& instructions = configuration.frame().expression().instructions(); auto max_ip_value = InstructionPointer { instructions.size() }; auto& current_ip_value = configuration.ip(); auto const should_limit_instruction_count = configuration.should_limit_instruction_count(); u64 executed_instructions = 0; while (current_ip_value < max_ip_value) { if (should_limit_instruction_count) { if (executed_instructions++ >= Constants::max_allowed_executed_instructions_per_call) [[unlikely]] { m_trap = Trap { "Exceeded maximum allowed number of instructions" }; return; } } auto& instruction = instructions[current_ip_value.value()]; auto old_ip = current_ip_value; interpret_instruction(configuration, current_ip_value, instruction); if (did_trap()) return; if (current_ip_value == old_ip) // If no jump occurred ++current_ip_value; } } void BytecodeInterpreter::branch_to_label(Configuration& configuration, LabelIndex index) { dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value()); for (size_t i = 0; i < index.value(); ++i) configuration.label_stack().take_last(); auto label = configuration.label_stack().last(); dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label.continuation().value(), label.arity()); configuration.value_stack().remove(label.stack_height(), configuration.value_stack().size() - label.stack_height() - label.arity()); configuration.ip() = label.continuation(); } template void BytecodeInterpreter::load_and_push(Configuration& configuration, Instruction const& instruction) { auto& arg = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[arg.memory_index.value()]; auto memory = configuration.store().get(address); auto& entry = configuration.value_stack().last(); auto base = entry.to(); u64 instance_address = static_cast(bit_cast(base)) + arg.offset; if (instance_address + sizeof(ReadType) > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + sizeof(ReadType), memory->size()); return; } dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType)); auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType)); entry = Value(static_cast(read_value(slice))); } template ALWAYS_INLINE static TDst convert_vector(TSrc v) { return __builtin_convertvector(v, TDst); } template typename SetSign> void BytecodeInterpreter::load_and_push_mxn(Configuration& configuration, Instruction const& instruction) { auto& arg = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[arg.memory_index.value()]; auto memory = configuration.store().get(address); auto& entry = configuration.value_stack().last(); auto base = entry.to(); u64 instance_address = static_cast(bit_cast(base)) + arg.offset; if (instance_address + M * N / 8 > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + M * N / 8, memory->size()); return; } dbgln_if(WASM_TRACE_DEBUG, "vec-load({} : {}) -> stack", instance_address, M * N / 8); auto slice = memory->data().bytes().slice(instance_address, M * N / 8); using V64 = NativeVectorType; using V128 = NativeVectorType; V64 bytes { 0 }; if (bit_cast(slice.data()) % sizeof(V64) == 0) bytes = *bit_cast(slice.data()); else ByteReader::load(slice.data(), bytes); entry = Value(bit_cast(convert_vector(bytes))); } template void BytecodeInterpreter::load_and_push_lane_n(Configuration& configuration, Instruction const& instruction) { auto memarg_and_lane = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[memarg_and_lane.memory.memory_index.value()]; auto memory = configuration.store().get(address); auto vector = configuration.value_stack().take_last().to(); auto base = configuration.value_stack().take_last().to(); u64 instance_address = static_cast(bit_cast(base)) + memarg_and_lane.memory.offset; if (instance_address + N / 8 > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; return; } auto slice = memory->data().bytes().slice(instance_address, N / 8); auto dst = bit_cast(&vector) + memarg_and_lane.lane * N / 8; memcpy(dst, slice.data(), N / 8); configuration.value_stack().append(Value(vector)); } template void BytecodeInterpreter::load_and_push_zero_n(Configuration& configuration, Instruction const& instruction) { auto memarg_and_lane = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[memarg_and_lane.memory_index.value()]; auto memory = configuration.store().get(address); auto base = configuration.value_stack().take_last().to(); u64 instance_address = static_cast(bit_cast(base)) + memarg_and_lane.offset; if (instance_address + N / 8 > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; return; } auto slice = memory->data().bytes().slice(instance_address, N / 8); u128 vector = 0; memcpy(&vector, slice.data(), N / 8); configuration.value_stack().append(Value(vector)); } template void BytecodeInterpreter::load_and_push_m_splat(Configuration& configuration, Instruction const& instruction) { auto& arg = instruction.arguments().get(); auto& address = configuration.frame().module().memories()[arg.memory_index.value()]; auto memory = configuration.store().get(address); auto& entry = configuration.value_stack().last(); auto base = entry.to(); u64 instance_address = static_cast(bit_cast(base)) + arg.offset; if (instance_address + M / 8 > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + M / 8, memory->size()); return; } dbgln_if(WASM_TRACE_DEBUG, "vec-splat({} : {}) -> stack", instance_address, M / 8); auto slice = memory->data().bytes().slice(instance_address, M / 8); auto value = read_value>(slice); set_top_m_splat(configuration, value); } template typename NativeType> void BytecodeInterpreter::set_top_m_splat(Wasm::Configuration& configuration, NativeType value) { auto push = [&](auto result) { configuration.value_stack().last() = Value(bit_cast(result)); }; if constexpr (IsFloatingPoint>) { if constexpr (M == 32) // 32 -> 32x4 push(expand4(value)); else if constexpr (M == 64) // 64 -> 64x2 push(f64x2 { value, value }); else static_assert(DependentFalse>, "Invalid vector size"); } else { if constexpr (M == 8) // 8 -> 8x4 -> 32x4 push(expand4(bit_cast(u8x4 { value, value, value, value }))); else if constexpr (M == 16) // 16 -> 16x2 -> 32x4 push(expand4(bit_cast(u16x2 { value, value }))); else if constexpr (M == 32) // 32 -> 32x4 push(expand4(value)); else if constexpr (M == 64) // 64 -> 64x2 push(u64x2 { value, value }); else static_assert(DependentFalse>, "Invalid vector size"); } } template typename NativeType> void BytecodeInterpreter::pop_and_push_m_splat(Wasm::Configuration& configuration, Instruction const&) { using PopT = Conditional, NativeType<64>>; using ReadT = NativeType; auto entry = configuration.value_stack().last(); auto value = static_cast(entry.to()); dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> splat({})", value, M); set_top_m_splat(configuration, value); } template typename SetSign, typename VectorType> VectorType BytecodeInterpreter::pop_vector(Configuration& configuration) { return bit_cast(configuration.value_stack().take_last().to()); } void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address) { TRAP_IF_NOT(m_stack_info.size_free() >= Constants::minimum_stack_space_to_keep_free); auto instance = configuration.store().get(address); FunctionType const* type { nullptr }; instance->visit([&](auto const& function) { type = &function.type(); }); Vector args; args.ensure_capacity(type->parameters().size()); auto span = configuration.value_stack().span().slice_from_end(type->parameters().size()); for (auto& value : span) args.unchecked_append(value); configuration.value_stack().remove(configuration.value_stack().size() - span.size(), span.size()); Result result { Trap { ""sv } }; if (instance->has()) { CallFrameHandle handle { *this, configuration }; result = configuration.call(*this, address, move(args)); } else { result = configuration.call(*this, address, move(args)); } if (result.is_trap()) { m_trap = move(result.trap()); return; } if (result.is_completion()) { m_trap = move(result.completion()); return; } configuration.value_stack().ensure_capacity(configuration.value_stack().size() + result.values().size()); for (auto& entry : result.values().in_reverse()) configuration.value_stack().unchecked_append(entry); } template void BytecodeInterpreter::binary_numeric_operation(Configuration& configuration, Args&&... args) { auto rhs = configuration.value_stack().take_last().to(); auto& lhs_entry = configuration.value_stack().last(); auto lhs = lhs_entry.to(); PushType result; auto call_result = Operator { forward(args)... }(lhs, rhs); if constexpr (IsSpecializationOf) { if (call_result.is_error()) { trap_if_not(false, call_result.error()); return; } result = call_result.release_value(); } else { result = call_result; } dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), Operator::name(), rhs.value(), result); lhs_entry = Value(result); } template void BytecodeInterpreter::unary_operation(Configuration& configuration, Args&&... args) { auto& entry = configuration.value_stack().last(); auto value = entry.to(); auto call_result = Operator { forward(args)... }(value); PushType result; if constexpr (IsSpecializationOf) { if (call_result.is_error()) { trap_if_not(false, call_result.error()); return; } result = call_result.release_value(); } else { result = call_result; } dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", Operator::name(), *value, result); entry = Value(result); } template struct ConvertToRaw { T operator()(T value) { return LittleEndian(value); } }; template<> struct ConvertToRaw { u32 operator()(float value) { ReadonlyBytes bytes { &value, sizeof(float) }; FixedMemoryStream stream { bytes }; auto res = stream.read_value>().release_value_but_fixme_should_propagate_errors(); return static_cast(res); } }; template<> struct ConvertToRaw { u64 operator()(double value) { ReadonlyBytes bytes { &value, sizeof(double) }; FixedMemoryStream stream { bytes }; auto res = stream.read_value>().release_value_but_fixme_should_propagate_errors(); return static_cast(res); } }; template void BytecodeInterpreter::pop_and_store(Configuration& configuration, Instruction const& instruction) { auto& memarg = instruction.arguments().get(); auto entry = configuration.value_stack().take_last(); auto value = ConvertToRaw {}(entry.to()); dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(StoreT)); auto base = configuration.value_stack().take_last().to(); store_to_memory(configuration, memarg, { &value, sizeof(StoreT) }, base); } template void BytecodeInterpreter::pop_and_store_lane_n(Configuration& configuration, Instruction const& instruction) { auto& memarg_and_lane = instruction.arguments().get(); auto vector = configuration.value_stack().take_last().to(); auto src = bit_cast(&vector) + memarg_and_lane.lane * N / 8; auto base = configuration.value_stack().take_last().to(); store_to_memory(configuration, memarg_and_lane.memory, { src, N / 8 }, base); } void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction::MemoryArgument const& arg, ReadonlyBytes data, u32 base) { auto& address = configuration.frame().module().memories()[arg.memory_index.value()]; auto memory = configuration.store().get(address); u64 instance_address = static_cast(base) + arg.offset; Checked addition { instance_address }; addition += data.size(); if (addition.has_overflow() || addition.value() > memory->size()) { m_trap = Trap { "Memory access out of bounds" }; dbgln("LibWasm: Memory access out of bounds (expected 0 <= {} and {} <= {})", instance_address, instance_address + data.size(), memory->size()); return; } dbgln_if(WASM_TRACE_DEBUG, "temporary({}b) -> store({})", data.size(), instance_address); data.copy_to(memory->data().bytes().slice(instance_address, data.size())); } template T BytecodeInterpreter::read_value(ReadonlyBytes data) { FixedMemoryStream stream { data }; auto value_or_error = stream.read_value>(); if (value_or_error.is_error()) { dbgln("Read from {} failed", data.data()); m_trap = Trap { "Read from memory failed" }; } return value_or_error.release_value(); } template<> float BytecodeInterpreter::read_value(ReadonlyBytes data) { FixedMemoryStream stream { data }; auto raw_value_or_error = stream.read_value>(); if (raw_value_or_error.is_error()) m_trap = Trap { "Read from memory failed" }; auto raw_value = raw_value_or_error.release_value(); return bit_cast(static_cast(raw_value)); } template<> double BytecodeInterpreter::read_value(ReadonlyBytes data) { FixedMemoryStream stream { data }; auto raw_value_or_error = stream.read_value>(); if (raw_value_or_error.is_error()) m_trap = Trap { "Read from memory failed" }; auto raw_value = raw_value_or_error.release_value(); return bit_cast(static_cast(raw_value)); } ALWAYS_INLINE void BytecodeInterpreter::interpret_instruction(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction) { dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value()); switch (instruction.opcode().value()) { case Instructions::unreachable.value(): m_trap = Trap { "Unreachable" }; return; case Instructions::nop.value(): return; case Instructions::local_get.value(): configuration.value_stack().append(Value(configuration.frame().locals()[instruction.arguments().get().value()])); return; case Instructions::local_set.value(): { auto value = configuration.value_stack().take_last(); configuration.frame().locals()[instruction.arguments().get().value()] = value; return; } case Instructions::i32_const.value(): configuration.value_stack().append(Value(instruction.arguments().get())); return; case Instructions::i64_const.value(): configuration.value_stack().append(Value(instruction.arguments().get())); return; case Instructions::f32_const.value(): configuration.value_stack().append(Value(instruction.arguments().get())); return; case Instructions::f64_const.value(): configuration.value_stack().append(Value(instruction.arguments().get())); return; case Instructions::block.value(): { size_t arity = 0; size_t param_arity = 0; auto& args = instruction.arguments().get(); switch (args.block_type.kind()) { case BlockType::Empty: break; case BlockType::Type: arity = 1; break; case BlockType::Index: { auto& type = configuration.frame().module().types()[args.block_type.type_index().value()]; arity = type.results().size(); param_arity = type.parameters().size(); } } configuration.label_stack().append(Label(arity, args.end_ip, configuration.value_stack().size() - param_arity)); return; } case Instructions::loop.value(): { auto& args = instruction.arguments().get(); size_t arity = 0; if (args.block_type.kind() == BlockType::Index) { auto& type = configuration.frame().module().types()[args.block_type.type_index().value()]; arity = type.parameters().size(); } configuration.label_stack().append(Label(arity, ip.value() + 1, configuration.value_stack().size() - arity)); return; } case Instructions::if_.value(): { size_t arity = 0; size_t param_arity = 0; auto& args = instruction.arguments().get(); switch (args.block_type.kind()) { case BlockType::Empty: break; case BlockType::Type: arity = 1; break; case BlockType::Index: { auto& type = configuration.frame().module().types()[args.block_type.type_index().value()]; arity = type.results().size(); param_arity = type.parameters().size(); } } auto value = configuration.value_stack().take_last().to(); auto end_label = Label(arity, args.end_ip.value(), configuration.value_stack().size() - param_arity); if (value == 0) { if (args.else_ip.has_value()) { configuration.ip() = args.else_ip.value(); configuration.label_stack().append(end_label); } else { configuration.ip() = args.end_ip.value() + 1; } } else { configuration.label_stack().append(end_label); } return; } case Instructions::structured_end.value(): configuration.label_stack().take_last(); return; case Instructions::structured_else.value(): { auto label = configuration.label_stack().take_last(); // Jump to the end label configuration.ip() = label.continuation(); return; } case Instructions::return_.value(): { while (configuration.label_stack().size() - 1 != configuration.frame().label_index()) configuration.label_stack().take_last(); configuration.ip() = configuration.frame().expression().instructions().size(); return; } case Instructions::br.value(): return branch_to_label(configuration, instruction.arguments().get()); case Instructions::br_if.value(): { auto cond = configuration.value_stack().take_last().to(); if (cond == 0) return; return branch_to_label(configuration, instruction.arguments().get()); } case Instructions::br_table.value(): { auto& arguments = instruction.arguments().get(); auto i = configuration.value_stack().take_last().to(); if (i >= arguments.labels.size()) { return branch_to_label(configuration, arguments.default_); } return branch_to_label(configuration, arguments.labels[i]); } case Instructions::call.value(): { auto index = instruction.arguments().get(); auto address = configuration.frame().module().functions()[index.value()]; dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value()); call_address(configuration, address); return; } case Instructions::call_indirect.value(): { auto& args = instruction.arguments().get(); auto table_address = configuration.frame().module().tables()[args.table.value()]; auto table_instance = configuration.store().get(table_address); auto index = configuration.value_stack().take_last().to(); TRAP_IF_NOT(index >= 0); TRAP_IF_NOT(static_cast(index) < table_instance->elements().size()); auto element = table_instance->elements()[index]; TRAP_IF_NOT(element.ref().has()); auto address = element.ref().get().address; dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index, address.value()); call_address(configuration, address); return; } case Instructions::i32_load.value(): return load_and_push(configuration, instruction); case Instructions::i64_load.value(): return load_and_push(configuration, instruction); case Instructions::f32_load.value(): return load_and_push(configuration, instruction); case Instructions::f64_load.value(): return load_and_push(configuration, instruction); case Instructions::i32_load8_s.value(): return load_and_push(configuration, instruction); case Instructions::i32_load8_u.value(): return load_and_push(configuration, instruction); case Instructions::i32_load16_s.value(): return load_and_push(configuration, instruction); case Instructions::i32_load16_u.value(): return load_and_push(configuration, instruction); case Instructions::i64_load8_s.value(): return load_and_push(configuration, instruction); case Instructions::i64_load8_u.value(): return load_and_push(configuration, instruction); case Instructions::i64_load16_s.value(): return load_and_push(configuration, instruction); case Instructions::i64_load16_u.value(): return load_and_push(configuration, instruction); case Instructions::i64_load32_s.value(): return load_and_push(configuration, instruction); case Instructions::i64_load32_u.value(): return load_and_push(configuration, instruction); case Instructions::i32_store.value(): return pop_and_store(configuration, instruction); case Instructions::i64_store.value(): return pop_and_store(configuration, instruction); case Instructions::f32_store.value(): return pop_and_store(configuration, instruction); case Instructions::f64_store.value(): return pop_and_store(configuration, instruction); case Instructions::i32_store8.value(): return pop_and_store(configuration, instruction); case Instructions::i32_store16.value(): return pop_and_store(configuration, instruction); case Instructions::i64_store8.value(): return pop_and_store(configuration, instruction); case Instructions::i64_store16.value(): return pop_and_store(configuration, instruction); case Instructions::i64_store32.value(): return pop_and_store(configuration, instruction); case Instructions::local_tee.value(): { auto value = configuration.value_stack().last(); auto local_index = instruction.arguments().get(); dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value()); configuration.frame().locals()[local_index.value()] = value; return; } case Instructions::global_get.value(): { auto global_index = instruction.arguments().get(); // This check here is for const expressions. In non-const expressions, // a validation error would have been thrown. TRAP_IF_NOT(global_index < configuration.frame().module().globals().size()); auto address = configuration.frame().module().globals()[global_index.value()]; dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value()); auto global = configuration.store().get(address); configuration.value_stack().append(global->value()); return; } case Instructions::global_set.value(): { auto global_index = instruction.arguments().get(); auto address = configuration.frame().module().globals()[global_index.value()]; auto value = configuration.value_stack().take_last(); dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value()); auto global = configuration.store().get(address); global->set_value(value); return; } case Instructions::memory_size.value(): { auto& args = instruction.arguments().get(); auto address = configuration.frame().module().memories()[args.memory_index.value()]; auto instance = configuration.store().get(address); auto pages = instance->size() / Constants::page_size; dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages); configuration.value_stack().append(Value((i32)pages)); return; } case Instructions::memory_grow.value(): { auto& args = instruction.arguments().get(); auto address = configuration.frame().module().memories()[args.memory_index.value()]; auto instance = configuration.store().get(address); i32 old_pages = instance->size() / Constants::page_size; auto& entry = configuration.value_stack().last(); auto new_pages = entry.to(); dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", new_pages, old_pages); if (instance->grow(new_pages * Constants::page_size)) entry = Value((i32)old_pages); else entry = Value((i32)-1); return; } // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-fill case Instructions::memory_fill.value(): { auto& args = instruction.arguments().get(); auto address = configuration.frame().module().memories()[args.memory_index.value()]; auto instance = configuration.store().get(address); auto count = configuration.value_stack().take_last().to(); u8 value = static_cast(configuration.value_stack().take_last().to()); auto destination_offset = configuration.value_stack().take_last().to(); TRAP_IF_NOT(static_cast(destination_offset + count) <= instance->data().size()); if (count == 0) return; for (u32 i = 0; i < count; ++i) store_to_memory(configuration, Instruction::MemoryArgument { 0, 0 }, { &value, sizeof(value) }, destination_offset + i); return; } // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-copy case Instructions::memory_copy.value(): { auto& args = instruction.arguments().get(); auto source_address = configuration.frame().module().memories()[args.src_index.value()]; auto destination_address = configuration.frame().module().memories()[args.dst_index.value()]; auto source_instance = configuration.store().get(source_address); auto destination_instance = configuration.store().get(destination_address); auto count = configuration.value_stack().take_last().to(); auto source_offset = configuration.value_stack().take_last().to(); auto destination_offset = configuration.value_stack().take_last().to(); Checked source_position = source_offset; source_position.saturating_add(count); Checked destination_position = destination_offset; destination_position.saturating_add(count); TRAP_IF_NOT(source_position <= source_instance->data().size()); TRAP_IF_NOT(destination_position <= destination_instance->data().size()); if (count == 0) return; Instruction::MemoryArgument memarg { 0, 0, args.dst_index }; if (destination_offset <= source_offset) { for (auto i = 0; i < count; ++i) { auto value = source_instance->data()[source_offset + i]; store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i); } } else { for (auto i = count - 1; i >= 0; --i) { auto value = source_instance->data()[source_offset + i]; store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i); } } return; } // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-init case Instructions::memory_init.value(): { auto& args = instruction.arguments().get(); auto& data_address = configuration.frame().module().datas()[args.data_index.value()]; auto& data = *configuration.store().get(data_address); auto memory_address = configuration.frame().module().memories()[args.memory_index.value()]; auto memory = configuration.store().get(memory_address); auto count = configuration.value_stack().take_last().to(); auto source_offset = configuration.value_stack().take_last().to(); auto destination_offset = configuration.value_stack().take_last().to(); Checked source_position = source_offset; source_position.saturating_add(count); Checked destination_position = destination_offset; destination_position.saturating_add(count); TRAP_IF_NOT(source_position <= data.data().size()); TRAP_IF_NOT(destination_position <= memory->data().size()); if (count == 0) return; Instruction::MemoryArgument memarg { 0, 0, args.memory_index }; for (size_t i = 0; i < (size_t)count; ++i) { auto value = data.data()[source_offset + i]; store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i); } return; } // https://webassembly.github.io/spec/core/bikeshed/#exec-data-drop case Instructions::data_drop.value(): { auto data_index = instruction.arguments().get(); auto data_address = configuration.frame().module().datas()[data_index.value()]; *configuration.store().get(data_address) = DataInstance({}); return; } case Instructions::elem_drop.value(): { auto elem_index = instruction.arguments().get(); auto address = configuration.frame().module().elements()[elem_index.value()]; auto elem = configuration.store().get(address); *configuration.store().get(address) = ElementInstance(elem->type(), {}); return; } case Instructions::table_init.value(): { auto& args = instruction.arguments().get(); auto table_address = configuration.frame().module().tables()[args.table_index.value()]; auto table = configuration.store().get(table_address); auto element_address = configuration.frame().module().elements()[args.element_index.value()]; auto element = configuration.store().get(element_address); auto count = configuration.value_stack().take_last().to(); auto source_offset = configuration.value_stack().take_last().to(); auto destination_offset = configuration.value_stack().take_last().to(); Checked checked_source_offset = source_offset; Checked checked_destination_offset = destination_offset; checked_source_offset += count; checked_destination_offset += count; TRAP_IF_NOT(!checked_source_offset.has_overflow() && checked_source_offset <= (u32)element->references().size()); TRAP_IF_NOT(!checked_destination_offset.has_overflow() && checked_destination_offset <= (u32)table->elements().size()); for (u32 i = 0; i < count; ++i) table->elements()[destination_offset + i] = element->references()[source_offset + i]; return; } case Instructions::table_copy.value(): { auto& args = instruction.arguments().get(); auto source_address = configuration.frame().module().tables()[args.rhs.value()]; auto destination_address = configuration.frame().module().tables()[args.lhs.value()]; auto source_instance = configuration.store().get(source_address); auto destination_instance = configuration.store().get(destination_address); auto count = configuration.value_stack().take_last().to(); auto source_offset = configuration.value_stack().take_last().to(); auto destination_offset = configuration.value_stack().take_last().to(); Checked source_position = source_offset; source_position.saturating_add(count); Checked destination_position = destination_offset; destination_position.saturating_add(count); TRAP_IF_NOT(source_position <= source_instance->elements().size()); TRAP_IF_NOT(destination_position <= destination_instance->elements().size()); if (count == 0) return; if (destination_offset <= source_offset) { for (u32 i = 0; i < count; ++i) { auto value = source_instance->elements()[source_offset + i]; destination_instance->elements()[destination_offset + i] = value; } } else { for (u32 i = count - 1; i != NumericLimits::max(); --i) { auto value = source_instance->elements()[source_offset + i]; destination_instance->elements()[destination_offset + i] = value; } } return; } case Instructions::table_fill.value(): { auto table_index = instruction.arguments().get(); auto address = configuration.frame().module().tables()[table_index.value()]; auto table = configuration.store().get(address); auto count = configuration.value_stack().take_last().to(); auto value = configuration.value_stack().take_last().to(); auto start = configuration.value_stack().take_last().to(); Checked checked_offset = start; checked_offset += count; TRAP_IF_NOT(!checked_offset.has_overflow() && checked_offset <= (u32)table->elements().size()); for (u32 i = 0; i < count; ++i) table->elements()[start + i] = value; return; } case Instructions::table_set.value(): { auto ref = configuration.value_stack().take_last().to(); auto index = (size_t)(configuration.value_stack().take_last().to()); auto table_index = instruction.arguments().get(); auto address = configuration.frame().module().tables()[table_index.value()]; auto table = configuration.store().get(address); TRAP_IF_NOT(index < table->elements().size()); table->elements()[index] = ref; return; } case Instructions::table_get.value(): { auto index = (size_t)(configuration.value_stack().take_last().to()); auto table_index = instruction.arguments().get(); auto address = configuration.frame().module().tables()[table_index.value()]; auto table = configuration.store().get(address); TRAP_IF_NOT(index < table->elements().size()); auto ref = table->elements()[index]; configuration.value_stack().append(Value(ref)); return; } case Instructions::table_grow.value(): { auto size = configuration.value_stack().take_last().to(); auto fill_value = configuration.value_stack().take_last().to(); auto table_index = instruction.arguments().get(); auto address = configuration.frame().module().tables()[table_index.value()]; auto table = configuration.store().get(address); auto previous_size = table->elements().size(); auto did_grow = table->grow(size, fill_value); if (!did_grow) { configuration.value_stack().append(Value((i32)-1)); } else { configuration.value_stack().append(Value((i32)previous_size)); } return; } case Instructions::table_size.value(): { auto table_index = instruction.arguments().get(); auto address = configuration.frame().module().tables()[table_index.value()]; auto table = configuration.store().get(address); configuration.value_stack().append(Value((i32)table->elements().size())); return; } case Instructions::ref_null.value(): { auto type = instruction.arguments().get(); configuration.value_stack().append(Value(Reference(Reference::Null { type }))); return; }; case Instructions::ref_func.value(): { auto index = instruction.arguments().get().value(); auto& functions = configuration.frame().module().functions(); auto address = functions[index]; configuration.value_stack().append(Value(Reference { Reference::Func { address, configuration.store().get_module_for(address) } })); return; } case Instructions::ref_is_null.value(): { auto ref = configuration.value_stack().take_last().to(); configuration.value_stack().append(Value(static_cast(ref.ref().has() ? 1 : 0))); return; } case Instructions::drop.value(): configuration.value_stack().take_last(); return; case Instructions::select.value(): case Instructions::select_typed.value(): { // Note: The type seems to only be used for validation. auto value = configuration.value_stack().take_last().to(); dbgln_if(WASM_TRACE_DEBUG, "select({})", value); auto rhs = configuration.value_stack().take_last(); auto& lhs = configuration.value_stack().last(); lhs = value != 0 ? lhs : rhs; return; } case Instructions::i32_eqz.value(): return unary_operation(configuration); case Instructions::i32_eq.value(): return binary_numeric_operation(configuration); case Instructions::i32_ne.value(): return binary_numeric_operation(configuration); case Instructions::i32_lts.value(): return binary_numeric_operation(configuration); case Instructions::i32_ltu.value(): return binary_numeric_operation(configuration); case Instructions::i32_gts.value(): return binary_numeric_operation(configuration); case Instructions::i32_gtu.value(): return binary_numeric_operation(configuration); case Instructions::i32_les.value(): return binary_numeric_operation(configuration); case Instructions::i32_leu.value(): return binary_numeric_operation(configuration); case Instructions::i32_ges.value(): return binary_numeric_operation(configuration); case Instructions::i32_geu.value(): return binary_numeric_operation(configuration); case Instructions::i64_eqz.value(): return unary_operation(configuration); case Instructions::i64_eq.value(): return binary_numeric_operation(configuration); case Instructions::i64_ne.value(): return binary_numeric_operation(configuration); case Instructions::i64_lts.value(): return binary_numeric_operation(configuration); case Instructions::i64_ltu.value(): return binary_numeric_operation(configuration); case Instructions::i64_gts.value(): return binary_numeric_operation(configuration); case Instructions::i64_gtu.value(): return binary_numeric_operation(configuration); case Instructions::i64_les.value(): return binary_numeric_operation(configuration); case Instructions::i64_leu.value(): return binary_numeric_operation(configuration); case Instructions::i64_ges.value(): return binary_numeric_operation(configuration); case Instructions::i64_geu.value(): return binary_numeric_operation(configuration); case Instructions::f32_eq.value(): return binary_numeric_operation(configuration); case Instructions::f32_ne.value(): return binary_numeric_operation(configuration); case Instructions::f32_lt.value(): return binary_numeric_operation(configuration); case Instructions::f32_gt.value(): return binary_numeric_operation(configuration); case Instructions::f32_le.value(): return binary_numeric_operation(configuration); case Instructions::f32_ge.value(): return binary_numeric_operation(configuration); case Instructions::f64_eq.value(): return binary_numeric_operation(configuration); case Instructions::f64_ne.value(): return binary_numeric_operation(configuration); case Instructions::f64_lt.value(): return binary_numeric_operation(configuration); case Instructions::f64_gt.value(): return binary_numeric_operation(configuration); case Instructions::f64_le.value(): return binary_numeric_operation(configuration); case Instructions::f64_ge.value(): return binary_numeric_operation(configuration); case Instructions::i32_clz.value(): return unary_operation(configuration); case Instructions::i32_ctz.value(): return unary_operation(configuration); case Instructions::i32_popcnt.value(): return unary_operation(configuration); case Instructions::i32_add.value(): return binary_numeric_operation(configuration); case Instructions::i32_sub.value(): return binary_numeric_operation(configuration); case Instructions::i32_mul.value(): return binary_numeric_operation(configuration); case Instructions::i32_divs.value(): return binary_numeric_operation(configuration); case Instructions::i32_divu.value(): return binary_numeric_operation(configuration); case Instructions::i32_rems.value(): return binary_numeric_operation(configuration); case Instructions::i32_remu.value(): return binary_numeric_operation(configuration); case Instructions::i32_and.value(): return binary_numeric_operation(configuration); case Instructions::i32_or.value(): return binary_numeric_operation(configuration); case Instructions::i32_xor.value(): return binary_numeric_operation(configuration); case Instructions::i32_shl.value(): return binary_numeric_operation(configuration); case Instructions::i32_shrs.value(): return binary_numeric_operation(configuration); case Instructions::i32_shru.value(): return binary_numeric_operation(configuration); case Instructions::i32_rotl.value(): return binary_numeric_operation(configuration); case Instructions::i32_rotr.value(): return binary_numeric_operation(configuration); case Instructions::i64_clz.value(): return unary_operation(configuration); case Instructions::i64_ctz.value(): return unary_operation(configuration); case Instructions::i64_popcnt.value(): return unary_operation(configuration); case Instructions::i64_add.value(): return binary_numeric_operation(configuration); case Instructions::i64_sub.value(): return binary_numeric_operation(configuration); case Instructions::i64_mul.value(): return binary_numeric_operation(configuration); case Instructions::i64_divs.value(): return binary_numeric_operation(configuration); case Instructions::i64_divu.value(): return binary_numeric_operation(configuration); case Instructions::i64_rems.value(): return binary_numeric_operation(configuration); case Instructions::i64_remu.value(): return binary_numeric_operation(configuration); case Instructions::i64_and.value(): return binary_numeric_operation(configuration); case Instructions::i64_or.value(): return binary_numeric_operation(configuration); case Instructions::i64_xor.value(): return binary_numeric_operation(configuration); case Instructions::i64_shl.value(): return binary_numeric_operation(configuration); case Instructions::i64_shrs.value(): return binary_numeric_operation(configuration); case Instructions::i64_shru.value(): return binary_numeric_operation(configuration); case Instructions::i64_rotl.value(): return binary_numeric_operation(configuration); case Instructions::i64_rotr.value(): return binary_numeric_operation(configuration); case Instructions::f32_abs.value(): return unary_operation(configuration); case Instructions::f32_neg.value(): return unary_operation(configuration); case Instructions::f32_ceil.value(): return unary_operation(configuration); case Instructions::f32_floor.value(): return unary_operation(configuration); case Instructions::f32_trunc.value(): return unary_operation(configuration); case Instructions::f32_nearest.value(): return unary_operation(configuration); case Instructions::f32_sqrt.value(): return unary_operation(configuration); case Instructions::f32_add.value(): return binary_numeric_operation(configuration); case Instructions::f32_sub.value(): return binary_numeric_operation(configuration); case Instructions::f32_mul.value(): return binary_numeric_operation(configuration); case Instructions::f32_div.value(): return binary_numeric_operation(configuration); case Instructions::f32_min.value(): return binary_numeric_operation(configuration); case Instructions::f32_max.value(): return binary_numeric_operation(configuration); case Instructions::f32_copysign.value(): return binary_numeric_operation(configuration); case Instructions::f64_abs.value(): return unary_operation(configuration); case Instructions::f64_neg.value(): return unary_operation(configuration); case Instructions::f64_ceil.value(): return unary_operation(configuration); case Instructions::f64_floor.value(): return unary_operation(configuration); case Instructions::f64_trunc.value(): return unary_operation(configuration); case Instructions::f64_nearest.value(): return unary_operation(configuration); case Instructions::f64_sqrt.value(): return unary_operation(configuration); case Instructions::f64_add.value(): return binary_numeric_operation(configuration); case Instructions::f64_sub.value(): return binary_numeric_operation(configuration); case Instructions::f64_mul.value(): return binary_numeric_operation(configuration); case Instructions::f64_div.value(): return binary_numeric_operation(configuration); case Instructions::f64_min.value(): return binary_numeric_operation(configuration); case Instructions::f64_max.value(): return binary_numeric_operation(configuration); case Instructions::f64_copysign.value(): return binary_numeric_operation(configuration); case Instructions::i32_wrap_i64.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sf32.value(): return unary_operation>(configuration); case Instructions::i32_trunc_uf32.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sf64.value(): return unary_operation>(configuration); case Instructions::i32_trunc_uf64.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sf32.value(): return unary_operation>(configuration); case Instructions::i64_trunc_uf32.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sf64.value(): return unary_operation>(configuration); case Instructions::i64_trunc_uf64.value(): return unary_operation>(configuration); case Instructions::i64_extend_si32.value(): return unary_operation>(configuration); case Instructions::i64_extend_ui32.value(): return unary_operation>(configuration); case Instructions::f32_convert_si32.value(): return unary_operation>(configuration); case Instructions::f32_convert_ui32.value(): return unary_operation>(configuration); case Instructions::f32_convert_si64.value(): return unary_operation>(configuration); case Instructions::f32_convert_ui64.value(): return unary_operation>(configuration); case Instructions::f32_demote_f64.value(): return unary_operation(configuration); case Instructions::f64_convert_si32.value(): return unary_operation>(configuration); case Instructions::f64_convert_ui32.value(): return unary_operation>(configuration); case Instructions::f64_convert_si64.value(): return unary_operation>(configuration); case Instructions::f64_convert_ui64.value(): return unary_operation>(configuration); case Instructions::f64_promote_f32.value(): return unary_operation(configuration); case Instructions::i32_reinterpret_f32.value(): return unary_operation>(configuration); case Instructions::i64_reinterpret_f64.value(): return unary_operation>(configuration); case Instructions::f32_reinterpret_i32.value(): return unary_operation>(configuration); case Instructions::f64_reinterpret_i64.value(): return unary_operation>(configuration); case Instructions::i32_extend8_s.value(): return unary_operation>(configuration); case Instructions::i32_extend16_s.value(): return unary_operation>(configuration); case Instructions::i64_extend8_s.value(): return unary_operation>(configuration); case Instructions::i64_extend16_s.value(): return unary_operation>(configuration); case Instructions::i64_extend32_s.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sat_f32_s.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sat_f32_u.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sat_f64_s.value(): return unary_operation>(configuration); case Instructions::i32_trunc_sat_f64_u.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sat_f32_s.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sat_f32_u.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sat_f64_s.value(): return unary_operation>(configuration); case Instructions::i64_trunc_sat_f64_u.value(): return unary_operation>(configuration); case Instructions::v128_const.value(): configuration.value_stack().append(Value(instruction.arguments().get())); return; case Instructions::v128_load.value(): return load_and_push(configuration, instruction); case Instructions::v128_load8x8_s.value(): return load_and_push_mxn<8, 8, MakeSigned>(configuration, instruction); case Instructions::v128_load8x8_u.value(): return load_and_push_mxn<8, 8, MakeUnsigned>(configuration, instruction); case Instructions::v128_load16x4_s.value(): return load_and_push_mxn<16, 4, MakeSigned>(configuration, instruction); case Instructions::v128_load16x4_u.value(): return load_and_push_mxn<16, 4, MakeUnsigned>(configuration, instruction); case Instructions::v128_load32x2_s.value(): return load_and_push_mxn<32, 2, MakeSigned>(configuration, instruction); case Instructions::v128_load32x2_u.value(): return load_and_push_mxn<32, 2, MakeUnsigned>(configuration, instruction); case Instructions::v128_load8_splat.value(): return load_and_push_m_splat<8>(configuration, instruction); case Instructions::v128_load16_splat.value(): return load_and_push_m_splat<16>(configuration, instruction); case Instructions::v128_load32_splat.value(): return load_and_push_m_splat<32>(configuration, instruction); case Instructions::v128_load64_splat.value(): return load_and_push_m_splat<64>(configuration, instruction); case Instructions::i8x16_splat.value(): return pop_and_push_m_splat<8, NativeIntegralType>(configuration, instruction); case Instructions::i16x8_splat.value(): return pop_and_push_m_splat<16, NativeIntegralType>(configuration, instruction); case Instructions::i32x4_splat.value(): return pop_and_push_m_splat<32, NativeIntegralType>(configuration, instruction); case Instructions::i64x2_splat.value(): return pop_and_push_m_splat<64, NativeIntegralType>(configuration, instruction); case Instructions::f32x4_splat.value(): return pop_and_push_m_splat<32, NativeFloatingType>(configuration, instruction); case Instructions::f64x2_splat.value(): return pop_and_push_m_splat<64, NativeFloatingType>(configuration, instruction); case Instructions::i8x16_shuffle.value(): { auto& arg = instruction.arguments().get(); auto b = pop_vector(configuration); auto a = pop_vector(configuration); using VectorType = Native128ByteVectorOf; VectorType result; for (size_t i = 0; i < 16; ++i) if (arg.lanes[i] < 16) result[i] = a[arg.lanes[i]]; else result[i] = b[arg.lanes[i] - 16]; configuration.value_stack().append(Value(bit_cast(result))); return; } case Instructions::v128_store.value(): return pop_and_store(configuration, instruction); case Instructions::i8x16_shl.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i8x16_shr_u.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i8x16_shr_s.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i16x8_shl.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i16x8_shr_u.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i16x8_shr_s.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i32x4_shl.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i32x4_shr_u.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i32x4_shr_s.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i64x2_shl.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i64x2_shr_u.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i64x2_shr_s.value(): return binary_numeric_operation, i32>(configuration); case Instructions::i8x16_swizzle.value(): return binary_numeric_operation(configuration); case Instructions::i8x16_extract_lane_s.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i8x16_extract_lane_u.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i16x8_extract_lane_s.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i16x8_extract_lane_u.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i32x4_extract_lane.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i64x2_extract_lane.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::f32x4_extract_lane.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::f64x2_extract_lane.value(): return unary_operation>(configuration, instruction.arguments().get().lane); case Instructions::i8x16_replace_lane.value(): return binary_numeric_operation, i32>(configuration, instruction.arguments().get().lane); case Instructions::i16x8_replace_lane.value(): return binary_numeric_operation, i32>(configuration, instruction.arguments().get().lane); case Instructions::i32x4_replace_lane.value(): return binary_numeric_operation, i32>(configuration, instruction.arguments().get().lane); case Instructions::i64x2_replace_lane.value(): return binary_numeric_operation, i64>(configuration, instruction.arguments().get().lane); case Instructions::f32x4_replace_lane.value(): return binary_numeric_operation, float>(configuration, instruction.arguments().get().lane); case Instructions::f64x2_replace_lane.value(): return binary_numeric_operation, double>(configuration, instruction.arguments().get().lane); case Instructions::i8x16_eq.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_ne.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_lt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_lt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_gt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_gt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_le_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_le_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_ge_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_ge_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_abs.value(): return unary_operation>(configuration); case Instructions::i8x16_neg.value(): return unary_operation>(configuration); case Instructions::i8x16_all_true.value(): return unary_operation>(configuration); case Instructions::i8x16_popcnt.value(): return unary_operation>(configuration); case Instructions::i8x16_add.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_sub.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_avgr_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_add_sat_s.value(): return binary_numeric_operation, MakeSigned>>(configuration); case Instructions::i8x16_add_sat_u.value(): return binary_numeric_operation, MakeUnsigned>>(configuration); case Instructions::i8x16_sub_sat_s.value(): return binary_numeric_operation, MakeSigned>>(configuration); case Instructions::i8x16_sub_sat_u.value(): return binary_numeric_operation, MakeUnsigned>>(configuration); case Instructions::i8x16_min_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_min_u.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_max_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_max_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_eq.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_ne.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_lt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_lt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_gt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_gt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_le_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_le_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_ge_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_ge_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_abs.value(): return unary_operation>(configuration); case Instructions::i16x8_neg.value(): return unary_operation>(configuration); case Instructions::i16x8_all_true.value(): return unary_operation>(configuration); case Instructions::i16x8_add.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_sub.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_mul.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_avgr_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_add_sat_s.value(): return binary_numeric_operation, MakeSigned>>(configuration); case Instructions::i16x8_add_sat_u.value(): return binary_numeric_operation, MakeUnsigned>>(configuration); case Instructions::i16x8_sub_sat_s.value(): return binary_numeric_operation, MakeSigned>>(configuration); case Instructions::i16x8_sub_sat_u.value(): return binary_numeric_operation, MakeUnsigned>>(configuration); case Instructions::i16x8_min_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_min_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_max_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_max_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_extend_low_i8x16_s.value(): return unary_operation>(configuration); case Instructions::i16x8_extend_high_i8x16_s.value(): return unary_operation>(configuration); case Instructions::i16x8_extend_low_i8x16_u.value(): return unary_operation>(configuration); case Instructions::i16x8_extend_high_i8x16_u.value(): return unary_operation>(configuration); case Instructions::i16x8_extadd_pairwise_i8x16_s.value(): return unary_operation>(configuration); case Instructions::i16x8_extadd_pairwise_i8x16_u.value(): return unary_operation>(configuration); case Instructions::i16x8_extmul_low_i8x16_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_extmul_high_i8x16_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_extmul_low_i8x16_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_extmul_high_i8x16_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_eq.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_ne.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_lt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_lt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_gt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_gt_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_le_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_le_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_ge_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_ge_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_abs.value(): return unary_operation>(configuration); case Instructions::i32x4_neg.value(): return unary_operation>(configuration); case Instructions::i32x4_all_true.value(): return unary_operation>(configuration); case Instructions::i32x4_add.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_sub.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_mul.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_min_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_min_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_max_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_max_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_extend_low_i16x8_s.value(): return unary_operation>(configuration); case Instructions::i32x4_extend_high_i16x8_s.value(): return unary_operation>(configuration); case Instructions::i32x4_extend_low_i16x8_u.value(): return unary_operation>(configuration); case Instructions::i32x4_extend_high_i16x8_u.value(): return unary_operation>(configuration); case Instructions::i32x4_extadd_pairwise_i16x8_s.value(): return unary_operation>(configuration); case Instructions::i32x4_extadd_pairwise_i16x8_u.value(): return unary_operation>(configuration); case Instructions::i32x4_extmul_low_i16x8_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_extmul_high_i16x8_s.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_extmul_low_i16x8_u.value(): return binary_numeric_operation>(configuration); case Instructions::i32x4_extmul_high_i16x8_u.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_eq.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_ne.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_lt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_gt_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_le_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_ge_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_abs.value(): return unary_operation>(configuration); case Instructions::i64x2_neg.value(): return unary_operation>(configuration); case Instructions::i64x2_all_true.value(): return unary_operation>(configuration); case Instructions::i64x2_add.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_sub.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_mul.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_extend_low_i32x4_s.value(): return unary_operation>(configuration); case Instructions::i64x2_extend_high_i32x4_s.value(): return unary_operation>(configuration); case Instructions::i64x2_extend_low_i32x4_u.value(): return unary_operation>(configuration); case Instructions::i64x2_extend_high_i32x4_u.value(): return unary_operation>(configuration); case Instructions::i64x2_extmul_low_i32x4_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_extmul_high_i32x4_s.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_extmul_low_i32x4_u.value(): return binary_numeric_operation>(configuration); case Instructions::i64x2_extmul_high_i32x4_u.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_eq.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_ne.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_lt.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_gt.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_le.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_ge.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_min.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_max.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_eq.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_ne.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_lt.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_gt.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_le.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_ge.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_min.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_max.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_div.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_mul.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_sub.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_add.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_pmin.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_pmax.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_div.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_mul.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_sub.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_add.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_pmin.value(): return binary_numeric_operation>(configuration); case Instructions::f64x2_pmax.value(): return binary_numeric_operation>(configuration); case Instructions::f32x4_ceil.value(): return unary_operation>(configuration); case Instructions::f32x4_floor.value(): return unary_operation>(configuration); case Instructions::f32x4_trunc.value(): return unary_operation>(configuration); case Instructions::f32x4_nearest.value(): return unary_operation>(configuration); case Instructions::f32x4_sqrt.value(): return unary_operation>(configuration); case Instructions::f32x4_neg.value(): return unary_operation>(configuration); case Instructions::f32x4_abs.value(): return unary_operation>(configuration); case Instructions::f64x2_ceil.value(): return unary_operation>(configuration); case Instructions::f64x2_floor.value(): return unary_operation>(configuration); case Instructions::f64x2_trunc.value(): return unary_operation>(configuration); case Instructions::f64x2_nearest.value(): return unary_operation>(configuration); case Instructions::f64x2_sqrt.value(): return unary_operation>(configuration); case Instructions::f64x2_neg.value(): return unary_operation>(configuration); case Instructions::f64x2_abs.value(): return unary_operation>(configuration); case Instructions::v128_and.value(): return binary_numeric_operation(configuration); case Instructions::v128_or.value(): return binary_numeric_operation(configuration); case Instructions::v128_xor.value(): return binary_numeric_operation(configuration); case Instructions::v128_not.value(): return unary_operation(configuration); case Instructions::v128_andnot.value(): return binary_numeric_operation(configuration); case Instructions::v128_bitselect.value(): { auto mask = configuration.value_stack().take_last().to(); auto false_vector = configuration.value_stack().take_last().to(); auto true_vector = configuration.value_stack().take_last().to(); u128 result = (true_vector & mask) | (false_vector & ~mask); configuration.value_stack().append(Value(result)); return; } case Instructions::v128_any_true.value(): { auto vector = configuration.value_stack().take_last().to(); configuration.value_stack().append(Value(static_cast(vector != 0))); return; } case Instructions::v128_load8_lane.value(): return load_and_push_lane_n<8>(configuration, instruction); case Instructions::v128_load16_lane.value(): return load_and_push_lane_n<16>(configuration, instruction); case Instructions::v128_load32_lane.value(): return load_and_push_lane_n<32>(configuration, instruction); case Instructions::v128_load64_lane.value(): return load_and_push_lane_n<64>(configuration, instruction); case Instructions::v128_load32_zero.value(): return load_and_push_zero_n<32>(configuration, instruction); case Instructions::v128_load64_zero.value(): return load_and_push_zero_n<64>(configuration, instruction); case Instructions::v128_store8_lane.value(): return pop_and_store_lane_n<8>(configuration, instruction); case Instructions::v128_store16_lane.value(): return pop_and_store_lane_n<16>(configuration, instruction); case Instructions::v128_store32_lane.value(): return pop_and_store_lane_n<32>(configuration, instruction); case Instructions::v128_store64_lane.value(): return pop_and_store_lane_n<64>(configuration, instruction); case Instructions::i32x4_trunc_sat_f32x4_s.value(): return unary_operation>>(configuration); case Instructions::i32x4_trunc_sat_f32x4_u.value(): return unary_operation>>(configuration); case Instructions::i8x16_bitmask.value(): return unary_operation>(configuration); case Instructions::i16x8_bitmask.value(): return unary_operation>(configuration); case Instructions::i32x4_bitmask.value(): return unary_operation>(configuration); case Instructions::i64x2_bitmask.value(): return unary_operation>(configuration); case Instructions::i32x4_dot_i16x8_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_narrow_i16x8_s.value(): return binary_numeric_operation>(configuration); case Instructions::i8x16_narrow_i16x8_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_narrow_i32x4_s.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_narrow_i32x4_u.value(): return binary_numeric_operation>(configuration); case Instructions::i16x8_q15mulr_sat_s.value(): return binary_numeric_operation, MakeSigned>>(configuration); case Instructions::f32x4_convert_i32x4_s.value(): return unary_operation>>(configuration); case Instructions::f32x4_convert_i32x4_u.value(): return unary_operation>>(configuration); case Instructions::f64x2_convert_low_i32x4_s.value(): return unary_operation>>(configuration); case Instructions::f64x2_convert_low_i32x4_u.value(): return unary_operation>>(configuration); case Instructions::f32x4_demote_f64x2_zero.value(): return unary_operation>>(configuration); case Instructions::f64x2_promote_low_f32x4.value(): return unary_operation>>(configuration); case Instructions::i32x4_trunc_sat_f64x2_s_zero.value(): return unary_operation>>(configuration); case Instructions::i32x4_trunc_sat_f64x2_u_zero.value(): return unary_operation>>(configuration); } } void DebuggerBytecodeInterpreter::interpret_instruction(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction) { if (pre_interpret_hook) { auto result = pre_interpret_hook(configuration, ip, instruction); if (!result) { m_trap = Trap { "Trapped by user request" }; return; } } BytecodeInterpreter::interpret_instruction(configuration, ip, instruction); if (post_interpret_hook) { auto result = post_interpret_hook(configuration, ip, instruction, *this); if (!result) { m_trap = Trap { "Trapped by user request" }; return; } } } }