BytecodeInterpreter.cpp 48 KB

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