BytecodeInterpreter.cpp 78 KB

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