util-riscv64.h (2254B)
1 // Copyright 2022 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 #ifndef jit_riscv64_constant_util_riscv64__h_ 5 #define jit_riscv64_constant_util_riscv64__h_ 6 #include <stdarg.h> 7 #include <stdio.h> 8 #include <string.h> 9 namespace js { 10 namespace jit { 11 template <typename T> 12 class V8Vector { 13 public: 14 V8Vector() : start_(nullptr), length_(0) {} 15 V8Vector(T* data, int length) : start_(data), length_(length) { 16 MOZ_ASSERT(length == 0 || (length > 0 && data != nullptr)); 17 } 18 19 // Returns the length of the vector. 20 int length() const { return length_; } 21 22 // Returns the pointer to the start of the data in the vector. 23 T* start() const { return start_; } 24 25 // Access individual vector elements - checks bounds in debug mode. 26 T& operator[](int index) const { 27 MOZ_ASSERT(0 <= index && index < length_); 28 return start_[index]; 29 } 30 31 inline V8Vector<T> operator+(int offset) { 32 MOZ_ASSERT(offset < length_); 33 return V8Vector<T>(start_ + offset, length_ - offset); 34 } 35 36 private: 37 T* start_; 38 int length_; 39 }; 40 41 template <typename T, int kSize> 42 class EmbeddedVector : public V8Vector<T> { 43 public: 44 EmbeddedVector() : V8Vector<T>(buffer_, kSize) {} 45 46 explicit EmbeddedVector(T initial_value) : V8Vector<T>(buffer_, kSize) { 47 for (int i = 0; i < kSize; ++i) { 48 buffer_[i] = initial_value; 49 } 50 } 51 52 // When copying, make underlying Vector to reference our buffer. 53 EmbeddedVector(const EmbeddedVector& rhs) : V8Vector<T>(rhs) { 54 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); 55 this->set_start(buffer_); 56 } 57 58 EmbeddedVector& operator=(const EmbeddedVector& rhs) { 59 if (this == &rhs) return *this; 60 V8Vector<T>::operator=(rhs); 61 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); 62 this->set_start(buffer_); 63 return *this; 64 } 65 66 private: 67 T buffer_[kSize]; 68 }; 69 70 // Helper function for printing to a Vector. 71 static inline int MOZ_FORMAT_PRINTF(2, 3) 72 SNPrintF(V8Vector<char> str, const char* format, ...) { 73 va_list args; 74 va_start(args, format); 75 int result = vsnprintf(str.start(), str.length(), format, args); 76 va_end(args); 77 return result; 78 } 79 } // namespace jit 80 } // namespace js 81 #endif