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/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: 17 Signature(size_t return_count, size_t parameter_count, const T* reps) 18 : return_count_(return_count), 19 parameter_count_(parameter_count), 20 reps_(reps) {} 21 22 size_t return_count() const { return return_count_; } 23 size_t parameter_count() const { return parameter_count_; } 24 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 bool Equals(const Signature* that) const { 36 if (this == that) return true; 37 if (this->parameter_count() != that->parameter_count()) return false; 38 if (this->return_count() != that->return_count()) return false; 39 size_t size = this->return_count() + this->parameter_count(); 40 for (size_t i = 0; i < size; i++) { 41 if (this->reps_[i] != that->reps_[i]) return false; 42 } 43 return true; 44 } 45 46 // For incrementally building signatures. 47 class Builder { 48 public: 49 Builder(Zone* zone, size_t return_count, size_t parameter_count) 50 : return_count_(return_count), 51 parameter_count_(parameter_count), 52 zone_(zone), 53 rcursor_(0), 54 pcursor_(0), 55 buffer_(zone->NewArray<T>( 56 static_cast<int>(return_count + parameter_count))) {} 57 58 const size_t return_count_; 59 const size_t parameter_count_; 60 61 void AddReturn(T val) { 62 DCHECK(rcursor_ < return_count_); 63 buffer_[rcursor_++] = val; 64 } 65 void AddParam(T val) { 66 DCHECK(pcursor_ < parameter_count_); 67 buffer_[return_count_ + pcursor_++] = val; 68 } 69 Signature<T>* Build() { 70 DCHECK(rcursor_ == return_count_); 71 DCHECK(pcursor_ == parameter_count_); 72 return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_); 73 } 74 75 private: 76 Zone* zone_; 77 size_t rcursor_; 78 size_t pcursor_; 79 T* buffer_; 80 }; 81 82 protected: 83 size_t return_count_; 84 size_t parameter_count_; 85 const T* reps_; 86 }; 87 88 } // namespace internal 89 } // namespace v8 90 91 #endif // V8_SIGNATURE_H_ 92