Configuration.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWasm/AbstractMachine/Configuration.h>
  7. #include <LibWasm/AbstractMachine/Interpreter.h>
  8. namespace Wasm {
  9. Optional<Label> Configuration::nth_label(size_t i)
  10. {
  11. for (auto& entry : m_stack.entries()) {
  12. if (auto ptr = entry.get_pointer<NonnullOwnPtr<Label>>()) {
  13. if (i == 0)
  14. return **ptr;
  15. --i;
  16. }
  17. }
  18. return {};
  19. }
  20. Result Configuration::call(FunctionAddress address, Vector<Value> arguments)
  21. {
  22. auto* function = m_store.get(address);
  23. if (!function)
  24. return Trap {};
  25. if (auto* wasm_function = function->get_pointer<WasmFunction>()) {
  26. Vector<Value> locals;
  27. locals.ensure_capacity(arguments.size() + wasm_function->code().locals().size());
  28. for (auto& value : arguments)
  29. locals.append(Value { value });
  30. for (auto& type : wasm_function->code().locals())
  31. locals.empend(type, 0ull);
  32. auto frame = make<Frame>(
  33. wasm_function->module(),
  34. move(locals),
  35. wasm_function->code().body(),
  36. wasm_function->type().results().size());
  37. set_frame(move(frame));
  38. return execute();
  39. }
  40. // It better be a host function, else something is really wrong.
  41. auto& host_function = function->get<HostFunction>();
  42. auto result = bit_cast<HostFunctionType>(host_function.ptr())(m_store, arguments);
  43. auto count = host_function.type().results().size();
  44. if (count == 0)
  45. return Result { Vector<Value> {} };
  46. if (count == 1)
  47. return Result { Vector<Value> { Value { host_function.type().results().first(), result } } };
  48. TODO();
  49. }
  50. Result Configuration::execute()
  51. {
  52. Interpreter interpreter;
  53. interpreter.interpret(*this);
  54. if (interpreter.did_trap())
  55. return Trap {};
  56. Vector<NonnullOwnPtr<Value>> results;
  57. for (size_t i = 0; i < m_current_frame->arity(); ++i)
  58. results.append(move(stack().pop().get<NonnullOwnPtr<Value>>()));
  59. auto label = stack().pop();
  60. // ASSERT: label == current frame
  61. if (!label.has<NonnullOwnPtr<Label>>())
  62. return Trap {};
  63. Vector<Value> results_moved;
  64. results_moved.ensure_capacity(results.size());
  65. for (auto& entry : results)
  66. results_moved.unchecked_append(move(*entry));
  67. return Result { move(results_moved) };
  68. }
  69. void Configuration::dump_stack()
  70. {
  71. for (const auto& entry : stack().entries()) {
  72. entry.visit(
  73. [](const NonnullOwnPtr<Value>& v) {
  74. v->value().visit([]<typename T>(const T& v) {
  75. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  76. dbgln(" {}", v);
  77. else
  78. dbgln(" *{}", v.value());
  79. });
  80. },
  81. [](const NonnullOwnPtr<Frame>& f) {
  82. dbgln(" frame({})", f->arity());
  83. for (auto& local : f->locals()) {
  84. local.value().visit([]<typename T>(const T& v) {
  85. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  86. dbgln(" {}", v);
  87. else
  88. dbgln(" *{}", v.value());
  89. });
  90. }
  91. },
  92. [](const NonnullOwnPtr<Label>& l) {
  93. dbgln(" label({}) -> {}", l->arity(), l->continuation());
  94. });
  95. }
  96. }
  97. }