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/zone.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 // Describes the inputs and outputs of a function or call.
14 template <typename T>
15 class Signature : public ZoneObject {
16  public:
Signature(size_t return_count,size_t parameter_count,T * reps)17   Signature(size_t return_count, size_t parameter_count, T* reps)
18       : return_count_(return_count),
19         parameter_count_(parameter_count),
20         reps_(reps) {}
21 
return_count()22   size_t return_count() const { return return_count_; }
parameter_count()23   size_t parameter_count() const { return parameter_count_; }
24 
GetParam(size_t index)25   T GetParam(size_t index) const {
26     DCHECK(index < parameter_count_);
27     return reps_[return_count_ + index];
28   }
29 
30   T GetReturn(size_t index = 0) const {
31     DCHECK(index < return_count_);
32     return reps_[index];
33   }
34 
35   // For incrementally building signatures.
36   class Builder {
37    public:
Builder(Zone * zone,size_t return_count,size_t parameter_count)38     Builder(Zone* zone, size_t return_count, size_t parameter_count)
39         : return_count_(return_count),
40           parameter_count_(parameter_count),
41           zone_(zone),
42           rcursor_(0),
43           pcursor_(0),
44           buffer_(zone->NewArray<T>(
45               static_cast<int>(return_count + parameter_count))) {}
46 
47     const size_t return_count_;
48     const size_t parameter_count_;
49 
AddReturn(T val)50     void AddReturn(T val) {
51       DCHECK(rcursor_ < return_count_);
52       buffer_[rcursor_++] = val;
53     }
AddParam(T val)54     void AddParam(T val) {
55       DCHECK(pcursor_ < parameter_count_);
56       buffer_[return_count_ + pcursor_++] = val;
57     }
Build()58     Signature<T>* Build() {
59       DCHECK(rcursor_ == return_count_);
60       DCHECK(pcursor_ == parameter_count_);
61       return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_);
62     }
63 
64    private:
65     Zone* zone_;
66     size_t rcursor_;
67     size_t pcursor_;
68     T* buffer_;
69   };
70 
71  protected:
72   size_t return_count_;
73   size_t parameter_count_;
74   T* reps_;
75 };
76 
77 }  // namespace internal
78 }  // namespace v8
79 
80 #endif  // V8_SIGNATURE_H_
81