Configuration.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. return host_function.function()(*this, arguments);
  44. }
  45. Result Configuration::execute()
  46. {
  47. Interpreter interpreter;
  48. interpreter.pre_interpret_hook = pre_interpret_hook;
  49. interpreter.post_interpret_hook = post_interpret_hook;
  50. interpreter.interpret(*this);
  51. if (interpreter.did_trap())
  52. return Trap {};
  53. Vector<NonnullOwnPtr<Value>> results;
  54. for (size_t i = 0; i < m_current_frame->arity(); ++i)
  55. results.append(move(stack().pop().get<NonnullOwnPtr<Value>>()));
  56. auto label = stack().pop();
  57. // ASSERT: label == current frame
  58. if (!label.has<NonnullOwnPtr<Label>>())
  59. return Trap {};
  60. Vector<Value> results_moved;
  61. results_moved.ensure_capacity(results.size());
  62. for (auto& entry : results)
  63. results_moved.unchecked_append(move(*entry));
  64. return Result { move(results_moved) };
  65. }
  66. void Configuration::dump_stack()
  67. {
  68. for (const auto& entry : stack().entries()) {
  69. entry.visit(
  70. [](const NonnullOwnPtr<Value>& v) {
  71. v->value().visit([]<typename T>(const T& v) {
  72. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  73. dbgln(" {}", v);
  74. else
  75. dbgln(" *{}", v.value());
  76. });
  77. },
  78. [](const NonnullOwnPtr<Frame>& f) {
  79. dbgln(" frame({})", f->arity());
  80. for (auto& local : f->locals()) {
  81. local.value().visit([]<typename T>(const T& v) {
  82. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  83. dbgln(" {}", v);
  84. else
  85. dbgln(" *{}", v.value());
  86. });
  87. }
  88. },
  89. [](const NonnullOwnPtr<Label>& l) {
  90. dbgln(" label({}) -> {}", l->arity(), l->continuation());
  91. });
  92. }
  93. }
  94. }