signature.h 3.35 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_SIGNATURE_H_
#define V8_SIGNATURE_H_

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 14 15 16 17 18 19

namespace v8 {
namespace internal {

// Describes the inputs and outputs of a function or call.
template <typename T>
class Signature : public ZoneObject {
 public:
20 21
  constexpr Signature(size_t return_count, size_t parameter_count,
                      const T* reps)
22 23 24 25 26 27 28 29
      : return_count_(return_count),
        parameter_count_(parameter_count),
        reps_(reps) {}

  size_t return_count() const { return return_count_; }
  size_t parameter_count() const { return parameter_count_; }

  T GetParam(size_t index) const {
30
    DCHECK_LT(index, parameter_count_);
31 32 33 34
    return reps_[return_count_ + index];
  }

  T GetReturn(size_t index = 0) const {
35
    DCHECK_LT(index, return_count_);
36 37 38
    return reps_[index];
  }

39 40 41 42 43 44 45 46 47 48 49
  // Iteration support.
  base::iterator_range<const T*> parameters() const {
    return {reps_ + return_count_, reps_ + return_count_ + parameter_count_};
  }
  base::iterator_range<const T*> returns() const {
    return {reps_, reps_ + return_count_};
  }
  base::iterator_range<const T*> all() const {
    return {reps_, reps_ + return_count_ + parameter_count_};
  }

50 51 52 53 54
  bool operator==(const Signature& other) const {
    if (this == &other) return true;
    if (parameter_count() != other.parameter_count()) return false;
    if (return_count() != other.return_count()) return false;
    return std::equal(all().begin(), all().end(), other.all().begin());
55
  }
56
  bool operator!=(const Signature& other) const { return !(*this == other); }
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  // For incrementally building signatures.
  class Builder {
   public:
    Builder(Zone* zone, size_t return_count, size_t parameter_count)
        : return_count_(return_count),
          parameter_count_(parameter_count),
          zone_(zone),
          rcursor_(0),
          pcursor_(0),
          buffer_(zone->NewArray<T>(
              static_cast<int>(return_count + parameter_count))) {}

    const size_t return_count_;
    const size_t parameter_count_;

    void AddReturn(T val) {
74
      DCHECK_LT(rcursor_, return_count_);
75 76
      buffer_[rcursor_++] = val;
    }
77

78
    void AddParam(T val) {
79
      DCHECK_LT(pcursor_, parameter_count_);
80 81
      buffer_[return_count_ + pcursor_++] = val;
    }
82 83 84 85 86 87 88

    void AddParamAt(size_t index, T val) {
      DCHECK_LT(index, parameter_count_);
      buffer_[return_count_ + index] = val;
      pcursor_ = std::max(pcursor_, index + 1);
    }

89
    Signature<T>* Build() {
90 91
      DCHECK_EQ(rcursor_, return_count_);
      DCHECK_EQ(pcursor_, parameter_count_);
92 93 94 95 96 97 98 99 100 101 102 103 104
      return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_);
    }

   private:
    Zone* zone_;
    size_t rcursor_;
    size_t pcursor_;
    T* buffer_;
  };

 protected:
  size_t return_count_;
  size_t parameter_count_;
105
  const T* reps_;
106 107
};

108 109
typedef Signature<MachineType> MachineSignature;

110 111 112 113 114 115 116
template <typename T>
size_t hash_value(const Signature<T>& sig) {
  size_t hash = base::hash_combine(sig.parameter_count(), sig.return_count());
  for (const T& t : sig.all()) hash = base::hash_combine(hash, t);
  return hash;
}

117 118 119 120
}  // namespace internal
}  // namespace v8

#endif  // V8_SIGNATURE_H_