Configuration.cpp 3.5 KB

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