1 // Copyright 2014 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.
5 #ifndef V8_COMPILER_C_SIGNATURE_H_
6 #define V8_COMPILER_C_SIGNATURE_H_
8 #include "src/compiler/machine-type.h"
14 #define FOREACH_CTYPE_MACHINE_TYPE_MAPPING(V) \
17 V(int8_t, kMachInt8) \
18 V(uint8_t, kMachUint8) \
19 V(int16_t, kMachInt16) \
20 V(uint16_t, kMachUint16) \
21 V(int32_t, kMachInt32) \
22 V(uint32_t, kMachUint32) \
23 V(int64_t, kMachInt64) \
24 V(uint64_t, kMachUint64) \
25 V(float, kMachFloat32) \
26 V(double, kMachFloat64) \
31 inline MachineType MachineTypeForC() {
33 // All other types T must be assignable to Object*
34 *(static_cast<Object* volatile*>(0)) = static_cast<T>(0);
36 return kMachAnyTagged;
39 #define DECLARE_TEMPLATE_SPECIALIZATION(ctype, mtype) \
41 inline MachineType MachineTypeForC<ctype>() { \
44 FOREACH_CTYPE_MACHINE_TYPE_MAPPING(DECLARE_TEMPLATE_SPECIALIZATION)
45 #undef DECLARE_TEMPLATE_SPECIALIZATION
47 // Helper for building machine signatures from C types.
48 class CSignature : public MachineSignature {
50 CSignature(size_t return_count, size_t parameter_count, MachineType* reps)
51 : MachineSignature(return_count, parameter_count, reps) {}
54 template <typename P1 = void, typename P2 = void, typename P3 = void,
55 typename P4 = void, typename P5 = void>
57 // Verifies the C signature against the machine types. Maximum {5} params.
58 CHECK_LT(parameter_count(), 6u);
60 MachineType params[] = {MachineTypeForC<P1>(), MachineTypeForC<P2>(),
61 MachineTypeForC<P3>(), MachineTypeForC<P4>(),
62 MachineTypeForC<P5>()};
63 for (int p = kMax - 1; p >= 0; p--) {
64 if (p < static_cast<int>(parameter_count())) {
65 CHECK_EQ(GetParam(p), params[p]);
67 CHECK_EQ(kMachNone, params[p]);
72 static CSignature* FromMachine(Zone* zone, MachineSignature* msig) {
73 return reinterpret_cast<CSignature*>(msig);
76 static CSignature* New(Zone* zone, MachineType ret,
77 MachineType p1 = kMachNone, MachineType p2 = kMachNone,
78 MachineType p3 = kMachNone, MachineType p4 = kMachNone,
79 MachineType p5 = kMachNone) {
80 MachineType* buffer = zone->NewArray<MachineType>(6);
82 size_t return_count = 0;
83 if (ret != kMachNone) {
92 size_t param_count = 5;
93 if (p5 == kMachNone) param_count--;
94 if (p4 == kMachNone) param_count--;
95 if (p3 == kMachNone) param_count--;
96 if (p2 == kMachNone) param_count--;
97 if (p1 == kMachNone) param_count--;
98 for (size_t i = 0; i < param_count; i++) {
99 // Check that there are no kMachNone's in the middle of parameters.
100 CHECK_NE(kMachNone, buffer[return_count + i]);
102 return new (zone) CSignature(return_count, param_count, buffer);
107 template <typename Ret, uint16_t kParamCount>
108 class CSignatureOf : public CSignature {
110 MachineType storage_[1 + kParamCount];
113 : CSignature(MachineTypeForC<Ret>() != kMachNone ? 1 : 0, kParamCount,
114 reinterpret_cast<MachineType*>(&storage_)) {
115 if (return_count_ == 1) storage_[0] = MachineTypeForC<Ret>();
117 void Set(int index, MachineType type) {
118 DCHECK(index >= 0 && index < kParamCount);
119 reps_[return_count_ + index] = type;
123 // Helper classes for instantiating Signature objects to be callable from C.
124 template <typename Ret>
125 class CSignature0 : public CSignatureOf<Ret, 0> {
127 CSignature0() : CSignatureOf<Ret, 0>() {}
130 template <typename Ret, typename P1>
131 class CSignature1 : public CSignatureOf<Ret, 1> {
133 CSignature1() : CSignatureOf<Ret, 1>() {
134 this->Set(0, MachineTypeForC<P1>());
138 template <typename Ret, typename P1, typename P2>
139 class CSignature2 : public CSignatureOf<Ret, 2> {
141 CSignature2() : CSignatureOf<Ret, 2>() {
142 this->Set(0, MachineTypeForC<P1>());
143 this->Set(1, MachineTypeForC<P2>());
147 template <typename Ret, typename P1, typename P2, typename P3>
148 class CSignature3 : public CSignatureOf<Ret, 3> {
150 CSignature3() : CSignatureOf<Ret, 3>() {
151 this->Set(0, MachineTypeForC<P1>());
152 this->Set(1, MachineTypeForC<P2>());
153 this->Set(2, MachineTypeForC<P3>());
157 typedef CSignature2<int32_t, int32_t, int32_t> CSignature_i_ii;
158 typedef CSignature2<uint32_t, uint32_t, uint32_t> CSignature_u_uu;
159 typedef CSignature2<float, float, float> CSignature_f_ff;
160 typedef CSignature2<double, double, double> CSignature_d_dd;
161 typedef CSignature2<Object*, Object*, Object*> CSignature_o_oo;
164 } // namespace v8::internal::compiler
166 #endif // V8_COMPILER_C_SIGNATURE_H_