Configuration.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MemoryStream.h>
  7. #include <LibWasm/AbstractMachine/Configuration.h>
  8. #include <LibWasm/AbstractMachine/Interpreter.h>
  9. #include <LibWasm/Printer/Printer.h>
  10. namespace Wasm {
  11. Optional<size_t> Configuration::nth_label_index(size_t i)
  12. {
  13. for (size_t index = m_stack.size(); index > 0; --index) {
  14. auto& entry = m_stack.entries()[index - 1];
  15. if (entry.has<Label>()) {
  16. if (i == 0)
  17. return index - 1;
  18. --i;
  19. }
  20. }
  21. return {};
  22. }
  23. void Configuration::unwind(Badge<CallFrameHandle>, CallFrameHandle const& frame_handle)
  24. {
  25. if (m_stack.size() == frame_handle.stack_size && frame_handle.frame_index == m_current_frame_index)
  26. return;
  27. VERIFY(m_stack.size() > frame_handle.stack_size);
  28. m_stack.entries().remove(frame_handle.stack_size, m_stack.size() - frame_handle.stack_size);
  29. m_current_frame_index = frame_handle.frame_index;
  30. m_depth--;
  31. m_ip = frame_handle.ip;
  32. VERIFY(m_stack.size() == frame_handle.stack_size);
  33. }
  34. Result Configuration::call(Interpreter& interpreter, FunctionAddress address, Vector<Value> arguments)
  35. {
  36. auto* function = m_store.get(address);
  37. if (!function)
  38. return Trap {};
  39. if (auto* wasm_function = function->get_pointer<WasmFunction>()) {
  40. Vector<Value> locals = move(arguments);
  41. locals.ensure_capacity(locals.size() + wasm_function->code().func().locals().size());
  42. for (auto& local : wasm_function->code().func().locals()) {
  43. for (size_t i = 0; i < local.n(); ++i)
  44. locals.empend(local.type(), 0ull);
  45. }
  46. set_frame(Frame {
  47. wasm_function->module(),
  48. move(locals),
  49. wasm_function->code().func().body(),
  50. wasm_function->type().results().size(),
  51. });
  52. m_ip = 0;
  53. return execute(interpreter);
  54. }
  55. // It better be a host function, else something is really wrong.
  56. auto& host_function = function->get<HostFunction>();
  57. return host_function.function()(*this, arguments);
  58. }
  59. Result Configuration::execute(Interpreter& interpreter)
  60. {
  61. interpreter.interpret(*this);
  62. if (interpreter.did_trap())
  63. return Trap { interpreter.trap_reason() };
  64. if (stack().size() <= frame().arity() + 1)
  65. return Trap { "Not enough values to return from call" };
  66. Vector<Value> results;
  67. results.ensure_capacity(frame().arity());
  68. for (size_t i = 0; i < frame().arity(); ++i)
  69. results.append(move(stack().pop().get<Value>()));
  70. auto label = stack().pop();
  71. // ASSERT: label == current frame
  72. if (!label.has<Label>())
  73. return Trap { "Invalid stack configuration" };
  74. return Result { move(results) };
  75. }
  76. void Configuration::dump_stack()
  77. {
  78. auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> format, Ts... vs) {
  79. AllocatingMemoryStream memory_stream;
  80. Printer { memory_stream }.print(vs...);
  81. auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
  82. memory_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
  83. dbgln(format.view(), StringView(buffer).trim_whitespace());
  84. };
  85. for (auto const& entry : stack().entries()) {
  86. entry.visit(
  87. [&](Value const& v) {
  88. print_value(" {}", v);
  89. },
  90. [&](Frame const& f) {
  91. dbgln(" frame({})", f.arity());
  92. for (auto& local : f.locals()) {
  93. print_value(" {}", local);
  94. }
  95. },
  96. [](Label const& l) {
  97. dbgln(" label({}) -> {}", l.arity(), l.continuation());
  98. });
  99. }
  100. }
  101. }