11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#if !V8_ENABLE_WEBASSEMBLY
61cb0ef41Sopenharmony_ci#error This header should only be included if WebAssembly is enabled.
71cb0ef41Sopenharmony_ci#endif  // !V8_ENABLE_WEBASSEMBLY
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef V8_WASM_WASM_ARGUMENTS_H_
101cb0ef41Sopenharmony_ci#define V8_WASM_WASM_ARGUMENTS_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include <stdint.h>
131cb0ef41Sopenharmony_ci#include <vector>
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci#include "src/base/memory.h"
161cb0ef41Sopenharmony_ci#include "src/codegen/signature.h"
171cb0ef41Sopenharmony_ci#include "src/common/globals.h"
181cb0ef41Sopenharmony_ci#include "src/wasm/value-type.h"
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cinamespace v8 {
211cb0ef41Sopenharmony_cinamespace internal {
221cb0ef41Sopenharmony_cinamespace wasm {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// Helper class for {Push}ing Wasm value arguments onto the stack in the format
251cb0ef41Sopenharmony_ci// that the CWasmEntryStub expects, as well as for {Pop}ping return values.
261cb0ef41Sopenharmony_ci// {Reset} must be called if a packer instance used for pushing is then
271cb0ef41Sopenharmony_ci// reused for popping: it resets the internal pointer to the beginning of
281cb0ef41Sopenharmony_ci// the stack region.
291cb0ef41Sopenharmony_ciclass CWasmArgumentsPacker {
301cb0ef41Sopenharmony_ci public:
311cb0ef41Sopenharmony_ci  explicit CWasmArgumentsPacker(size_t buffer_size)
321cb0ef41Sopenharmony_ci      : heap_buffer_(buffer_size <= kMaxOnStackBuffer ? 0 : buffer_size),
331cb0ef41Sopenharmony_ci        buffer_((buffer_size <= kMaxOnStackBuffer) ? on_stack_buffer_
341cb0ef41Sopenharmony_ci                                                   : heap_buffer_.data()) {}
351cb0ef41Sopenharmony_ci  i::Address argv() const { return reinterpret_cast<i::Address>(buffer_); }
361cb0ef41Sopenharmony_ci  void Reset() { offset_ = 0; }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  template <typename T>
391cb0ef41Sopenharmony_ci  void Push(T val) {
401cb0ef41Sopenharmony_ci    Address address = reinterpret_cast<Address>(buffer_ + offset_);
411cb0ef41Sopenharmony_ci    offset_ += sizeof(val);
421cb0ef41Sopenharmony_ci    base::WriteUnalignedValue(address, val);
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  template <typename T>
461cb0ef41Sopenharmony_ci  T Pop() {
471cb0ef41Sopenharmony_ci    Address address = reinterpret_cast<Address>(buffer_ + offset_);
481cb0ef41Sopenharmony_ci    offset_ += sizeof(T);
491cb0ef41Sopenharmony_ci    return base::ReadUnalignedValue<T>(address);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  static int TotalSize(const FunctionSig* sig) {
531cb0ef41Sopenharmony_ci    int return_size = 0;
541cb0ef41Sopenharmony_ci    for (ValueType t : sig->returns()) {
551cb0ef41Sopenharmony_ci      return_size += t.value_kind_size();
561cb0ef41Sopenharmony_ci    }
571cb0ef41Sopenharmony_ci    int param_size = 0;
581cb0ef41Sopenharmony_ci    for (ValueType t : sig->parameters()) {
591cb0ef41Sopenharmony_ci      param_size += t.value_kind_size();
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci    return std::max(return_size, param_size);
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci private:
651cb0ef41Sopenharmony_ci  static const size_t kMaxOnStackBuffer = 10 * i::kSystemPointerSize;
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  uint8_t on_stack_buffer_[kMaxOnStackBuffer];
681cb0ef41Sopenharmony_ci  std::vector<uint8_t> heap_buffer_;
691cb0ef41Sopenharmony_ci  uint8_t* buffer_;
701cb0ef41Sopenharmony_ci  size_t offset_ = 0;
711cb0ef41Sopenharmony_ci};
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci}  // namespace wasm
741cb0ef41Sopenharmony_ci}  // namespace internal
751cb0ef41Sopenharmony_ci}  // namespace v8
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci#endif  // V8_WASM_WASM_ARGUMENTS_H_
78