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
10namespace v8 {
11namespace internal {
12namespace compiler {
13
14template <typename T>
15inline MachineType MachineTypeForC() {
16  CHECK(false);  // Instantiated with invalid type.
17  return kMachNone;
18}
19
20template <>
21inline MachineType MachineTypeForC<void>() {
22  return kMachNone;
23}
24
25template <>
26inline MachineType MachineTypeForC<int8_t>() {
27  return kMachInt8;
28}
29
30template <>
31inline MachineType MachineTypeForC<uint8_t>() {
32  return kMachUint8;
33}
34
35template <>
36inline MachineType MachineTypeForC<int16_t>() {
37  return kMachInt16;
38}
39
40template <>
41inline MachineType MachineTypeForC<uint16_t>() {
42  return kMachUint16;
43}
44
45template <>
46inline MachineType MachineTypeForC<int32_t>() {
47  return kMachInt32;
48}
49
50template <>
51inline MachineType MachineTypeForC<uint32_t>() {
52  return kMachUint32;
53}
54
55template <>
56inline MachineType MachineTypeForC<int64_t>() {
57  return kMachInt64;
58}
59
60template <>
61inline MachineType MachineTypeForC<uint64_t>() {
62  return kMachUint64;
63}
64
65template <>
66inline MachineType MachineTypeForC<double>() {
67  return kMachFloat64;
68}
69
70template <>
71inline MachineType MachineTypeForC<Object*>() {
72  return kMachAnyTagged;
73}
74
75template <typename Ret, uint16_t kParamCount>
76class CSignatureOf : public MachineSignature {
77 protected:
78  MachineType storage_[1 + kParamCount];
79
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  }
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.
93template <typename Ret>
94class CSignature0 : public CSignatureOf<Ret, 0> {
95 public:
96  CSignature0() : CSignatureOf<Ret, 0>() {}
97};
98
99template <typename Ret, typename P1>
100class CSignature1 : public CSignatureOf<Ret, 1> {
101 public:
102  CSignature1() : CSignatureOf<Ret, 1>() {
103    this->Set(0, MachineTypeForC<P1>());
104  }
105};
106
107template <typename Ret, typename P1, typename P2>
108class CSignature2 : public CSignatureOf<Ret, 2> {
109 public:
110  CSignature2() : CSignatureOf<Ret, 2>() {
111    this->Set(0, MachineTypeForC<P1>());
112    this->Set(1, MachineTypeForC<P2>());
113  }
114};
115
116template <typename Ret, typename P1, typename P2, typename P3>
117class CSignature3 : public CSignatureOf<Ret, 3> {
118 public:
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
126static const CSignature2<int32_t, int32_t, int32_t> int32_int32_to_int32;
127static const CSignature2<uint32_t, uint32_t, uint32_t> uint32_uint32_to_uint32;
128static 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