Configuration.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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. set_frame(Frame {
  34. wasm_function->module(),
  35. move(locals),
  36. wasm_function->code().body(),
  37. wasm_function->type().results().size(),
  38. });
  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<Value> results;
  54. results.ensure_capacity(frame().arity());
  55. for (size_t i = 0; i < frame().arity(); ++i)
  56. results.append(move(stack().pop().get<Value>()));
  57. auto label = stack().pop();
  58. // ASSERT: label == current frame
  59. if (!label.has<Label>())
  60. return Trap {};
  61. return Result { move(results) };
  62. }
  63. void Configuration::dump_stack()
  64. {
  65. for (const auto& entry : stack().entries()) {
  66. entry.visit(
  67. [](const Value& v) {
  68. v.value().visit([]<typename T>(const T& v) {
  69. if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
  70. dbgln(" {}", v);
  71. else
  72. dbgln(" *{}", v.value());
  73. });
  74. },
  75. [](const Frame& f) {
  76. dbgln(" frame({})", f.arity());
  77. for (auto& local : f.locals()) {
  78. local.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. },
  86. [](const Label& l) {
  87. dbgln(" label({}) -> {}", l.arity(), l.continuation());
  88. });
  89. }
  90. }
  91. }