BytecodeInterpreter.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/MemoryStream.h>
  10. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  11. #include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
  12. #include <LibWasm/AbstractMachine/Configuration.h>
  13. #include <LibWasm/AbstractMachine/Operators.h>
  14. #include <LibWasm/Opcode.h>
  15. #include <LibWasm/Printer/Printer.h>
  16. namespace Wasm {
  17. #define TRAP_IF_NOT(x) \
  18. do { \
  19. if (trap_if_not(x, #x##sv)) { \
  20. dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
  21. return; \
  22. } \
  23. } while (false)
  24. #define TRAP_IF_NOT_NORETURN(x) \
  25. do { \
  26. if (trap_if_not(x, #x##sv)) { \
  27. dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
  28. } \
  29. } while (false)
  30. void BytecodeInterpreter::interpret(Configuration& configuration)
  31. {
  32. m_trap = Empty {};
  33. auto& instructions = configuration.frame().expression().instructions();
  34. auto max_ip_value = InstructionPointer { instructions.size() };
  35. auto& current_ip_value = configuration.ip();
  36. auto const should_limit_instruction_count = configuration.should_limit_instruction_count();
  37. u64 executed_instructions = 0;
  38. while (current_ip_value < max_ip_value) {
  39. if (should_limit_instruction_count) {
  40. if (executed_instructions++ >= Constants::max_allowed_executed_instructions_per_call) [[unlikely]] {
  41. m_trap = Trap { "Exceeded maximum allowed number of instructions" };
  42. return;
  43. }
  44. }
  45. auto& instruction = instructions[current_ip_value.value()];
  46. auto old_ip = current_ip_value;
  47. interpret(configuration, current_ip_value, instruction);
  48. if (did_trap())
  49. return;
  50. if (current_ip_value == old_ip) // If no jump occurred
  51. ++current_ip_value;
  52. }
  53. }
  54. void BytecodeInterpreter::branch_to_label(Configuration& configuration, LabelIndex index)
  55. {
  56. dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value());
  57. auto label = configuration.nth_label(index.value());
  58. dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label->continuation().value(), label->arity());
  59. auto results = pop_values(configuration, label->arity());
  60. size_t drop_count = index.value() + 1;
  61. for (; !configuration.stack().is_empty();) {
  62. auto& entry = configuration.stack().peek();
  63. if (entry.has<Label>()) {
  64. if (--drop_count == 0)
  65. break;
  66. }
  67. configuration.stack().pop();
  68. }
  69. for (auto& result : results)
  70. configuration.stack().push(move(result));
  71. configuration.ip() = label->continuation();
  72. }
  73. template<typename ReadType, typename PushType>
  74. void BytecodeInterpreter::load_and_push(Configuration& configuration, Instruction const& instruction)
  75. {
  76. auto& address = configuration.frame().module().memories().first();
  77. auto memory = configuration.store().get(address);
  78. if (!memory) {
  79. m_trap = Trap { "Nonexistent memory" };
  80. return;
  81. }
  82. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  83. auto& entry = configuration.stack().peek();
  84. auto base = entry.get<Value>().to<i32>();
  85. if (!base.has_value()) {
  86. m_trap = Trap { "Memory access out of bounds" };
  87. return;
  88. }
  89. u64 instance_address = static_cast<u64>(bit_cast<u32>(base.value())) + arg.offset;
  90. Checked addition { instance_address };
  91. addition += sizeof(ReadType);
  92. if (addition.has_overflow() || addition.value() > memory->size()) {
  93. m_trap = Trap { "Memory access out of bounds" };
  94. dbgln("LibWasm: Memory access out of bounds (expected {} to be less than or equal to {})", instance_address + sizeof(ReadType), memory->size());
  95. return;
  96. }
  97. dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType));
  98. auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType));
  99. configuration.stack().peek() = Value(static_cast<PushType>(read_value<ReadType>(slice)));
  100. }
  101. void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address)
  102. {
  103. TRAP_IF_NOT(m_stack_info.size_free() >= Constants::minimum_stack_space_to_keep_free);
  104. auto instance = configuration.store().get(address);
  105. FunctionType const* type { nullptr };
  106. instance->visit([&](auto const& function) { type = &function.type(); });
  107. TRAP_IF_NOT(configuration.stack().entries().size() > type->parameters().size());
  108. Vector<Value> args;
  109. args.ensure_capacity(type->parameters().size());
  110. auto span = configuration.stack().entries().span().slice_from_end(type->parameters().size());
  111. for (auto& entry : span) {
  112. auto* call_argument = entry.get_pointer<Value>();
  113. TRAP_IF_NOT(call_argument);
  114. args.unchecked_append(move(*call_argument));
  115. }
  116. configuration.stack().entries().remove(configuration.stack().size() - span.size(), span.size());
  117. Result result { Trap { ""sv } };
  118. {
  119. CallFrameHandle handle { *this, configuration };
  120. result = configuration.call(*this, address, move(args));
  121. }
  122. if (result.is_trap()) {
  123. m_trap = move(result.trap());
  124. return;
  125. }
  126. if (result.is_completion()) {
  127. m_trap = move(result.completion());
  128. return;
  129. }
  130. configuration.stack().entries().ensure_capacity(configuration.stack().size() + result.values().size());
  131. for (auto& entry : result.values().in_reverse())
  132. configuration.stack().entries().unchecked_append(move(entry));
  133. }
  134. template<typename PopType, typename PushType, typename Operator>
  135. void BytecodeInterpreter::binary_numeric_operation(Configuration& configuration)
  136. {
  137. auto rhs_entry = configuration.stack().pop();
  138. auto& lhs_entry = configuration.stack().peek();
  139. auto rhs_ptr = rhs_entry.get_pointer<Value>();
  140. auto lhs_ptr = lhs_entry.get_pointer<Value>();
  141. auto rhs = rhs_ptr->to<PopType>();
  142. auto lhs = lhs_ptr->to<PopType>();
  143. PushType result;
  144. auto call_result = Operator {}(lhs.value(), rhs.value());
  145. if constexpr (IsSpecializationOf<decltype(call_result), AK::Result>) {
  146. if (call_result.is_error()) {
  147. trap_if_not(false, call_result.error());
  148. return;
  149. }
  150. result = call_result.release_value();
  151. } else {
  152. result = call_result;
  153. }
  154. dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), Operator::name(), rhs.value(), result);
  155. lhs_entry = Value(result);
  156. }
  157. template<typename PopType, typename PushType, typename Operator>
  158. void BytecodeInterpreter::unary_operation(Configuration& configuration)
  159. {
  160. auto& entry = configuration.stack().peek();
  161. auto entry_ptr = entry.get_pointer<Value>();
  162. auto value = entry_ptr->to<PopType>();
  163. auto call_result = Operator {}(*value);
  164. PushType result;
  165. if constexpr (IsSpecializationOf<decltype(call_result), AK::Result>) {
  166. if (call_result.is_error()) {
  167. trap_if_not(false, call_result.error());
  168. return;
  169. }
  170. result = call_result.release_value();
  171. } else {
  172. result = call_result;
  173. }
  174. dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", Operator::name(), *value, result);
  175. entry = Value(result);
  176. }
  177. template<typename T>
  178. struct ConvertToRaw {
  179. T operator()(T value)
  180. {
  181. return LittleEndian<T>(value);
  182. }
  183. };
  184. template<>
  185. struct ConvertToRaw<float> {
  186. u32 operator()(float value)
  187. {
  188. ReadonlyBytes bytes { &value, sizeof(float) };
  189. FixedMemoryStream stream { bytes };
  190. auto res = stream.read_value<LittleEndian<u32>>().release_value_but_fixme_should_propagate_errors();
  191. return static_cast<u32>(res);
  192. }
  193. };
  194. template<>
  195. struct ConvertToRaw<double> {
  196. u64 operator()(double value)
  197. {
  198. ReadonlyBytes bytes { &value, sizeof(double) };
  199. FixedMemoryStream stream { bytes };
  200. auto res = stream.read_value<LittleEndian<u64>>().release_value_but_fixme_should_propagate_errors();
  201. return static_cast<u64>(res);
  202. }
  203. };
  204. template<typename PopT, typename StoreT>
  205. void BytecodeInterpreter::pop_and_store(Configuration& configuration, Instruction const& instruction)
  206. {
  207. auto entry = configuration.stack().pop();
  208. auto value = ConvertToRaw<StoreT> {}(*entry.get<Value>().to<PopT>());
  209. dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(StoreT));
  210. auto base_entry = configuration.stack().pop();
  211. auto base = base_entry.get<Value>().to<i32>();
  212. store_to_memory(configuration, instruction, { &value, sizeof(StoreT) }, *base);
  213. }
  214. void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction const& instruction, ReadonlyBytes data, i32 base)
  215. {
  216. auto& address = configuration.frame().module().memories().first();
  217. auto memory = configuration.store().get(address);
  218. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  219. u64 instance_address = static_cast<u64>(bit_cast<u32>(base)) + arg.offset;
  220. Checked addition { instance_address };
  221. addition += data.size();
  222. if (addition.has_overflow() || addition.value() > memory->size()) {
  223. m_trap = Trap { "Memory access out of bounds" };
  224. dbgln("LibWasm: Memory access out of bounds (expected 0 <= {} and {} <= {})", instance_address, instance_address + data.size(), memory->size());
  225. return;
  226. }
  227. dbgln_if(WASM_TRACE_DEBUG, "temporary({}b) -> store({})", data.size(), instance_address);
  228. data.copy_to(memory->data().bytes().slice(instance_address, data.size()));
  229. }
  230. template<typename T>
  231. T BytecodeInterpreter::read_value(ReadonlyBytes data)
  232. {
  233. FixedMemoryStream stream { data };
  234. auto value_or_error = stream.read_value<LittleEndian<T>>();
  235. if (value_or_error.is_error()) {
  236. dbgln("Read from {} failed", data.data());
  237. m_trap = Trap { "Read from memory failed" };
  238. }
  239. return value_or_error.release_value();
  240. }
  241. template<>
  242. float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
  243. {
  244. FixedMemoryStream stream { data };
  245. auto raw_value_or_error = stream.read_value<LittleEndian<u32>>();
  246. if (raw_value_or_error.is_error())
  247. m_trap = Trap { "Read from memory failed" };
  248. auto raw_value = raw_value_or_error.release_value();
  249. return bit_cast<float>(static_cast<u32>(raw_value));
  250. }
  251. template<>
  252. double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
  253. {
  254. FixedMemoryStream stream { data };
  255. auto raw_value_or_error = stream.read_value<LittleEndian<u64>>();
  256. if (raw_value_or_error.is_error())
  257. m_trap = Trap { "Read from memory failed" };
  258. auto raw_value = raw_value_or_error.release_value();
  259. return bit_cast<double>(static_cast<u64>(raw_value));
  260. }
  261. template<typename V, typename T>
  262. MakeSigned<T> BytecodeInterpreter::checked_signed_truncate(V value)
  263. {
  264. if (isnan(value) || isinf(value)) { // "undefined", let's just trap.
  265. m_trap = Trap { "Signed truncation undefined behavior" };
  266. return 0;
  267. }
  268. double truncated;
  269. if constexpr (IsSame<float, V>)
  270. truncated = truncf(value);
  271. else
  272. truncated = trunc(value);
  273. using SignedT = MakeSigned<T>;
  274. if (NumericLimits<SignedT>::min() <= truncated && static_cast<double>(NumericLimits<SignedT>::max()) >= truncated)
  275. return static_cast<SignedT>(truncated);
  276. dbgln_if(WASM_TRACE_DEBUG, "Truncate out of range error");
  277. m_trap = Trap { "Signed truncation out of range" };
  278. return true;
  279. }
  280. template<typename V, typename T>
  281. MakeUnsigned<T> BytecodeInterpreter::checked_unsigned_truncate(V value)
  282. {
  283. if (isnan(value) || isinf(value)) { // "undefined", let's just trap.
  284. m_trap = Trap { "Unsigned truncation undefined behavior" };
  285. return 0;
  286. }
  287. double truncated;
  288. if constexpr (IsSame<float, V>)
  289. truncated = truncf(value);
  290. else
  291. truncated = trunc(value);
  292. using UnsignedT = MakeUnsigned<T>;
  293. if (NumericLimits<UnsignedT>::min() <= truncated && static_cast<double>(NumericLimits<UnsignedT>::max()) >= truncated)
  294. return static_cast<UnsignedT>(truncated);
  295. dbgln_if(WASM_TRACE_DEBUG, "Truncate out of range error");
  296. m_trap = Trap { "Unsigned truncation out of range" };
  297. return true;
  298. }
  299. Vector<Value> BytecodeInterpreter::pop_values(Configuration& configuration, size_t count)
  300. {
  301. Vector<Value> results;
  302. results.resize(count);
  303. for (size_t i = 0; i < count; ++i) {
  304. auto top_of_stack = configuration.stack().pop();
  305. if (auto value = top_of_stack.get_pointer<Value>())
  306. results[i] = move(*value);
  307. else
  308. TRAP_IF_NOT_NORETURN(value);
  309. }
  310. return results;
  311. }
  312. void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  313. {
  314. dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value());
  315. switch (instruction.opcode().value()) {
  316. case Instructions::unreachable.value():
  317. m_trap = Trap { "Unreachable" };
  318. return;
  319. case Instructions::nop.value():
  320. return;
  321. case Instructions::local_get.value():
  322. configuration.stack().push(Value(configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()]));
  323. return;
  324. case Instructions::local_set.value(): {
  325. auto entry = configuration.stack().pop();
  326. configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()] = move(entry.get<Value>());
  327. return;
  328. }
  329. case Instructions::i32_const.value():
  330. configuration.stack().push(Value(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
  331. return;
  332. case Instructions::i64_const.value():
  333. configuration.stack().push(Value(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
  334. return;
  335. case Instructions::f32_const.value():
  336. configuration.stack().push(Value(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>())));
  337. return;
  338. case Instructions::f64_const.value():
  339. configuration.stack().push(Value(ValueType { ValueType::F64 }, instruction.arguments().get<double>()));
  340. return;
  341. case Instructions::block.value(): {
  342. size_t arity = 0;
  343. size_t parameter_count = 0;
  344. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  345. switch (args.block_type.kind()) {
  346. case BlockType::Empty:
  347. break;
  348. case BlockType::Type:
  349. arity = 1;
  350. break;
  351. case BlockType::Index: {
  352. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  353. arity = type.results().size();
  354. parameter_count = type.parameters().size();
  355. }
  356. }
  357. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, Label(arity, args.end_ip));
  358. return;
  359. }
  360. case Instructions::loop.value(): {
  361. size_t arity = 0;
  362. size_t parameter_count = 0;
  363. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  364. switch (args.block_type.kind()) {
  365. case BlockType::Empty:
  366. break;
  367. case BlockType::Type:
  368. arity = 1;
  369. break;
  370. case BlockType::Index: {
  371. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  372. arity = type.results().size();
  373. parameter_count = type.parameters().size();
  374. }
  375. }
  376. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, Label(arity, ip.value() + 1));
  377. return;
  378. }
  379. case Instructions::if_.value(): {
  380. size_t arity = 0;
  381. size_t parameter_count = 0;
  382. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  383. switch (args.block_type.kind()) {
  384. case BlockType::Empty:
  385. break;
  386. case BlockType::Type:
  387. arity = 1;
  388. break;
  389. case BlockType::Index: {
  390. auto& type = configuration.frame().module().types()[args.block_type.type_index().value()];
  391. arity = type.results().size();
  392. parameter_count = type.parameters().size();
  393. }
  394. }
  395. auto entry = configuration.stack().pop();
  396. auto value = entry.get<Value>().to<i32>();
  397. auto end_label = Label(arity, args.end_ip.value());
  398. if (value.value() == 0) {
  399. if (args.else_ip.has_value()) {
  400. configuration.ip() = args.else_ip.value();
  401. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, end_label);
  402. } else {
  403. configuration.ip() = args.end_ip.value() + 1;
  404. }
  405. } else {
  406. configuration.stack().entries().insert(configuration.stack().size() - parameter_count, end_label);
  407. }
  408. return;
  409. }
  410. case Instructions::structured_end.value():
  411. case Instructions::structured_else.value(): {
  412. auto index = configuration.nth_label_index(0);
  413. auto label = configuration.stack().entries()[*index].get<Label>();
  414. configuration.stack().entries().remove(*index, 1);
  415. if (instruction.opcode() == Instructions::structured_end)
  416. return;
  417. // Jump to the end label
  418. configuration.ip() = label.continuation();
  419. return;
  420. }
  421. case Instructions::return_.value(): {
  422. auto& frame = configuration.frame();
  423. Checked checked_index { configuration.stack().size() };
  424. checked_index -= frame.arity();
  425. VERIFY(!checked_index.has_overflow());
  426. auto index = checked_index.value();
  427. size_t i = 1;
  428. for (; i <= index; ++i) {
  429. auto& entry = configuration.stack().entries()[index - i];
  430. if (entry.has<Label>()) {
  431. if (configuration.stack().entries()[index - i - 1].has<Frame>())
  432. break;
  433. }
  434. }
  435. configuration.stack().entries().remove(index - i + 1, i - 1);
  436. // Jump past the call/indirect instruction
  437. configuration.ip() = configuration.frame().expression().instructions().size();
  438. return;
  439. }
  440. case Instructions::br.value():
  441. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  442. case Instructions::br_if.value(): {
  443. auto entry = configuration.stack().pop();
  444. if (entry.get<Value>().to<i32>().value_or(0) == 0)
  445. return;
  446. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  447. }
  448. case Instructions::br_table.value(): {
  449. auto& arguments = instruction.arguments().get<Instruction::TableBranchArgs>();
  450. auto entry = configuration.stack().pop();
  451. auto maybe_i = entry.get<Value>().to<i32>();
  452. if (0 <= *maybe_i) {
  453. size_t i = *maybe_i;
  454. if (i < arguments.labels.size())
  455. return branch_to_label(configuration, arguments.labels[i]);
  456. }
  457. return branch_to_label(configuration, arguments.default_);
  458. }
  459. case Instructions::call.value(): {
  460. auto index = instruction.arguments().get<FunctionIndex>();
  461. auto address = configuration.frame().module().functions()[index.value()];
  462. dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value());
  463. call_address(configuration, address);
  464. return;
  465. }
  466. case Instructions::call_indirect.value(): {
  467. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  468. auto table_address = configuration.frame().module().tables()[args.table.value()];
  469. auto table_instance = configuration.store().get(table_address);
  470. auto entry = configuration.stack().pop();
  471. auto index = entry.get<Value>().to<i32>();
  472. TRAP_IF_NOT(index.value() >= 0);
  473. TRAP_IF_NOT(static_cast<size_t>(index.value()) < table_instance->elements().size());
  474. auto element = table_instance->elements()[index.value()];
  475. TRAP_IF_NOT(element.has_value());
  476. TRAP_IF_NOT(element->ref().has<Reference::Func>());
  477. auto address = element->ref().get<Reference::Func>().address;
  478. dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index.value(), address.value());
  479. call_address(configuration, address);
  480. return;
  481. }
  482. case Instructions::i32_load.value():
  483. return load_and_push<i32, i32>(configuration, instruction);
  484. case Instructions::i64_load.value():
  485. return load_and_push<i64, i64>(configuration, instruction);
  486. case Instructions::f32_load.value():
  487. return load_and_push<float, float>(configuration, instruction);
  488. case Instructions::f64_load.value():
  489. return load_and_push<double, double>(configuration, instruction);
  490. case Instructions::i32_load8_s.value():
  491. return load_and_push<i8, i32>(configuration, instruction);
  492. case Instructions::i32_load8_u.value():
  493. return load_and_push<u8, i32>(configuration, instruction);
  494. case Instructions::i32_load16_s.value():
  495. return load_and_push<i16, i32>(configuration, instruction);
  496. case Instructions::i32_load16_u.value():
  497. return load_and_push<u16, i32>(configuration, instruction);
  498. case Instructions::i64_load8_s.value():
  499. return load_and_push<i8, i64>(configuration, instruction);
  500. case Instructions::i64_load8_u.value():
  501. return load_and_push<u8, i64>(configuration, instruction);
  502. case Instructions::i64_load16_s.value():
  503. return load_and_push<i16, i64>(configuration, instruction);
  504. case Instructions::i64_load16_u.value():
  505. return load_and_push<u16, i64>(configuration, instruction);
  506. case Instructions::i64_load32_s.value():
  507. return load_and_push<i32, i64>(configuration, instruction);
  508. case Instructions::i64_load32_u.value():
  509. return load_and_push<u32, i64>(configuration, instruction);
  510. case Instructions::i32_store.value():
  511. return pop_and_store<i32, i32>(configuration, instruction);
  512. case Instructions::i64_store.value():
  513. return pop_and_store<i64, i64>(configuration, instruction);
  514. case Instructions::f32_store.value():
  515. return pop_and_store<float, float>(configuration, instruction);
  516. case Instructions::f64_store.value():
  517. return pop_and_store<double, double>(configuration, instruction);
  518. case Instructions::i32_store8.value():
  519. return pop_and_store<i32, i8>(configuration, instruction);
  520. case Instructions::i32_store16.value():
  521. return pop_and_store<i32, i16>(configuration, instruction);
  522. case Instructions::i64_store8.value():
  523. return pop_and_store<i64, i8>(configuration, instruction);
  524. case Instructions::i64_store16.value():
  525. return pop_and_store<i64, i16>(configuration, instruction);
  526. case Instructions::i64_store32.value():
  527. return pop_and_store<i64, i32>(configuration, instruction);
  528. case Instructions::local_tee.value(): {
  529. auto& entry = configuration.stack().peek();
  530. auto value = entry.get<Value>();
  531. auto local_index = instruction.arguments().get<LocalIndex>();
  532. dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
  533. configuration.frame().locals()[local_index.value()] = move(value);
  534. return;
  535. }
  536. case Instructions::global_get.value(): {
  537. auto global_index = instruction.arguments().get<GlobalIndex>();
  538. auto address = configuration.frame().module().globals()[global_index.value()];
  539. dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
  540. auto global = configuration.store().get(address);
  541. configuration.stack().push(Value(global->value()));
  542. return;
  543. }
  544. case Instructions::global_set.value(): {
  545. auto global_index = instruction.arguments().get<GlobalIndex>();
  546. auto address = configuration.frame().module().globals()[global_index.value()];
  547. auto entry = configuration.stack().pop();
  548. auto value = entry.get<Value>();
  549. dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
  550. auto global = configuration.store().get(address);
  551. global->set_value(move(value));
  552. return;
  553. }
  554. case Instructions::memory_size.value(): {
  555. auto address = configuration.frame().module().memories()[0];
  556. auto instance = configuration.store().get(address);
  557. auto pages = instance->size() / Constants::page_size;
  558. dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
  559. configuration.stack().push(Value((i32)pages));
  560. return;
  561. }
  562. case Instructions::memory_grow.value(): {
  563. auto address = configuration.frame().module().memories()[0];
  564. auto instance = configuration.store().get(address);
  565. i32 old_pages = instance->size() / Constants::page_size;
  566. auto& entry = configuration.stack().peek();
  567. auto new_pages = entry.get<Value>().to<i32>();
  568. dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
  569. if (instance->grow(new_pages.value() * Constants::page_size))
  570. configuration.stack().peek() = Value((i32)old_pages);
  571. else
  572. configuration.stack().peek() = Value((i32)-1);
  573. return;
  574. }
  575. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-fill
  576. case Instructions::memory_fill.value(): {
  577. auto address = configuration.frame().module().memories()[0];
  578. auto instance = configuration.store().get(address);
  579. auto count = configuration.stack().pop().get<Value>().to<i32>().value();
  580. auto value = configuration.stack().pop().get<Value>().to<i32>().value();
  581. auto destination_offset = configuration.stack().pop().get<Value>().to<i32>().value();
  582. TRAP_IF_NOT(static_cast<size_t>(destination_offset + count) <= instance->data().size());
  583. if (count == 0)
  584. return;
  585. Instruction synthetic_store_instruction {
  586. Instructions::i32_store8,
  587. Instruction::MemoryArgument { 0, 0 }
  588. };
  589. for (auto i = 0; i < count; ++i) {
  590. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset);
  591. }
  592. return;
  593. }
  594. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-copy
  595. case Instructions::memory_copy.value(): {
  596. auto address = configuration.frame().module().memories()[0];
  597. auto instance = configuration.store().get(address);
  598. auto count = configuration.stack().pop().get<Value>().to<i32>().value();
  599. auto source_offset = configuration.stack().pop().get<Value>().to<i32>().value();
  600. auto destination_offset = configuration.stack().pop().get<Value>().to<i32>().value();
  601. TRAP_IF_NOT(static_cast<size_t>(source_offset + count) <= instance->data().size());
  602. TRAP_IF_NOT(static_cast<size_t>(destination_offset + count) <= instance->data().size());
  603. if (count == 0)
  604. return;
  605. Instruction synthetic_store_instruction {
  606. Instructions::i32_store8,
  607. Instruction::MemoryArgument { 0, 0 }
  608. };
  609. if (destination_offset <= source_offset) {
  610. for (auto i = 0; i < count; ++i) {
  611. auto value = instance->data()[source_offset + i];
  612. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset + i);
  613. }
  614. } else {
  615. for (auto i = count - 1; i >= 0; --i) {
  616. auto value = instance->data()[source_offset + i];
  617. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset + i);
  618. }
  619. }
  620. return;
  621. }
  622. // https://webassembly.github.io/spec/core/bikeshed/#exec-memory-init
  623. case Instructions::memory_init.value(): {
  624. auto data_index = instruction.arguments().get<DataIndex>();
  625. auto& data_address = configuration.frame().module().datas()[data_index.value()];
  626. auto& data = *configuration.store().get(data_address);
  627. auto count = *configuration.stack().pop().get<Value>().to<i32>();
  628. auto source_offset = *configuration.stack().pop().get<Value>().to<i32>();
  629. auto destination_offset = *configuration.stack().pop().get<Value>().to<i32>();
  630. TRAP_IF_NOT(count > 0);
  631. TRAP_IF_NOT(source_offset + count > 0);
  632. TRAP_IF_NOT(static_cast<size_t>(source_offset + count) <= data.size());
  633. Instruction synthetic_store_instruction {
  634. Instructions::i32_store8,
  635. Instruction::MemoryArgument { 0, 0 }
  636. };
  637. for (size_t i = 0; i < (size_t)count; ++i) {
  638. auto value = data.data()[source_offset + i];
  639. store_to_memory(configuration, synthetic_store_instruction, { &value, sizeof(value) }, destination_offset + i);
  640. }
  641. return;
  642. }
  643. // https://webassembly.github.io/spec/core/bikeshed/#exec-data-drop
  644. case Instructions::data_drop.value(): {
  645. auto data_index = instruction.arguments().get<DataIndex>();
  646. auto data_address = configuration.frame().module().datas()[data_index.value()];
  647. *configuration.store().get(data_address) = DataInstance({});
  648. return;
  649. }
  650. case Instructions::table_get.value():
  651. case Instructions::table_set.value():
  652. goto unimplemented;
  653. case Instructions::ref_null.value(): {
  654. auto type = instruction.arguments().get<ValueType>();
  655. configuration.stack().push(Value(Reference(Reference::Null { type })));
  656. return;
  657. };
  658. case Instructions::ref_func.value(): {
  659. auto index = instruction.arguments().get<FunctionIndex>().value();
  660. auto& functions = configuration.frame().module().functions();
  661. auto address = functions[index];
  662. configuration.stack().push(Value(ValueType(ValueType::FunctionReference), address.value()));
  663. return;
  664. }
  665. case Instructions::ref_is_null.value(): {
  666. auto top = configuration.stack().peek().get_pointer<Value>();
  667. TRAP_IF_NOT(top->type().is_reference());
  668. auto is_null = top->to<Reference::Null>().has_value();
  669. configuration.stack().peek() = Value(ValueType(ValueType::I32), static_cast<u64>(is_null ? 1 : 0));
  670. return;
  671. }
  672. case Instructions::drop.value():
  673. configuration.stack().pop();
  674. return;
  675. case Instructions::select.value():
  676. case Instructions::select_typed.value(): {
  677. // Note: The type seems to only be used for validation.
  678. auto entry = configuration.stack().pop();
  679. auto value = entry.get<Value>().to<i32>();
  680. dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
  681. auto rhs_entry = configuration.stack().pop();
  682. auto& lhs_entry = configuration.stack().peek();
  683. auto rhs = move(rhs_entry.get<Value>());
  684. auto lhs = move(lhs_entry.get<Value>());
  685. configuration.stack().peek() = value.value() != 0 ? move(lhs) : move(rhs);
  686. return;
  687. }
  688. case Instructions::i32_eqz.value():
  689. return unary_operation<i32, i32, Operators::EqualsZero>(configuration);
  690. case Instructions::i32_eq.value():
  691. return binary_numeric_operation<i32, i32, Operators::Equals>(configuration);
  692. case Instructions::i32_ne.value():
  693. return binary_numeric_operation<i32, i32, Operators::NotEquals>(configuration);
  694. case Instructions::i32_lts.value():
  695. return binary_numeric_operation<i32, i32, Operators::LessThan>(configuration);
  696. case Instructions::i32_ltu.value():
  697. return binary_numeric_operation<u32, i32, Operators::LessThan>(configuration);
  698. case Instructions::i32_gts.value():
  699. return binary_numeric_operation<i32, i32, Operators::GreaterThan>(configuration);
  700. case Instructions::i32_gtu.value():
  701. return binary_numeric_operation<u32, i32, Operators::GreaterThan>(configuration);
  702. case Instructions::i32_les.value():
  703. return binary_numeric_operation<i32, i32, Operators::LessThanOrEquals>(configuration);
  704. case Instructions::i32_leu.value():
  705. return binary_numeric_operation<u32, i32, Operators::LessThanOrEquals>(configuration);
  706. case Instructions::i32_ges.value():
  707. return binary_numeric_operation<i32, i32, Operators::GreaterThanOrEquals>(configuration);
  708. case Instructions::i32_geu.value():
  709. return binary_numeric_operation<u32, i32, Operators::GreaterThanOrEquals>(configuration);
  710. case Instructions::i64_eqz.value():
  711. return unary_operation<i64, i32, Operators::EqualsZero>(configuration);
  712. case Instructions::i64_eq.value():
  713. return binary_numeric_operation<i64, i32, Operators::Equals>(configuration);
  714. case Instructions::i64_ne.value():
  715. return binary_numeric_operation<i64, i32, Operators::NotEquals>(configuration);
  716. case Instructions::i64_lts.value():
  717. return binary_numeric_operation<i64, i32, Operators::LessThan>(configuration);
  718. case Instructions::i64_ltu.value():
  719. return binary_numeric_operation<u64, i32, Operators::LessThan>(configuration);
  720. case Instructions::i64_gts.value():
  721. return binary_numeric_operation<i64, i32, Operators::GreaterThan>(configuration);
  722. case Instructions::i64_gtu.value():
  723. return binary_numeric_operation<u64, i32, Operators::GreaterThan>(configuration);
  724. case Instructions::i64_les.value():
  725. return binary_numeric_operation<i64, i32, Operators::LessThanOrEquals>(configuration);
  726. case Instructions::i64_leu.value():
  727. return binary_numeric_operation<u64, i32, Operators::LessThanOrEquals>(configuration);
  728. case Instructions::i64_ges.value():
  729. return binary_numeric_operation<i64, i32, Operators::GreaterThanOrEquals>(configuration);
  730. case Instructions::i64_geu.value():
  731. return binary_numeric_operation<u64, i32, Operators::GreaterThanOrEquals>(configuration);
  732. case Instructions::f32_eq.value():
  733. return binary_numeric_operation<float, i32, Operators::Equals>(configuration);
  734. case Instructions::f32_ne.value():
  735. return binary_numeric_operation<float, i32, Operators::NotEquals>(configuration);
  736. case Instructions::f32_lt.value():
  737. return binary_numeric_operation<float, i32, Operators::LessThan>(configuration);
  738. case Instructions::f32_gt.value():
  739. return binary_numeric_operation<float, i32, Operators::GreaterThan>(configuration);
  740. case Instructions::f32_le.value():
  741. return binary_numeric_operation<float, i32, Operators::LessThanOrEquals>(configuration);
  742. case Instructions::f32_ge.value():
  743. return binary_numeric_operation<float, i32, Operators::GreaterThanOrEquals>(configuration);
  744. case Instructions::f64_eq.value():
  745. return binary_numeric_operation<double, i32, Operators::Equals>(configuration);
  746. case Instructions::f64_ne.value():
  747. return binary_numeric_operation<double, i32, Operators::NotEquals>(configuration);
  748. case Instructions::f64_lt.value():
  749. return binary_numeric_operation<double, i32, Operators::LessThan>(configuration);
  750. case Instructions::f64_gt.value():
  751. return binary_numeric_operation<double, i32, Operators::GreaterThan>(configuration);
  752. case Instructions::f64_le.value():
  753. return binary_numeric_operation<double, i32, Operators::LessThanOrEquals>(configuration);
  754. case Instructions::f64_ge.value():
  755. return binary_numeric_operation<double, i32, Operators::GreaterThanOrEquals>(configuration);
  756. case Instructions::i32_clz.value():
  757. return unary_operation<i32, i32, Operators::CountLeadingZeros>(configuration);
  758. case Instructions::i32_ctz.value():
  759. return unary_operation<i32, i32, Operators::CountTrailingZeros>(configuration);
  760. case Instructions::i32_popcnt.value():
  761. return unary_operation<i32, i32, Operators::PopCount>(configuration);
  762. case Instructions::i32_add.value():
  763. return binary_numeric_operation<u32, i32, Operators::Add>(configuration);
  764. case Instructions::i32_sub.value():
  765. return binary_numeric_operation<u32, i32, Operators::Subtract>(configuration);
  766. case Instructions::i32_mul.value():
  767. return binary_numeric_operation<u32, i32, Operators::Multiply>(configuration);
  768. case Instructions::i32_divs.value():
  769. return binary_numeric_operation<i32, i32, Operators::Divide>(configuration);
  770. case Instructions::i32_divu.value():
  771. return binary_numeric_operation<u32, i32, Operators::Divide>(configuration);
  772. case Instructions::i32_rems.value():
  773. return binary_numeric_operation<i32, i32, Operators::Modulo>(configuration);
  774. case Instructions::i32_remu.value():
  775. return binary_numeric_operation<u32, i32, Operators::Modulo>(configuration);
  776. case Instructions::i32_and.value():
  777. return binary_numeric_operation<i32, i32, Operators::BitAnd>(configuration);
  778. case Instructions::i32_or.value():
  779. return binary_numeric_operation<i32, i32, Operators::BitOr>(configuration);
  780. case Instructions::i32_xor.value():
  781. return binary_numeric_operation<i32, i32, Operators::BitXor>(configuration);
  782. case Instructions::i32_shl.value():
  783. return binary_numeric_operation<u32, i32, Operators::BitShiftLeft>(configuration);
  784. case Instructions::i32_shrs.value():
  785. return binary_numeric_operation<i32, i32, Operators::BitShiftRight>(configuration);
  786. case Instructions::i32_shru.value():
  787. return binary_numeric_operation<u32, i32, Operators::BitShiftRight>(configuration);
  788. case Instructions::i32_rotl.value():
  789. return binary_numeric_operation<u32, i32, Operators::BitRotateLeft>(configuration);
  790. case Instructions::i32_rotr.value():
  791. return binary_numeric_operation<u32, i32, Operators::BitRotateRight>(configuration);
  792. case Instructions::i64_clz.value():
  793. return unary_operation<i64, i64, Operators::CountLeadingZeros>(configuration);
  794. case Instructions::i64_ctz.value():
  795. return unary_operation<i64, i64, Operators::CountTrailingZeros>(configuration);
  796. case Instructions::i64_popcnt.value():
  797. return unary_operation<i64, i64, Operators::PopCount>(configuration);
  798. case Instructions::i64_add.value():
  799. return binary_numeric_operation<u64, i64, Operators::Add>(configuration);
  800. case Instructions::i64_sub.value():
  801. return binary_numeric_operation<u64, i64, Operators::Subtract>(configuration);
  802. case Instructions::i64_mul.value():
  803. return binary_numeric_operation<u64, i64, Operators::Multiply>(configuration);
  804. case Instructions::i64_divs.value():
  805. return binary_numeric_operation<i64, i64, Operators::Divide>(configuration);
  806. case Instructions::i64_divu.value():
  807. return binary_numeric_operation<u64, i64, Operators::Divide>(configuration);
  808. case Instructions::i64_rems.value():
  809. return binary_numeric_operation<i64, i64, Operators::Modulo>(configuration);
  810. case Instructions::i64_remu.value():
  811. return binary_numeric_operation<u64, i64, Operators::Modulo>(configuration);
  812. case Instructions::i64_and.value():
  813. return binary_numeric_operation<i64, i64, Operators::BitAnd>(configuration);
  814. case Instructions::i64_or.value():
  815. return binary_numeric_operation<i64, i64, Operators::BitOr>(configuration);
  816. case Instructions::i64_xor.value():
  817. return binary_numeric_operation<i64, i64, Operators::BitXor>(configuration);
  818. case Instructions::i64_shl.value():
  819. return binary_numeric_operation<u64, i64, Operators::BitShiftLeft>(configuration);
  820. case Instructions::i64_shrs.value():
  821. return binary_numeric_operation<i64, i64, Operators::BitShiftRight>(configuration);
  822. case Instructions::i64_shru.value():
  823. return binary_numeric_operation<u64, i64, Operators::BitShiftRight>(configuration);
  824. case Instructions::i64_rotl.value():
  825. return binary_numeric_operation<u64, i64, Operators::BitRotateLeft>(configuration);
  826. case Instructions::i64_rotr.value():
  827. return binary_numeric_operation<u64, i64, Operators::BitRotateRight>(configuration);
  828. case Instructions::f32_abs.value():
  829. return unary_operation<float, float, Operators::Absolute>(configuration);
  830. case Instructions::f32_neg.value():
  831. return unary_operation<float, float, Operators::Negate>(configuration);
  832. case Instructions::f32_ceil.value():
  833. return unary_operation<float, float, Operators::Ceil>(configuration);
  834. case Instructions::f32_floor.value():
  835. return unary_operation<float, float, Operators::Floor>(configuration);
  836. case Instructions::f32_trunc.value():
  837. return unary_operation<float, float, Operators::Truncate>(configuration);
  838. case Instructions::f32_nearest.value():
  839. return unary_operation<float, float, Operators::NearbyIntegral>(configuration);
  840. case Instructions::f32_sqrt.value():
  841. return unary_operation<float, float, Operators::SquareRoot>(configuration);
  842. case Instructions::f32_add.value():
  843. return binary_numeric_operation<float, float, Operators::Add>(configuration);
  844. case Instructions::f32_sub.value():
  845. return binary_numeric_operation<float, float, Operators::Subtract>(configuration);
  846. case Instructions::f32_mul.value():
  847. return binary_numeric_operation<float, float, Operators::Multiply>(configuration);
  848. case Instructions::f32_div.value():
  849. return binary_numeric_operation<float, float, Operators::Divide>(configuration);
  850. case Instructions::f32_min.value():
  851. return binary_numeric_operation<float, float, Operators::Minimum>(configuration);
  852. case Instructions::f32_max.value():
  853. return binary_numeric_operation<float, float, Operators::Maximum>(configuration);
  854. case Instructions::f32_copysign.value():
  855. return binary_numeric_operation<float, float, Operators::CopySign>(configuration);
  856. case Instructions::f64_abs.value():
  857. return unary_operation<double, double, Operators::Absolute>(configuration);
  858. case Instructions::f64_neg.value():
  859. return unary_operation<double, double, Operators::Negate>(configuration);
  860. case Instructions::f64_ceil.value():
  861. return unary_operation<double, double, Operators::Ceil>(configuration);
  862. case Instructions::f64_floor.value():
  863. return unary_operation<double, double, Operators::Floor>(configuration);
  864. case Instructions::f64_trunc.value():
  865. return unary_operation<double, double, Operators::Truncate>(configuration);
  866. case Instructions::f64_nearest.value():
  867. return unary_operation<double, double, Operators::NearbyIntegral>(configuration);
  868. case Instructions::f64_sqrt.value():
  869. return unary_operation<double, double, Operators::SquareRoot>(configuration);
  870. case Instructions::f64_add.value():
  871. return binary_numeric_operation<double, double, Operators::Add>(configuration);
  872. case Instructions::f64_sub.value():
  873. return binary_numeric_operation<double, double, Operators::Subtract>(configuration);
  874. case Instructions::f64_mul.value():
  875. return binary_numeric_operation<double, double, Operators::Multiply>(configuration);
  876. case Instructions::f64_div.value():
  877. return binary_numeric_operation<double, double, Operators::Divide>(configuration);
  878. case Instructions::f64_min.value():
  879. return binary_numeric_operation<double, double, Operators::Minimum>(configuration);
  880. case Instructions::f64_max.value():
  881. return binary_numeric_operation<double, double, Operators::Maximum>(configuration);
  882. case Instructions::f64_copysign.value():
  883. return binary_numeric_operation<double, double, Operators::CopySign>(configuration);
  884. case Instructions::i32_wrap_i64.value():
  885. return unary_operation<i64, i32, Operators::Wrap<i32>>(configuration);
  886. case Instructions::i32_trunc_sf32.value():
  887. return unary_operation<float, i32, Operators::CheckedTruncate<i32>>(configuration);
  888. case Instructions::i32_trunc_uf32.value():
  889. return unary_operation<float, i32, Operators::CheckedTruncate<u32>>(configuration);
  890. case Instructions::i32_trunc_sf64.value():
  891. return unary_operation<double, i32, Operators::CheckedTruncate<i32>>(configuration);
  892. case Instructions::i32_trunc_uf64.value():
  893. return unary_operation<double, i32, Operators::CheckedTruncate<u32>>(configuration);
  894. case Instructions::i64_trunc_sf32.value():
  895. return unary_operation<float, i64, Operators::CheckedTruncate<i64>>(configuration);
  896. case Instructions::i64_trunc_uf32.value():
  897. return unary_operation<float, i64, Operators::CheckedTruncate<u64>>(configuration);
  898. case Instructions::i64_trunc_sf64.value():
  899. return unary_operation<double, i64, Operators::CheckedTruncate<i64>>(configuration);
  900. case Instructions::i64_trunc_uf64.value():
  901. return unary_operation<double, i64, Operators::CheckedTruncate<u64>>(configuration);
  902. case Instructions::i64_extend_si32.value():
  903. return unary_operation<i32, i64, Operators::Extend<i64>>(configuration);
  904. case Instructions::i64_extend_ui32.value():
  905. return unary_operation<u32, i64, Operators::Extend<i64>>(configuration);
  906. case Instructions::f32_convert_si32.value():
  907. return unary_operation<i32, float, Operators::Convert<float>>(configuration);
  908. case Instructions::f32_convert_ui32.value():
  909. return unary_operation<u32, float, Operators::Convert<float>>(configuration);
  910. case Instructions::f32_convert_si64.value():
  911. return unary_operation<i64, float, Operators::Convert<float>>(configuration);
  912. case Instructions::f32_convert_ui64.value():
  913. return unary_operation<u64, float, Operators::Convert<float>>(configuration);
  914. case Instructions::f32_demote_f64.value():
  915. return unary_operation<double, float, Operators::Demote>(configuration);
  916. case Instructions::f64_convert_si32.value():
  917. return unary_operation<i32, double, Operators::Convert<double>>(configuration);
  918. case Instructions::f64_convert_ui32.value():
  919. return unary_operation<u32, double, Operators::Convert<double>>(configuration);
  920. case Instructions::f64_convert_si64.value():
  921. return unary_operation<i64, double, Operators::Convert<double>>(configuration);
  922. case Instructions::f64_convert_ui64.value():
  923. return unary_operation<u64, double, Operators::Convert<double>>(configuration);
  924. case Instructions::f64_promote_f32.value():
  925. return unary_operation<float, double, Operators::Promote>(configuration);
  926. case Instructions::i32_reinterpret_f32.value():
  927. return unary_operation<float, i32, Operators::Reinterpret<i32>>(configuration);
  928. case Instructions::i64_reinterpret_f64.value():
  929. return unary_operation<double, i64, Operators::Reinterpret<i64>>(configuration);
  930. case Instructions::f32_reinterpret_i32.value():
  931. return unary_operation<i32, float, Operators::Reinterpret<float>>(configuration);
  932. case Instructions::f64_reinterpret_i64.value():
  933. return unary_operation<i64, double, Operators::Reinterpret<double>>(configuration);
  934. case Instructions::i32_extend8_s.value():
  935. return unary_operation<i32, i32, Operators::SignExtend<i8>>(configuration);
  936. case Instructions::i32_extend16_s.value():
  937. return unary_operation<i32, i32, Operators::SignExtend<i16>>(configuration);
  938. case Instructions::i64_extend8_s.value():
  939. return unary_operation<i64, i64, Operators::SignExtend<i8>>(configuration);
  940. case Instructions::i64_extend16_s.value():
  941. return unary_operation<i64, i64, Operators::SignExtend<i16>>(configuration);
  942. case Instructions::i64_extend32_s.value():
  943. return unary_operation<i64, i64, Operators::SignExtend<i32>>(configuration);
  944. case Instructions::i32_trunc_sat_f32_s.value():
  945. return unary_operation<float, i32, Operators::SaturatingTruncate<i32>>(configuration);
  946. case Instructions::i32_trunc_sat_f32_u.value():
  947. return unary_operation<float, i32, Operators::SaturatingTruncate<u32>>(configuration);
  948. case Instructions::i32_trunc_sat_f64_s.value():
  949. return unary_operation<double, i32, Operators::SaturatingTruncate<i32>>(configuration);
  950. case Instructions::i32_trunc_sat_f64_u.value():
  951. return unary_operation<double, i32, Operators::SaturatingTruncate<u32>>(configuration);
  952. case Instructions::i64_trunc_sat_f32_s.value():
  953. return unary_operation<float, i64, Operators::SaturatingTruncate<i64>>(configuration);
  954. case Instructions::i64_trunc_sat_f32_u.value():
  955. return unary_operation<float, i64, Operators::SaturatingTruncate<u64>>(configuration);
  956. case Instructions::i64_trunc_sat_f64_s.value():
  957. return unary_operation<double, i64, Operators::SaturatingTruncate<i64>>(configuration);
  958. case Instructions::i64_trunc_sat_f64_u.value():
  959. return unary_operation<double, i64, Operators::SaturatingTruncate<u64>>(configuration);
  960. case Instructions::table_init.value():
  961. case Instructions::elem_drop.value():
  962. case Instructions::table_copy.value():
  963. case Instructions::table_grow.value():
  964. case Instructions::table_size.value():
  965. case Instructions::table_fill.value():
  966. default:
  967. unimplemented:;
  968. dbgln("Instruction '{}' not implemented", instruction_name(instruction.opcode()));
  969. m_trap = Trap { DeprecatedString::formatted("Unimplemented instruction {}", instruction_name(instruction.opcode())) };
  970. return;
  971. }
  972. }
  973. void DebuggerBytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  974. {
  975. if (pre_interpret_hook) {
  976. auto result = pre_interpret_hook(configuration, ip, instruction);
  977. if (!result) {
  978. m_trap = Trap { "Trapped by user request" };
  979. return;
  980. }
  981. }
  982. BytecodeInterpreter::interpret(configuration, ip, instruction);
  983. if (post_interpret_hook) {
  984. auto result = post_interpret_hook(configuration, ip, instruction, *this);
  985. if (!result) {
  986. m_trap = Trap { "Trapped by user request" };
  987. return;
  988. }
  989. }
  990. }
  991. }