BytecodeInterpreter.cpp 80 KB

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