BytecodeInterpreter.cpp 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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)
  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, i32 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>(bit_cast<u32>(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.results().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<i32>().value();
  716. auto value = configuration.stack().pop().get<Value>().to<i32>().value();
  717. auto destination_offset = configuration.stack().pop().get<Value>().to<i32>().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 (auto i = 0; i < count; ++i) {
  726. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset);
  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<i32>();
  771. auto source_offset = *configuration.stack().pop().get<Value>().to<i32>();
  772. auto destination_offset = *configuration.stack().pop().get<Value>().to<i32>();
  773. TRAP_IF_NOT(count > 0);
  774. TRAP_IF_NOT(source_offset + count > 0);
  775. TRAP_IF_NOT(static_cast<size_t>(source_offset + count) <= data.size());
  776. Instruction synthetic_store_instruction {
  777. Instructions::i32_store8,
  778. Instruction::MemoryArgument { 0, 0, args.memory_index }
  779. };
  780. for (size_t i = 0; i < (size_t)count; ++i) {
  781. auto value = data.data()[source_offset + i];
  782. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset + i);
  783. }
  784. return;
  785. }
  786. // https://webassembly.github.io/spec/core/bikeshed/#exec-data-drop
  787. case Instructions::data_drop.value(): {
  788. auto data_index = instruction.arguments().get<DataIndex>();
  789. auto data_address = configuration.frame().module().datas()[data_index.value()];
  790. *configuration.store().get(data_address) = DataInstance({});
  791. return;
  792. }
  793. case Instructions::elem_drop.value(): {
  794. auto elem_index = instruction.arguments().get<ElementIndex>();
  795. auto address = configuration.frame().module().elements()[elem_index.value()];
  796. auto elem = configuration.store().get(address);
  797. *configuration.store().get(address) = ElementInstance(elem->type(), {});
  798. return;
  799. }
  800. case Instructions::table_set.value(): {
  801. auto ref = *configuration.stack().pop().get<Value>().to<Reference>();
  802. auto index = (size_t)(*configuration.stack().pop().get<Value>().to<i32>());
  803. auto table_index = instruction.arguments().get<TableIndex>();
  804. auto address = configuration.frame().module().tables()[table_index.value()];
  805. auto table = configuration.store().get(address);
  806. TRAP_IF_NOT(index < table->elements().size());
  807. table->elements()[index] = ref;
  808. return;
  809. }
  810. case Instructions::table_get.value(): {
  811. auto index = (size_t)(*configuration.stack().pop().get<Value>().to<i32>());
  812. auto table_index = instruction.arguments().get<TableIndex>();
  813. auto address = configuration.frame().module().tables()[table_index.value()];
  814. auto table = configuration.store().get(address);
  815. TRAP_IF_NOT(index < table->elements().size());
  816. auto ref = table->elements()[index];
  817. configuration.stack().push(Value(ref));
  818. return;
  819. }
  820. case Instructions::table_grow.value(): {
  821. auto size = *configuration.stack().pop().get<Value>().to<u32>();
  822. auto fill_value = *configuration.stack().pop().get<Value>().to<Reference>();
  823. auto table_index = instruction.arguments().get<TableIndex>();
  824. auto address = configuration.frame().module().tables()[table_index.value()];
  825. auto table = configuration.store().get(address);
  826. auto previous_size = table->elements().size();
  827. auto did_grow = table->grow(size, fill_value);
  828. if (!did_grow) {
  829. configuration.stack().push(Value((i32)-1));
  830. } else {
  831. configuration.stack().push(Value((i32)previous_size));
  832. }
  833. return;
  834. }
  835. case Instructions::table_size.value(): {
  836. auto table_index = instruction.arguments().get<TableIndex>();
  837. auto address = configuration.frame().module().tables()[table_index.value()];
  838. auto table = configuration.store().get(address);
  839. configuration.stack().push(Value((i32)table->elements().size()));
  840. return;
  841. }
  842. case Instructions::ref_null.value(): {
  843. auto type = instruction.arguments().get<ValueType>();
  844. configuration.stack().push(Value(Reference(Reference::Null { type })));
  845. return;
  846. };
  847. case Instructions::ref_func.value(): {
  848. auto index = instruction.arguments().get<FunctionIndex>().value();
  849. auto& functions = configuration.frame().module().functions();
  850. auto address = functions[index];
  851. configuration.stack().push(Value(ValueType(ValueType::FunctionReference), address.value()));
  852. return;
  853. }
  854. case Instructions::ref_is_null.value(): {
  855. auto top = configuration.stack().peek().get_pointer<Value>();
  856. TRAP_IF_NOT(top->type().is_reference());
  857. auto is_null = top->to<Reference::Null>().has_value();
  858. configuration.stack().peek() = Value(ValueType(ValueType::I32), static_cast<u64>(is_null ? 1 : 0));
  859. return;
  860. }
  861. case Instructions::drop.value():
  862. configuration.stack().pop();
  863. return;
  864. case Instructions::select.value():
  865. case Instructions::select_typed.value(): {
  866. // Note: The type seems to only be used for validation.
  867. auto entry = configuration.stack().pop();
  868. auto value = entry.get<Value>().to<i32>();
  869. dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
  870. auto rhs_entry = configuration.stack().pop();
  871. auto& lhs_entry = configuration.stack().peek();
  872. auto rhs = move(rhs_entry.get<Value>());
  873. auto lhs = move(lhs_entry.get<Value>());
  874. configuration.stack().peek() = value.value() != 0 ? move(lhs) : move(rhs);
  875. return;
  876. }
  877. case Instructions::i32_eqz.value():
  878. return unary_operation<i32, i32, Operators::EqualsZero>(configuration);
  879. case Instructions::i32_eq.value():
  880. return binary_numeric_operation<i32, i32, Operators::Equals>(configuration);
  881. case Instructions::i32_ne.value():
  882. return binary_numeric_operation<i32, i32, Operators::NotEquals>(configuration);
  883. case Instructions::i32_lts.value():
  884. return binary_numeric_operation<i32, i32, Operators::LessThan>(configuration);
  885. case Instructions::i32_ltu.value():
  886. return binary_numeric_operation<u32, i32, Operators::LessThan>(configuration);
  887. case Instructions::i32_gts.value():
  888. return binary_numeric_operation<i32, i32, Operators::GreaterThan>(configuration);
  889. case Instructions::i32_gtu.value():
  890. return binary_numeric_operation<u32, i32, Operators::GreaterThan>(configuration);
  891. case Instructions::i32_les.value():
  892. return binary_numeric_operation<i32, i32, Operators::LessThanOrEquals>(configuration);
  893. case Instructions::i32_leu.value():
  894. return binary_numeric_operation<u32, i32, Operators::LessThanOrEquals>(configuration);
  895. case Instructions::i32_ges.value():
  896. return binary_numeric_operation<i32, i32, Operators::GreaterThanOrEquals>(configuration);
  897. case Instructions::i32_geu.value():
  898. return binary_numeric_operation<u32, i32, Operators::GreaterThanOrEquals>(configuration);
  899. case Instructions::i64_eqz.value():
  900. return unary_operation<i64, i32, Operators::EqualsZero>(configuration);
  901. case Instructions::i64_eq.value():
  902. return binary_numeric_operation<i64, i32, Operators::Equals>(configuration);
  903. case Instructions::i64_ne.value():
  904. return binary_numeric_operation<i64, i32, Operators::NotEquals>(configuration);
  905. case Instructions::i64_lts.value():
  906. return binary_numeric_operation<i64, i32, Operators::LessThan>(configuration);
  907. case Instructions::i64_ltu.value():
  908. return binary_numeric_operation<u64, i32, Operators::LessThan>(configuration);
  909. case Instructions::i64_gts.value():
  910. return binary_numeric_operation<i64, i32, Operators::GreaterThan>(configuration);
  911. case Instructions::i64_gtu.value():
  912. return binary_numeric_operation<u64, i32, Operators::GreaterThan>(configuration);
  913. case Instructions::i64_les.value():
  914. return binary_numeric_operation<i64, i32, Operators::LessThanOrEquals>(configuration);
  915. case Instructions::i64_leu.value():
  916. return binary_numeric_operation<u64, i32, Operators::LessThanOrEquals>(configuration);
  917. case Instructions::i64_ges.value():
  918. return binary_numeric_operation<i64, i32, Operators::GreaterThanOrEquals>(configuration);
  919. case Instructions::i64_geu.value():
  920. return binary_numeric_operation<u64, i32, Operators::GreaterThanOrEquals>(configuration);
  921. case Instructions::f32_eq.value():
  922. return binary_numeric_operation<float, i32, Operators::Equals>(configuration);
  923. case Instructions::f32_ne.value():
  924. return binary_numeric_operation<float, i32, Operators::NotEquals>(configuration);
  925. case Instructions::f32_lt.value():
  926. return binary_numeric_operation<float, i32, Operators::LessThan>(configuration);
  927. case Instructions::f32_gt.value():
  928. return binary_numeric_operation<float, i32, Operators::GreaterThan>(configuration);
  929. case Instructions::f32_le.value():
  930. return binary_numeric_operation<float, i32, Operators::LessThanOrEquals>(configuration);
  931. case Instructions::f32_ge.value():
  932. return binary_numeric_operation<float, i32, Operators::GreaterThanOrEquals>(configuration);
  933. case Instructions::f64_eq.value():
  934. return binary_numeric_operation<double, i32, Operators::Equals>(configuration);
  935. case Instructions::f64_ne.value():
  936. return binary_numeric_operation<double, i32, Operators::NotEquals>(configuration);
  937. case Instructions::f64_lt.value():
  938. return binary_numeric_operation<double, i32, Operators::LessThan>(configuration);
  939. case Instructions::f64_gt.value():
  940. return binary_numeric_operation<double, i32, Operators::GreaterThan>(configuration);
  941. case Instructions::f64_le.value():
  942. return binary_numeric_operation<double, i32, Operators::LessThanOrEquals>(configuration);
  943. case Instructions::f64_ge.value():
  944. return binary_numeric_operation<double, i32, Operators::GreaterThanOrEquals>(configuration);
  945. case Instructions::i32_clz.value():
  946. return unary_operation<i32, i32, Operators::CountLeadingZeros>(configuration);
  947. case Instructions::i32_ctz.value():
  948. return unary_operation<i32, i32, Operators::CountTrailingZeros>(configuration);
  949. case Instructions::i32_popcnt.value():
  950. return unary_operation<i32, i32, Operators::PopCount>(configuration);
  951. case Instructions::i32_add.value():
  952. return binary_numeric_operation<u32, i32, Operators::Add>(configuration);
  953. case Instructions::i32_sub.value():
  954. return binary_numeric_operation<u32, i32, Operators::Subtract>(configuration);
  955. case Instructions::i32_mul.value():
  956. return binary_numeric_operation<u32, i32, Operators::Multiply>(configuration);
  957. case Instructions::i32_divs.value():
  958. return binary_numeric_operation<i32, i32, Operators::Divide>(configuration);
  959. case Instructions::i32_divu.value():
  960. return binary_numeric_operation<u32, i32, Operators::Divide>(configuration);
  961. case Instructions::i32_rems.value():
  962. return binary_numeric_operation<i32, i32, Operators::Modulo>(configuration);
  963. case Instructions::i32_remu.value():
  964. return binary_numeric_operation<u32, i32, Operators::Modulo>(configuration);
  965. case Instructions::i32_and.value():
  966. return binary_numeric_operation<i32, i32, Operators::BitAnd>(configuration);
  967. case Instructions::i32_or.value():
  968. return binary_numeric_operation<i32, i32, Operators::BitOr>(configuration);
  969. case Instructions::i32_xor.value():
  970. return binary_numeric_operation<i32, i32, Operators::BitXor>(configuration);
  971. case Instructions::i32_shl.value():
  972. return binary_numeric_operation<u32, i32, Operators::BitShiftLeft>(configuration);
  973. case Instructions::i32_shrs.value():
  974. return binary_numeric_operation<i32, i32, Operators::BitShiftRight>(configuration);
  975. case Instructions::i32_shru.value():
  976. return binary_numeric_operation<u32, i32, Operators::BitShiftRight>(configuration);
  977. case Instructions::i32_rotl.value():
  978. return binary_numeric_operation<u32, i32, Operators::BitRotateLeft>(configuration);
  979. case Instructions::i32_rotr.value():
  980. return binary_numeric_operation<u32, i32, Operators::BitRotateRight>(configuration);
  981. case Instructions::i64_clz.value():
  982. return unary_operation<i64, i64, Operators::CountLeadingZeros>(configuration);
  983. case Instructions::i64_ctz.value():
  984. return unary_operation<i64, i64, Operators::CountTrailingZeros>(configuration);
  985. case Instructions::i64_popcnt.value():
  986. return unary_operation<i64, i64, Operators::PopCount>(configuration);
  987. case Instructions::i64_add.value():
  988. return binary_numeric_operation<u64, i64, Operators::Add>(configuration);
  989. case Instructions::i64_sub.value():
  990. return binary_numeric_operation<u64, i64, Operators::Subtract>(configuration);
  991. case Instructions::i64_mul.value():
  992. return binary_numeric_operation<u64, i64, Operators::Multiply>(configuration);
  993. case Instructions::i64_divs.value():
  994. return binary_numeric_operation<i64, i64, Operators::Divide>(configuration);
  995. case Instructions::i64_divu.value():
  996. return binary_numeric_operation<u64, i64, Operators::Divide>(configuration);
  997. case Instructions::i64_rems.value():
  998. return binary_numeric_operation<i64, i64, Operators::Modulo>(configuration);
  999. case Instructions::i64_remu.value():
  1000. return binary_numeric_operation<u64, i64, Operators::Modulo>(configuration);
  1001. case Instructions::i64_and.value():
  1002. return binary_numeric_operation<i64, i64, Operators::BitAnd>(configuration);
  1003. case Instructions::i64_or.value():
  1004. return binary_numeric_operation<i64, i64, Operators::BitOr>(configuration);
  1005. case Instructions::i64_xor.value():
  1006. return binary_numeric_operation<i64, i64, Operators::BitXor>(configuration);
  1007. case Instructions::i64_shl.value():
  1008. return binary_numeric_operation<u64, i64, Operators::BitShiftLeft>(configuration);
  1009. case Instructions::i64_shrs.value():
  1010. return binary_numeric_operation<i64, i64, Operators::BitShiftRight>(configuration);
  1011. case Instructions::i64_shru.value():
  1012. return binary_numeric_operation<u64, i64, Operators::BitShiftRight>(configuration);
  1013. case Instructions::i64_rotl.value():
  1014. return binary_numeric_operation<u64, i64, Operators::BitRotateLeft>(configuration);
  1015. case Instructions::i64_rotr.value():
  1016. return binary_numeric_operation<u64, i64, Operators::BitRotateRight>(configuration);
  1017. case Instructions::f32_abs.value():
  1018. return unary_operation<float, float, Operators::Absolute>(configuration);
  1019. case Instructions::f32_neg.value():
  1020. return unary_operation<float, float, Operators::Negate>(configuration);
  1021. case Instructions::f32_ceil.value():
  1022. return unary_operation<float, float, Operators::Ceil>(configuration);
  1023. case Instructions::f32_floor.value():
  1024. return unary_operation<float, float, Operators::Floor>(configuration);
  1025. case Instructions::f32_trunc.value():
  1026. return unary_operation<float, float, Operators::Truncate>(configuration);
  1027. case Instructions::f32_nearest.value():
  1028. return unary_operation<float, float, Operators::NearbyIntegral>(configuration);
  1029. case Instructions::f32_sqrt.value():
  1030. return unary_operation<float, float, Operators::SquareRoot>(configuration);
  1031. case Instructions::f32_add.value():
  1032. return binary_numeric_operation<float, float, Operators::Add>(configuration);
  1033. case Instructions::f32_sub.value():
  1034. return binary_numeric_operation<float, float, Operators::Subtract>(configuration);
  1035. case Instructions::f32_mul.value():
  1036. return binary_numeric_operation<float, float, Operators::Multiply>(configuration);
  1037. case Instructions::f32_div.value():
  1038. return binary_numeric_operation<float, float, Operators::Divide>(configuration);
  1039. case Instructions::f32_min.value():
  1040. return binary_numeric_operation<float, float, Operators::Minimum>(configuration);
  1041. case Instructions::f32_max.value():
  1042. return binary_numeric_operation<float, float, Operators::Maximum>(configuration);
  1043. case Instructions::f32_copysign.value():
  1044. return binary_numeric_operation<float, float, Operators::CopySign>(configuration);
  1045. case Instructions::f64_abs.value():
  1046. return unary_operation<double, double, Operators::Absolute>(configuration);
  1047. case Instructions::f64_neg.value():
  1048. return unary_operation<double, double, Operators::Negate>(configuration);
  1049. case Instructions::f64_ceil.value():
  1050. return unary_operation<double, double, Operators::Ceil>(configuration);
  1051. case Instructions::f64_floor.value():
  1052. return unary_operation<double, double, Operators::Floor>(configuration);
  1053. case Instructions::f64_trunc.value():
  1054. return unary_operation<double, double, Operators::Truncate>(configuration);
  1055. case Instructions::f64_nearest.value():
  1056. return unary_operation<double, double, Operators::NearbyIntegral>(configuration);
  1057. case Instructions::f64_sqrt.value():
  1058. return unary_operation<double, double, Operators::SquareRoot>(configuration);
  1059. case Instructions::f64_add.value():
  1060. return binary_numeric_operation<double, double, Operators::Add>(configuration);
  1061. case Instructions::f64_sub.value():
  1062. return binary_numeric_operation<double, double, Operators::Subtract>(configuration);
  1063. case Instructions::f64_mul.value():
  1064. return binary_numeric_operation<double, double, Operators::Multiply>(configuration);
  1065. case Instructions::f64_div.value():
  1066. return binary_numeric_operation<double, double, Operators::Divide>(configuration);
  1067. case Instructions::f64_min.value():
  1068. return binary_numeric_operation<double, double, Operators::Minimum>(configuration);
  1069. case Instructions::f64_max.value():
  1070. return binary_numeric_operation<double, double, Operators::Maximum>(configuration);
  1071. case Instructions::f64_copysign.value():
  1072. return binary_numeric_operation<double, double, Operators::CopySign>(configuration);
  1073. case Instructions::i32_wrap_i64.value():
  1074. return unary_operation<i64, i32, Operators::Wrap<i32>>(configuration);
  1075. case Instructions::i32_trunc_sf32.value():
  1076. return unary_operation<float, i32, Operators::CheckedTruncate<i32>>(configuration);
  1077. case Instructions::i32_trunc_uf32.value():
  1078. return unary_operation<float, i32, Operators::CheckedTruncate<u32>>(configuration);
  1079. case Instructions::i32_trunc_sf64.value():
  1080. return unary_operation<double, i32, Operators::CheckedTruncate<i32>>(configuration);
  1081. case Instructions::i32_trunc_uf64.value():
  1082. return unary_operation<double, i32, Operators::CheckedTruncate<u32>>(configuration);
  1083. case Instructions::i64_trunc_sf32.value():
  1084. return unary_operation<float, i64, Operators::CheckedTruncate<i64>>(configuration);
  1085. case Instructions::i64_trunc_uf32.value():
  1086. return unary_operation<float, i64, Operators::CheckedTruncate<u64>>(configuration);
  1087. case Instructions::i64_trunc_sf64.value():
  1088. return unary_operation<double, i64, Operators::CheckedTruncate<i64>>(configuration);
  1089. case Instructions::i64_trunc_uf64.value():
  1090. return unary_operation<double, i64, Operators::CheckedTruncate<u64>>(configuration);
  1091. case Instructions::i64_extend_si32.value():
  1092. return unary_operation<i32, i64, Operators::Extend<i64>>(configuration);
  1093. case Instructions::i64_extend_ui32.value():
  1094. return unary_operation<u32, i64, Operators::Extend<i64>>(configuration);
  1095. case Instructions::f32_convert_si32.value():
  1096. return unary_operation<i32, float, Operators::Convert<float>>(configuration);
  1097. case Instructions::f32_convert_ui32.value():
  1098. return unary_operation<u32, float, Operators::Convert<float>>(configuration);
  1099. case Instructions::f32_convert_si64.value():
  1100. return unary_operation<i64, float, Operators::Convert<float>>(configuration);
  1101. case Instructions::f32_convert_ui64.value():
  1102. return unary_operation<u64, float, Operators::Convert<float>>(configuration);
  1103. case Instructions::f32_demote_f64.value():
  1104. return unary_operation<double, float, Operators::Demote>(configuration);
  1105. case Instructions::f64_convert_si32.value():
  1106. return unary_operation<i32, double, Operators::Convert<double>>(configuration);
  1107. case Instructions::f64_convert_ui32.value():
  1108. return unary_operation<u32, double, Operators::Convert<double>>(configuration);
  1109. case Instructions::f64_convert_si64.value():
  1110. return unary_operation<i64, double, Operators::Convert<double>>(configuration);
  1111. case Instructions::f64_convert_ui64.value():
  1112. return unary_operation<u64, double, Operators::Convert<double>>(configuration);
  1113. case Instructions::f64_promote_f32.value():
  1114. return unary_operation<float, double, Operators::Promote>(configuration);
  1115. case Instructions::i32_reinterpret_f32.value():
  1116. return unary_operation<float, i32, Operators::Reinterpret<i32>>(configuration);
  1117. case Instructions::i64_reinterpret_f64.value():
  1118. return unary_operation<double, i64, Operators::Reinterpret<i64>>(configuration);
  1119. case Instructions::f32_reinterpret_i32.value():
  1120. return unary_operation<i32, float, Operators::Reinterpret<float>>(configuration);
  1121. case Instructions::f64_reinterpret_i64.value():
  1122. return unary_operation<i64, double, Operators::Reinterpret<double>>(configuration);
  1123. case Instructions::i32_extend8_s.value():
  1124. return unary_operation<i32, i32, Operators::SignExtend<i8>>(configuration);
  1125. case Instructions::i32_extend16_s.value():
  1126. return unary_operation<i32, i32, Operators::SignExtend<i16>>(configuration);
  1127. case Instructions::i64_extend8_s.value():
  1128. return unary_operation<i64, i64, Operators::SignExtend<i8>>(configuration);
  1129. case Instructions::i64_extend16_s.value():
  1130. return unary_operation<i64, i64, Operators::SignExtend<i16>>(configuration);
  1131. case Instructions::i64_extend32_s.value():
  1132. return unary_operation<i64, i64, Operators::SignExtend<i32>>(configuration);
  1133. case Instructions::i32_trunc_sat_f32_s.value():
  1134. return unary_operation<float, i32, Operators::SaturatingTruncate<i32>>(configuration);
  1135. case Instructions::i32_trunc_sat_f32_u.value():
  1136. return unary_operation<float, i32, Operators::SaturatingTruncate<u32>>(configuration);
  1137. case Instructions::i32_trunc_sat_f64_s.value():
  1138. return unary_operation<double, i32, Operators::SaturatingTruncate<i32>>(configuration);
  1139. case Instructions::i32_trunc_sat_f64_u.value():
  1140. return unary_operation<double, i32, Operators::SaturatingTruncate<u32>>(configuration);
  1141. case Instructions::i64_trunc_sat_f32_s.value():
  1142. return unary_operation<float, i64, Operators::SaturatingTruncate<i64>>(configuration);
  1143. case Instructions::i64_trunc_sat_f32_u.value():
  1144. return unary_operation<float, i64, Operators::SaturatingTruncate<u64>>(configuration);
  1145. case Instructions::i64_trunc_sat_f64_s.value():
  1146. return unary_operation<double, i64, Operators::SaturatingTruncate<i64>>(configuration);
  1147. case Instructions::i64_trunc_sat_f64_u.value():
  1148. return unary_operation<double, i64, Operators::SaturatingTruncate<u64>>(configuration);
  1149. case Instructions::v128_const.value():
  1150. configuration.stack().push(Value(instruction.arguments().get<u128>()));
  1151. return;
  1152. case Instructions::v128_load.value():
  1153. return load_and_push<u128, u128>(configuration, instruction);
  1154. case Instructions::v128_load8x8_s.value():
  1155. return load_and_push_mxn<8, 8, MakeSigned>(configuration, instruction);
  1156. case Instructions::v128_load8x8_u.value():
  1157. return load_and_push_mxn<8, 8, MakeUnsigned>(configuration, instruction);
  1158. case Instructions::v128_load16x4_s.value():
  1159. return load_and_push_mxn<16, 4, MakeSigned>(configuration, instruction);
  1160. case Instructions::v128_load16x4_u.value():
  1161. return load_and_push_mxn<16, 4, MakeUnsigned>(configuration, instruction);
  1162. case Instructions::v128_load32x2_s.value():
  1163. return load_and_push_mxn<32, 2, MakeSigned>(configuration, instruction);
  1164. case Instructions::v128_load32x2_u.value():
  1165. return load_and_push_mxn<32, 2, MakeUnsigned>(configuration, instruction);
  1166. case Instructions::v128_load8_splat.value():
  1167. return load_and_push_m_splat<8>(configuration, instruction);
  1168. case Instructions::v128_load16_splat.value():
  1169. return load_and_push_m_splat<16>(configuration, instruction);
  1170. case Instructions::v128_load32_splat.value():
  1171. return load_and_push_m_splat<32>(configuration, instruction);
  1172. case Instructions::v128_load64_splat.value():
  1173. return load_and_push_m_splat<64>(configuration, instruction);
  1174. case Instructions::i8x16_splat.value():
  1175. return pop_and_push_m_splat<8, NativeIntegralType>(configuration, instruction);
  1176. case Instructions::i16x8_splat.value():
  1177. return pop_and_push_m_splat<16, NativeIntegralType>(configuration, instruction);
  1178. case Instructions::i32x4_splat.value():
  1179. return pop_and_push_m_splat<32, NativeIntegralType>(configuration, instruction);
  1180. case Instructions::i64x2_splat.value():
  1181. return pop_and_push_m_splat<64, NativeIntegralType>(configuration, instruction);
  1182. case Instructions::f32x4_splat.value():
  1183. return pop_and_push_m_splat<32, NativeFloatingType>(configuration, instruction);
  1184. case Instructions::f64x2_splat.value():
  1185. return pop_and_push_m_splat<64, NativeFloatingType>(configuration, instruction);
  1186. case Instructions::i8x16_shuffle.value(): {
  1187. auto indices = pop_vector<u8, MakeSigned>(configuration);
  1188. TRAP_IF_NOT(indices.has_value());
  1189. auto vector = peek_vector<u8, MakeSigned>(configuration);
  1190. TRAP_IF_NOT(vector.has_value());
  1191. auto result = shuffle_vector(vector.value(), indices.value());
  1192. configuration.stack().peek() = Value(result);
  1193. return;
  1194. }
  1195. case Instructions::v128_store.value():
  1196. return pop_and_store<u128, u128>(configuration, instruction);
  1197. case Instructions::i8x16_shl.value():
  1198. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<16>, i32>(configuration);
  1199. case Instructions::i8x16_shr_u.value():
  1200. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<16, MakeUnsigned>, i32>(configuration);
  1201. case Instructions::i8x16_shr_s.value():
  1202. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<16, MakeSigned>, i32>(configuration);
  1203. case Instructions::i16x8_shl.value():
  1204. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<8>, i32>(configuration);
  1205. case Instructions::i16x8_shr_u.value():
  1206. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<8, MakeUnsigned>, i32>(configuration);
  1207. case Instructions::i16x8_shr_s.value():
  1208. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<8, MakeSigned>, i32>(configuration);
  1209. case Instructions::i32x4_shl.value():
  1210. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<4>, i32>(configuration);
  1211. case Instructions::i32x4_shr_u.value():
  1212. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<4, MakeUnsigned>, i32>(configuration);
  1213. case Instructions::i32x4_shr_s.value():
  1214. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<4, MakeSigned>, i32>(configuration);
  1215. case Instructions::i64x2_shl.value():
  1216. return binary_numeric_operation<u128, u128, Operators::VectorShiftLeft<2>, i32>(configuration);
  1217. case Instructions::i64x2_shr_u.value():
  1218. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<2, MakeUnsigned>, i32>(configuration);
  1219. case Instructions::i64x2_shr_s.value():
  1220. return binary_numeric_operation<u128, u128, Operators::VectorShiftRight<2, MakeSigned>, i32>(configuration);
  1221. case Instructions::i8x16_swizzle.value():
  1222. return binary_numeric_operation<u128, u128, Operators::VectorSwizzle>(configuration);
  1223. case Instructions::i8x16_extract_lane_s.value():
  1224. return unary_operation<u128, i8, Operators::VectorExtractLane<16, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1225. case Instructions::i8x16_extract_lane_u.value():
  1226. return unary_operation<u128, u8, Operators::VectorExtractLane<16, MakeUnsigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1227. case Instructions::i16x8_extract_lane_s.value():
  1228. return unary_operation<u128, i16, Operators::VectorExtractLane<8, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1229. case Instructions::i16x8_extract_lane_u.value():
  1230. return unary_operation<u128, u16, Operators::VectorExtractLane<8, MakeUnsigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1231. case Instructions::i32x4_extract_lane.value():
  1232. return unary_operation<u128, i32, Operators::VectorExtractLane<4, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1233. case Instructions::i64x2_extract_lane.value():
  1234. return unary_operation<u128, i64, Operators::VectorExtractLane<2, MakeSigned>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1235. case Instructions::f32x4_extract_lane.value():
  1236. return unary_operation<u128, float, Operators::VectorExtractLaneFloat<4>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1237. case Instructions::f64x2_extract_lane.value():
  1238. return unary_operation<u128, double, Operators::VectorExtractLaneFloat<2>>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1239. case Instructions::i8x16_replace_lane.value():
  1240. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<16, i32>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1241. case Instructions::i16x8_replace_lane.value():
  1242. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<8, i32>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1243. case Instructions::i32x4_replace_lane.value():
  1244. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<4>, i32>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1245. case Instructions::i64x2_replace_lane.value():
  1246. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<2>, i64>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1247. case Instructions::f32x4_replace_lane.value():
  1248. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<4, float>, float>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1249. case Instructions::f64x2_replace_lane.value():
  1250. return binary_numeric_operation<u128, u128, Operators::VectorReplaceLane<2, double>, double>(configuration, instruction.arguments().get<Instruction::LaneIndex>().lane);
  1251. case Instructions::i8x16_eq.value():
  1252. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::Equals>>(configuration);
  1253. case Instructions::i8x16_ne.value():
  1254. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::NotEquals>>(configuration);
  1255. case Instructions::i8x16_lt_s.value():
  1256. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThan, MakeSigned>>(configuration);
  1257. case Instructions::i8x16_lt_u.value():
  1258. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThan, MakeUnsigned>>(configuration);
  1259. case Instructions::i8x16_gt_s.value():
  1260. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThan, MakeSigned>>(configuration);
  1261. case Instructions::i8x16_gt_u.value():
  1262. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1263. case Instructions::i8x16_le_s.value():
  1264. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1265. case Instructions::i8x16_le_u.value():
  1266. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1267. case Instructions::i8x16_ge_s.value():
  1268. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1269. case Instructions::i8x16_ge_u.value():
  1270. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<16, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1271. case Instructions::i16x8_eq.value():
  1272. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::Equals>>(configuration);
  1273. case Instructions::i16x8_ne.value():
  1274. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::NotEquals>>(configuration);
  1275. case Instructions::i16x8_lt_s.value():
  1276. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThan, MakeSigned>>(configuration);
  1277. case Instructions::i16x8_lt_u.value():
  1278. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThan, MakeUnsigned>>(configuration);
  1279. case Instructions::i16x8_gt_s.value():
  1280. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThan, MakeSigned>>(configuration);
  1281. case Instructions::i16x8_gt_u.value():
  1282. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1283. case Instructions::i16x8_le_s.value():
  1284. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1285. case Instructions::i16x8_le_u.value():
  1286. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1287. case Instructions::i16x8_ge_s.value():
  1288. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1289. case Instructions::i16x8_ge_u.value():
  1290. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<8, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1291. case Instructions::i32x4_eq.value():
  1292. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::Equals>>(configuration);
  1293. case Instructions::i32x4_ne.value():
  1294. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::NotEquals>>(configuration);
  1295. case Instructions::i32x4_lt_s.value():
  1296. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThan, MakeSigned>>(configuration);
  1297. case Instructions::i32x4_lt_u.value():
  1298. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThan, MakeUnsigned>>(configuration);
  1299. case Instructions::i32x4_gt_s.value():
  1300. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThan, MakeSigned>>(configuration);
  1301. case Instructions::i32x4_gt_u.value():
  1302. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThan, MakeUnsigned>>(configuration);
  1303. case Instructions::i32x4_le_s.value():
  1304. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThanOrEquals, MakeSigned>>(configuration);
  1305. case Instructions::i32x4_le_u.value():
  1306. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::LessThanOrEquals, MakeUnsigned>>(configuration);
  1307. case Instructions::i32x4_ge_s.value():
  1308. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThanOrEquals, MakeSigned>>(configuration);
  1309. case Instructions::i32x4_ge_u.value():
  1310. return binary_numeric_operation<u128, u128, Operators::VectorCmpOp<4, Operators::GreaterThanOrEquals, MakeUnsigned>>(configuration);
  1311. case Instructions::f32x4_eq.value():
  1312. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::Equals>>(configuration);
  1313. case Instructions::f32x4_ne.value():
  1314. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::NotEquals>>(configuration);
  1315. case Instructions::f32x4_lt.value():
  1316. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::LessThan>>(configuration);
  1317. case Instructions::f32x4_gt.value():
  1318. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::GreaterThan>>(configuration);
  1319. case Instructions::f32x4_le.value():
  1320. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::LessThanOrEquals>>(configuration);
  1321. case Instructions::f32x4_ge.value():
  1322. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<4, Operators::GreaterThanOrEquals>>(configuration);
  1323. case Instructions::f64x2_eq.value():
  1324. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::Equals>>(configuration);
  1325. case Instructions::f64x2_ne.value():
  1326. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::NotEquals>>(configuration);
  1327. case Instructions::f64x2_lt.value():
  1328. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::LessThan>>(configuration);
  1329. case Instructions::f64x2_gt.value():
  1330. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::GreaterThan>>(configuration);
  1331. case Instructions::f64x2_le.value():
  1332. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::LessThanOrEquals>>(configuration);
  1333. case Instructions::f64x2_ge.value():
  1334. return binary_numeric_operation<u128, u128, Operators::VectorFloatCmpOp<2, Operators::GreaterThanOrEquals>>(configuration);
  1335. case Instructions::v128_not.value():
  1336. case Instructions::v128_and.value():
  1337. case Instructions::v128_andnot.value():
  1338. case Instructions::v128_or.value():
  1339. case Instructions::v128_xor.value():
  1340. case Instructions::v128_bitselect.value():
  1341. case Instructions::v128_any_true.value():
  1342. case Instructions::v128_load8_lane.value():
  1343. case Instructions::v128_load16_lane.value():
  1344. case Instructions::v128_load32_lane.value():
  1345. case Instructions::v128_load64_lane.value():
  1346. case Instructions::v128_store8_lane.value():
  1347. case Instructions::v128_store16_lane.value():
  1348. case Instructions::v128_store32_lane.value():
  1349. case Instructions::v128_store64_lane.value():
  1350. case Instructions::v128_load32_zero.value():
  1351. case Instructions::v128_load64_zero.value():
  1352. case Instructions::f32x4_demote_f64x2_zero.value():
  1353. case Instructions::f64x2_promote_low_f32x4.value():
  1354. case Instructions::i8x16_abs.value():
  1355. case Instructions::i8x16_neg.value():
  1356. case Instructions::i8x16_popcnt.value():
  1357. case Instructions::i8x16_all_true.value():
  1358. case Instructions::i8x16_bitmask.value():
  1359. case Instructions::i8x16_narrow_i16x8_s.value():
  1360. case Instructions::i8x16_narrow_i16x8_u.value():
  1361. case Instructions::f32x4_ceil.value():
  1362. case Instructions::f32x4_floor.value():
  1363. case Instructions::f32x4_trunc.value():
  1364. case Instructions::f32x4_nearest.value():
  1365. case Instructions::i8x16_add.value():
  1366. case Instructions::i8x16_add_sat_s.value():
  1367. case Instructions::i8x16_add_sat_u.value():
  1368. case Instructions::i8x16_sub.value():
  1369. case Instructions::i8x16_sub_sat_s.value():
  1370. case Instructions::i8x16_sub_sat_u.value():
  1371. case Instructions::f64x2_ceil.value():
  1372. case Instructions::f64x2_floor.value():
  1373. case Instructions::i8x16_min_s.value():
  1374. case Instructions::i8x16_min_u.value():
  1375. case Instructions::i8x16_max_s.value():
  1376. case Instructions::i8x16_max_u.value():
  1377. case Instructions::f64x2_trunc.value():
  1378. case Instructions::i8x16_avgr_u.value():
  1379. case Instructions::i16x8_extadd_pairwise_i8x16_s.value():
  1380. case Instructions::i16x8_extadd_pairwise_i8x16_u.value():
  1381. case Instructions::i32x4_extadd_pairwise_i16x8_s.value():
  1382. case Instructions::i32x4_extadd_pairwise_i16x8_u.value():
  1383. case Instructions::i16x8_abs.value():
  1384. case Instructions::i16x8_neg.value():
  1385. case Instructions::i16x8_q15mulr_sat_s.value():
  1386. case Instructions::i16x8_all_true.value():
  1387. case Instructions::i16x8_bitmask.value():
  1388. case Instructions::i16x8_narrow_i32x4_s.value():
  1389. case Instructions::i16x8_narrow_i32x4_u.value():
  1390. case Instructions::i16x8_extend_low_i8x16_s.value():
  1391. case Instructions::i16x8_extend_high_i8x16_s.value():
  1392. case Instructions::i16x8_extend_low_i8x16_u.value():
  1393. case Instructions::i16x8_extend_high_i8x16_u.value():
  1394. case Instructions::i16x8_add.value():
  1395. case Instructions::i16x8_add_sat_s.value():
  1396. case Instructions::i16x8_add_sat_u.value():
  1397. case Instructions::i16x8_sub.value():
  1398. case Instructions::i16x8_sub_sat_s.value():
  1399. case Instructions::i16x8_sub_sat_u.value():
  1400. case Instructions::f64x2_nearest.value():
  1401. case Instructions::i16x8_mul.value():
  1402. case Instructions::i16x8_min_s.value():
  1403. case Instructions::i16x8_min_u.value():
  1404. case Instructions::i16x8_max_s.value():
  1405. case Instructions::i16x8_max_u.value():
  1406. case Instructions::i16x8_avgr_u.value():
  1407. case Instructions::i16x8_extmul_low_i8x16_s.value():
  1408. case Instructions::i16x8_extmul_high_i8x16_s.value():
  1409. case Instructions::i16x8_extmul_low_i8x16_u.value():
  1410. case Instructions::i16x8_extmul_high_i8x16_u.value():
  1411. case Instructions::i32x4_abs.value():
  1412. case Instructions::i32x4_neg.value():
  1413. case Instructions::i32x4_all_true.value():
  1414. case Instructions::i32x4_bitmask.value():
  1415. case Instructions::i32x4_extend_low_i16x8_s.value():
  1416. case Instructions::i32x4_extend_high_i16x8_s.value():
  1417. case Instructions::i32x4_extend_low_i16x8_u.value():
  1418. case Instructions::i32x4_extend_high_i16x8_u.value():
  1419. case Instructions::i32x4_add.value():
  1420. case Instructions::i32x4_sub.value():
  1421. case Instructions::i32x4_mul.value():
  1422. case Instructions::i32x4_min_s.value():
  1423. case Instructions::i32x4_min_u.value():
  1424. case Instructions::i32x4_max_s.value():
  1425. case Instructions::i32x4_max_u.value():
  1426. case Instructions::i32x4_dot_i16x8_s.value():
  1427. case Instructions::i32x4_extmul_low_i16x8_s.value():
  1428. case Instructions::i32x4_extmul_high_i16x8_s.value():
  1429. case Instructions::i32x4_extmul_low_i16x8_u.value():
  1430. case Instructions::i32x4_extmul_high_i16x8_u.value():
  1431. case Instructions::i64x2_abs.value():
  1432. case Instructions::i64x2_neg.value():
  1433. case Instructions::i64x2_all_true.value():
  1434. case Instructions::i64x2_bitmask.value():
  1435. case Instructions::i64x2_extend_low_i32x4_s.value():
  1436. case Instructions::i64x2_extend_high_i32x4_s.value():
  1437. case Instructions::i64x2_extend_low_i32x4_u.value():
  1438. case Instructions::i64x2_extend_high_i32x4_u.value():
  1439. case Instructions::i64x2_add.value():
  1440. case Instructions::i64x2_sub.value():
  1441. case Instructions::i64x2_mul.value():
  1442. case Instructions::i64x2_eq.value():
  1443. case Instructions::i64x2_ne.value():
  1444. case Instructions::i64x2_lt_s.value():
  1445. case Instructions::i64x2_gt_s.value():
  1446. case Instructions::i64x2_le_s.value():
  1447. case Instructions::i64x2_ge_s.value():
  1448. case Instructions::i64x2_extmul_low_i32x4_s.value():
  1449. case Instructions::i64x2_extmul_high_i32x4_s.value():
  1450. case Instructions::i64x2_extmul_low_i32x4_u.value():
  1451. case Instructions::i64x2_extmul_high_i32x4_u.value():
  1452. case Instructions::f32x4_abs.value():
  1453. case Instructions::f32x4_neg.value():
  1454. case Instructions::f32x4_sqrt.value():
  1455. case Instructions::f32x4_add.value():
  1456. case Instructions::f32x4_sub.value():
  1457. case Instructions::f32x4_mul.value():
  1458. case Instructions::f32x4_div.value():
  1459. case Instructions::f32x4_min.value():
  1460. case Instructions::f32x4_max.value():
  1461. case Instructions::f32x4_pmin.value():
  1462. case Instructions::f32x4_pmax.value():
  1463. case Instructions::f64x2_abs.value():
  1464. case Instructions::f64x2_neg.value():
  1465. case Instructions::f64x2_sqrt.value():
  1466. case Instructions::f64x2_add.value():
  1467. case Instructions::f64x2_sub.value():
  1468. case Instructions::f64x2_mul.value():
  1469. case Instructions::f64x2_div.value():
  1470. case Instructions::f64x2_min.value():
  1471. case Instructions::f64x2_max.value():
  1472. case Instructions::f64x2_pmin.value():
  1473. case Instructions::f64x2_pmax.value():
  1474. case Instructions::i32x4_trunc_sat_f32x4_s.value():
  1475. case Instructions::i32x4_trunc_sat_f32x4_u.value():
  1476. case Instructions::f32x4_convert_i32x4_s.value():
  1477. case Instructions::f32x4_convert_i32x4_u.value():
  1478. case Instructions::i32x4_trunc_sat_f64x2_s_zero.value():
  1479. case Instructions::i32x4_trunc_sat_f64x2_u_zero.value():
  1480. case Instructions::f64x2_convert_low_i32x4_s.value():
  1481. case Instructions::f64x2_convert_low_i32x4_u.value():
  1482. case Instructions::table_init.value():
  1483. case Instructions::table_copy.value():
  1484. case Instructions::table_fill.value():
  1485. default:
  1486. dbgln_if(WASM_TRACE_DEBUG, "Instruction '{}' not implemented", instruction_name(instruction.opcode()));
  1487. m_trap = Trap { ByteString::formatted("Unimplemented instruction {}", instruction_name(instruction.opcode())) };
  1488. return;
  1489. }
  1490. }
  1491. void DebuggerBytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  1492. {
  1493. if (pre_interpret_hook) {
  1494. auto result = pre_interpret_hook(configuration, ip, instruction);
  1495. if (!result) {
  1496. m_trap = Trap { "Trapped by user request" };
  1497. return;
  1498. }
  1499. }
  1500. BytecodeInterpreter::interpret(configuration, ip, instruction);
  1501. if (post_interpret_hook) {
  1502. auto result = post_interpret_hook(configuration, ip, instruction, *this);
  1503. if (!result) {
  1504. m_trap = Trap { "Trapped by user request" };
  1505. return;
  1506. }
  1507. }
  1508. }
  1509. }