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