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