8eaf6325f22011081d6a6a957440c66aae44248d
[platform/upstream/v8.git] / test / cctest / compiler / c-signature.h
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.
4
5 #ifndef V8_COMPILER_C_SIGNATURE_H_
6 #define V8_COMPILER_C_SIGNATURE_H_
7
8 #include "src/compiler/machine-type.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
14 #define FOREACH_CTYPE_MACHINE_TYPE_MAPPING(V) \
15   V(void, kMachNone)                          \
16   V(bool, kMachBool)                          \
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)                     \
27   V(void*, kMachPtr)                          \
28   V(int*, kMachPtr)
29
30 template <typename T>
31 inline MachineType MachineTypeForC() {
32   while (false) {
33     // All other types T must be assignable to Object*
34     *(static_cast<Object* volatile*>(0)) = static_cast<T>(0);
35   }
36   return kMachAnyTagged;
37 }
38
39 #define DECLARE_TEMPLATE_SPECIALIZATION(ctype, mtype) \
40   template <>                                         \
41   inline MachineType MachineTypeForC<ctype>() {       \
42     return mtype;                                     \
43   }
44 FOREACH_CTYPE_MACHINE_TYPE_MAPPING(DECLARE_TEMPLATE_SPECIALIZATION)
45 #undef DECLARE_TEMPLATE_SPECIALIZATION
46
47 // Helper for building machine signatures from C types.
48 class CSignature : public MachineSignature {
49  protected:
50   CSignature(size_t return_count, size_t parameter_count, MachineType* reps)
51       : MachineSignature(return_count, parameter_count, reps) {}
52
53  public:
54   template <typename P1 = void, typename P2 = void, typename P3 = void,
55             typename P4 = void, typename P5 = void>
56   void VerifyParams() {
57     // Verifies the C signature against the machine types. Maximum {5} params.
58     CHECK_LT(parameter_count(), 6u);
59     const int kMax = 5;
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]);
66       } else {
67         CHECK_EQ(kMachNone, params[p]);
68       }
69     }
70   }
71
72   static CSignature* FromMachine(Zone* zone, MachineSignature* msig) {
73     return reinterpret_cast<CSignature*>(msig);
74   }
75
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);
81     int pos = 0;
82     size_t return_count = 0;
83     if (ret != kMachNone) {
84       buffer[pos++] = ret;
85       return_count++;
86     }
87     buffer[pos++] = p1;
88     buffer[pos++] = p2;
89     buffer[pos++] = p3;
90     buffer[pos++] = p4;
91     buffer[pos++] = p5;
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]);
101     }
102     return new (zone) CSignature(return_count, param_count, buffer);
103   }
104 };
105
106
107 template <typename Ret, uint16_t kParamCount>
108 class CSignatureOf : public CSignature {
109  protected:
110   MachineType storage_[1 + kParamCount];
111
112   CSignatureOf()
113       : CSignature(MachineTypeForC<Ret>() != kMachNone ? 1 : 0, kParamCount,
114                    reinterpret_cast<MachineType*>(&storage_)) {
115     if (return_count_ == 1) storage_[0] = MachineTypeForC<Ret>();
116   }
117   void Set(int index, MachineType type) {
118     DCHECK(index >= 0 && index < kParamCount);
119     reps_[return_count_ + index] = type;
120   }
121 };
122
123 // Helper classes for instantiating Signature objects to be callable from C.
124 template <typename Ret>
125 class CSignature0 : public CSignatureOf<Ret, 0> {
126  public:
127   CSignature0() : CSignatureOf<Ret, 0>() {}
128 };
129
130 template <typename Ret, typename P1>
131 class CSignature1 : public CSignatureOf<Ret, 1> {
132  public:
133   CSignature1() : CSignatureOf<Ret, 1>() {
134     this->Set(0, MachineTypeForC<P1>());
135   }
136 };
137
138 template <typename Ret, typename P1, typename P2>
139 class CSignature2 : public CSignatureOf<Ret, 2> {
140  public:
141   CSignature2() : CSignatureOf<Ret, 2>() {
142     this->Set(0, MachineTypeForC<P1>());
143     this->Set(1, MachineTypeForC<P2>());
144   }
145 };
146
147 template <typename Ret, typename P1, typename P2, typename P3>
148 class CSignature3 : public CSignatureOf<Ret, 3> {
149  public:
150   CSignature3() : CSignatureOf<Ret, 3>() {
151     this->Set(0, MachineTypeForC<P1>());
152     this->Set(1, MachineTypeForC<P2>());
153     this->Set(2, MachineTypeForC<P3>());
154   }
155 };
156
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;
162 }
163 }
164 }  // namespace v8::internal::compiler
165
166 #endif  // V8_COMPILER_C_SIGNATURE_H_