1// Copyright 2015 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_SIGNATURE_H_
6#define V8_SIGNATURE_H_
7
8#include "src/base/functional.h"
9#include "src/base/iterator.h"
10#include "src/machine-type.h"
11#include "src/zone/zone.h"
12
13namespace v8 {
14namespace internal {
15
16// Describes the inputs and outputs of a function or call.
17template <typename T>
18class Signature : public ZoneObject {
19 public:
20 constexpr Signature(size_t return_count, size_t parameter_count,
21 const T* reps)
22 : return_count_(return_count),
23 parameter_count_(parameter_count),
24 reps_(reps) {}
25
26 size_t return_count() const { return return_count_; }
27 size_t parameter_count() const { return parameter_count_; }
28
29 T GetParam(size_t index) const {
30 DCHECK_LT(index, parameter_count_);
31 return reps_[return_count_ + index];
32 }
33
34 T GetReturn(size_t index = 0) const {
35 DCHECK_LT(index, return_count_);
36 return reps_[index];
37 }
38
39 // Iteration support.
40 base::iterator_range<const T*> parameters() const {
41 return {reps_ + return_count_, reps_ + return_count_ + parameter_count_};
42 }
43 base::iterator_range<const T*> returns() const {
44 return {reps_, reps_ + return_count_};
45 }
46 base::iterator_range<const T*> all() const {
47 return {reps_, reps_ + return_count_ + parameter_count_};
48 }
49
50 bool operator==(const Signature& other) const {
51 if (this == &other) return true;
52 if (parameter_count() != other.parameter_count()) return false;
53 if (return_count() != other.return_count()) return false;
54 return std::equal(all().begin(), all().end(), other.all().begin());
55 }
56 bool operator!=(const Signature& other) const { return !(*this == other); }
57
58 // For incrementally building signatures.
59 class Builder {
60 public:
61 Builder(Zone* zone, size_t return_count, size_t parameter_count)
62 : return_count_(return_count),
63 parameter_count_(parameter_count),
64 zone_(zone),
65 rcursor_(0),
66 pcursor_(0),
67 buffer_(zone->NewArray<T>(
68 static_cast<int>(return_count + parameter_count))) {}
69
70 const size_t return_count_;
71 const size_t parameter_count_;
72
73 void AddReturn(T val) {
74 DCHECK_LT(rcursor_, return_count_);
75 buffer_[rcursor_++] = val;
76 }
77
78 void AddParam(T val) {
79 DCHECK_LT(pcursor_, parameter_count_);
80 buffer_[return_count_ + pcursor_++] = val;
81 }
82
83 void AddParamAt(size_t index, T val) {
84 DCHECK_LT(index, parameter_count_);
85 buffer_[return_count_ + index] = val;
86 pcursor_ = std::max(pcursor_, index + 1);
87 }
88
89 Signature<T>* Build() {
90 DCHECK_EQ(rcursor_, return_count_);
91 DCHECK_EQ(pcursor_, parameter_count_);
92 return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_);
93 }
94
95 private:
96 Zone* zone_;
97 size_t rcursor_;
98 size_t pcursor_;
99 T* buffer_;
100 };
101
102 protected:
103 size_t return_count_;
104 size_t parameter_count_;
105 const T* reps_;
106};
107
108typedef Signature<MachineType> MachineSignature;
109
110template <typename T>
111size_t hash_value(const Signature<T>& sig) {
112 size_t hash = base::hash_combine(sig.parameter_count(), sig.return_count());
113 for (const T& t : sig.all()) hash = base::hash_combine(hash, t);
114 return hash;
115}
116
117} // namespace internal
118} // namespace v8
119
120#endif // V8_SIGNATURE_H_
121