BytecodeInterpreter.cpp 89 KB

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