Configuration.cpp 3.5 KB

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