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 template <typename T>
MachineTypeForC()15 inline MachineType MachineTypeForC() {
16 CHECK(false); // Instantiated with invalid type.
17 return kMachNone;
18 }
19
20 template <>
21 inline MachineType MachineTypeForC<void>() {
22 return kMachNone;
23 }
24
25 template <>
26 inline MachineType MachineTypeForC<int8_t>() {
27 return kMachInt8;
28 }
29
30 template <>
31 inline MachineType MachineTypeForC<uint8_t>() {
32 return kMachUint8;
33 }
34
35 template <>
36 inline MachineType MachineTypeForC<int16_t>() {
37 return kMachInt16;
38 }
39
40 template <>
41 inline MachineType MachineTypeForC<uint16_t>() {
42 return kMachUint16;
43 }
44
45 template <>
46 inline MachineType MachineTypeForC<int32_t>() {
47 return kMachInt32;
48 }
49
50 template <>
51 inline MachineType MachineTypeForC<uint32_t>() {
52 return kMachUint32;
53 }
54
55 template <>
56 inline MachineType MachineTypeForC<int64_t>() {
57 return kMachInt64;
58 }
59
60 template <>
61 inline MachineType MachineTypeForC<uint64_t>() {
62 return kMachUint64;
63 }
64
65 template <>
66 inline MachineType MachineTypeForC<double>() {
67 return kMachFloat64;
68 }
69
70 template <>
71 inline MachineType MachineTypeForC<Object*>() {
72 return kMachAnyTagged;
73 }
74
75 template <typename Ret, uint16_t kParamCount>
76 class CSignatureOf : public MachineSignature {
77 protected:
78 MachineType storage_[1 + kParamCount];
79
CSignatureOf()80 CSignatureOf()
81 : MachineSignature(MachineTypeForC<Ret>() != kMachNone ? 1 : 0,
82 kParamCount,
83 reinterpret_cast<MachineType*>(&storage_)) {
84 if (return_count_ == 1) storage_[0] = MachineTypeForC<Ret>();
85 }
Set(int index,MachineType type)86 void Set(int index, MachineType type) {
87 DCHECK(index >= 0 && index < kParamCount);
88 reps_[return_count_ + index] = type;
89 }
90 };
91
92 // Helper classes for instantiating Signature objects to be callable from C.
93 template <typename Ret>
94 class CSignature0 : public CSignatureOf<Ret, 0> {
95 public:
CSignature0()96 CSignature0() : CSignatureOf<Ret, 0>() {}
97 };
98
99 template <typename Ret, typename P1>
100 class CSignature1 : public CSignatureOf<Ret, 1> {
101 public:
CSignature1()102 CSignature1() : CSignatureOf<Ret, 1>() {
103 this->Set(0, MachineTypeForC<P1>());
104 }
105 };
106
107 template <typename Ret, typename P1, typename P2>
108 class CSignature2 : public CSignatureOf<Ret, 2> {
109 public:
CSignature2()110 CSignature2() : CSignatureOf<Ret, 2>() {
111 this->Set(0, MachineTypeForC<P1>());
112 this->Set(1, MachineTypeForC<P2>());
113 }
114 };
115
116 template <typename Ret, typename P1, typename P2, typename P3>
117 class CSignature3 : public CSignatureOf<Ret, 3> {
118 public:
CSignature3()119 CSignature3() : CSignatureOf<Ret, 3>() {
120 this->Set(0, MachineTypeForC<P1>());
121 this->Set(1, MachineTypeForC<P2>());
122 this->Set(2, MachineTypeForC<P3>());
123 }
124 };
125
126 static const CSignature2<int32_t, int32_t, int32_t> int32_int32_to_int32;
127 static const CSignature2<uint32_t, uint32_t, uint32_t> uint32_uint32_to_uint32;
128 static const CSignature2<double, double, double> float64_float64_to_float64;
129 }
130 }
131 } // namespace v8::internal::compiler
132
133 #endif // V8_COMPILER_C_SIGNATURE_H_
134