summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/c-signature.h
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/test/cctest/compiler/c-signature.h')
-rw-r--r--deps/v8/test/cctest/compiler/c-signature.h143
1 files changed, 48 insertions, 95 deletions
diff --git a/deps/v8/test/cctest/compiler/c-signature.h b/deps/v8/test/cctest/compiler/c-signature.h
index 1c2f9638f4..0aea6e938b 100644
--- a/deps/v8/test/cctest/compiler/c-signature.h
+++ b/deps/v8/test/cctest/compiler/c-signature.h
@@ -28,18 +28,16 @@ namespace compiler {
V(int*, MachineType::Pointer())
template <typename T>
-inline MachineType MachineTypeForC() {
- while (false) {
- // All other types T must be assignable to Object*
- *(static_cast<Object* volatile*>(0)) = static_cast<T>(0);
- }
+inline constexpr MachineType MachineTypeForC() {
+ static_assert(std::is_convertible<T, Object*>::value,
+ "all non-specialized types must be convertible to Object*");
return MachineType::AnyTagged();
}
-#define DECLARE_TEMPLATE_SPECIALIZATION(ctype, mtype) \
- template <> \
- inline MachineType MachineTypeForC<ctype>() { \
- return mtype; \
+#define DECLARE_TEMPLATE_SPECIALIZATION(ctype, mtype) \
+ template <> \
+ inline MachineType constexpr MachineTypeForC<ctype>() { \
+ return mtype; \
}
FOREACH_CTYPE_MACHINE_TYPE_MAPPING(DECLARE_TEMPLATE_SPECIALIZATION)
#undef DECLARE_TEMPLATE_SPECIALIZATION
@@ -51,21 +49,13 @@ class CSignature : public MachineSignature {
: MachineSignature(return_count, parameter_count, reps) {}
public:
- template <typename P1 = void, typename P2 = void, typename P3 = void,
- typename P4 = void, typename P5 = void>
+ template <typename... Params>
static void VerifyParams(MachineSignature* sig) {
- // Verifies the C signature against the machine types. Maximum {5} params.
- CHECK_LT(sig->parameter_count(), 6u);
- const int kMax = 5;
- MachineType params[] = {MachineTypeForC<P1>(), MachineTypeForC<P2>(),
- MachineTypeForC<P3>(), MachineTypeForC<P4>(),
- MachineTypeForC<P5>()};
- for (int p = kMax - 1; p >= 0; p--) {
- if (p < static_cast<int>(sig->parameter_count())) {
- CHECK_EQ(sig->GetParam(p), params[p]);
- } else {
- CHECK_EQ(MachineType::None(), params[p]);
- }
+ // Verifies the C signature against the machine types.
+ std::array<MachineType, sizeof...(Params)> params{
+ {MachineTypeForC<Params>()...}};
+ for (size_t p = 0; p < params.size(); ++p) {
+ CHECK_EQ(sig->GetParam(p), params[p]);
}
}
@@ -73,96 +63,59 @@ class CSignature : public MachineSignature {
return reinterpret_cast<CSignature*>(msig);
}
+ template <typename... ParamMachineTypes>
static CSignature* New(Zone* zone, MachineType ret,
- MachineType p1 = MachineType::None(),
- MachineType p2 = MachineType::None(),
- MachineType p3 = MachineType::None(),
- MachineType p4 = MachineType::None(),
- MachineType p5 = MachineType::None()) {
- MachineType* buffer = zone->NewArray<MachineType>(6);
- int pos = 0;
+ ParamMachineTypes... params) {
+ constexpr size_t param_count = sizeof...(params);
+ std::array<MachineType, param_count> param_arr{{params...}};
+ const size_t buffer_size =
+ param_count + (ret == MachineType::None() ? 0 : 1);
+ MachineType* buffer = zone->NewArray<MachineType>(buffer_size);
+ size_t pos = 0;
size_t return_count = 0;
if (ret != MachineType::None()) {
buffer[pos++] = ret;
return_count++;
}
- buffer[pos++] = p1;
- buffer[pos++] = p2;
- buffer[pos++] = p3;
- buffer[pos++] = p4;
- buffer[pos++] = p5;
- size_t param_count = 5;
- if (p5 == MachineType::None()) param_count--;
- if (p4 == MachineType::None()) param_count--;
- if (p3 == MachineType::None()) param_count--;
- if (p2 == MachineType::None()) param_count--;
- if (p1 == MachineType::None()) param_count--;
- for (size_t i = 0; i < param_count; i++) {
- // Check that there are no MachineType::None()'s in the middle of
- // parameters.
- CHECK_NE(MachineType::None(), buffer[return_count + i]);
+ for (MachineType p : param_arr) {
+ // Check that there are no MachineType::None()'s in the parameters.
+ CHECK_NE(MachineType::None(), p);
+ buffer[pos++] = p;
}
+ DCHECK_EQ(buffer_size, pos);
return new (zone) CSignature(return_count, param_count, buffer);
}
};
-
-template <typename Ret, uint16_t kParamCount>
-class CSignatureOf : public CSignature {
- protected:
- MachineType storage_[1 + kParamCount];
-
- CSignatureOf()
- : CSignature(MachineTypeForC<Ret>() != MachineType::None() ? 1 : 0,
- kParamCount, reinterpret_cast<MachineType*>(&storage_)) {
- if (return_count_ == 1) storage_[0] = MachineTypeForC<Ret>();
- }
- void Set(int index, MachineType type) {
- CHECK_LE(0, index);
- CHECK_LT(index, kParamCount);
- reps_[return_count_ + index] = type;
- }
-};
-
// Helper classes for instantiating Signature objects to be callable from C.
-template <typename Ret>
-class CSignature0 : public CSignatureOf<Ret, 0> {
- public:
- CSignature0() : CSignatureOf<Ret, 0>() {}
-};
-
-template <typename Ret, typename P1>
-class CSignature1 : public CSignatureOf<Ret, 1> {
+template <typename Ret, typename... Params>
+class CSignatureOf : public CSignature {
public:
- CSignature1() : CSignatureOf<Ret, 1>() {
- this->Set(0, MachineTypeForC<P1>());
+ CSignatureOf() : CSignature(kReturnCount, kParamCount, storage_) {
+ constexpr std::array<MachineType, kParamCount> param_types{
+ MachineTypeForC<Params>()...};
+ if (kReturnCount == 1) storage_[0] = MachineTypeForC<Ret>();
+ static_assert(
+ std::is_same<decltype(*reps_), decltype(*param_types.data())>::value,
+ "type mismatch, cannot memcpy");
+ memcpy(storage_ + kReturnCount, param_types.data(),
+ sizeof(*storage_) * kParamCount);
}
-};
-template <typename Ret, typename P1, typename P2>
-class CSignature2 : public CSignatureOf<Ret, 2> {
- public:
- CSignature2() : CSignatureOf<Ret, 2>() {
- this->Set(0, MachineTypeForC<P1>());
- this->Set(1, MachineTypeForC<P2>());
- }
-};
+ private:
+ static constexpr size_t kReturnCount =
+ MachineTypeForC<Ret>() == MachineType::None() ? 0 : 1;
+ static constexpr size_t kParamCount = sizeof...(Params);
-template <typename Ret, typename P1, typename P2, typename P3>
-class CSignature3 : public CSignatureOf<Ret, 3> {
- public:
- CSignature3() : CSignatureOf<Ret, 3>() {
- this->Set(0, MachineTypeForC<P1>());
- this->Set(1, MachineTypeForC<P2>());
- this->Set(2, MachineTypeForC<P3>());
- }
+ MachineType storage_[kReturnCount + kParamCount];
};
-typedef CSignature2<int32_t, int32_t, int32_t> CSignature_i_ii;
-typedef CSignature2<uint32_t, uint32_t, uint32_t> CSignature_u_uu;
-typedef CSignature2<float, float, float> CSignature_f_ff;
-typedef CSignature2<double, double, double> CSignature_d_dd;
-typedef CSignature2<Object*, Object*, Object*> CSignature_o_oo;
+typedef CSignatureOf<int32_t, int32_t, int32_t> CSignature_i_ii;
+typedef CSignatureOf<uint32_t, uint32_t, uint32_t> CSignature_u_uu;
+typedef CSignatureOf<float, float, float> CSignature_f_ff;
+typedef CSignatureOf<double, double, double> CSignature_d_dd;
+typedef CSignatureOf<Object*, Object*, Object*> CSignature_o_oo;
+
} // namespace compiler
} // namespace internal
} // namespace v8