BytecodeInterpreter.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 InputT, typename OutputT>
  354. ALWAYS_INLINE static OutputT extend_signed(InputT value)
  355. {
  356. // Note: C++ will take care of sign extension.
  357. return value;
  358. }
  359. template<typename TruncT, typename T>
  360. ALWAYS_INLINE static TruncT saturating_truncate(T value)
  361. {
  362. if (isnan(value))
  363. return 0;
  364. if (isinf(value)) {
  365. if (value < 0)
  366. return NumericLimits<TruncT>::min();
  367. return NumericLimits<TruncT>::max();
  368. }
  369. constexpr auto convert = [](auto truncated_value) {
  370. if (truncated_value < NumericLimits<TruncT>::min())
  371. return NumericLimits<TruncT>::min();
  372. if (static_cast<double>(truncated_value) > static_cast<double>(NumericLimits<TruncT>::max()))
  373. return NumericLimits<TruncT>::max();
  374. return static_cast<TruncT>(truncated_value);
  375. };
  376. if constexpr (IsSame<T, float>)
  377. return convert(truncf(value));
  378. else
  379. return convert(trunc(value));
  380. }
  381. template<typename T>
  382. ALWAYS_INLINE static T float_max(T lhs, T rhs)
  383. {
  384. if (isnan(lhs))
  385. return lhs;
  386. if (isnan(rhs))
  387. return rhs;
  388. if (isinf(lhs))
  389. return lhs > 0 ? lhs : rhs;
  390. if (isinf(rhs))
  391. return rhs > 0 ? rhs : lhs;
  392. return max(lhs, rhs);
  393. }
  394. template<typename T>
  395. ALWAYS_INLINE static T float_min(T lhs, T rhs)
  396. {
  397. if (isnan(lhs))
  398. return lhs;
  399. if (isnan(rhs))
  400. return rhs;
  401. if (isinf(lhs))
  402. return lhs > 0 ? rhs : lhs;
  403. if (isinf(rhs))
  404. return rhs > 0 ? lhs : rhs;
  405. return min(lhs, rhs);
  406. }
  407. void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  408. {
  409. dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value());
  410. switch (instruction.opcode().value()) {
  411. case Instructions::unreachable.value():
  412. m_do_trap = true;
  413. return;
  414. case Instructions::nop.value():
  415. return;
  416. case Instructions::local_get.value():
  417. configuration.stack().push(Value(configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()]));
  418. return;
  419. case Instructions::local_set.value(): {
  420. auto entry = configuration.stack().pop();
  421. configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()] = move(entry.get<Value>());
  422. return;
  423. }
  424. case Instructions::i32_const.value():
  425. configuration.stack().push(Value(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
  426. return;
  427. case Instructions::i64_const.value():
  428. configuration.stack().push(Value(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
  429. return;
  430. case Instructions::f32_const.value():
  431. configuration.stack().push(Value(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>())));
  432. return;
  433. case Instructions::f64_const.value():
  434. configuration.stack().push(Value(ValueType { ValueType::F64 }, instruction.arguments().get<double>()));
  435. return;
  436. case Instructions::block.value(): {
  437. size_t arity = 0;
  438. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  439. if (args.block_type.kind() != BlockType::Empty)
  440. arity = 1;
  441. configuration.stack().push(Label(arity, args.end_ip));
  442. return;
  443. }
  444. case Instructions::loop.value(): {
  445. size_t arity = 0;
  446. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  447. if (args.block_type.kind() != BlockType::Empty)
  448. arity = 1;
  449. configuration.stack().push(Label(arity, ip.value() + 1));
  450. return;
  451. }
  452. case Instructions::if_.value(): {
  453. size_t arity = 0;
  454. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  455. if (args.block_type.kind() != BlockType::Empty)
  456. arity = 1;
  457. auto entry = configuration.stack().pop();
  458. auto value = entry.get<Value>().to<i32>();
  459. TRAP_IF_NOT(value.has_value());
  460. auto end_label = Label(arity, args.end_ip.value());
  461. if (value.value() == 0) {
  462. if (args.else_ip.has_value()) {
  463. configuration.ip() = args.else_ip.value();
  464. configuration.stack().push(move(end_label));
  465. } else {
  466. configuration.ip() = args.end_ip.value() + 1;
  467. }
  468. } else {
  469. configuration.stack().push(move(end_label));
  470. }
  471. return;
  472. }
  473. case Instructions::structured_end.value():
  474. case Instructions::structured_else.value(): {
  475. auto label = configuration.nth_label(0);
  476. TRAP_IF_NOT(label.has_value());
  477. size_t end = configuration.stack().size() - label->arity() - 1;
  478. size_t start = end;
  479. while (start > 0 && start < configuration.stack().size() && !configuration.stack().entries()[start].has<Label>())
  480. --start;
  481. configuration.stack().entries().remove(start, end - start + 1);
  482. if (instruction.opcode() == Instructions::structured_end)
  483. return;
  484. // Jump to the end label
  485. configuration.ip() = label->continuation();
  486. return;
  487. }
  488. case Instructions::return_.value(): {
  489. auto& frame = configuration.frame();
  490. size_t end = configuration.stack().size() - frame.arity();
  491. size_t start = end;
  492. for (; start + 1 > 0 && start < configuration.stack().size(); --start) {
  493. auto& entry = configuration.stack().entries()[start];
  494. if (entry.has<Frame>()) {
  495. // Leave the frame, _and_ its label.
  496. start += 2;
  497. break;
  498. }
  499. }
  500. configuration.stack().entries().remove(start, end - start);
  501. // Jump past the call/indirect instruction
  502. configuration.ip() = configuration.frame().expression().instructions().size();
  503. return;
  504. }
  505. case Instructions::br.value():
  506. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  507. case Instructions::br_if.value(): {
  508. if (configuration.stack().pop().get<Value>().to<i32>().value_or(0) == 0)
  509. return;
  510. return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
  511. }
  512. case Instructions::br_table.value(): {
  513. auto& arguments = instruction.arguments().get<Instruction::TableBranchArgs>();
  514. auto entry = configuration.stack().pop();
  515. TRAP_IF_NOT(entry.has<Value>());
  516. auto maybe_i = entry.get<Value>().to<i32>();
  517. TRAP_IF_NOT(maybe_i.has_value());
  518. TRAP_IF_NOT(maybe_i.value() >= 0);
  519. size_t i = *maybe_i;
  520. if (i < arguments.labels.size())
  521. return branch_to_label(configuration, arguments.labels[i]);
  522. return branch_to_label(configuration, arguments.default_);
  523. }
  524. case Instructions::call.value(): {
  525. auto index = instruction.arguments().get<FunctionIndex>();
  526. TRAP_IF_NOT(index.value() < configuration.frame().module().functions().size());
  527. auto address = configuration.frame().module().functions()[index.value()];
  528. dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value());
  529. call_address(configuration, address);
  530. return;
  531. }
  532. case Instructions::call_indirect.value(): {
  533. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  534. TRAP_IF_NOT(args.table.value() < configuration.frame().module().tables().size());
  535. auto table_address = configuration.frame().module().tables()[args.table.value()];
  536. auto table_instance = configuration.store().get(table_address);
  537. auto index = configuration.stack().pop().get<Value>().to<i32>();
  538. TRAP_IF_NOT(index.has_value());
  539. TRAP_IF_NOT(index.value() >= 0);
  540. TRAP_IF_NOT(static_cast<size_t>(index.value()) < table_instance->elements().size());
  541. auto element = table_instance->elements()[index.value()];
  542. TRAP_IF_NOT(element.has_value());
  543. TRAP_IF_NOT(element->ref().has<Reference::Func>());
  544. auto address = element->ref().get<Reference::Func>().address;
  545. dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index.value(), address.value());
  546. call_address(configuration, address);
  547. return;
  548. }
  549. case Instructions::i32_load.value():
  550. LOAD_AND_PUSH(i32, i32);
  551. case Instructions::i64_load.value():
  552. LOAD_AND_PUSH(i64, i64);
  553. case Instructions::f32_load.value():
  554. LOAD_AND_PUSH(float, float);
  555. case Instructions::f64_load.value():
  556. LOAD_AND_PUSH(double, double);
  557. case Instructions::i32_load8_s.value():
  558. LOAD_AND_PUSH(i8, i32);
  559. case Instructions::i32_load8_u.value():
  560. LOAD_AND_PUSH(u8, i32);
  561. case Instructions::i32_load16_s.value():
  562. LOAD_AND_PUSH(i16, i32);
  563. case Instructions::i32_load16_u.value():
  564. LOAD_AND_PUSH(u16, i32);
  565. case Instructions::i64_load8_s.value():
  566. LOAD_AND_PUSH(i8, i64);
  567. case Instructions::i64_load8_u.value():
  568. LOAD_AND_PUSH(u8, i64);
  569. case Instructions::i64_load16_s.value():
  570. LOAD_AND_PUSH(i16, i64);
  571. case Instructions::i64_load16_u.value():
  572. LOAD_AND_PUSH(u16, i64);
  573. case Instructions::i64_load32_s.value():
  574. LOAD_AND_PUSH(i32, i64);
  575. case Instructions::i64_load32_u.value():
  576. LOAD_AND_PUSH(u32, i64);
  577. case Instructions::i32_store.value():
  578. POP_AND_STORE(i32, i32);
  579. case Instructions::i64_store.value():
  580. POP_AND_STORE(i64, i64);
  581. case Instructions::f32_store.value():
  582. POP_AND_STORE(float, float);
  583. case Instructions::f64_store.value():
  584. POP_AND_STORE(double, double);
  585. case Instructions::i32_store8.value():
  586. POP_AND_STORE(i32, i8);
  587. case Instructions::i32_store16.value():
  588. POP_AND_STORE(i32, i16);
  589. case Instructions::i64_store8.value():
  590. POP_AND_STORE(i64, i8);
  591. case Instructions::i64_store16.value():
  592. POP_AND_STORE(i64, i16);
  593. case Instructions::i64_store32.value():
  594. POP_AND_STORE(i64, i32);
  595. case Instructions::local_tee.value(): {
  596. auto value = configuration.stack().peek().get<Value>();
  597. auto local_index = instruction.arguments().get<LocalIndex>();
  598. TRAP_IF_NOT(configuration.frame().locals().size() > local_index.value());
  599. dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
  600. configuration.frame().locals()[local_index.value()] = move(value);
  601. return;
  602. }
  603. case Instructions::global_get.value(): {
  604. auto global_index = instruction.arguments().get<GlobalIndex>();
  605. TRAP_IF_NOT(configuration.frame().module().globals().size() > global_index.value());
  606. auto address = configuration.frame().module().globals()[global_index.value()];
  607. dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
  608. auto global = configuration.store().get(address);
  609. configuration.stack().push(Value(global->value()));
  610. return;
  611. }
  612. case Instructions::global_set.value(): {
  613. auto global_index = instruction.arguments().get<GlobalIndex>();
  614. TRAP_IF_NOT(configuration.frame().module().globals().size() > global_index.value());
  615. auto address = configuration.frame().module().globals()[global_index.value()];
  616. auto value = configuration.stack().pop().get<Value>();
  617. dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
  618. auto global = configuration.store().get(address);
  619. global->set_value(move(value));
  620. return;
  621. }
  622. case Instructions::memory_size.value(): {
  623. TRAP_IF_NOT(configuration.frame().module().memories().size() > 0);
  624. auto address = configuration.frame().module().memories()[0];
  625. auto instance = configuration.store().get(address);
  626. auto pages = instance->size() / Constants::page_size;
  627. dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
  628. configuration.stack().push(Value((i32)pages));
  629. return;
  630. }
  631. case Instructions::memory_grow.value(): {
  632. TRAP_IF_NOT(configuration.frame().module().memories().size() > 0);
  633. auto address = configuration.frame().module().memories()[0];
  634. auto instance = configuration.store().get(address);
  635. i32 old_pages = instance->size() / Constants::page_size;
  636. auto new_pages = configuration.stack().peek().get<Value>().to<i32>();
  637. TRAP_IF_NOT(new_pages.has_value());
  638. dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
  639. if (instance->grow(new_pages.value() * Constants::page_size))
  640. configuration.stack().peek() = Value((i32)old_pages);
  641. else
  642. configuration.stack().peek() = Value((i32)-1);
  643. return;
  644. }
  645. case Instructions::table_get.value():
  646. case Instructions::table_set.value():
  647. goto unimplemented;
  648. case Instructions::ref_null.value(): {
  649. auto type = instruction.arguments().get<ValueType>();
  650. TRAP_IF_NOT(type.is_reference());
  651. configuration.stack().push(Value(Reference(Reference::Null { type })));
  652. return;
  653. };
  654. case Instructions::ref_func.value(): {
  655. auto index = instruction.arguments().get<FunctionIndex>().value();
  656. auto& functions = configuration.frame().module().functions();
  657. TRAP_IF_NOT(functions.size() > index);
  658. auto address = functions[index];
  659. configuration.stack().push(Value(ValueType(ValueType::FunctionReference), address.value()));
  660. return;
  661. }
  662. case Instructions::ref_is_null.value(): {
  663. auto top = configuration.stack().peek().get_pointer<Value>();
  664. TRAP_IF_NOT(top);
  665. TRAP_IF_NOT(top->type().is_reference());
  666. auto is_null = top->to<Reference::Null>().has_value();
  667. configuration.stack().peek() = Value(ValueType(ValueType::I32), static_cast<u64>(is_null ? 1 : 0));
  668. return;
  669. }
  670. case Instructions::drop.value():
  671. configuration.stack().pop();
  672. return;
  673. case Instructions::select.value():
  674. case Instructions::select_typed.value(): {
  675. // Note: The type seems to only be used for validation.
  676. auto value = configuration.stack().pop().get<Value>().to<i32>();
  677. TRAP_IF_NOT(value.has_value());
  678. dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
  679. auto rhs = move(configuration.stack().pop().get<Value>());
  680. auto lhs = move(configuration.stack().peek().get<Value>());
  681. configuration.stack().peek() = value.value() != 0 ? move(lhs) : move(rhs);
  682. return;
  683. }
  684. case Instructions::i32_eqz.value():
  685. UNARY_NUMERIC_OPERATION(i32, 0 ==);
  686. case Instructions::i32_eq.value():
  687. BINARY_NUMERIC_OPERATION(i32, ==, i32);
  688. case Instructions::i32_ne.value():
  689. BINARY_NUMERIC_OPERATION(i32, !=, i32);
  690. case Instructions::i32_lts.value():
  691. BINARY_NUMERIC_OPERATION(i32, <, i32);
  692. case Instructions::i32_ltu.value():
  693. BINARY_NUMERIC_OPERATION(u32, <, i32);
  694. case Instructions::i32_gts.value():
  695. BINARY_NUMERIC_OPERATION(i32, >, i32);
  696. case Instructions::i32_gtu.value():
  697. BINARY_NUMERIC_OPERATION(u32, >, i32);
  698. case Instructions::i32_les.value():
  699. BINARY_NUMERIC_OPERATION(i32, <=, i32);
  700. case Instructions::i32_leu.value():
  701. BINARY_NUMERIC_OPERATION(u32, <=, i32);
  702. case Instructions::i32_ges.value():
  703. BINARY_NUMERIC_OPERATION(i32, >=, i32);
  704. case Instructions::i32_geu.value():
  705. BINARY_NUMERIC_OPERATION(u32, >=, i32);
  706. case Instructions::i64_eqz.value():
  707. UNARY_NUMERIC_OPERATION(i64, 0ull ==);
  708. case Instructions::i64_eq.value():
  709. BINARY_NUMERIC_OPERATION(i64, ==, i32);
  710. case Instructions::i64_ne.value():
  711. BINARY_NUMERIC_OPERATION(i64, !=, i32);
  712. case Instructions::i64_lts.value():
  713. BINARY_NUMERIC_OPERATION(i64, <, i32);
  714. case Instructions::i64_ltu.value():
  715. BINARY_NUMERIC_OPERATION(u64, <, i32);
  716. case Instructions::i64_gts.value():
  717. BINARY_NUMERIC_OPERATION(i64, >, i32);
  718. case Instructions::i64_gtu.value():
  719. BINARY_NUMERIC_OPERATION(u64, >, i32);
  720. case Instructions::i64_les.value():
  721. BINARY_NUMERIC_OPERATION(i64, <=, i32);
  722. case Instructions::i64_leu.value():
  723. BINARY_NUMERIC_OPERATION(u64, <=, i32);
  724. case Instructions::i64_ges.value():
  725. BINARY_NUMERIC_OPERATION(i64, >=, i32);
  726. case Instructions::i64_geu.value():
  727. BINARY_NUMERIC_OPERATION(u64, >=, i32);
  728. case Instructions::f32_eq.value():
  729. BINARY_NUMERIC_OPERATION(float, ==, i32);
  730. case Instructions::f32_ne.value():
  731. BINARY_NUMERIC_OPERATION(float, !=, i32);
  732. case Instructions::f32_lt.value():
  733. BINARY_NUMERIC_OPERATION(float, <, i32);
  734. case Instructions::f32_gt.value():
  735. BINARY_NUMERIC_OPERATION(float, >, i32);
  736. case Instructions::f32_le.value():
  737. BINARY_NUMERIC_OPERATION(float, <=, i32);
  738. case Instructions::f32_ge.value():
  739. BINARY_NUMERIC_OPERATION(float, >=, i32);
  740. case Instructions::f64_eq.value():
  741. BINARY_NUMERIC_OPERATION(double, ==, i32);
  742. case Instructions::f64_ne.value():
  743. BINARY_NUMERIC_OPERATION(double, !=, i32);
  744. case Instructions::f64_lt.value():
  745. BINARY_NUMERIC_OPERATION(double, <, i32);
  746. case Instructions::f64_gt.value():
  747. BINARY_NUMERIC_OPERATION(double, >, i32);
  748. case Instructions::f64_le.value():
  749. BINARY_NUMERIC_OPERATION(double, <=, i32);
  750. case Instructions::f64_ge.value():
  751. BINARY_NUMERIC_OPERATION(double, >, i32);
  752. case Instructions::i32_clz.value():
  753. UNARY_NUMERIC_OPERATION(i32, clz);
  754. case Instructions::i32_ctz.value():
  755. UNARY_NUMERIC_OPERATION(i32, ctz);
  756. case Instructions::i32_popcnt.value():
  757. UNARY_NUMERIC_OPERATION(i32, __builtin_popcount);
  758. case Instructions::i32_add.value():
  759. BINARY_NUMERIC_OPERATION(i32, +, i32);
  760. case Instructions::i32_sub.value():
  761. BINARY_NUMERIC_OPERATION(i32, -, i32);
  762. case Instructions::i32_mul.value():
  763. BINARY_NUMERIC_OPERATION(i32, *, i32);
  764. case Instructions::i32_divs.value():
  765. BINARY_NUMERIC_OPERATION(i32, /, i32, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
  766. case Instructions::i32_divu.value():
  767. BINARY_NUMERIC_OPERATION(u32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
  768. case Instructions::i32_rems.value():
  769. BINARY_NUMERIC_OPERATION(i32, %, i32, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
  770. case Instructions::i32_remu.value():
  771. BINARY_NUMERIC_OPERATION(u32, %, i32, TRAP_IF_NOT(rhs.value() != 0));
  772. case Instructions::i32_and.value():
  773. BINARY_NUMERIC_OPERATION(i32, &, i32);
  774. case Instructions::i32_or.value():
  775. BINARY_NUMERIC_OPERATION(i32, |, i32);
  776. case Instructions::i32_xor.value():
  777. BINARY_NUMERIC_OPERATION(i32, ^, i32);
  778. case Instructions::i32_shl.value():
  779. BINARY_NUMERIC_OPERATION(i32, <<, i32);
  780. case Instructions::i32_shrs.value():
  781. BINARY_NUMERIC_OPERATION(i32, >>, i32);
  782. case Instructions::i32_shru.value():
  783. BINARY_NUMERIC_OPERATION(u32, >>, i32);
  784. case Instructions::i32_rotl.value():
  785. BINARY_PREFIX_NUMERIC_OPERATION(u32, rotl, i32);
  786. case Instructions::i32_rotr.value():
  787. BINARY_PREFIX_NUMERIC_OPERATION(u32, rotr, i32);
  788. case Instructions::i64_clz.value():
  789. UNARY_NUMERIC_OPERATION(i64, clz);
  790. case Instructions::i64_ctz.value():
  791. UNARY_NUMERIC_OPERATION(i64, ctz);
  792. case Instructions::i64_popcnt.value():
  793. UNARY_NUMERIC_OPERATION(i64, __builtin_popcountll);
  794. case Instructions::i64_add.value():
  795. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, +, i64);
  796. case Instructions::i64_sub.value():
  797. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, -, i64);
  798. case Instructions::i64_mul.value():
  799. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, *, i64);
  800. case Instructions::i64_divs.value():
  801. OVF_CHECKED_BINARY_NUMERIC_OPERATION(i64, /, i64, TRAP_IF_NOT(rhs.value() != 0));
  802. case Instructions::i64_divu.value():
  803. OVF_CHECKED_BINARY_NUMERIC_OPERATION(u64, /, i64, TRAP_IF_NOT(rhs.value() != 0));
  804. case Instructions::i64_rems.value():
  805. BINARY_NUMERIC_OPERATION(i64, %, i64, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
  806. case Instructions::i64_remu.value():
  807. BINARY_NUMERIC_OPERATION(u64, %, i64, TRAP_IF_NOT(rhs.value() != 0));
  808. case Instructions::i64_and.value():
  809. BINARY_NUMERIC_OPERATION(i64, &, i64);
  810. case Instructions::i64_or.value():
  811. BINARY_NUMERIC_OPERATION(i64, |, i64);
  812. case Instructions::i64_xor.value():
  813. BINARY_NUMERIC_OPERATION(i64, ^, i64);
  814. case Instructions::i64_shl.value():
  815. BINARY_NUMERIC_OPERATION(i64, <<, i64);
  816. case Instructions::i64_shrs.value():
  817. BINARY_NUMERIC_OPERATION(i64, >>, i64);
  818. case Instructions::i64_shru.value():
  819. BINARY_NUMERIC_OPERATION(u64, >>, i64);
  820. case Instructions::i64_rotl.value():
  821. BINARY_PREFIX_NUMERIC_OPERATION(u64, rotl, i64);
  822. case Instructions::i64_rotr.value():
  823. BINARY_PREFIX_NUMERIC_OPERATION(u64, rotr, i64);
  824. case Instructions::f32_abs.value():
  825. UNARY_NUMERIC_OPERATION(float, fabsf);
  826. case Instructions::f32_neg.value():
  827. UNARY_NUMERIC_OPERATION(float, -);
  828. case Instructions::f32_ceil.value():
  829. UNARY_NUMERIC_OPERATION(float, ceilf);
  830. case Instructions::f32_floor.value():
  831. UNARY_NUMERIC_OPERATION(float, floorf);
  832. case Instructions::f32_trunc.value():
  833. UNARY_NUMERIC_OPERATION(float, truncf);
  834. case Instructions::f32_nearest.value():
  835. UNARY_NUMERIC_OPERATION(float, roundf);
  836. case Instructions::f32_sqrt.value():
  837. UNARY_NUMERIC_OPERATION(float, sqrtf);
  838. case Instructions::f32_add.value():
  839. BINARY_NUMERIC_OPERATION(float, +, float);
  840. case Instructions::f32_sub.value():
  841. BINARY_NUMERIC_OPERATION(float, -, float);
  842. case Instructions::f32_mul.value():
  843. BINARY_NUMERIC_OPERATION(float, *, float);
  844. case Instructions::f32_div.value():
  845. BINARY_NUMERIC_OPERATION(float, /, float);
  846. case Instructions::f32_min.value():
  847. BINARY_PREFIX_NUMERIC_OPERATION(float, float_min, float);
  848. case Instructions::f32_max.value():
  849. BINARY_PREFIX_NUMERIC_OPERATION(float, float_max, float);
  850. case Instructions::f32_copysign.value():
  851. BINARY_PREFIX_NUMERIC_OPERATION(float, copysignf, float);
  852. case Instructions::f64_abs.value():
  853. UNARY_NUMERIC_OPERATION(double, fabs);
  854. case Instructions::f64_neg.value():
  855. UNARY_NUMERIC_OPERATION(double, -);
  856. case Instructions::f64_ceil.value():
  857. UNARY_NUMERIC_OPERATION(double, ceil);
  858. case Instructions::f64_floor.value():
  859. UNARY_NUMERIC_OPERATION(double, floor);
  860. case Instructions::f64_trunc.value():
  861. UNARY_NUMERIC_OPERATION(double, trunc);
  862. case Instructions::f64_nearest.value():
  863. UNARY_NUMERIC_OPERATION(double, round);
  864. case Instructions::f64_sqrt.value():
  865. UNARY_NUMERIC_OPERATION(double, sqrt);
  866. case Instructions::f64_add.value():
  867. BINARY_NUMERIC_OPERATION(double, +, double);
  868. case Instructions::f64_sub.value():
  869. BINARY_NUMERIC_OPERATION(double, -, double);
  870. case Instructions::f64_mul.value():
  871. BINARY_NUMERIC_OPERATION(double, *, double);
  872. case Instructions::f64_div.value():
  873. BINARY_NUMERIC_OPERATION(double, /, double);
  874. case Instructions::f64_min.value():
  875. BINARY_PREFIX_NUMERIC_OPERATION(double, float_min, double);
  876. case Instructions::f64_max.value():
  877. BINARY_PREFIX_NUMERIC_OPERATION(double, float_max, double);
  878. case Instructions::f64_copysign.value():
  879. BINARY_PREFIX_NUMERIC_OPERATION(double, copysign, double);
  880. case Instructions::i32_wrap_i64.value():
  881. UNARY_MAP(i64, i32, i32);
  882. case Instructions::i32_trunc_sf32.value(): {
  883. auto fn = [this](auto& v) { return checked_signed_truncate<float, i32>(v); };
  884. UNARY_MAP(float, fn, i32);
  885. }
  886. case Instructions::i32_trunc_uf32.value(): {
  887. auto fn = [this](auto& value) { return checked_unsigned_truncate<float, i32>(value); };
  888. UNARY_MAP(float, fn, i32);
  889. }
  890. case Instructions::i32_trunc_sf64.value(): {
  891. auto fn = [this](auto& value) { return checked_signed_truncate<double, i32>(value); };
  892. UNARY_MAP(double, fn, i32);
  893. }
  894. case Instructions::i32_trunc_uf64.value(): {
  895. auto fn = [this](auto& value) { return checked_unsigned_truncate<double, i32>(value); };
  896. UNARY_MAP(double, fn, i32);
  897. }
  898. case Instructions::i64_trunc_sf32.value(): {
  899. auto fn = [this](auto& value) { return checked_signed_truncate<float, i64>(value); };
  900. UNARY_MAP(float, fn, i64);
  901. }
  902. case Instructions::i64_trunc_uf32.value(): {
  903. auto fn = [this](auto& value) { return checked_unsigned_truncate<float, i64>(value); };
  904. UNARY_MAP(float, fn, i64);
  905. }
  906. case Instructions::i64_trunc_sf64.value(): {
  907. auto fn = [this](auto& value) { return checked_signed_truncate<double, i64>(value); };
  908. UNARY_MAP(double, fn, i64);
  909. }
  910. case Instructions::i64_trunc_uf64.value(): {
  911. auto fn = [this](auto& value) { return checked_unsigned_truncate<double, i64>(value); };
  912. UNARY_MAP(double, fn, i64);
  913. }
  914. case Instructions::i64_extend_si32.value():
  915. UNARY_MAP(i32, i64, i64);
  916. case Instructions::i64_extend_ui32.value():
  917. UNARY_MAP(u32, i64, i64);
  918. case Instructions::f32_convert_si32.value():
  919. UNARY_MAP(i32, float, float);
  920. case Instructions::f32_convert_ui32.value():
  921. UNARY_MAP(u32, float, float);
  922. case Instructions::f32_convert_si64.value():
  923. UNARY_MAP(i64, float, float);
  924. case Instructions::f32_convert_ui64.value():
  925. UNARY_MAP(u32, float, float);
  926. case Instructions::f32_demote_f64.value():
  927. UNARY_MAP(double, float, float);
  928. case Instructions::f64_convert_si32.value():
  929. UNARY_MAP(i32, double, double);
  930. case Instructions::f64_convert_ui32.value():
  931. UNARY_MAP(u32, double, double);
  932. case Instructions::f64_convert_si64.value():
  933. UNARY_MAP(i64, double, double);
  934. case Instructions::f64_convert_ui64.value():
  935. UNARY_MAP(u64, double, double);
  936. case Instructions::f64_promote_f32.value():
  937. UNARY_MAP(float, double, double);
  938. case Instructions::i32_reinterpret_f32.value():
  939. UNARY_MAP(float, bit_cast<i32>, i32);
  940. case Instructions::i64_reinterpret_f64.value():
  941. UNARY_MAP(double, bit_cast<i64>, i64);
  942. case Instructions::f32_reinterpret_i32.value():
  943. UNARY_MAP(i32, bit_cast<float>, float);
  944. case Instructions::f64_reinterpret_i64.value():
  945. UNARY_MAP(i64, bit_cast<double>, double);
  946. case Instructions::i32_extend8_s.value():
  947. UNARY_MAP(i32, (extend_signed<i8, i32>), i32);
  948. case Instructions::i32_extend16_s.value():
  949. UNARY_MAP(i32, (extend_signed<i16, i32>), i32);
  950. case Instructions::i64_extend8_s.value():
  951. UNARY_MAP(i64, (extend_signed<i8, i64>), i64);
  952. case Instructions::i64_extend16_s.value():
  953. UNARY_MAP(i64, (extend_signed<i16, i64>), i64);
  954. case Instructions::i64_extend32_s.value():
  955. UNARY_MAP(i64, (extend_signed<i32, i64>), i64);
  956. case Instructions::i32_trunc_sat_f32_s.value():
  957. UNARY_MAP(float, saturating_truncate<i32>, i32);
  958. case Instructions::i32_trunc_sat_f32_u.value():
  959. UNARY_MAP(float, saturating_truncate<u32>, i32);
  960. case Instructions::i32_trunc_sat_f64_s.value():
  961. UNARY_MAP(double, saturating_truncate<i32>, i32);
  962. case Instructions::i32_trunc_sat_f64_u.value():
  963. UNARY_MAP(double, saturating_truncate<u32>, i32);
  964. case Instructions::i64_trunc_sat_f32_s.value():
  965. UNARY_MAP(float, saturating_truncate<i64>, i64);
  966. case Instructions::i64_trunc_sat_f32_u.value():
  967. UNARY_MAP(float, saturating_truncate<u64>, i64);
  968. case Instructions::i64_trunc_sat_f64_s.value():
  969. UNARY_MAP(double, saturating_truncate<i64>, i64);
  970. case Instructions::i64_trunc_sat_f64_u.value():
  971. UNARY_MAP(double, saturating_truncate<u64>, i64);
  972. case Instructions::memory_init.value():
  973. case Instructions::data_drop.value():
  974. case Instructions::memory_copy.value():
  975. case Instructions::memory_fill.value():
  976. case Instructions::table_init.value():
  977. case Instructions::elem_drop.value():
  978. case Instructions::table_copy.value():
  979. case Instructions::table_grow.value():
  980. case Instructions::table_size.value():
  981. case Instructions::table_fill.value():
  982. default:
  983. unimplemented:;
  984. dbgln("Instruction '{}' not implemented", instruction_name(instruction.opcode()));
  985. m_do_trap = true;
  986. return;
  987. }
  988. }
  989. void DebuggerBytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
  990. {
  991. if (pre_interpret_hook) {
  992. auto result = pre_interpret_hook(configuration, ip, instruction);
  993. if (!result) {
  994. m_do_trap = true;
  995. return;
  996. }
  997. }
  998. ScopeGuard guard { [&] {
  999. if (post_interpret_hook) {
  1000. auto result = post_interpret_hook(configuration, ip, instruction, *this);
  1001. if (!result) {
  1002. m_do_trap = true;
  1003. return;
  1004. }
  1005. }
  1006. } };
  1007. BytecodeInterpreter::interpret(configuration, ip, instruction);
  1008. }
  1009. }