BytecodeInterpreter.cpp 50 KB

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