BytecodeInterpreter.cpp 84 KB

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