BytecodeInterpreter.cpp 50 KB

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