BytecodeInterpreter.cpp 47 KB

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