| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_OPERATOR_H_ | 5 #ifndef V8_COMPILER_OPERATOR_H_ |
| 6 #define V8_COMPILER_OPERATOR_H_ | 6 #define V8_COMPILER_OPERATOR_H_ |
| 7 | 7 |
| 8 #include "src/base/flags.h" | 8 #include "src/base/flags.h" |
| 9 #include "src/ostreams.h" | 9 #include "src/ostreams.h" |
| 10 #include "src/unique.h" | 10 #include "src/unique.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 80 |
| 81 // Number of data outputs from the operator, for verifying graph structure. | 81 // Number of data outputs from the operator, for verifying graph structure. |
| 82 virtual int OutputCount() const = 0; | 82 virtual int OutputCount() const = 0; |
| 83 | 83 |
| 84 Properties properties() const { return properties_; } | 84 Properties properties() const { return properties_; } |
| 85 | 85 |
| 86 // TODO(titzer): API for input and output types, for typechecking graph. | 86 // TODO(titzer): API for input and output types, for typechecking graph. |
| 87 protected: | 87 protected: |
| 88 // Print the full operator into the given stream, including any | 88 // Print the full operator into the given stream, including any |
| 89 // static parameters. Useful for debugging and visualizing the IR. | 89 // static parameters. Useful for debugging and visualizing the IR. |
| 90 virtual OStream& PrintTo(OStream& os) const = 0; // NOLINT | 90 virtual std::ostream& PrintTo(std::ostream& os) const = 0; // NOLINT |
| 91 friend OStream& operator<<(OStream& os, const Operator& op); | 91 friend std::ostream& operator<<(std::ostream& os, const Operator& op); |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 Opcode opcode_; | 94 Opcode opcode_; |
| 95 Properties properties_; | 95 Properties properties_; |
| 96 const char* mnemonic_; | 96 const char* mnemonic_; |
| 97 | 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(Operator); | 98 DISALLOW_COPY_AND_ASSIGN(Operator); |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 DEFINE_OPERATORS_FOR_FLAGS(Operator::Properties) | 101 DEFINE_OPERATORS_FOR_FLAGS(Operator::Properties) |
| 102 | 102 |
| 103 OStream& operator<<(OStream& os, const Operator& op); | 103 std::ostream& operator<<(std::ostream& os, const Operator& op); |
| 104 | 104 |
| 105 // An implementation of Operator that has no static parameters. Such operators | 105 // An implementation of Operator that has no static parameters. Such operators |
| 106 // have just a name, an opcode, and a fixed number of inputs and outputs. | 106 // have just a name, an opcode, and a fixed number of inputs and outputs. |
| 107 // They can represented by singletons and shared globally. | 107 // They can represented by singletons and shared globally. |
| 108 class SimpleOperator : public Operator { | 108 class SimpleOperator : public Operator { |
| 109 public: | 109 public: |
| 110 SimpleOperator(Opcode opcode, Properties properties, int input_count, | 110 SimpleOperator(Opcode opcode, Properties properties, int input_count, |
| 111 int output_count, const char* mnemonic); | 111 int output_count, const char* mnemonic); |
| 112 ~SimpleOperator(); | 112 ~SimpleOperator(); |
| 113 | 113 |
| 114 virtual bool Equals(const Operator* that) const FINAL { | 114 virtual bool Equals(const Operator* that) const FINAL { |
| 115 return opcode() == that->opcode(); | 115 return opcode() == that->opcode(); |
| 116 } | 116 } |
| 117 virtual int HashCode() const FINAL { return opcode(); } | 117 virtual int HashCode() const FINAL { return opcode(); } |
| 118 virtual int InputCount() const FINAL { return input_count_; } | 118 virtual int InputCount() const FINAL { return input_count_; } |
| 119 virtual int OutputCount() const FINAL { return output_count_; } | 119 virtual int OutputCount() const FINAL { return output_count_; } |
| 120 | 120 |
| 121 private: | 121 private: |
| 122 virtual OStream& PrintTo(OStream& os) const FINAL { // NOLINT | 122 virtual std::ostream& PrintTo(std::ostream& os) const FINAL { // NOLINT |
| 123 return os << mnemonic(); | 123 return os << mnemonic(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 int input_count_; | 126 int input_count_; |
| 127 int output_count_; | 127 int output_count_; |
| 128 | 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(SimpleOperator); | 129 DISALLOW_COPY_AND_ASSIGN(SimpleOperator); |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 // Template specialization implements a kind of type class for dealing with the | 132 // Template specialization implements a kind of type class for dealing with the |
| 133 // static parameters of Operator1 automatically. | 133 // static parameters of Operator1 automatically. |
| 134 template <typename T> | 134 template <typename T> |
| 135 struct StaticParameterTraits { | 135 struct StaticParameterTraits { |
| 136 static OStream& PrintTo(OStream& os, T val) { // NOLINT | 136 static std::ostream& PrintTo(std::ostream& os, T val) { // NOLINT |
| 137 return os << "??"; | 137 return os << "??"; |
| 138 } | 138 } |
| 139 static int HashCode(T a) { return 0; } | 139 static int HashCode(T a) { return 0; } |
| 140 static bool Equals(T a, T b) { | 140 static bool Equals(T a, T b) { |
| 141 return false; // Not every T has a ==. By default, be conservative. | 141 return false; // Not every T has a ==. By default, be conservative. |
| 142 } | 142 } |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 // Specialization for static parameters of type {int}. | 145 // Specialization for static parameters of type {int}. |
| 146 template <> | 146 template <> |
| 147 struct StaticParameterTraits<int> { | 147 struct StaticParameterTraits<int> { |
| 148 static OStream& PrintTo(OStream& os, int val) { // NOLINT | 148 static std::ostream& PrintTo(std::ostream& os, int val) { // NOLINT |
| 149 return os << val; | 149 return os << val; |
| 150 } | 150 } |
| 151 static int HashCode(int a) { return a; } | 151 static int HashCode(int a) { return a; } |
| 152 static bool Equals(int a, int b) { return a == b; } | 152 static bool Equals(int a, int b) { return a == b; } |
| 153 }; | 153 }; |
| 154 | 154 |
| 155 // Specialization for static parameters of type {double}. | 155 // Specialization for static parameters of type {double}. |
| 156 template <> | 156 template <> |
| 157 struct StaticParameterTraits<double> { | 157 struct StaticParameterTraits<double> { |
| 158 static OStream& PrintTo(OStream& os, double val) { // NOLINT | 158 static std::ostream& PrintTo(std::ostream& os, double val) { // NOLINT |
| 159 return os << val; | 159 return os << val; |
| 160 } | 160 } |
| 161 static int HashCode(double a) { | 161 static int HashCode(double a) { |
| 162 return static_cast<int>(bit_cast<int64_t>(a)); | 162 return static_cast<int>(bit_cast<int64_t>(a)); |
| 163 } | 163 } |
| 164 static bool Equals(double a, double b) { | 164 static bool Equals(double a, double b) { |
| 165 return bit_cast<int64_t>(a) == bit_cast<int64_t>(b); | 165 return bit_cast<int64_t>(a) == bit_cast<int64_t>(b); |
| 166 } | 166 } |
| 167 }; | 167 }; |
| 168 | 168 |
| 169 // Specialization for static parameters of type {Unique<Object>}. | 169 // Specialization for static parameters of type {Unique<Object>}. |
| 170 template <> | 170 template <> |
| 171 struct StaticParameterTraits<Unique<Object> > { | 171 struct StaticParameterTraits<Unique<Object> > { |
| 172 static OStream& PrintTo(OStream& os, Unique<Object> val) { // NOLINT | 172 static std::ostream& PrintTo(std::ostream& os, |
| 173 Unique<Object> val) { // NOLINT |
| 173 return os << Brief(*val.handle()); | 174 return os << Brief(*val.handle()); |
| 174 } | 175 } |
| 175 static int HashCode(Unique<Object> a) { | 176 static int HashCode(Unique<Object> a) { |
| 176 return static_cast<int>(a.Hashcode()); | 177 return static_cast<int>(a.Hashcode()); |
| 177 } | 178 } |
| 178 static bool Equals(Unique<Object> a, Unique<Object> b) { return a == b; } | 179 static bool Equals(Unique<Object> a, Unique<Object> b) { return a == b; } |
| 179 }; | 180 }; |
| 180 | 181 |
| 181 // Specialization for static parameters of type {Unique<Name>}. | 182 // Specialization for static parameters of type {Unique<Name>}. |
| 182 template <> | 183 template <> |
| 183 struct StaticParameterTraits<Unique<Name> > { | 184 struct StaticParameterTraits<Unique<Name> > { |
| 184 static OStream& PrintTo(OStream& os, Unique<Name> val) { // NOLINT | 185 static std::ostream& PrintTo(std::ostream& os, Unique<Name> val) { // NOLINT |
| 185 return os << Brief(*val.handle()); | 186 return os << Brief(*val.handle()); |
| 186 } | 187 } |
| 187 static int HashCode(Unique<Name> a) { return static_cast<int>(a.Hashcode()); } | 188 static int HashCode(Unique<Name> a) { return static_cast<int>(a.Hashcode()); } |
| 188 static bool Equals(Unique<Name> a, Unique<Name> b) { return a == b; } | 189 static bool Equals(Unique<Name> a, Unique<Name> b) { return a == b; } |
| 189 }; | 190 }; |
| 190 | 191 |
| 191 #if DEBUG | 192 #if DEBUG |
| 192 // Specialization for static parameters of type {Handle<Object>} to prevent any | 193 // Specialization for static parameters of type {Handle<Object>} to prevent any |
| 193 // direct usage of Handles in constants. | 194 // direct usage of Handles in constants. |
| 194 template <> | 195 template <> |
| 195 struct StaticParameterTraits<Handle<Object> > { | 196 struct StaticParameterTraits<Handle<Object> > { |
| 196 static OStream& PrintTo(OStream& os, Handle<Object> val) { // NOLINT | 197 static std::ostream& PrintTo(std::ostream& os, |
| 198 Handle<Object> val) { // NOLINT |
| 197 UNREACHABLE(); // Should use Unique<Object> instead | 199 UNREACHABLE(); // Should use Unique<Object> instead |
| 198 return os; | 200 return os; |
| 199 } | 201 } |
| 200 static int HashCode(Handle<Object> a) { | 202 static int HashCode(Handle<Object> a) { |
| 201 UNREACHABLE(); // Should use Unique<Object> instead | 203 UNREACHABLE(); // Should use Unique<Object> instead |
| 202 return 0; | 204 return 0; |
| 203 } | 205 } |
| 204 static bool Equals(Handle<Object> a, Handle<Object> b) { | 206 static bool Equals(Handle<Object> a, Handle<Object> b) { |
| 205 UNREACHABLE(); // Should use Unique<Object> instead | 207 UNREACHABLE(); // Should use Unique<Object> instead |
| 206 return false; | 208 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 226 virtual bool Equals(const Operator* other) const OVERRIDE { | 228 virtual bool Equals(const Operator* other) const OVERRIDE { |
| 227 if (opcode() != other->opcode()) return false; | 229 if (opcode() != other->opcode()) return false; |
| 228 const Operator1<T>* that = static_cast<const Operator1<T>*>(other); | 230 const Operator1<T>* that = static_cast<const Operator1<T>*>(other); |
| 229 return StaticParameterTraits<T>::Equals(this->parameter_, that->parameter_); | 231 return StaticParameterTraits<T>::Equals(this->parameter_, that->parameter_); |
| 230 } | 232 } |
| 231 virtual int HashCode() const OVERRIDE { | 233 virtual int HashCode() const OVERRIDE { |
| 232 return opcode() + 33 * StaticParameterTraits<T>::HashCode(this->parameter_); | 234 return opcode() + 33 * StaticParameterTraits<T>::HashCode(this->parameter_); |
| 233 } | 235 } |
| 234 virtual int InputCount() const OVERRIDE { return input_count_; } | 236 virtual int InputCount() const OVERRIDE { return input_count_; } |
| 235 virtual int OutputCount() const OVERRIDE { return output_count_; } | 237 virtual int OutputCount() const OVERRIDE { return output_count_; } |
| 236 virtual OStream& PrintParameter(OStream& os) const { // NOLINT | 238 virtual std::ostream& PrintParameter(std::ostream& os) const { // NOLINT |
| 237 return StaticParameterTraits<T>::PrintTo(os << "[", parameter_) << "]"; | 239 return StaticParameterTraits<T>::PrintTo(os << "[", parameter_) << "]"; |
| 238 } | 240 } |
| 239 | 241 |
| 240 protected: | 242 protected: |
| 241 virtual OStream& PrintTo(OStream& os) const FINAL { // NOLINT | 243 virtual std::ostream& PrintTo(std::ostream& os) const FINAL { // NOLINT |
| 242 return PrintParameter(os << mnemonic()); | 244 return PrintParameter(os << mnemonic()); |
| 243 } | 245 } |
| 244 | 246 |
| 245 private: | 247 private: |
| 246 int input_count_; | 248 int input_count_; |
| 247 int output_count_; | 249 int output_count_; |
| 248 T parameter_; | 250 T parameter_; |
| 249 }; | 251 }; |
| 250 | 252 |
| 251 | 253 |
| 252 // Helper to extract parameters from Operator1<*> operator. | 254 // Helper to extract parameters from Operator1<*> operator. |
| 253 template <typename T> | 255 template <typename T> |
| 254 static inline const T& OpParameter(const Operator* op) { | 256 static inline const T& OpParameter(const Operator* op) { |
| 255 return reinterpret_cast<const Operator1<T>*>(op)->parameter(); | 257 return reinterpret_cast<const Operator1<T>*>(op)->parameter(); |
| 256 } | 258 } |
| 257 | 259 |
| 258 } // namespace compiler | 260 } // namespace compiler |
| 259 } // namespace internal | 261 } // namespace internal |
| 260 } // namespace v8 | 262 } // namespace v8 |
| 261 | 263 |
| 262 #endif // V8_COMPILER_OPERATOR_H_ | 264 #endif // V8_COMPILER_OPERATOR_H_ |
| OLD | NEW |