BytecodeInterpreter.cpp 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteReader.h>
  8. #include <AK/Debug.h>
  9. #include <AK/Endian.h>
  10. #include <AK/MemoryStream.h>
  11. #include <AK/NumericLimits.h>
  12. #include <AK/SIMDExtras.h>
  13. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  14. #include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
  15. #include <LibWasm/AbstractMachine/Configuration.h>
  16. #include <LibWasm/AbstractMachine/Operators.h>
  17. #include <LibWasm/Opcode.h>
  18. #include <LibWasm/Printer/Printer.h>
  19. using namespace AK::SIMD;
  20. namespace Wasm {
  21. #define TRAP_IF_NOT(x) \
  22. do { \
  23. if (trap_if_not(x, #x##sv)) { \
  24. dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
  25. return; \
  26. } \
  27. } while (false)
  28. #define TRAP_IF_NOT_NORETURN(x) \
  29. do { \
  30. if (trap_if_not(x, #x##sv)) { \
  31. dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
  32. } \
  33. } while (false)
  34. void BytecodeInterpreter::interpret(Configuration& configuration)
  35. {
  36. m_trap = Empty {};
  37. auto& instructions = configuration.frame().expression().instructions();
  38. auto max_ip_value = InstructionPointer { instructions.size() };
  39. auto& current_ip_value = configuration.ip();
  40. auto const should_limit_instruction_count = configuration.should_limit_instruction_count();
  41. u64 executed_instructions = 0;
  42. while (current_ip_value < max_ip_value) {
  43. if (should_limit_instruction_count) {
  44. if (executed_instructions++ >= Constants::max_allowed_executed_instructions_per_call) [[unlikely]] {
  45. m_trap = Trap { "Exceeded maximum allowed number of instructions" };
  46. return;
  47. }
  48. }
  49. auto& instruction = instructions[current_ip_value.value()];
  50. auto old_ip = current_ip_value;
  51. interpret(configuration, current_ip_value, instruction);
  52. if (did_trap())
  53. return;
  54. if (current_ip_value == old_ip) // If no jump occurred
  55. ++current_ip_value;
  56. }
  57. }
  58. void BytecodeInterpreter::branch_to_label(Configuration& configuration, LabelIndex index)
  59. {
  60. dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value());
  61. auto label = configuration.nth_label(index.value());
  62. dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label->continuation().value(), label->arity());
  63. auto results = pop_values(configuration, label->arity());
  64. size_t drop_count = index.value() + 1;
  65. for (; !configuration.stack().is_empty();) {
  66. auto& entry = configuration.stack().peek();
  67. if (entry.has<Label>()) {
  68. if (--drop_count == 0)
  69. break;
  70. }
  71. configuration.stack().pop();
  72. }
  73. for (auto& result : results.in_reverse())
  74. configuration.stack().push(move(result));
  75. configuration.ip() = label->continuation();
  76. }
  77. template<typename ReadType, typename PushType>
  78. void BytecodeInterpreter::load_and_push(Configuration& configuration, Instruction const& instruction)
  79. {
  80. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  81. auto& address = configuration.frame().module().memories()[arg.memory_index.value()];
  82. auto memory = configuration.store().get(address);
  83. auto& entry = configuration.stack().peek();
  84. auto base = *entry.get<Value>().to<i32>();
  85. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + arg.offset;
  86. if (instance_address + sizeof(ReadType) > memory->size()) {
  87. m_trap = Trap { "Memory access out of bounds" };
  88. dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + sizeof(ReadType), memory->size());
  89. return;
  90. }
  91. dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType));
  92. auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType));
  93. configuration.stack().peek() = Value(static_cast<PushType>(read_value<ReadType>(slice)));
  94. }
  95. template<typename TDst, typename TSrc>
  96. ALWAYS_INLINE static TDst convert_vector(TSrc v)
  97. {
  98. return __builtin_convertvector(v, TDst);
  99. }
  100. template<size_t M, size_t N, template<typename> typename SetSign>
  101. void BytecodeInterpreter::load_and_push_mxn(Configuration& configuration, Instruction const& instruction)
  102. {
  103. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  104. auto& address = configuration.frame().module().memories()[arg.memory_index.value()];
  105. auto memory = configuration.store().get(address);
  106. auto& entry = configuration.stack().peek();
  107. auto base = *entry.get<Value>().to<i32>();
  108. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + arg.offset;
  109. if (instance_address + M * N / 8 > memory->size()) {
  110. m_trap = Trap { "Memory access out of bounds" };
  111. dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + M * N / 8, memory->size());
  112. return;
  113. }
  114. dbgln_if(WASM_TRACE_DEBUG, "vec-load({} : {}) -> stack", instance_address, M * N / 8);
  115. auto slice = memory->data().bytes().slice(instance_address, M * N / 8);
  116. using V64 = NativeVectorType<M, N, SetSign>;
  117. using V128 = NativeVectorType<M * 2, N, SetSign>;
  118. V64 bytes { 0 };
  119. if (bit_cast<FlatPtr>(slice.data()) % sizeof(V64) == 0)
  120. bytes = *bit_cast<V64*>(slice.data());
  121. else
  122. ByteReader::load(slice.data(), bytes);
  123. configuration.stack().peek() = Value(bit_cast<u128>(convert_vector<V128>(bytes)));
  124. }
  125. template<size_t N>
  126. void BytecodeInterpreter::load_and_push_lane_n(Configuration& configuration, Instruction const& instruction)
  127. {
  128. auto memarg_and_lane = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  129. auto& address = configuration.frame().module().memories()[memarg_and_lane.memory.memory_index.value()];
  130. auto memory = configuration.store().get(address);
  131. auto vector = *configuration.stack().pop().get<Value>().to<u128>();
  132. auto base = *configuration.stack().pop().get<Value>().to<u32>();
  133. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + memarg_and_lane.memory.offset;
  134. if (instance_address + N / 8 > memory->size()) {
  135. m_trap = Trap { "Memory access out of bounds" };
  136. return;
  137. }
  138. auto slice = memory->data().bytes().slice(instance_address, N / 8);
  139. auto dst = bit_cast<u8*>(&vector) + memarg_and_lane.lane * N / 8;
  140. memcpy(dst, slice.data(), N / 8);
  141. configuration.stack().push(Value(vector));
  142. }
  143. template<size_t N>
  144. void BytecodeInterpreter::load_and_push_zero_n(Configuration& configuration, Instruction const& instruction)
  145. {
  146. auto memarg_and_lane = instruction.arguments().get<Instruction::MemoryArgument>();
  147. auto& address = configuration.frame().module().memories()[memarg_and_lane.memory_index.value()];
  148. auto memory = configuration.store().get(address);
  149. auto base = *configuration.stack().pop().get<Value>().to<u32>();
  150. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + memarg_and_lane.offset;
  151. if (instance_address + N / 8 > memory->size()) {
  152. m_trap = Trap { "Memory access out of bounds" };
  153. return;
  154. }
  155. auto slice = memory->data().bytes().slice(instance_address, N / 8);
  156. u128 vector = 0;
  157. memcpy(&vector, slice.data(), N / 8);
  158. configuration.stack().push(Value(vector));
  159. }
  160. template<size_t M>
  161. void BytecodeInterpreter::load_and_push_m_splat(Configuration& configuration, Instruction const& instruction)
  162. {
  163. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  164. auto& address = configuration.frame().module().memories()[arg.memory_index.value()];
  165. auto memory = configuration.store().get(address);
  166. auto& entry = configuration.stack().peek();
  167. auto base = *entry.get<Value>().to<i32>();
  168. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + arg.offset;
  169. if (instance_address + M / 8 > memory->size()) {
  170. m_trap = Trap { "Memory access out of bounds" };
  171. dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + M / 8, memory->size());
  172. return;
  173. }
  174. dbgln_if(WASM_TRACE_DEBUG, "vec-splat({} : {}) -> stack", instance_address, M / 8);
  175. auto slice = memory->data().bytes().slice(instance_address, M / 8);
  176. auto value = read_value<NativeIntegralType<M>>(slice);
  177. set_top_m_splat<M, NativeIntegralType>(configuration, value);
  178. }
  179. template<size_t M, template<size_t> typename NativeType>
  180. void BytecodeInterpreter::set_top_m_splat(Wasm::Configuration& configuration, NativeType<M> value)
  181. {
  182. auto push = [&](auto result) {
  183. configuration.stack().peek() = Value(bit_cast<u128>(result));
  184. };
  185. if constexpr (IsFloatingPoint<NativeType<32>>) {
  186. if constexpr (M == 32) // 32 -> 32x4
  187. push(expand4(value));
  188. else if constexpr (M == 64) // 64 -> 64x2
  189. push(f64x2 { value, value });
  190. else
  191. static_assert(DependentFalse<NativeType<M>>, "Invalid vector size");
  192. } else {
  193. if constexpr (M == 8) // 8 -> 8x4 -> 32x4
  194. push(expand4(bit_cast<u32>(u8x4 { value, value, value, value })));
  195. else if constexpr (M == 16) // 16 -> 16x2 -> 32x4
  196. push(expand4(bit_cast<u32>(u16x2 { value, value })));
  197. else if constexpr (M == 32) // 32 -> 32x4
  198. push(expand4(value));
  199. else if constexpr (M == 64) // 64 -> 64x2
  200. push(u64x2 { value, value });
  201. else
  202. static_assert(DependentFalse<NativeType<M>>, "Invalid vector size");
  203. }
  204. }
  205. template<size_t M, template<size_t> typename NativeType>
  206. void BytecodeInterpreter::pop_and_push_m_splat(Wasm::Configuration& configuration, Instruction const&)
  207. {
  208. using PopT = Conditional<M <= 32, NativeType<32>, NativeType<64>>;
  209. using ReadT = NativeType<M>;
  210. auto entry = configuration.stack().peek();
  211. auto value = static_cast<ReadT>(*entry.get<Value>().to<PopT>());
  212. dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> splat({})", value, M);
  213. set_top_m_splat<M, NativeType>(configuration, value);
  214. }
  215. template<typename M, template<typename> typename SetSign, typename VectorType>
  216. VectorType BytecodeInterpreter::pop_vector(Configuration& configuration)
  217. {
  218. return bit_cast<VectorType>(configuration.stack().pop().get<Value>().value().get<u128>());
  219. }
  220. void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address)
  221. {
  222. TRAP_IF_NOT(m_stack_info.size_free() >= Constants::minimum_stack_space_to_keep_free);
  223. auto instance = configuration.store().get(address);
  224. FunctionType const* type { nullptr };
  225. instance->visit([&](auto const& function) { type = &function.type(); });
  226. TRAP_IF_NOT(configuration.stack().entries().size() > type->parameters().size());
  227. Vector<Value> args;
  228. args.ensure_capacity(type->parameters().size());
  229. auto span = configuration.stack().entries().span().slice_from_end(type->parameters().size());
  230. for (auto& entry : span) {
  231. auto* call_argument = entry.get_pointer<Value>();
  232. TRAP_IF_NOT(call_argument);
  233. args.unchecked_append(move(*call_argument));
  234. }
  235. configuration.stack().entries().remove(configuration.stack().size() - span.size(), span.size());
  236. Result result { Trap { ""sv } };
  237. {
  238. CallFrameHandle handle { *this, configuration };
  239. result = configuration.call(*this, address, move(args));
  240. }
  241. if (result.is_trap()) {
  242. m_trap = move(result.trap());
  243. return;
  244. }
  245. if (result.is_completion()) {
  246. m_trap = move(result.completion());
  247. return;
  248. }
  249. configuration.stack().entries().ensure_capacity(configuration.stack().size() + result.values().size());
  250. for (auto& entry : result.values().in_reverse())
  251. configuration.stack().entries().unchecked_append(move(entry));
  252. }
  253. template<typename PopTypeLHS, typename PushType, typename Operator, typename PopTypeRHS, typename... Args>
  254. void BytecodeInterpreter::binary_numeric_operation(Configuration& configuration, Args&&... args)
  255. {
  256. auto rhs_entry = configuration.stack().pop();
  257. auto& lhs_entry = configuration.stack().peek();
  258. auto rhs_ptr = rhs_entry.get_pointer<Value>();
  259. auto lhs_ptr = lhs_entry.get_pointer<Value>();
  260. auto rhs = rhs_ptr->to<PopTypeRHS>();
  261. auto lhs = lhs_ptr->to<PopTypeLHS>();
  262. PushType result;
  263. auto call_result = Operator { forward<Args>(args)... }(lhs.value(), rhs.value());
  264. if constexpr (IsSpecializationOf<decltype(call_result), AK::ErrorOr>) {
  265. if (call_result.is_error()) {
  266. trap_if_not(false, call_result.error());
  267. return;
  268. }
  269. result = call_result.release_value();
  270. } else {
  271. result = call_result;
  272. }
  273. dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), Operator::name(), rhs.value(), result);
  274. lhs_entry = Value(result);
  275. }
  276. template<typename PopType, typename PushType, typename Operator, typename... Args>
  277. void BytecodeInterpreter::unary_operation(Configuration& configuration, Args&&... args)
  278. {
  279. auto& entry = configuration.stack().peek();
  280. auto entry_ptr = entry.get_pointer<Value>();
  281. auto value = entry_ptr->to<PopType>();
  282. auto call_result = Operator { forward<Args>(args)... }(*value);
  283. PushType result;
  284. if constexpr (IsSpecializationOf<decltype(call_result), AK::ErrorOr>) {
  285. if (call_result.is_error()) {
  286. trap_if_not(false, call_result.error());
  287. return;
  288. }
  289. result = call_result.release_value();
  290. } else {
  291. result = call_result;
  292. }
  293. dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", Operator::name(), *value, result);
  294. entry = Value(result);
  295. }
  296. template<typename T>
  297. struct ConvertToRaw {
  298. T operator()(T value)
  299. {
  300. return LittleEndian<T>(value);
  301. }
  302. };
  303. template<>
  304. struct ConvertToRaw<float> {
  305. u32 operator()(float value)
  306. {
  307. ReadonlyBytes bytes { &value, sizeof(float) };
  308. FixedMemoryStream stream { bytes };
  309. auto res = stream.read_value<LittleEndian<u32>>().release_value_but_fixme_should_propagate_errors();
  310. return static_cast<u32>(res);
  311. }
  312. };
  313. template<>
  314. struct ConvertToRaw<double> {
  315. u64 operator()(double value)
  316. {
  317. ReadonlyBytes bytes { &value, sizeof(double) };
  318. FixedMemoryStream stream { bytes };
  319. auto res = stream.read_value<LittleEndian<u64>>().release_value_but_fixme_should_propagate_errors();
  320. return static_cast<u64>(res);
  321. }
  322. };
  323. template<typename PopT, typename StoreT>
  324. void BytecodeInterpreter::pop_and_store(Configuration& configuration, Instruction const& instruction)
  325. {
  326. auto& memarg = instruction.arguments().get<Instruction::MemoryArgument>();
  327. auto entry = configuration.stack().pop();
  328. auto value = ConvertToRaw<StoreT> {}(*entry.get<Value>().to<PopT>());
  329. dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(StoreT));
  330. auto base_entry = configuration.stack().pop();
  331. auto base = base_entry.get<Value>().to<i32>();
  332. store_to_memory(configuration, memarg, { &value, sizeof(StoreT) }, *base);
  333. }
  334. template<size_t N>
  335. void BytecodeInterpreter::pop_and_store_lane_n(Configuration& configuration, Instruction const& instruction)
  336. {
  337. auto& memarg_and_lane = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  338. auto vector = *configuration.stack().pop().get<Value>().to<u128>();
  339. auto src = bit_cast<u8*>(&vector) + memarg_and_lane.lane * N / 8;
  340. auto base = *configuration.stack().pop().get<Value>().to<u32>();
  341. store_to_memory(configuration, memarg_and_lane.memory, { src, N / 8 }, base);
  342. }
  343. void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction::MemoryArgument const& arg, ReadonlyBytes data, u32 base)
  344. {
  345. auto& address = configuration.frame().module().memories()[arg.memory_index.value()];
  346. auto memory = configuration.store().get(address);
  347. u64 instance_address = static_cast<u64>(base) + arg.offset;
  348. Checked addition { instance_address };
  349. addition += data.size();
  350. if (addition.has_overflow() || addition.value() > memory->size()) {
  351. m_trap = Trap { "Memory access out of bounds" };
  352. dbgln("LibWasm: Memory access out of bounds (expected 0 <= {} and {} <= {})", instance_address, instance_address + data.size(), memory->size());
  353. return;
  354. }
  355. dbgln_if(WASM_TRACE_DEBUG, "temporary({}b) -> store({})", data.size(), instance_address);
  356. data.copy_to(memory->data().bytes().slice(instance_address, data.size()));
  357. }
  358. template<typename T>
  359. T BytecodeInterpreter::read_value(ReadonlyBytes data)
  360. {
  361. FixedMemoryStream stream { data };
  362. auto value_or_error = stream.read_value<LittleEndian<T>>();
  363. if (value_or_error.is_error()) {
  364. dbgln("Read from {} failed", data.data());
  365. m_trap = Trap { "Read from memory failed" };
  366. }
  367. return value_or_error.release_value();
  368. }
  369. template<>
  370. float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
  371. {
  372. FixedMemoryStream stream { data };
  373. auto raw_value_or_error = stream.read_value<LittleEndian<u32>>();
  374. if (raw_value_or_error.is_error())
  375. m_trap = Trap { "Read from memory failed" };
  376. auto raw_value = raw_value_or_error.release_value();
  377. return bit_cast<float>(static_cast<u32>(raw_value));
  378. }
  379. template<>
  380. double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
  381. {
  382. FixedMemoryStream stream { data };
  383. auto raw_value_or_error = stream.read_value<LittleEndian<u64>>();
  384. if (raw_value_or_error.is_error())
  385. m_trap = Trap { "Read from memory failed" };
  386. auto raw_value = raw_value_or_error.release_value();
  387. return bit_cast<double>(static_cast<u64>(raw_value));
  388. }
  389. Vector<Value> BytecodeInterpreter::pop_values(Configuration& configuration, size_t count)
  390. {
  391. Vector<Value> results;
  392. results.resize(count);
  393. for (size_t i = 0; i < count; ++i) {
  394. auto top_of_stack = configuration.stack().pop();
  395. if (auto value = top_of_stack.get_pointer<Value>())
  396. results[i] = move(*value);
  397. else
  398. TRAP_IF_NOT_NORETURN(value);
  399. }
  400. return results;
  401. }
  402. void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  403. {
  404. dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value());
  405. switch (instruction.opcode().value()) {
  406. case Instructions::unreachable.value():
  407. m_trap = Trap { "Unreachable" };
  408. return;
  409. case Instructions::nop.value():
  410. return;
  411. case Instructions::local_get.value():
  412. configuration.stack().push(Value(configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()]));
  413. return;
  414. case Instructions::local_set.value(): {
  415. auto entry = configuration.stack().pop();
  416. configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()] = move(entry.get<Value>());
  417. return;
  418. }
  419. case Instructions::i32_const.value():
  420. configuration.stack().push(Value(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
  421. return;
  422. case Instructions::i64_const.value():
  423. configuration.stack().push(Value(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
  424. return;
  425. case Instructions::f32_const.value():
  426. configuration.stack().push(Value(Value::AnyValueType(instruction.arguments().get<float>())));
  427. return;
  428. case Instructions::f64_const.value():
  429. configuration.stack().push(Value(Value::AnyValueType(instruction.arguments().get<double>())));
  430. return;
  431. case Instructions::block.value(): {
  432. size_t arity = 0;
  433. size_t parameter_count = 0;
  434. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  435. switch (args.block_type.kind()) {
  436. case BlockType::Empty:
  437. break;
  438. case BlockType::Type:
  439. arity = 1;
  440. break;
  441. case BlockType::Index: {
  442. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  443. arity = type.results().size();
  444. parameter_count = type.parameters().size();
  445. }
  446. }
  447. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, Label(arity, args.end_ip));
  448. return;
  449. }
  450. case Instructions::loop.value(): {
  451. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  452. size_t arity = 0;
  453. if (args.block_type.kind() == BlockType::Index) {
  454. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  455. arity = type.parameters().size();
  456. }
  457. configuration.stack().entries().insert(configuration.stack().size() - arity, Label(arity, ip.value() + 1));
  458. return;
  459. }
  460. case Instructions::if_.value(): {
  461. size_t arity = 0;
  462. size_t parameter_count = 0;
  463. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  464. switch (args.block_type.kind()) {
  465. case BlockType::Empty:
  466. break;
  467. case BlockType::Type:
  468. arity = 1;
  469. break;
  470. case BlockType::Index: {
  471. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  472. arity = type.results().size();
  473. parameter_count = type.parameters().size();
  474. }
  475. }
  476. auto entry = configuration.stack().pop();
  477. auto value = entry.get<Value>().to<i32>();
  478. auto end_label = Label(arity, args.end_ip.value());
  479. if (value.value() == 0) {
  480. if (args.else_ip.has_value()) {
  481. configuration.ip() = args.else_ip.value();
  482. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, end_label);
  483. } else {
  484. configuration.ip() = args.end_ip.value() + 1;
  485. }
  486. } else {
  487. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, end_label);
  488. }
  489. return;
  490. }
  491. case Instructions::structured_end.value():
  492. case Instructions::structured_else.value(): {
  493. auto index = configuration.nth_label_index(0);
  494. auto label = configuration.stack().entries()[*index].get<Label>();
  495. configuration.stack().entries().remove(*index, 1);
  496. if (instruction.opcode() == Instructions::structured_end)
  497. return;
  498. // Jump to the end label
  499. configuration.ip() = label.continuation();
  500. return;
  501. }
  502. case Instructions::return_.value(): {
  503. auto& frame = configuration.frame();
  504. Checked checked_index { configuration.stack().size() };
  505. checked_index -= frame.arity();
  506. VERIFY(!checked_index.has_overflow());
  507. auto index = checked_index.value();
  508. size_t i = 1;
  509. for (; i <= index; ++i) {
  510. auto& entry = configuration.stack().entries()[index - i];
  511. if (entry.has<Label>()) {
  512. if (configuration.stack().entries()[index - i - 1].has<Frame>())
  513. break;
  514. }
  515. }
  516. configuration.stack().entries().remove(index - i + 1, i - 1);
  517. // Jump past the call/indirect instruction
  518. configuration.ip() = configuration.frame().expression().instructions().size();
  519. return;
  520. }
  521. case Instructions::br.value():
  522. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  523. case Instructions::br_if.value(): {
  524. auto entry = configuration.stack().pop();
  525. if (entry.get<Value>().to<i32>().value_or(0) == 0)
  526. return;
  527. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  528. }
  529. case Instructions::br_table.value(): {
  530. auto& arguments = instruction.arguments().get<Instruction::TableBranchArgs>();
  531. auto entry = configuration.stack().pop();
  532. auto maybe_i = entry.get<Value>().to<i32>();
  533. if (0 <= *maybe_i) {
  534. size_t i = *maybe_i;
  535. if (i < arguments.labels.size())
  536. return branch_to_label(configuration, arguments.labels[i]);
  537. }
  538. return branch_to_label(configuration, arguments.default_);
  539. }
  540. case Instructions::call.value(): {
  541. auto index = instruction.arguments().get<FunctionIndex>();
  542. auto address = configuration.frame().module().functions()[index.value()];
  543. dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value());
  544. call_address(configuration, address);
  545. return;
  546. }
  547. case Instructions::call_indirect.value(): {
  548. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  549. auto table_address = configuration.frame().module().tables()[args.table.value()];
  550. auto table_instance = configuration.store().get(table_address);
  551. auto entry = configuration.stack().pop();
  552. auto index = entry.get<Value>().to<i32>();
  553. TRAP_IF_NOT(index.value() >= 0);
  554. TRAP_IF_NOT(static_cast<size_t>(index.value()) < table_instance->elements().size());
  555. auto element = table_instance->elements()[index.value()];
  556. TRAP_IF_NOT(element.ref().has<Reference::Func>());
  557. auto address = element.ref().get<Reference::Func>().address;
  558. dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index.value(), address.value());
  559. call_address(configuration, address);
  560. return;
  561. }
  562. case Instructions::i32_load.value():
  563. return load_and_push<i32, i32>(configuration, instruction);
  564. case Instructions::i64_load.value():
  565. return load_and_push<i64, i64>(configuration, instruction);
  566. case Instructions::f32_load.value():
  567. return load_and_push<float, float>(configuration, instruction);
  568. case Instructions::f64_load.value():
  569. return load_and_push<double, double>(configuration, instruction);
  570. case Instructions::i32_load8_s.value():
  571. return load_and_push<i8, i32>(configuration, instruction);
  572. case Instructions::i32_load8_u.value():
  573. return load_and_push<u8, i32>(configuration, instruction);
  574. case Instructions::i32_load16_s.value():
  575. return load_and_push<i16, i32>(configuration, instruction);
  576. case Instructions::i32_load16_u.value():
  577. return load_and_push<u16, i32>(configuration, instruction);
  578. case Instructions::i64_load8_s.value():
  579. return load_and_push<i8, i64>(configuration, instruction);
  580. case Instructions::i64_load8_u.value():
  581. return load_and_push<u8, i64>(configuration, instruction);
  582. case Instructions::i64_load16_s.value():
  583. return load_and_push<i16, i64>(configuration, instruction);
  584. case Instructions::i64_load16_u.value():
  585. return load_and_push<u16, i64>(configuration, instruction);
  586. case Instructions::i64_load32_s.value():
  587. return load_and_push<i32, i64>(configuration, instruction);
  588. case Instructions::i64_load32_u.value():
  589. return load_and_push<u32, i64>(configuration, instruction);
  590. case Instructions::i32_store.value():
  591. return pop_and_store<i32, i32>(configuration, instruction);
  592. case Instructions::i64_store.value():
  593. return pop_and_store<i64, i64>(configuration, instruction);
  594. case Instructions::f32_store.value():
  595. return pop_and_store<float, float>(configuration, instruction);
  596. case Instructions::f64_store.value():
  597. return pop_and_store<double, double>(configuration, instruction);
  598. case Instructions::i32_store8.value():
  599. return pop_and_store<i32, i8>(configuration, instruction);
  600. case Instructions::i32_store16.value():
  601. return pop_and_store<i32, i16>(configuration, instruction);
  602. case Instructions::i64_store8.value():
  603. return pop_and_store<i64, i8>(configuration, instruction);
  604. case Instructions::i64_store16.value():
  605. return pop_and_store<i64, i16>(configuration, instruction);
  606. case Instructions::i64_store32.value():
  607. return pop_and_store<i64, i32>(configuration, instruction);
  608. case Instructions::local_tee.value(): {
  609. auto& entry = configuration.stack().peek();
  610. auto value = entry.get<Value>();
  611. auto local_index = instruction.arguments().get<LocalIndex>();
  612. dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
  613. configuration.frame().locals()[local_index.value()] = move(value);
  614. return;
  615. }
  616. case Instructions::global_get.value(): {
  617. auto global_index = instruction.arguments().get<GlobalIndex>();
  618. // This check here is for const expressions. In non-const expressions,
  619. // a validation error would have been thrown.
  620. TRAP_IF_NOT(global_index < configuration.frame().module().globals().size());
  621. auto address = configuration.frame().module().globals()[global_index.value()];
  622. dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
  623. auto global = configuration.store().get(address);
  624. configuration.stack().push(Value(global->value()));
  625. return;
  626. }
  627. case Instructions::global_set.value(): {
  628. auto global_index = instruction.arguments().get<GlobalIndex>();
  629. auto address = configuration.frame().module().globals()[global_index.value()];
  630. auto entry = configuration.stack().pop();
  631. auto value = entry.get<Value>();
  632. dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
  633. auto global = configuration.store().get(address);
  634. global->set_value(move(value));
  635. return;
  636. }
  637. case Instructions::memory_size.value(): {
  638. auto& args = instruction.arguments().get<Instruction::MemoryIndexArgument>();
  639. auto address = configuration.frame().module().memories()[args.memory_index.value()];
  640. auto instance = configuration.store().get(address);
  641. auto pages = instance->size() / Constants::page_size;
  642. dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
  643. configuration.stack().push(Value((i32)pages));
  644. return;
  645. }
  646. case Instructions::memory_grow.value(): {
  647. auto& args = instruction.arguments().get<Instruction::MemoryIndexArgument>();
  648. auto address = configuration.frame().module().memories()[args.memory_index.value()];
  649. auto instance = configuration.store().get(address);
  650. i32 old_pages = instance->size() / Constants::page_size;
  651. auto& entry = configuration.stack().peek();
  652. auto new_pages = entry.get<Value>().to<i32>();
  653. dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
  654. if (instance->grow(new_pages.value() * Constants::page_size))
  655. configuration.stack().peek() = Value((i32)old_pages);
  656. else
  657. configuration.stack().peek() = Value((i32)-1);
  658. return;
  659. }
  660. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-fill
  661. case Instructions::memory_fill.value(): {
  662. auto& args = instruction.arguments().get<Instruction::MemoryIndexArgument>();
  663. auto address = configuration.frame().module().memories()[args.memory_index.value()];
  664. auto instance = configuration.store().get(address);
  665. auto count = configuration.stack().pop().get<Value>().to<u32>().value();
  666. u8 value = static_cast<u8>(configuration.stack().pop().get<Value>().to<u32>().value());
  667. auto destination_offset = configuration.stack().pop().get<Value>().to<u32>().value();
  668. TRAP_IF_NOT(static_cast<size_t>(destination_offset + count) <= instance->data().size());
  669. if (count == 0)
  670. return;
  671. for (u32 i = 0; i < count; ++i)
  672. store_to_memory(configuration, Instruction::MemoryArgument { 0, 0 }, { &value, sizeof(value) }, destination_offset + i);
  673. return;
  674. }
  675. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-copy
  676. case Instructions::memory_copy.value(): {
  677. auto& args = instruction.arguments().get<Instruction::MemoryCopyArgs>();
  678. auto source_address = configuration.frame().module().memories()[args.src_index.value()];
  679. auto destination_address = configuration.frame().module().memories()[args.dst_index.value()];
  680. auto source_instance = configuration.store().get(source_address);
  681. auto destination_instance = configuration.store().get(destination_address);
  682. auto count = configuration.stack().pop().get<Value>().to<i32>().value();
  683. auto source_offset = configuration.stack().pop().get<Value>().to<i32>().value();
  684. auto destination_offset = configuration.stack().pop().get<Value>().to<i32>().value();
  685. Checked<size_t> source_position = source_offset;
  686. source_position.saturating_add(count);
  687. Checked<size_t> destination_position = destination_offset;
  688. destination_position.saturating_add(count);
  689. TRAP_IF_NOT(source_position <= source_instance->data().size());
  690. TRAP_IF_NOT(destination_position <= destination_instance->data().size());
  691. if (count == 0)
  692. return;
  693. Instruction::MemoryArgument memarg { 0, 0, args.dst_index };
  694. if (destination_offset <= source_offset) {
  695. for (auto i = 0; i < count; ++i) {
  696. auto value = source_instance->data()[source_offset + i];
  697. store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i);
  698. }
  699. } else {
  700. for (auto i = count - 1; i >= 0; --i) {
  701. auto value = source_instance->data()[source_offset + i];
  702. store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i);
  703. }
  704. }
  705. return;
  706. }
  707. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-init
  708. case Instructions::memory_init.value(): {
  709. auto& args = instruction.arguments().get<Instruction::MemoryInitArgs>();
  710. auto& data_address = configuration.frame().module().datas()[args.data_index.value()];
  711. auto& data = *configuration.store().get(data_address);
  712. auto memory_address = configuration.frame().module().memories()[args.memory_index.value()];
  713. auto memory = configuration.store().get(memory_address);
  714. auto count = *configuration.stack().pop().get<Value>().to<u32>();
  715. auto source_offset = *configuration.stack().pop().get<Value>().to<u32>();
  716. auto destination_offset = *configuration.stack().pop().get<Value>().to<u32>();
  717. Checked<size_t> source_position = source_offset;
  718. source_position.saturating_add(count);
  719. Checked<size_t> destination_position = destination_offset;
  720. destination_position.saturating_add(count);
  721. TRAP_IF_NOT(source_position <= data.data().size());
  722. TRAP_IF_NOT(destination_position <= memory->data().size());
  723. if (count == 0)
  724. return;
  725. Instruction::MemoryArgument memarg { 0, 0, args.memory_index };
  726. for (size_t i = 0; i < (size_t)count; ++i) {
  727. auto value = data.data()[source_offset + i];
  728. store_to_memory(configuration, memarg, { &value, sizeof(value) }, destination_offset + i);
  729. }
  730. return;
  731. }
  732. // https://webassembly.github.io/spec/core/bikeshed/#exec-data-drop
  733. case Instructions::data_drop.value(): {
  734. auto data_index = instruction.arguments().get<DataIndex>();
  735. auto data_address = configuration.frame().module().datas()[data_index.value()];
  736. *configuration.store().get(data_address) = DataInstance({});
  737. return;
  738. }
  739. case Instructions::elem_drop.value(): {
  740. auto elem_index = instruction.arguments().get<ElementIndex>();
  741. auto address = configuration.frame().module().elements()[elem_index.value()];
  742. auto elem = configuration.store().get(address);
  743. *configuration.store().get(address) = ElementInstance(elem->type(), {});
  744. return;
  745. }
  746. case Instructions::table_init.value(): {
  747. auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
  748. auto table_address = configuration.frame().module().tables()[args.table_index.value()];
  749. auto table = configuration.store().get(table_address);
  750. auto element_address = configuration.frame().module().elements()[args.element_index.value()];
  751. auto element = configuration.store().get(element_address);
  752. auto count = *configuration.stack().pop().get<Value>().to<u32>();
  753. auto source_offset = *configuration.stack().pop().get<Value>().to<u32>();
  754. auto destination_offset = *configuration.stack().pop().get<Value>().to<u32>();
  755. Checked<u32> checked_source_offset = source_offset;
  756. Checked<u32> checked_destination_offset = destination_offset;
  757. checked_source_offset += count;
  758. checked_destination_offset += count;
  759. TRAP_IF_NOT(!checked_source_offset.has_overflow() && checked_source_offset <= (u32)element->references().size());
  760. TRAP_IF_NOT(!checked_destination_offset.has_overflow() && checked_destination_offset <= (u32)table->elements().size());
  761. for (u32 i = 0; i < count; ++i)
  762. table->elements()[destination_offset + i] = element->references()[source_offset + i];
  763. return;
  764. }
  765. case Instructions::table_copy.value(): {
  766. auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
  767. auto source_address = configuration.frame().module().tables()[args.rhs.value()];
  768. auto destination_address = configuration.frame().module().tables()[args.lhs.value()];
  769. auto source_instance = configuration.store().get(source_address);
  770. auto destination_instance = configuration.store().get(destination_address);
  771. auto count = configuration.stack().pop().get<Value>().to<u32>().value();
  772. auto source_offset = configuration.stack().pop().get<Value>().to<u32>().value();
  773. auto destination_offset = configuration.stack().pop().get<Value>().to<u32>().value();
  774. Checked<size_t> source_position = source_offset;
  775. source_position.saturating_add(count);
  776. Checked<size_t> destination_position = destination_offset;
  777. destination_position.saturating_add(count);
  778. TRAP_IF_NOT(source_position <= source_instance->elements().size());
  779. TRAP_IF_NOT(destination_position <= destination_instance->elements().size());
  780. if (count == 0)
  781. return;
  782. if (destination_offset <= source_offset) {
  783. for (u32 i = 0; i < count; ++i) {
  784. auto value = source_instance->elements()[source_offset + i];
  785. destination_instance->elements()[destination_offset + i] = value;
  786. }
  787. } else {
  788. for (u32 i = count - 1; i != NumericLimits<u32>::max(); --i) {
  789. auto value = source_instance->elements()[source_offset + i];
  790. destination_instance->elements()[destination_offset + i] = value;
  791. }
  792. }
  793. return;
  794. }
  795. case Instructions::table_fill.value(): {
  796. auto table_index = instruction.arguments().get<TableIndex>();
  797. auto address = configuration.frame().module().tables()[table_index.value()];
  798. auto table = configuration.store().get(address);
  799. auto count = *configuration.stack().pop().get<Value>().to<u32>();
  800. auto value = *configuration.stack().pop().get<Value>().to<Reference>();
  801. auto start = *configuration.stack().pop().get<Value>().to<u32>();
  802. Checked<u32> checked_offset = start;
  803. checked_offset += count;
  804. TRAP_IF_NOT(!checked_offset.has_overflow() && checked_offset <= (u32)table->elements().size());
  805. for (u32 i = 0; i < count; ++i)
  806. table->elements()[start + i] = value;
  807. return;
  808. }
  809. case Instructions::table_set.value(): {
  810. auto ref = *configuration.stack().pop().get<Value>().to<Reference>();
  811. auto index = (size_t)(*configuration.stack().pop().get<Value>().to<i32>());
  812. auto table_index = instruction.arguments().get<TableIndex>();
  813. auto address = configuration.frame().module().tables()[table_index.value()];
  814. auto table = configuration.store().get(address);
  815. TRAP_IF_NOT(index < table->elements().size());
  816. table->elements()[index] = ref;
  817. return;
  818. }
  819. case Instructions::table_get.value(): {
  820. auto index = (size_t)(*configuration.stack().pop().get<Value>().to<i32>());
  821. auto table_index = instruction.arguments().get<TableIndex>();
  822. auto address = configuration.frame().module().tables()[table_index.value()];
  823. auto table = configuration.store().get(address);
  824. TRAP_IF_NOT(index < table->elements().size());
  825. auto ref = table->elements()[index];
  826. configuration.stack().push(Value(ref));
  827. return;
  828. }
  829. case Instructions::table_grow.value(): {
  830. auto size = *configuration.stack().pop().get<Value>().to<u32>();
  831. auto fill_value = *configuration.stack().pop().get<Value>().to<Reference>();
  832. auto table_index = instruction.arguments().get<TableIndex>();
  833. auto address = configuration.frame().module().tables()[table_index.value()];
  834. auto table = configuration.store().get(address);
  835. auto previous_size = table->elements().size();
  836. auto did_grow = table->grow(size, fill_value);
  837. if (!did_grow) {
  838. configuration.stack().push(Value((i32)-1));
  839. } else {
  840. configuration.stack().push(Value((i32)previous_size));
  841. }
  842. return;
  843. }
  844. case Instructions::table_size.value(): {
  845. auto table_index = instruction.arguments().get<TableIndex>();
  846. auto address = configuration.frame().module().tables()[table_index.value()];
  847. auto table = configuration.store().get(address);
  848. configuration.stack().push(Value((i32)table->elements().size()));
  849. return;
  850. }
  851. case Instructions::ref_null.value(): {
  852. auto type = instruction.arguments().get<ValueType>();
  853. configuration.stack().push(Value(Reference(Reference::Null { type })));
  854. return;
  855. };
  856. case Instructions::ref_func.value(): {
  857. auto index = instruction.arguments().get<FunctionIndex>().value();
  858. auto& functions = configuration.frame().module().functions();
  859. auto address = functions[index];
  860. configuration.stack().push(Value(ValueType(ValueType::FunctionReference), address.value()));
  861. return;
  862. }
  863. case Instructions::ref_is_null.value(): {
  864. auto top = configuration.stack().peek().get_pointer<Value>();
  865. TRAP_IF_NOT(top->type().is_reference());
  866. auto is_null = top->to<Reference::Null>().has_value();
  867. configuration.stack().peek() = Value(ValueType(ValueType::I32), static_cast<u64>(is_null ? 1 : 0));
  868. return;
  869. }
  870. case Instructions::drop.value():
  871. configuration.stack().pop();
  872. return;
  873. case Instructions::select.value():
  874. case Instructions::select_typed.value(): {
  875. // Note: The type seems to only be used for validation.
  876. auto entry = configuration.stack().pop();
  877. auto value = entry.get<Value>().to<i32>();
  878. dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
  879. auto rhs_entry = configuration.stack().pop();
  880. auto& lhs_entry = configuration.stack().peek();
  881. auto rhs = move(rhs_entry.get<Value>());
  882. auto lhs = move(lhs_entry.get<Value>());
  883. configuration.stack().peek() = value.value() != 0 ? move(lhs) : move(rhs);
  884. return;
  885. }
  886. case Instructions::i32_eqz.value():
  887. return unary_operation<i32, i32, Operators::EqualsZero>(configuration);
  888. case Instructions::i32_eq.value():
  889. return binary_numeric_operation<i32, i32, Operators::Equals>(configuration);
  890. case Instructions::i32_ne.value():
  891. return binary_numeric_operation<i32, i32, Operators::NotEquals>(configuration);
  892. case Instructions::i32_lts.value():
  893. return binary_numeric_operation<i32, i32, Operators::LessThan>(configuration);
  894. case Instructions::i32_ltu.value():
  895. return binary_numeric_operation<u32, i32, Operators::LessThan>(configuration);
  896. case Instructions::i32_gts.value():
  897. return binary_numeric_operation<i32, i32, Operators::GreaterThan>(configuration);
  898. case Instructions::i32_gtu.value():
  899. return binary_numeric_operation<u32, i32, Operators::GreaterThan>(configuration);
  900. case Instructions::i32_les.value():
  901. return binary_numeric_operation<i32, i32, Operators::LessThanOrEquals>(configuration);
  902. case Instructions::i32_leu.value():
  903. return binary_numeric_operation<u32, i32, Operators::LessThanOrEquals>(configuration);
  904. case Instructions::i32_ges.value():
  905. return binary_numeric_operation<i32, i32, Operators::GreaterThanOrEquals>(configuration);
  906. case Instructions::i32_geu.value():
  907. return binary_numeric_operation<u32, i32, Operators::GreaterThanOrEquals>(configuration);
  908. case Instructions::i64_eqz.value():
  909. return unary_operation<i64, i32, Operators::EqualsZero>(configuration);
  910. case Instructions::i64_eq.value():
  911. return binary_numeric_operation<i64, i32, Operators::Equals>(configuration);
  912. case Instructions::i64_ne.value():
  913. return binary_numeric_operation<i64, i32, Operators::NotEquals>(configuration);
  914. case Instructions::i64_lts.value():
  915. return binary_numeric_operation<i64, i32, Operators::LessThan>(configuration);
  916. case Instructions::i64_ltu.value():
  917. return binary_numeric_operation<u64, i32, Operators::LessThan>(configuration);
  918. case Instructions::i64_gts.value():
  919. return binary_numeric_operation<i64, i32, Operators::GreaterThan>(configuration);
  920. case Instructions::i64_gtu.value():
  921. return binary_numeric_operation<u64, i32, Operators::GreaterThan>(configuration);
  922. case Instructions::i64_les.value():
  923. return binary_numeric_operation<i64, i32, Operators::LessThanOrEquals>(configuration);
  924. case Instructions::i64_leu.value():
  925. return binary_numeric_operation<u64, i32, Operators::LessThanOrEquals>(configuration);
  926. case Instructions::i64_ges.value():
  927. return binary_numeric_operation<i64, i32, Operators::GreaterThanOrEquals>(configuration);
  928. case Instructions::i64_geu.value():
  929. return binary_numeric_operation<u64, i32, Operators::GreaterThanOrEquals>(configuration);
  930. case Instructions::f32_eq.value():
  931. return binary_numeric_operation<float, i32, Operators::Equals>(configuration);
  932. case Instructions::f32_ne.value():
  933. return binary_numeric_operation<float, i32, Operators::NotEquals>(configuration);
  934. case Instructions::f32_lt.value():
  935. return binary_numeric_operation<float, i32, Operators::LessThan>(configuration);
  936. case Instructions::f32_gt.value():
  937. return binary_numeric_operation<float, i32, Operators::GreaterThan>(configuration);
  938. case Instructions::f32_le.value():
  939. return binary_numeric_operation<float, i32, Operators::LessThanOrEquals>(configuration);
  940. case Instructions::f32_ge.value():
  941. return binary_numeric_operation<float, i32, Operators::GreaterThanOrEquals>(configuration);
  942. case Instructions::f64_eq.value():
  943. return binary_numeric_operation<double, i32, Operators::Equals>(configuration);
  944. case Instructions::f64_ne.value():
  945. return binary_numeric_operation<double, i32, Operators::NotEquals>(configuration);
  946. case Instructions::f64_lt.value():
  947. return binary_numeric_operation<double, i32, Operators::LessThan>(configuration);
  948. case Instructions::f64_gt.value():
  949. return binary_numeric_operation<double, i32, Operators::GreaterThan>(configuration);
  950. case Instructions::f64_le.value():
  951. return binary_numeric_operation<double, i32, Operators::LessThanOrEquals>(configuration);
  952. case Instructions::f64_ge.value():
  953. return binary_numeric_operation<double, i32, Operators::GreaterThanOrEquals>(configuration);
  954. case Instructions::i32_clz.value():
  955. return unary_operation<i32, i32, Operators::CountLeadingZeros>(configuration);
  956. case Instructions::i32_ctz.value():
  957. return unary_operation<i32, i32, Operators::CountTrailingZeros>(configuration);
  958. case Instructions::i32_popcnt.value():
  959. return unary_operation<i32, i32, Operators::PopCount>(configuration);
  960. case Instructions::i32_add.value():
  961. return binary_numeric_operation<u32, i32, Operators::Add>(configuration);
  962. case Instructions::i32_sub.value():
  963. return binary_numeric_operation<u32, i32, Operators::Subtract>(configuration);
  964. case Instructions::i32_mul.value():
  965. return binary_numeric_operation<u32, i32, Operators::Multiply>(configuration);
  966. case Instructions::i32_divs.value():
  967. return binary_numeric_operation<i32, i32, Operators::Divide>(configuration);
  968. case Instructions::i32_divu.value():
  969. return binary_numeric_operation<u32, i32, Operators::Divide>(configuration);
  970. case Instructions::i32_rems.value():
  971. return binary_numeric_operation<i32, i32, Operators::Modulo>(configuration);
  972. case Instructions::i32_remu.value():
  973. return binary_numeric_operation<u32, i32, Operators::Modulo>(configuration);
  974. case Instructions::i32_and.value():
  975. return binary_numeric_operation<i32, i32, Operators::BitAnd>(configuration);
  976. case Instructions::i32_or.value():
  977. return binary_numeric_operation<i32, i32, Operators::BitOr>(configuration);
  978. case Instructions::i32_xor.value():
  979. return binary_numeric_operation<i32, i32, Operators::BitXor>(configuration);
  980. case Instructions::i32_shl.value():
  981. return binary_numeric_operation<u32, i32, Operators::BitShiftLeft>(configuration);
  982. case Instructions::i32_shrs.value():
  983. return binary_numeric_operation<i32, i32, Operators::BitShiftRight>(configuration);
  984. case Instructions::i32_shru.value():
  985. return binary_numeric_operation<u32, i32, Operators::BitShiftRight>(configuration);
  986. case Instructions::i32_rotl.value():
  987. return binary_numeric_operation<u32, i32, Operators::BitRotateLeft>(configuration);
  988. case Instructions::i32_rotr.value():
  989. return binary_numeric_operation<u32, i32, Operators::BitRotateRight>(configuration);
  990. case Instructions::i64_clz.value():
  991. return unary_operation<i64, i64, Operators::CountLeadingZeros>(configuration);
  992. case Instructions::i64_ctz.value():
  993. return unary_operation<i64, i64, Operators::CountTrailingZeros>(configuration);
  994. case Instructions::i64_popcnt.value():
  995. return unary_operation<i64, i64, Operators::PopCount>(configuration);
  996. case Instructions::i64_add.value():
  997. return binary_numeric_operation<u64, i64, Operators::Add>(configuration);
  998. case Instructions::i64_sub.value():
  999. return binary_numeric_operation<u64, i64, Operators::Subtract>(configuration);
  1000. case Instructions::i64_mul.value():
  1001. return binary_numeric_operation<u64, i64, Operators::Multiply>(configuration);
  1002. case Instructions::i64_divs.value():
  1003. return binary_numeric_operation<i64, i64, Operators::Divide>(configuration);
  1004. case Instructions::i64_divu.value():
  1005. return binary_numeric_operation<u64, i64, Operators::Divide>(configuration);
  1006. case Instructions::i64_rems.value():
  1007. return binary_numeric_operation<i64, i64, Operators::Modulo>(configuration);
  1008. case Instructions::i64_remu.value():
  1009. return binary_numeric_operation<u64, i64, Operators::Modulo>(configuration);
  1010. case Instructions::i64_and.value():
  1011. return binary_numeric_operation<i64, i64, Operators::BitAnd>(configuration);
  1012. case Instructions::i64_or.value():
  1013. return binary_numeric_operation<i64, i64, Operators::BitOr>(configuration);
  1014. case Instructions::i64_xor.value():
  1015. return binary_numeric_operation<i64, i64, Operators::BitXor>(configuration);
  1016. case Instructions::i64_shl.value():
  1017. return binary_numeric_operation<u64, i64, Operators::BitShiftLeft>(configuration);
  1018. case Instructions::i64_shrs.value():
  1019. return binary_numeric_operation<i64, i64, Operators::BitShiftRight>(configuration);
  1020. case Instructions::i64_shru.value():
  1021. return binary_numeric_operation<u64, i64, Operators::BitShiftRight>(configuration);
  1022. case Instructions::i64_rotl.value():
  1023. return binary_numeric_operation<u64, i64, Operators::BitRotateLeft>(configuration);
  1024. case Instructions::i64_rotr.value():
  1025. return binary_numeric_operation<u64, i64, Operators::BitRotateRight>(configuration);
  1026. case Instructions::f32_abs.value():
  1027. return unary_operation<float, float, Operators::Absolute>(configuration);
  1028. case Instructions::f32_neg.value():
  1029. return unary_operation<float, float, Operators::Negate>(configuration);
  1030. case Instructions::f32_ceil.value():
  1031. return unary_operation<float, float, Operators::Ceil>(configuration);
  1032. case Instructions::f32_floor.value():
  1033. return unary_operation<float, float, Operators::Floor>(configuration);
  1034. case Instructions::f32_trunc.value():
  1035. return unary_operation<float, float, Operators::Truncate>(configuration);
  1036. case Instructions::f32_nearest.value():
  1037. return unary_operation<float, float, Operators::NearbyIntegral>(configuration);
  1038. case Instructions::f32_sqrt.value():
  1039. return unary_operation<float, float, Operators::SquareRoot>(configuration);
  1040. case Instructions::f32_add.value():
  1041. return binary_numeric_operation<float, float, Operators::Add>(configuration);
  1042. case Instructions::f32_sub.value():
  1043. return binary_numeric_operation<float, float, Operators::Subtract>(configuration);
  1044. case Instructions::f32_mul.value():
  1045. return binary_numeric_operation<float, float, Operators::Multiply>(configuration);
  1046. case Instructions::f32_div.value():
  1047. return binary_numeric_operation<float, float, Operators::Divide>(configuration);
  1048. case Instructions::f32_min.value():
  1049. return binary_numeric_operation<float, float, Operators::Minimum>(configuration);
  1050. case Instructions::f32_max.value():
  1051. return binary_numeric_operation<float, float, Operators::Maximum>(configuration);
  1052. case Instructions::f32_copysign.value():
  1053. return binary_numeric_operation<float, float, Operators::CopySign>(configuration);
  1054. case Instructions::f64_abs.value():
  1055. return unary_operation<double, double, Operators::Absolute>(configuration);
  1056. case Instructions::f64_neg.value():
  1057. return unary_operation<double, double, Operators::Negate>(configuration);
  1058. case Instructions::f64_ceil.value():
  1059. return unary_operation<double, double, Operators::Ceil>(configuration);
  1060. case Instructions::f64_floor.value():
  1061. return unary_operation<double, double, Operators::Floor>(configuration);
  1062. case Instructions::f64_trunc.value():
  1063. return unary_operation<double, double, Operators::Truncate>(configuration);
  1064. case Instructions::f64_nearest.value():
  1065. return unary_operation<double, double, Operators::NearbyIntegral>(configuration);
  1066. case Instructions::f64_sqrt.value():
  1067. return unary_operation<double, double, Operators::SquareRoot>(configuration);
  1068. case Instructions::f64_add.value():
  1069. return binary_numeric_operation<double, double, Operators::Add>(configuration);
  1070. case Instructions::f64_sub.value():
  1071. return binary_numeric_operation<double, double, Operators::Subtract>(configuration);
  1072. case Instructions::f64_mul.value():
  1073. return binary_numeric_operation<double, double, Operators::Multiply>(configuration);
  1074. case Instructions::f64_div.value():
  1075. return binary_numeric_operation<double, double, Operators::Divide>(configuration);
  1076. case Instructions::f64_min.value():
  1077. return binary_numeric_operation<double, double, Operators::Minimum>(configuration);
  1078. case Instructions::f64_max.value():
  1079. return binary_numeric_operation<double, double, Operators::Maximum>(configuration);
  1080. case Instructions::f64_copysign.value():
  1081. return binary_numeric_operation<double, double, Operators::CopySign>(configuration);
  1082. case Instructions::i32_wrap_i64.value():
  1083. return unary_operation<i64, i32, Operators::Wrap<i32>>(configuration);
  1084. case Instructions::i32_trunc_sf32.value():
  1085. return unary_operation<float, i32, Operators::CheckedTruncate<i32>>(configuration);
  1086. case Instructions::i32_trunc_uf32.value():
  1087. return unary_operation<float, i32, Operators::CheckedTruncate<u32>>(configuration);
  1088. case Instructions::i32_trunc_sf64.value():
  1089. return unary_operation<double, i32, Operators::CheckedTruncate<i32>>(configuration);
  1090. case Instructions::i32_trunc_uf64.value():
  1091. return unary_operation<double, i32, Operators::CheckedTruncate<u32>>(configuration);
  1092. case Instructions::i64_trunc_sf32.value():
  1093. return unary_operation<float, i64, Operators::CheckedTruncate<i64>>(configuration);
  1094. case Instructions::i64_trunc_uf32.value():
  1095. return unary_operation<float, i64, Operators::CheckedTruncate<u64>>(configuration);
  1096. case Instructions::i64_trunc_sf64.value():
  1097. return unary_operation<double, i64, Operators::CheckedTruncate<i64>>(configuration);
  1098. case Instructions::i64_trunc_uf64.value():
  1099. return unary_operation<double, i64, Operators::CheckedTruncate<u64>>(configuration);
  1100. case Instructions::i64_extend_si32.value():
  1101. return unary_operation<i32, i64, Operators::Extend<i64>>(configuration);
  1102. case Instructions::i64_extend_ui32.value():
  1103. return unary_operation<u32, i64, Operators::Extend<i64>>(configuration);
  1104. case Instructions::f32_convert_si32.value():
  1105. return unary_operation<i32, float, Operators::Convert<float>>(configuration);
  1106. case Instructions::f32_convert_ui32.value():
  1107. return unary_operation<u32, float, Operators::Convert<float>>(configuration);
  1108. case Instructions::f32_convert_si64.value():
  1109. return unary_operation<i64, float, Operators::Convert<float>>(configuration);
  1110. case Instructions::f32_convert_ui64.value():
  1111. return unary_operation<u64, float, Operators::Convert<float>>(configuration);
  1112. case Instructions::f32_demote_f64.value():
  1113. return unary_operation<double, float, Operators::Demote>(configuration);
  1114. case Instructions::f64_convert_si32.value():
  1115. return unary_operation<i32, double, Operators::Convert<double>>(configuration);
  1116. case Instructions::f64_convert_ui32.value():
  1117. return unary_operation<u32, double, Operators::Convert<double>>(configuration);
  1118. case Instructions::f64_convert_si64.value():
  1119. return unary_operation<i64, double, Operators::Convert<double>>(configuration);
  1120. case Instructions::f64_convert_ui64.value():
  1121. return unary_operation<u64, double, Operators::Convert<double>>(configuration);
  1122. case Instructions::f64_promote_f32.value():
  1123. return unary_operation<float, double, Operators::Promote>(configuration);
  1124. case Instructions::i32_reinterpret_f32.value():
  1125. return unary_operation<float, i32, Operators::Reinterpret<i32>>(configuration);
  1126. case Instructions::i64_reinterpret_f64.value():
  1127. return unary_operation<double, i64, Operators::Reinterpret<i64>>(configuration);
  1128. case Instructions::f32_reinterpret_i32.value():
  1129. return unary_operation<i32, float, Operators::Reinterpret<float>>(configuration);
  1130. case Instructions::f64_reinterpret_i64.value():
  1131. return unary_operation<i64, double, Operators::Reinterpret<double>>(configuration);
  1132. case Instructions::i32_extend8_s.value():
  1133. return unary_operation<i32, i32, Operators::SignExtend<i8>>(configuration);
  1134. case Instructions::i32_extend16_s.value():
  1135. return unary_operation<i32, i32, Operators::SignExtend<i16>>(configuration);
  1136. case Instructions::i64_extend8_s.value():
  1137. return unary_operation<i64, i64, Operators::SignExtend<i8>>(configuration);
  1138. case Instructions::i64_extend16_s.value():
  1139. return unary_operation<i64, i64, Operators::SignExtend<i16>>(configuration);
  1140. case Instructions::i64_extend32_s.value():
  1141. return unary_operation<i64, i64, Operators::SignExtend<i32>>(configuration);
  1142. case Instructions::i32_trunc_sat_f32_s.value():
  1143. return unary_operation<float, i32, Operators::SaturatingTruncate<i32>>(configuration);
  1144. case Instructions::i32_trunc_sat_f32_u.value():
  1145. return unary_operation<float, i32, Operators::SaturatingTruncate<u32>>(configuration);
  1146. case Instructions::i32_trunc_sat_f64_s.value():
  1147. return unary_operation<double, i32, Operators::SaturatingTruncate<i32>>(configuration);
  1148. case Instructions::i32_trunc_sat_f64_u.value():
  1149. return unary_operation<double, i32, Operators::SaturatingTruncate<u32>>(configuration);
  1150. case Instructions::i64_trunc_sat_f32_s.value():
  1151. return unary_operation<float, i64, Operators::SaturatingTruncate<i64>>(configuration);
  1152. case Instructions::i64_trunc_sat_f32_u.value():
  1153. return unary_operation<float, i64, Operators::SaturatingTruncate<u64>>(configuration);
  1154. case Instructions::i64_trunc_sat_f64_s.value():
  1155. return unary_operation<double, i64, Operators::SaturatingTruncate<i64>>(configuration);
  1156. case Instructions::i64_trunc_sat_f64_u.value():
  1157. return unary_operation<double, i64, Operators::SaturatingTruncate<u64>>(configuration);
  1158. case Instructions::v128_const.value():
  1159. configuration.stack().push(Value(instruction.arguments().get<u128>()));
  1160. return;
  1161. case Instructions::v128_load.value():
  1162. return load_and_push<u128, u128>(configuration, instruction);
  1163. case Instructions::v128_load8x8_s.value():
  1164. return load_and_push_mxn<8, 8, MakeSigned>(configuration, instruction);
  1165. case Instructions::v128_load8x8_u.value():
  1166. return load_and_push_mxn<8, 8, MakeUnsigned>(configuration, instruction);
  1167. case Instructions::v128_load16x4_s.value():
  1168. return load_and_push_mxn<16, 4, MakeSigned>(configuration, instruction);
  1169. case Instructions::v128_load16x4_u.value():
  1170. return load_and_push_mxn<16, 4, MakeUnsigned>(configuration, instruction);
  1171. case Instructions::v128_load32x2_s.value():
  1172. return load_and_push_mxn<32, 2, MakeSigned>(configuration, instruction);
  1173. case Instructions::v128_load32x2_u.value():
  1174. return load_and_push_mxn<32, 2, MakeUnsigned>(configuration, instruction);
  1175. case Instructions::v128_load8_splat.value():
  1176. return load_and_push_m_splat<8>(configuration, instruction);
  1177. case Instructions::v128_load16_splat.value():
  1178. return load_and_push_m_splat<16>(configuration, instruction);
  1179. case Instructions::v128_load32_splat.value():
  1180. return load_and_push_m_splat<32>(configuration, instruction);
  1181. case Instructions::v128_load64_splat.value():
  1182. return load_and_push_m_splat<64>(configuration, instruction);
  1183. case Instructions::i8x16_splat.value():
  1184. return pop_and_push_m_splat<8, NativeIntegralType>(configuration, instruction);
  1185. case Instructions::i16x8_splat.value():
  1186. return pop_and_push_m_splat<16, NativeIntegralType>(configuration, instruction);
  1187. case Instructions::i32x4_splat.value():
  1188. return pop_and_push_m_splat<32, NativeIntegralType>(configuration, instruction);
  1189. case Instructions::i64x2_splat.value():
  1190. return pop_and_push_m_splat<64, NativeIntegralType>(configuration, instruction);
  1191. case Instructions::f32x4_splat.value():
  1192. return pop_and_push_m_splat<32, NativeFloatingType>(configuration, instruction);
  1193. case Instructions::f64x2_splat.value():
  1194. return pop_and_push_m_splat<64, NativeFloatingType>(configuration, instruction);
  1195. case Instructions::i8x16_shuffle.value(): {
  1196. auto& arg = instruction.arguments().get<Instruction::ShuffleArgument>();
  1197. auto b = pop_vector<u8, MakeUnsigned>(configuration);
  1198. auto a = pop_vector<u8, MakeUnsigned>(configuration);
  1199. using VectorType = Native128ByteVectorOf<u8, MakeUnsigned>;
  1200. VectorType result;
  1201. for (size_t i = 0; i < 16; ++i)
  1202. if (arg.lanes[i] < 16)
  1203. result[i] = a[arg.lanes[i]];
  1204. else
  1205. result[i] = b[arg.lanes[i] - 16];
  1206. configuration.stack().push(Value(bit_cast<u128>(result)));
  1207. return;
  1208. }
  1209. case Instructions::v128_store.value():
  1210. return pop_and_store<u128, u128>(configuration, instruction);
  1211. case Instructions::i8x16_shl.value():
  1212. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<16>, i32>(configuration);
  1213. case Instructions::i8x16_shr_u.value():
  1214. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<16, MakeUnsigned>, i32>(configuration);
  1215. case Instructions::i8x16_shr_s.value():
  1216. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<16, MakeSigned>, i32>(configuration);
  1217. case Instructions::i16x8_shl.value():
  1218. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<8>, i32>(configuration);
  1219. case Instructions::i16x8_shr_u.value():
  1220. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<8, MakeUnsigned>, i32>(configuration);
  1221. case Instructions::i16x8_shr_s.value():
  1222. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<8, MakeSigned>, i32>(configuration);
  1223. case Instructions::i32x4_shl.value():
  1224. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<4>, i32>(configuration);
  1225. case Instructions::i32x4_shr_u.value():
  1226. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<4, MakeUnsigned>, i32>(configuration);
  1227. case Instructions::i32x4_shr_s.value():
  1228. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<4, MakeSigned>, i32>(configuration);
  1229. case Instructions::i64x2_shl.value():
  1230. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<2>, i32>(configuration);
  1231. case Instructions::i64x2_shr_u.value():
  1232. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<2, MakeUnsigned>, i32>(configuration);
  1233. case Instructions::i64x2_shr_s.value():
  1234. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<2, MakeSigned>, i32>(configuration);
  1235. case Instructions::i8x16_swizzle.value():
  1236. return binary_numeric_operation<u128, u128, Operators::VectorSwizzle>(configuration);
  1237. case Instructions::i8x16_extract_lane_s.value():
  1238. return unary_operation<u128, i8, Operators::VectorExtractLane<16, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1239. case Instructions::i8x16_extract_lane_u.value():
  1240. return unary_operation<u128, u8, Operators::VectorExtractLane<16, MakeUnsigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1241. case Instructions::i16x8_extract_lane_s.value():
  1242. return unary_operation<u128, i16, Operators::VectorExtractLane<8, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1243. case Instructions::i16x8_extract_lane_u.value():
  1244. return unary_operation<u128, u16, Operators::VectorExtractLane<8, MakeUnsigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1245. case Instructions::i32x4_extract_lane.value():
  1246. return unary_operation<u128, i32, Operators::VectorExtractLane<4, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1247. case Instructions::i64x2_extract_lane.value():
  1248. return unary_operation<u128, i64, Operators::VectorExtractLane<2, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1249. case Instructions::f32x4_extract_lane.value():
  1250. return unary_operation<u128, float, Operators::VectorExtractLaneFloat<4>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1251. case Instructions::f64x2_extract_lane.value():
  1252. return unary_operation<u128, double, Operators::VectorExtractLaneFloat<2>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1253. case Instructions::i8x16_replace_lane.value():
  1254. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<16, i32>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1255. case Instructions::i16x8_replace_lane.value():
  1256. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<8, i32>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1257. case Instructions::i32x4_replace_lane.value():
  1258. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<4>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1259. case Instructions::i64x2_replace_lane.value():
  1260. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<2>, i64>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1261. case Instructions::f32x4_replace_lane.value():
  1262. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<4, float>, float>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1263. case Instructions::f64x2_replace_lane.value():
  1264. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<2, double>, double>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1265. case Instructions::i8x16_eq.value():
  1266. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::Equals>>(configuration);
  1267. case Instructions::i8x16_ne.value():
  1268. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::NotEquals>>(configuration);
  1269. case Instructions::i8x16_lt_s.value():
  1270. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThan, MakeSigned>>(configuration);
  1271. case Instructions::i8x16_lt_u.value():
  1272. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThan, MakeUnsigned>>(configuration);
  1273. case Instructions::i8x16_gt_s.value():
  1274. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThan, MakeSigned>>(configuration);
  1275. case Instructions::i8x16_gt_u.value():
  1276. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1277. case Instructions::i8x16_le_s.value():
  1278. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1279. case Instructions::i8x16_le_u.value():
  1280. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1281. case Instructions::i8x16_ge_s.value():
  1282. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1283. case Instructions::i8x16_ge_u.value():
  1284. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1285. case Instructions::i8x16_abs.value():
  1286. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<16, Operators::Absolute>>(configuration);
  1287. case Instructions::i8x16_neg.value():
  1288. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<16, Operators::Negate>>(configuration);
  1289. case Instructions::i8x16_all_true.value():
  1290. return unary_operation<u128, i32, Operators::VectorAllTrue<16>>(configuration);
  1291. case Instructions::i8x16_popcnt.value():
  1292. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<16, Operators::PopCount>>(configuration);
  1293. case Instructions::i8x16_add.value():
  1294. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Add>>(configuration);
  1295. case Instructions::i8x16_sub.value():
  1296. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Subtract>>(configuration);
  1297. case Instructions::i8x16_avgr_u.value():
  1298. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Average, MakeUnsigned>>(configuration);
  1299. case Instructions::i8x16_add_sat_s.value():
  1300. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::SaturatingOp<i8, Operators::Add>, MakeSigned>>(configuration);
  1301. case Instructions::i8x16_add_sat_u.value():
  1302. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::SaturatingOp<u8, Operators::Add>, MakeUnsigned>>(configuration);
  1303. case Instructions::i8x16_sub_sat_s.value():
  1304. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::SaturatingOp<i8, Operators::Subtract>, MakeSigned>>(configuration);
  1305. case Instructions::i8x16_sub_sat_u.value():
  1306. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::SaturatingOp<u8, Operators::Subtract>, MakeUnsigned>>(configuration);
  1307. case Instructions::i8x16_min_s.value():
  1308. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Minimum, MakeSigned>>(configuration);
  1309. case Instructions::i8x16_min_u.value():
  1310. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Minimum, MakeUnsigned>>(configuration);
  1311. case Instructions::i8x16_max_s.value():
  1312. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Maximum, MakeSigned>>(configuration);
  1313. case Instructions::i8x16_max_u.value():
  1314. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<16, Operators::Maximum, MakeUnsigned>>(configuration);
  1315. case Instructions::i16x8_eq.value():
  1316. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::Equals>>(configuration);
  1317. case Instructions::i16x8_ne.value():
  1318. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::NotEquals>>(configuration);
  1319. case Instructions::i16x8_lt_s.value():
  1320. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThan, MakeSigned>>(configuration);
  1321. case Instructions::i16x8_lt_u.value():
  1322. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThan, MakeUnsigned>>(configuration);
  1323. case Instructions::i16x8_gt_s.value():
  1324. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThan, MakeSigned>>(configuration);
  1325. case Instructions::i16x8_gt_u.value():
  1326. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1327. case Instructions::i16x8_le_s.value():
  1328. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1329. case Instructions::i16x8_le_u.value():
  1330. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1331. case Instructions::i16x8_ge_s.value():
  1332. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1333. case Instructions::i16x8_ge_u.value():
  1334. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1335. case Instructions::i16x8_abs.value():
  1336. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<8, Operators::Absolute>>(configuration);
  1337. case Instructions::i16x8_neg.value():
  1338. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<8, Operators::Negate>>(configuration);
  1339. case Instructions::i16x8_all_true.value():
  1340. return unary_operation<u128, i32, Operators::VectorAllTrue<8>>(configuration);
  1341. case Instructions::i16x8_add.value():
  1342. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Add>>(configuration);
  1343. case Instructions::i16x8_sub.value():
  1344. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Subtract>>(configuration);
  1345. case Instructions::i16x8_mul.value():
  1346. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Multiply>>(configuration);
  1347. case Instructions::i16x8_avgr_u.value():
  1348. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Average, MakeUnsigned>>(configuration);
  1349. case Instructions::i16x8_add_sat_s.value():
  1350. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::SaturatingOp<i16, Operators::Add>, MakeSigned>>(configuration);
  1351. case Instructions::i16x8_add_sat_u.value():
  1352. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::SaturatingOp<u16, Operators::Add>, MakeUnsigned>>(configuration);
  1353. case Instructions::i16x8_sub_sat_s.value():
  1354. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::SaturatingOp<i16, Operators::Subtract>, MakeSigned>>(configuration);
  1355. case Instructions::i16x8_sub_sat_u.value():
  1356. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::SaturatingOp<u16, Operators::Subtract>, MakeUnsigned>>(configuration);
  1357. case Instructions::i16x8_min_s.value():
  1358. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Minimum, MakeSigned>>(configuration);
  1359. case Instructions::i16x8_min_u.value():
  1360. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Minimum, MakeUnsigned>>(configuration);
  1361. case Instructions::i16x8_max_s.value():
  1362. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Maximum, MakeSigned>>(configuration);
  1363. case Instructions::i16x8_max_u.value():
  1364. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::Maximum, MakeUnsigned>>(configuration);
  1365. case Instructions::i16x8_extend_low_i8x16_s.value():
  1366. return unary_operation<u128, u128, Operators::VectorIntegerExt<8, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1367. case Instructions::i16x8_extend_high_i8x16_s.value():
  1368. return unary_operation<u128, u128, Operators::VectorIntegerExt<8, Operators::VectorExt::High, MakeSigned>>(configuration);
  1369. case Instructions::i16x8_extend_low_i8x16_u.value():
  1370. return unary_operation<u128, u128, Operators::VectorIntegerExt<8, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1371. case Instructions::i16x8_extend_high_i8x16_u.value():
  1372. return unary_operation<u128, u128, Operators::VectorIntegerExt<8, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1373. case Instructions::i16x8_extadd_pairwise_i8x16_s.value():
  1374. return unary_operation<u128, u128, Operators::VectorIntegerExtOpPairwise<8, Operators::Add, MakeSigned>>(configuration);
  1375. case Instructions::i16x8_extadd_pairwise_i8x16_u.value():
  1376. return unary_operation<u128, u128, Operators::VectorIntegerExtOpPairwise<8, Operators::Add, MakeUnsigned>>(configuration);
  1377. case Instructions::i16x8_extmul_low_i8x16_s.value():
  1378. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<8, Operators::Multiply, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1379. case Instructions::i16x8_extmul_high_i8x16_s.value():
  1380. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<8, Operators::Multiply, Operators::VectorExt::High, MakeSigned>>(configuration);
  1381. case Instructions::i16x8_extmul_low_i8x16_u.value():
  1382. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<8, Operators::Multiply, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1383. case Instructions::i16x8_extmul_high_i8x16_u.value():
  1384. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<8, Operators::Multiply, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1385. case Instructions::i32x4_eq.value():
  1386. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::Equals>>(configuration);
  1387. case Instructions::i32x4_ne.value():
  1388. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::NotEquals>>(configuration);
  1389. case Instructions::i32x4_lt_s.value():
  1390. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThan, MakeSigned>>(configuration);
  1391. case Instructions::i32x4_lt_u.value():
  1392. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThan, MakeUnsigned>>(configuration);
  1393. case Instructions::i32x4_gt_s.value():
  1394. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThan, MakeSigned>>(configuration);
  1395. case Instructions::i32x4_gt_u.value():
  1396. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1397. case Instructions::i32x4_le_s.value():
  1398. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1399. case Instructions::i32x4_le_u.value():
  1400. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1401. case Instructions::i32x4_ge_s.value():
  1402. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1403. case Instructions::i32x4_ge_u.value():
  1404. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1405. case Instructions::i32x4_abs.value():
  1406. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<4, Operators::Absolute>>(configuration);
  1407. case Instructions::i32x4_neg.value():
  1408. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<4, Operators::Negate, MakeUnsigned>>(configuration);
  1409. case Instructions::i32x4_all_true.value():
  1410. return unary_operation<u128, i32, Operators::VectorAllTrue<4>>(configuration);
  1411. case Instructions::i32x4_add.value():
  1412. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Add, MakeUnsigned>>(configuration);
  1413. case Instructions::i32x4_sub.value():
  1414. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Subtract, MakeUnsigned>>(configuration);
  1415. case Instructions::i32x4_mul.value():
  1416. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Multiply, MakeUnsigned>>(configuration);
  1417. case Instructions::i32x4_min_s.value():
  1418. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Minimum, MakeSigned>>(configuration);
  1419. case Instructions::i32x4_min_u.value():
  1420. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Minimum, MakeUnsigned>>(configuration);
  1421. case Instructions::i32x4_max_s.value():
  1422. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Maximum, MakeSigned>>(configuration);
  1423. case Instructions::i32x4_max_u.value():
  1424. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<4, Operators::Maximum, MakeUnsigned>>(configuration);
  1425. case Instructions::i32x4_extend_low_i16x8_s.value():
  1426. return unary_operation<u128, u128, Operators::VectorIntegerExt<4, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1427. case Instructions::i32x4_extend_high_i16x8_s.value():
  1428. return unary_operation<u128, u128, Operators::VectorIntegerExt<4, Operators::VectorExt::High, MakeSigned>>(configuration);
  1429. case Instructions::i32x4_extend_low_i16x8_u.value():
  1430. return unary_operation<u128, u128, Operators::VectorIntegerExt<4, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1431. case Instructions::i32x4_extend_high_i16x8_u.value():
  1432. return unary_operation<u128, u128, Operators::VectorIntegerExt<4, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1433. case Instructions::i32x4_extadd_pairwise_i16x8_s.value():
  1434. return unary_operation<u128, u128, Operators::VectorIntegerExtOpPairwise<4, Operators::Add, MakeSigned>>(configuration);
  1435. case Instructions::i32x4_extadd_pairwise_i16x8_u.value():
  1436. return unary_operation<u128, u128, Operators::VectorIntegerExtOpPairwise<4, Operators::Add, MakeUnsigned>>(configuration);
  1437. case Instructions::i32x4_extmul_low_i16x8_s.value():
  1438. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<4, Operators::Multiply, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1439. case Instructions::i32x4_extmul_high_i16x8_s.value():
  1440. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<4, Operators::Multiply, Operators::VectorExt::High, MakeSigned>>(configuration);
  1441. case Instructions::i32x4_extmul_low_i16x8_u.value():
  1442. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<4, Operators::Multiply, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1443. case Instructions::i32x4_extmul_high_i16x8_u.value():
  1444. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<4, Operators::Multiply, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1445. case Instructions::i64x2_eq.value():
  1446. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::Equals>>(configuration);
  1447. case Instructions::i64x2_ne.value():
  1448. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::NotEquals>>(configuration);
  1449. case Instructions::i64x2_lt_s.value():
  1450. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::LessThan, MakeSigned>>(configuration);
  1451. case Instructions::i64x2_gt_s.value():
  1452. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::GreaterThan, MakeSigned>>(configuration);
  1453. case Instructions::i64x2_le_s.value():
  1454. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1455. case Instructions::i64x2_ge_s.value():
  1456. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<2, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1457. case Instructions::i64x2_abs.value():
  1458. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<2, Operators::Absolute>>(configuration);
  1459. case Instructions::i64x2_neg.value():
  1460. return unary_operation<u128, u128, Operators::VectorIntegerUnaryOp<2, Operators::Negate, MakeUnsigned>>(configuration);
  1461. case Instructions::i64x2_all_true.value():
  1462. return unary_operation<u128, i32, Operators::VectorAllTrue<2>>(configuration);
  1463. case Instructions::i64x2_add.value():
  1464. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<2, Operators::Add, MakeUnsigned>>(configuration);
  1465. case Instructions::i64x2_sub.value():
  1466. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<2, Operators::Subtract, MakeUnsigned>>(configuration);
  1467. case Instructions::i64x2_mul.value():
  1468. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<2, Operators::Multiply, MakeUnsigned>>(configuration);
  1469. case Instructions::i64x2_extend_low_i32x4_s.value():
  1470. return unary_operation<u128, u128, Operators::VectorIntegerExt<2, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1471. case Instructions::i64x2_extend_high_i32x4_s.value():
  1472. return unary_operation<u128, u128, Operators::VectorIntegerExt<2, Operators::VectorExt::High, MakeSigned>>(configuration);
  1473. case Instructions::i64x2_extend_low_i32x4_u.value():
  1474. return unary_operation<u128, u128, Operators::VectorIntegerExt<2, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1475. case Instructions::i64x2_extend_high_i32x4_u.value():
  1476. return unary_operation<u128, u128, Operators::VectorIntegerExt<2, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1477. case Instructions::i64x2_extmul_low_i32x4_s.value():
  1478. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<2, Operators::Multiply, Operators::VectorExt::Low, MakeSigned>>(configuration);
  1479. case Instructions::i64x2_extmul_high_i32x4_s.value():
  1480. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<2, Operators::Multiply, Operators::VectorExt::High, MakeSigned>>(configuration);
  1481. case Instructions::i64x2_extmul_low_i32x4_u.value():
  1482. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<2, Operators::Multiply, Operators::VectorExt::Low, MakeUnsigned>>(configuration);
  1483. case Instructions::i64x2_extmul_high_i32x4_u.value():
  1484. return binary_numeric_operation<u128, u128, Operators::VectorIntegerExtOp<2, Operators::Multiply, Operators::VectorExt::High, MakeUnsigned>>(configuration);
  1485. case Instructions::f32x4_eq.value():
  1486. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::Equals>>(configuration);
  1487. case Instructions::f32x4_ne.value():
  1488. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::NotEquals>>(configuration);
  1489. case Instructions::f32x4_lt.value():
  1490. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::LessThan>>(configuration);
  1491. case Instructions::f32x4_gt.value():
  1492. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::GreaterThan>>(configuration);
  1493. case Instructions::f32x4_le.value():
  1494. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::LessThanOrEquals>>(configuration);
  1495. case Instructions::f32x4_ge.value():
  1496. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::GreaterThanOrEquals>>(configuration);
  1497. case Instructions::f32x4_min.value():
  1498. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Minimum>>(configuration);
  1499. case Instructions::f32x4_max.value():
  1500. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Maximum>>(configuration);
  1501. case Instructions::f64x2_eq.value():
  1502. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::Equals>>(configuration);
  1503. case Instructions::f64x2_ne.value():
  1504. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::NotEquals>>(configuration);
  1505. case Instructions::f64x2_lt.value():
  1506. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::LessThan>>(configuration);
  1507. case Instructions::f64x2_gt.value():
  1508. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::GreaterThan>>(configuration);
  1509. case Instructions::f64x2_le.value():
  1510. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::LessThanOrEquals>>(configuration);
  1511. case Instructions::f64x2_ge.value():
  1512. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::GreaterThanOrEquals>>(configuration);
  1513. case Instructions::f64x2_min.value():
  1514. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Minimum>>(configuration);
  1515. case Instructions::f64x2_max.value():
  1516. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Maximum>>(configuration);
  1517. case Instructions::f32x4_div.value():
  1518. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Divide>>(configuration);
  1519. case Instructions::f32x4_mul.value():
  1520. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Multiply>>(configuration);
  1521. case Instructions::f32x4_sub.value():
  1522. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Subtract>>(configuration);
  1523. case Instructions::f32x4_add.value():
  1524. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::Add>>(configuration);
  1525. case Instructions::f32x4_pmin.value():
  1526. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::PseudoMinimum>>(configuration);
  1527. case Instructions::f32x4_pmax.value():
  1528. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<4, Operators::PseudoMaximum>>(configuration);
  1529. case Instructions::f64x2_div.value():
  1530. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Divide>>(configuration);
  1531. case Instructions::f64x2_mul.value():
  1532. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Multiply>>(configuration);
  1533. case Instructions::f64x2_sub.value():
  1534. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Subtract>>(configuration);
  1535. case Instructions::f64x2_add.value():
  1536. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::Add>>(configuration);
  1537. case Instructions::f64x2_pmin.value():
  1538. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::PseudoMinimum>>(configuration);
  1539. case Instructions::f64x2_pmax.value():
  1540. return binary_numeric_operation<u128, u128, Operators::VectorFloatBinaryOp<2, Operators::PseudoMaximum>>(configuration);
  1541. case Instructions::f32x4_ceil.value():
  1542. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::Ceil>>(configuration);
  1543. case Instructions::f32x4_floor.value():
  1544. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::Floor>>(configuration);
  1545. case Instructions::f32x4_trunc.value():
  1546. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::Truncate>>(configuration);
  1547. case Instructions::f32x4_nearest.value():
  1548. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::NearbyIntegral>>(configuration);
  1549. case Instructions::f32x4_sqrt.value():
  1550. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::SquareRoot>>(configuration);
  1551. case Instructions::f32x4_neg.value():
  1552. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::Negate>>(configuration);
  1553. case Instructions::f32x4_abs.value():
  1554. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<4, Operators::Absolute>>(configuration);
  1555. case Instructions::f64x2_ceil.value():
  1556. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::Ceil>>(configuration);
  1557. case Instructions::f64x2_floor.value():
  1558. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::Floor>>(configuration);
  1559. case Instructions::f64x2_trunc.value():
  1560. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::Truncate>>(configuration);
  1561. case Instructions::f64x2_nearest.value():
  1562. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::NearbyIntegral>>(configuration);
  1563. case Instructions::f64x2_sqrt.value():
  1564. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::SquareRoot>>(configuration);
  1565. case Instructions::f64x2_neg.value():
  1566. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::Negate>>(configuration);
  1567. case Instructions::f64x2_abs.value():
  1568. return unary_operation<u128, u128, Operators::VectorFloatUnaryOp<2, Operators::Absolute>>(configuration);
  1569. case Instructions::v128_and.value():
  1570. return binary_numeric_operation<u128, u128, Operators::BitAnd>(configuration);
  1571. case Instructions::v128_or.value():
  1572. return binary_numeric_operation<u128, u128, Operators::BitOr>(configuration);
  1573. case Instructions::v128_xor.value():
  1574. return binary_numeric_operation<u128, u128, Operators::BitXor>(configuration);
  1575. case Instructions::v128_not.value():
  1576. return unary_operation<u128, u128, Operators::BitNot>(configuration);
  1577. case Instructions::v128_andnot.value():
  1578. return binary_numeric_operation<u128, u128, Operators::BitAndNot>(configuration);
  1579. case Instructions::v128_bitselect.value(): {
  1580. auto mask = *configuration.stack().pop().get<Value>().to<u128>();
  1581. auto false_vector = *configuration.stack().pop().get<Value>().to<u128>();
  1582. auto true_vector = *configuration.stack().pop().get<Value>().to<u128>();
  1583. u128 result = (true_vector & mask) | (false_vector & ~mask);
  1584. configuration.stack().push(Value(result));
  1585. return;
  1586. }
  1587. case Instructions::v128_any_true.value(): {
  1588. auto vector = *configuration.stack().pop().get<Value>().to<u128>();
  1589. configuration.stack().push(Value(static_cast<i32>(vector != 0)));
  1590. return;
  1591. }
  1592. case Instructions::v128_load8_lane.value():
  1593. return load_and_push_lane_n<8>(configuration, instruction);
  1594. case Instructions::v128_load16_lane.value():
  1595. return load_and_push_lane_n<16>(configuration, instruction);
  1596. case Instructions::v128_load32_lane.value():
  1597. return load_and_push_lane_n<32>(configuration, instruction);
  1598. case Instructions::v128_load64_lane.value():
  1599. return load_and_push_lane_n<64>(configuration, instruction);
  1600. case Instructions::v128_load32_zero.value():
  1601. return load_and_push_zero_n<32>(configuration, instruction);
  1602. case Instructions::v128_load64_zero.value():
  1603. return load_and_push_zero_n<64>(configuration, instruction);
  1604. case Instructions::v128_store8_lane.value():
  1605. return pop_and_store_lane_n<8>(configuration, instruction);
  1606. case Instructions::v128_store16_lane.value():
  1607. return pop_and_store_lane_n<16>(configuration, instruction);
  1608. case Instructions::v128_store32_lane.value():
  1609. return pop_and_store_lane_n<32>(configuration, instruction);
  1610. case Instructions::v128_store64_lane.value():
  1611. return pop_and_store_lane_n<64>(configuration, instruction);
  1612. case Instructions::i32x4_trunc_sat_f32x4_s.value():
  1613. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 4, u32, f32, Operators::SaturatingTruncate<i32>>>(configuration);
  1614. case Instructions::i32x4_trunc_sat_f32x4_u.value():
  1615. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 4, u32, f32, Operators::SaturatingTruncate<u32>>>(configuration);
  1616. case Instructions::i8x16_bitmask.value():
  1617. return unary_operation<u128, i32, Operators::VectorBitmask<16>>(configuration);
  1618. case Instructions::i16x8_bitmask.value():
  1619. return unary_operation<u128, i32, Operators::VectorBitmask<8>>(configuration);
  1620. case Instructions::i32x4_bitmask.value():
  1621. return unary_operation<u128, i32, Operators::VectorBitmask<4>>(configuration);
  1622. case Instructions::i64x2_bitmask.value():
  1623. return unary_operation<u128, i32, Operators::VectorBitmask<2>>(configuration);
  1624. case Instructions::i32x4_dot_i16x8_s.value():
  1625. return binary_numeric_operation<u128, u128, Operators::VectorDotProduct<4>>(configuration);
  1626. case Instructions::i8x16_narrow_i16x8_s.value():
  1627. return binary_numeric_operation<u128, u128, Operators::VectorNarrow<16, i8>>(configuration);
  1628. case Instructions::i8x16_narrow_i16x8_u.value():
  1629. return binary_numeric_operation<u128, u128, Operators::VectorNarrow<16, u8>>(configuration);
  1630. case Instructions::i16x8_narrow_i32x4_s.value():
  1631. return binary_numeric_operation<u128, u128, Operators::VectorNarrow<8, i16>>(configuration);
  1632. case Instructions::i16x8_narrow_i32x4_u.value():
  1633. return binary_numeric_operation<u128, u128, Operators::VectorNarrow<8, u16>>(configuration);
  1634. case Instructions::i16x8_q15mulr_sat_s.value():
  1635. return binary_numeric_operation<u128, u128, Operators::VectorIntegerBinaryOp<8, Operators::SaturatingOp<i16, Operators::Q15Mul>, MakeSigned>>(configuration);
  1636. case Instructions::f32x4_convert_i32x4_s.value():
  1637. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 4, u32, i32, Operators::Convert<f32>>>(configuration);
  1638. case Instructions::f32x4_convert_i32x4_u.value():
  1639. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 4, u32, u32, Operators::Convert<f32>>>(configuration);
  1640. case Instructions::f64x2_convert_low_i32x4_s.value():
  1641. return unary_operation<u128, u128, Operators::VectorConvertOp<2, 4, u64, i32, Operators::Convert<f64>>>(configuration);
  1642. case Instructions::f64x2_convert_low_i32x4_u.value():
  1643. return unary_operation<u128, u128, Operators::VectorConvertOp<2, 4, u64, u32, Operators::Convert<f64>>>(configuration);
  1644. case Instructions::f32x4_demote_f64x2_zero.value():
  1645. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 2, u32, f64, Operators::Convert<f32>>>(configuration);
  1646. case Instructions::f64x2_promote_low_f32x4.value():
  1647. return unary_operation<u128, u128, Operators::VectorConvertOp<2, 4, u64, f32, Operators::Convert<f64>>>(configuration);
  1648. case Instructions::i32x4_trunc_sat_f64x2_s_zero.value():
  1649. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 2, u32, f64, Operators::SaturatingTruncate<i32>>>(configuration);
  1650. case Instructions::i32x4_trunc_sat_f64x2_u_zero.value():
  1651. return unary_operation<u128, u128, Operators::VectorConvertOp<4, 2, u32, f64, Operators::SaturatingTruncate<u32>>>(configuration);
  1652. }
  1653. }
  1654. void DebuggerBytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  1655. {
  1656. if (pre_interpret_hook) {
  1657. auto result = pre_interpret_hook(configuration, ip, instruction);
  1658. if (!result) {
  1659. m_trap = Trap { "Trapped by user request" };
  1660. return;
  1661. }
  1662. }
  1663. BytecodeInterpreter::interpret(configuration, ip, instruction);
  1664. if (post_interpret_hook) {
  1665. auto result = post_interpret_hook(configuration, ip, instruction, *this);
  1666. if (!result) {
  1667. m_trap = Trap { "Trapped by user request" };
  1668. return;
  1669. }
  1670. }
  1671. }
  1672. }