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
13 namespace v8 {
14 namespace internal {
15
16 // Describes the inputs and outputs of a function or call.
17 template <typename T>
18 class Signature : public ZoneObject {
19 public:
Signature(size_t return_count,size_t parameter_count,const T * reps)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
return_count()26 size_t return_count() const { return return_count_; }
parameter_count()27 size_t parameter_count() const { return parameter_count_; }
28
GetParam(size_t index)29 T GetParam(size_t index) const {
30 DCHECK(index < parameter_count_);
31 return reps_[return_count_ + index];
32 }
33
34 T GetReturn(size_t index = 0) const {
35 DCHECK(index < return_count_);
36 return reps_[index];
37 }
38
39 // Iteration support.
parameters()40 base::iterator_range<const T*> parameters() const {
41 return {reps_ + return_count_, reps_ + return_count_ + parameter_count_};
42 }
returns()43 base::iterator_range<const T*> returns() const {
44 return {reps_, reps_ + return_count_};
45 }
all()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:
Builder(Zone * zone,size_t return_count,size_t parameter_count)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
AddReturn(T val)73 void AddReturn(T val) {
74 DCHECK(rcursor_ < return_count_);
75 buffer_[rcursor_++] = val;
76 }
AddParam(T val)77 void AddParam(T val) {
78 DCHECK(pcursor_ < parameter_count_);
79 buffer_[return_count_ + pcursor_++] = val;
80 }
Build()81 Signature<T>* Build() {
82 DCHECK(rcursor_ == return_count_);
83 DCHECK(pcursor_ == parameter_count_);
84 return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_);
85 }
86
87 private:
88 Zone* zone_;
89 size_t rcursor_;
90 size_t pcursor_;
91 T* buffer_;
92 };
93
94 protected:
95 size_t return_count_;
96 size_t parameter_count_;
97 const T* reps_;
98 };
99
100 typedef Signature<MachineType> MachineSignature;
101
102 template <typename T>
hash_value(const Signature<T> & sig)103 size_t hash_value(const Signature<T>& sig) {
104 size_t hash = base::hash_combine(sig.parameter_count(), sig.return_count());
105 for (const T& t : sig.all()) hash = base::hash_combine(hash, t);
106 return hash;
107 }
108
109 } // namespace internal
110 } // namespace v8
111
112 #endif // V8_SIGNATURE_H_
113