BytecodeInterpreter.cpp 44 KB

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