2014-08-28 13:17:38 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef V8_COMPILER_C_SIGNATURE_H_
|
|
|
|
#define V8_COMPILER_C_SIGNATURE_H_
|
|
|
|
|
2015-12-03 13:33:14 +00:00
|
|
|
#include "src/machine-type.h"
|
2014-08-28 13:17:38 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2015-06-01 15:54:53 +00:00
|
|
|
#define FOREACH_CTYPE_MACHINE_TYPE_MAPPING(V) \
|
2015-12-10 09:03:30 +00:00
|
|
|
V(void, MachineType::None()) \
|
|
|
|
V(bool, MachineType::Uint8()) \
|
|
|
|
V(int8_t, MachineType::Int8()) \
|
|
|
|
V(uint8_t, MachineType::Uint8()) \
|
|
|
|
V(int16_t, MachineType::Int16()) \
|
|
|
|
V(uint16_t, MachineType::Uint16()) \
|
|
|
|
V(int32_t, MachineType::Int32()) \
|
|
|
|
V(uint32_t, MachineType::Uint32()) \
|
|
|
|
V(int64_t, MachineType::Int64()) \
|
|
|
|
V(uint64_t, MachineType::Uint64()) \
|
|
|
|
V(float, MachineType::Float32()) \
|
|
|
|
V(double, MachineType::Float64()) \
|
|
|
|
V(void*, MachineType::Pointer()) \
|
|
|
|
V(int*, MachineType::Pointer())
|
2015-06-01 15:54:53 +00:00
|
|
|
|
2014-08-28 13:17:38 +00:00
|
|
|
template <typename T>
|
|
|
|
inline MachineType MachineTypeForC() {
|
2015-06-01 15:54:53 +00:00
|
|
|
while (false) {
|
|
|
|
// All other types T must be assignable to Object*
|
|
|
|
*(static_cast<Object* volatile*>(0)) = static_cast<T>(0);
|
|
|
|
}
|
2015-12-10 09:03:30 +00:00
|
|
|
return MachineType::AnyTagged();
|
2014-08-28 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 15:54:53 +00:00
|
|
|
#define DECLARE_TEMPLATE_SPECIALIZATION(ctype, mtype) \
|
|
|
|
template <> \
|
|
|
|
inline MachineType MachineTypeForC<ctype>() { \
|
|
|
|
return mtype; \
|
|
|
|
}
|
|
|
|
FOREACH_CTYPE_MACHINE_TYPE_MAPPING(DECLARE_TEMPLATE_SPECIALIZATION)
|
|
|
|
#undef DECLARE_TEMPLATE_SPECIALIZATION
|
2014-08-28 13:17:38 +00:00
|
|
|
|
2015-06-01 15:54:53 +00:00
|
|
|
// Helper for building machine signatures from C types.
|
|
|
|
class CSignature : public MachineSignature {
|
|
|
|
protected:
|
|
|
|
CSignature(size_t return_count, size_t parameter_count, MachineType* reps)
|
|
|
|
: MachineSignature(return_count, parameter_count, reps) {}
|
2014-08-28 13:17:38 +00:00
|
|
|
|
2015-06-01 15:54:53 +00:00
|
|
|
public:
|
|
|
|
template <typename P1 = void, typename P2 = void, typename P3 = void,
|
|
|
|
typename P4 = void, typename P5 = void>
|
2015-06-02 15:00:55 +00:00
|
|
|
void VerifyParams() {
|
2015-06-01 15:54:53 +00:00
|
|
|
// Verifies the C signature against the machine types. Maximum {5} params.
|
2015-06-02 15:00:55 +00:00
|
|
|
CHECK_LT(parameter_count(), 6u);
|
2015-06-01 15:54:53 +00:00
|
|
|
const int kMax = 5;
|
|
|
|
MachineType params[] = {MachineTypeForC<P1>(), MachineTypeForC<P2>(),
|
|
|
|
MachineTypeForC<P3>(), MachineTypeForC<P4>(),
|
|
|
|
MachineTypeForC<P5>()};
|
|
|
|
for (int p = kMax - 1; p >= 0; p--) {
|
2015-06-02 15:00:55 +00:00
|
|
|
if (p < static_cast<int>(parameter_count())) {
|
2015-06-01 15:54:53 +00:00
|
|
|
CHECK_EQ(GetParam(p), params[p]);
|
|
|
|
} else {
|
2015-12-10 09:03:30 +00:00
|
|
|
CHECK_EQ(MachineType::None(), params[p]);
|
2015-06-01 15:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 15:00:55 +00:00
|
|
|
|
2015-08-11 15:23:04 +00:00
|
|
|
static CSignature* FromMachine(Zone* zone, MachineSignature* msig) {
|
|
|
|
return reinterpret_cast<CSignature*>(msig);
|
|
|
|
}
|
|
|
|
|
2015-06-02 15:00:55 +00:00
|
|
|
static CSignature* New(Zone* zone, MachineType ret,
|
2015-12-10 09:03:30 +00:00
|
|
|
MachineType p1 = MachineType::None(),
|
|
|
|
MachineType p2 = MachineType::None(),
|
|
|
|
MachineType p3 = MachineType::None(),
|
|
|
|
MachineType p4 = MachineType::None(),
|
|
|
|
MachineType p5 = MachineType::None()) {
|
2015-06-02 15:00:55 +00:00
|
|
|
MachineType* buffer = zone->NewArray<MachineType>(6);
|
|
|
|
int pos = 0;
|
|
|
|
size_t return_count = 0;
|
2015-12-10 09:03:30 +00:00
|
|
|
if (ret != MachineType::None()) {
|
2015-06-02 15:00:55 +00:00
|
|
|
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;
|
2015-12-10 09:03:30 +00:00
|
|
|
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--;
|
2015-06-02 15:00:55 +00:00
|
|
|
for (size_t i = 0; i < param_count; i++) {
|
2015-12-10 09:03:30 +00:00
|
|
|
// Check that there are no MachineType::None()'s in the middle of
|
|
|
|
// parameters.
|
|
|
|
CHECK_NE(MachineType::None(), buffer[return_count + i]);
|
2015-06-02 15:00:55 +00:00
|
|
|
}
|
|
|
|
return new (zone) CSignature(return_count, param_count, buffer);
|
|
|
|
}
|
2015-06-01 15:54:53 +00:00
|
|
|
};
|
2014-08-28 13:17:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Ret, uint16_t kParamCount>
|
2015-06-01 15:54:53 +00:00
|
|
|
class CSignatureOf : public CSignature {
|
2014-08-28 13:17:38 +00:00
|
|
|
protected:
|
|
|
|
MachineType storage_[1 + kParamCount];
|
|
|
|
|
|
|
|
CSignatureOf()
|
2015-12-10 09:03:30 +00:00
|
|
|
: CSignature(MachineTypeForC<Ret>() != MachineType::None() ? 1 : 0,
|
|
|
|
kParamCount, reinterpret_cast<MachineType*>(&storage_)) {
|
2014-08-28 13:17:38 +00:00
|
|
|
if (return_count_ == 1) storage_[0] = MachineTypeForC<Ret>();
|
|
|
|
}
|
|
|
|
void Set(int index, MachineType type) {
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK_LE(0, index);
|
|
|
|
CHECK_LT(index, kParamCount);
|
2014-08-28 13:17:38 +00:00
|
|
|
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> {
|
|
|
|
public:
|
|
|
|
CSignature1() : CSignatureOf<Ret, 1>() {
|
|
|
|
this->Set(0, MachineTypeForC<P1>());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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>());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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>());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-01 15:54:53 +00:00
|
|
|
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;
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2014-08-28 13:17:38 +00:00
|
|
|
|
|
|
|
#endif // V8_COMPILER_C_SIGNATURE_H_
|