BytecodeInterpreter.cpp 47 KB

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