Configuration.cpp 3.6 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(Interpreter& interpreter, 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(interpreter);
  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(Interpreter& interpreter)
  56. {
  57. interpreter.interpret(*this);
  58. if (interpreter.did_trap())
  59. return Trap {};
  60. Vector<Value> results;
  61. results.ensure_capacity(frame().arity());
  62. for (size_t i = 0; i < frame().arity(); ++i)
  63. results.append(move(stack().pop().get<Value>()));
  64. auto label = stack().pop();
  65. // ASSERT: label == current frame
  66. if (!label.has<Label>())
  67. return Trap {};
  68. return Result { move(results) };
  69. }
  70. void Configuration::dump_stack()
  71. {
  72. for (const auto& entry : stack().entries()) {
  73. entry.visit(
  74. [](const Value& v) {
  75. v.value().visit([]<typename T>(const T& v) {
  76. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  77. dbgln(" {}", v);
  78. else if constexpr (IsSame<Value::Null, T>)
  79. dbgln(" *null");
  80. else
  81. dbgln(" *{}", v.value());
  82. });
  83. },
  84. [](const Frame& f) {
  85. dbgln(" frame({})", f.arity());
  86. for (auto& local : f.locals()) {
  87. local.value().visit([]<typename T>(const T& v) {
  88. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  89. dbgln(" {}", v);
  90. else if constexpr (IsSame<Value::Null, T>)
  91. dbgln(" *null");
  92. else
  93. dbgln(" *{}", v.value());
  94. });
  95. }
  96. },
  97. [](const Label& l) {
  98. dbgln(" label({}) -> {}", l.arity(), l.continuation());
  99. });
  100. }
  101. }
  102. }