Configuration.cpp 3.5 KB

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