1// Copyright 2019 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#if !V8_ENABLE_WEBASSEMBLY 6#error This header should only be included if WebAssembly is enabled. 7#endif // !V8_ENABLE_WEBASSEMBLY 8 9#ifndef V8_WASM_WASM_ARGUMENTS_H_ 10#define V8_WASM_WASM_ARGUMENTS_H_ 11 12#include <stdint.h> 13#include <vector> 14 15#include "src/base/memory.h" 16#include "src/codegen/signature.h" 17#include "src/common/globals.h" 18#include "src/wasm/value-type.h" 19 20namespace v8 { 21namespace internal { 22namespace wasm { 23 24// Helper class for {Push}ing Wasm value arguments onto the stack in the format 25// that the CWasmEntryStub expects, as well as for {Pop}ping return values. 26// {Reset} must be called if a packer instance used for pushing is then 27// reused for popping: it resets the internal pointer to the beginning of 28// the stack region. 29class CWasmArgumentsPacker { 30 public: 31 explicit CWasmArgumentsPacker(size_t buffer_size) 32 : heap_buffer_(buffer_size <= kMaxOnStackBuffer ? 0 : buffer_size), 33 buffer_((buffer_size <= kMaxOnStackBuffer) ? on_stack_buffer_ 34 : heap_buffer_.data()) {} 35 i::Address argv() const { return reinterpret_cast<i::Address>(buffer_); } 36 void Reset() { offset_ = 0; } 37 38 template <typename T> 39 void Push(T val) { 40 Address address = reinterpret_cast<Address>(buffer_ + offset_); 41 offset_ += sizeof(val); 42 base::WriteUnalignedValue(address, val); 43 } 44 45 template <typename T> 46 T Pop() { 47 Address address = reinterpret_cast<Address>(buffer_ + offset_); 48 offset_ += sizeof(T); 49 return base::ReadUnalignedValue<T>(address); 50 } 51 52 static int TotalSize(const FunctionSig* sig) { 53 int return_size = 0; 54 for (ValueType t : sig->returns()) { 55 return_size += t.value_kind_size(); 56 } 57 int param_size = 0; 58 for (ValueType t : sig->parameters()) { 59 param_size += t.value_kind_size(); 60 } 61 return std::max(return_size, param_size); 62 } 63 64 private: 65 static const size_t kMaxOnStackBuffer = 10 * i::kSystemPointerSize; 66 67 uint8_t on_stack_buffer_[kMaxOnStackBuffer]; 68 std::vector<uint8_t> heap_buffer_; 69 uint8_t* buffer_; 70 size_t offset_ = 0; 71}; 72 73} // namespace wasm 74} // namespace internal 75} // namespace v8 76 77#endif // V8_WASM_WASM_ARGUMENTS_H_ 78