| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/wasm/asm-types.h" | 7 #include "src/wasm/asm-types.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| 11 namespace wasm { | 11 namespace wasm { |
| 12 | 12 |
| 13 AsmCallableType* AsmType::AsCallableType() { | 13 AsmCallableType* AsmType::AsCallableType() { |
| 14 if (AsValueType() != nullptr) { |
| 15 return nullptr; |
| 16 } |
| 17 |
| 14 DCHECK(this->AsFunctionType() != nullptr || | 18 DCHECK(this->AsFunctionType() != nullptr || |
| 15 this->AsOverloadedFunctionType() != nullptr); | 19 this->AsOverloadedFunctionType() != nullptr || |
| 20 this->AsFFIType() != nullptr || |
| 21 this->AsFunctionTableType() != nullptr); |
| 16 return reinterpret_cast<AsmCallableType*>(this); | 22 return reinterpret_cast<AsmCallableType*>(this); |
| 17 } | 23 } |
| 18 | 24 |
| 19 std::string AsmType::Name() { | 25 std::string AsmType::Name() { |
| 20 AsmValueType* avt = this->AsValueType(); | 26 AsmValueType* avt = this->AsValueType(); |
| 21 if (avt != nullptr) { | 27 if (avt != nullptr) { |
| 22 switch (avt->Bitset()) { | 28 switch (avt->Bitset()) { |
| 23 #define RETURN_TYPE_NAME(CamelName, string_name, number, parent_types) \ | 29 #define RETURN_TYPE_NAME(CamelName, string_name, number, parent_types) \ |
| 24 case AsmValueType::kAsm##CamelName: \ | 30 case AsmValueType::kAsm##CamelName: \ |
| 25 return string_name; | 31 return string_name; |
| 26 FOR_EACH_ASM_VALUE_TYPE_LIST(RETURN_TYPE_NAME) | 32 FOR_EACH_ASM_VALUE_TYPE_LIST(RETURN_TYPE_NAME) |
| 27 #undef RETURN_TYPE_NAME | 33 #undef RETURN_TYPE_NAME |
| 28 default: | 34 default: |
| 29 UNREACHABLE(); | 35 UNREACHABLE(); |
| 30 } | 36 } |
| 31 } | 37 } |
| 38 |
| 32 return this->AsCallableType()->Name(); | 39 return this->AsCallableType()->Name(); |
| 33 } | 40 } |
| 34 | 41 |
| 35 bool AsmType::IsExactly(AsmType* that) { | 42 bool AsmType::IsExactly(AsmType* that) { |
| 36 // TODO(jpp): maybe this can become this == that. | 43 // TODO(jpp): maybe this can become this == that. |
| 37 AsmValueType* avt = this->AsValueType(); | 44 AsmValueType* avt = this->AsValueType(); |
| 38 if (avt != nullptr) { | 45 if (avt != nullptr) { |
| 39 AsmValueType* tavt = that->AsValueType(); | 46 AsmValueType* tavt = that->AsValueType(); |
| 40 if (tavt == nullptr) { | 47 if (tavt == nullptr) { |
| 41 return false; | 48 return false; |
| 42 } | 49 } |
| 43 return avt->Bitset() == tavt->Bitset(); | 50 return avt->Bitset() == tavt->Bitset(); |
| 44 } | 51 } |
| 52 |
| 45 // TODO(jpp): is it useful to allow non-value types to be tested with | 53 // TODO(jpp): is it useful to allow non-value types to be tested with |
| 46 // IsExactly? | 54 // IsExactly? |
| 47 return that == this; | 55 return that == this; |
| 48 } | 56 } |
| 49 | 57 |
| 50 bool AsmType::IsA(AsmType* that) { | 58 bool AsmType::IsA(AsmType* that) { |
| 51 // IsA is used for querying inheritance relationships. Therefore it is only | 59 // IsA is used for querying inheritance relationships. Therefore it is only |
| 52 // meaningful for basic types. | 60 // meaningful for basic types. |
| 53 AsmValueType* tavt = that->AsValueType(); | 61 AsmValueType* tavt = that->AsValueType(); |
| 54 if (tavt != nullptr) { | 62 if (tavt != nullptr) { |
| 55 AsmValueType* avt = this->AsValueType(); | 63 AsmValueType* avt = this->AsValueType(); |
| 56 if (avt == nullptr) { | 64 if (avt == nullptr) { |
| 57 return false; | 65 return false; |
| 58 } | 66 } |
| 59 return (avt->Bitset() & tavt->Bitset()) == tavt->Bitset(); | 67 return (avt->Bitset() & tavt->Bitset()) == tavt->Bitset(); |
| 60 } | 68 } |
| 69 |
| 61 // TODO(jpp): is it useful to allow non-value types to be tested with IsA? | 70 // TODO(jpp): is it useful to allow non-value types to be tested with IsA? |
| 62 return that == this; | 71 return that == this; |
| 63 } | 72 } |
| 64 | 73 |
| 65 int32_t AsmType::ElementSizeInBytes() { | 74 int32_t AsmType::ElementSizeInBytes() { |
| 66 auto* value = AsValueType(); | 75 auto* value = AsValueType(); |
| 67 if (value == nullptr) { | 76 if (value == nullptr) { |
| 68 return AsmType::kNotHeapType; | 77 return AsmType::kNotHeapType; |
| 69 } | 78 } |
| 70 switch (value->Bitset()) { | 79 switch (value->Bitset()) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 case AsmValueType::kAsmFloat32Array: | 132 case AsmValueType::kAsmFloat32Array: |
| 124 return AsmType::FloatishDoubleQ(); | 133 return AsmType::FloatishDoubleQ(); |
| 125 case AsmValueType::kAsmFloat64Array: | 134 case AsmValueType::kAsmFloat64Array: |
| 126 return AsmType::FloatQDoubleQ(); | 135 return AsmType::FloatQDoubleQ(); |
| 127 default: | 136 default: |
| 128 return AsmType::None(); | 137 return AsmType::None(); |
| 129 } | 138 } |
| 130 } | 139 } |
| 131 | 140 |
| 132 std::string AsmFunctionType::Name() { | 141 std::string AsmFunctionType::Name() { |
| 142 if (IsFroundType()) { |
| 143 return "fround"; |
| 144 } |
| 145 |
| 133 std::string ret; | 146 std::string ret; |
| 134 ret += "("; | 147 ret += "("; |
| 135 for (size_t ii = 0; ii < args_.size(); ++ii) { | 148 for (size_t ii = 0; ii < args_.size(); ++ii) { |
| 136 ret += args_[ii]->Name(); | 149 ret += args_[ii]->Name(); |
| 137 if (ii != args_.size() - 1) { | 150 if (ii != args_.size() - 1) { |
| 138 ret += ", "; | 151 ret += ", "; |
| 139 } | 152 } |
| 140 } | 153 } |
| 141 if (IsMinMaxType()) { | 154 if (IsMinMaxType()) { |
| 142 DCHECK_EQ(args_.size(), 2); | 155 DCHECK_EQ(args_.size(), 2); |
| 143 ret += "..."; | 156 ret += "..."; |
| 144 } | 157 } |
| 145 ret += ") -> "; | 158 ret += ") -> "; |
| 146 ret += return_type_->Name(); | 159 ret += return_type_->Name(); |
| 147 return ret; | 160 return ret; |
| 148 } | 161 } |
| 149 | 162 |
| 150 namespace { | 163 namespace { |
| 151 class AsmFroundType final : public AsmFunctionType { | 164 class AsmFroundType final : public AsmFunctionType { |
| 152 public: | 165 public: |
| 153 bool IsFroundType() const override { return true; } | 166 bool IsFroundType() const override { return true; } |
| 154 | 167 |
| 155 private: | 168 private: |
| 156 friend AsmType; | 169 friend AsmType; |
| 157 | 170 |
| 158 AsmFroundType(Zone* zone, AsmType* src) | 171 explicit AsmFroundType(Zone* zone) |
| 159 : AsmFunctionType(zone, AsmType::Float()) { | 172 : AsmFunctionType(zone, AsmType::Float()) {} |
| 160 AddArgument(src); | 173 |
| 161 } | 174 AsmType* ValidateCall(AsmType* function_type) override; |
| 162 }; | 175 }; |
| 163 } // namespace | 176 } // namespace |
| 164 | 177 |
| 165 AsmType* AsmType::FroundType(Zone* zone, AsmType* src) { | 178 AsmType* AsmType::FroundType(Zone* zone) { |
| 166 DCHECK(src->AsValueType() != nullptr); | 179 auto* Fround = new (zone) AsmFroundType(zone); |
| 167 auto* Fround = new (zone) AsmFroundType(zone, src); | |
| 168 return reinterpret_cast<AsmType*>(Fround); | 180 return reinterpret_cast<AsmType*>(Fround); |
| 169 } | 181 } |
| 170 | 182 |
| 183 AsmType* AsmFroundType::ValidateCall(AsmType* function_type) { |
| 184 auto* callable = function_type->AsFunctionType(); |
| 185 if (callable->Arguments().size() != 1) { |
| 186 return AsmType::None(); |
| 187 } |
| 188 |
| 189 auto* arg = callable->Arguments()[0]; |
| 190 if (!arg->IsA(AsmType::Floatish()) && !arg->IsA(AsmType::DoubleQ()) && |
| 191 !arg->IsA(AsmType::Signed()) && !arg->IsA(AsmType::Unsigned())) { |
| 192 return AsmType::None(); |
| 193 } |
| 194 |
| 195 return AsmType::Float(); |
| 196 } |
| 197 |
| 171 namespace { | 198 namespace { |
| 172 class AsmMinMaxType final : public AsmFunctionType { | 199 class AsmMinMaxType final : public AsmFunctionType { |
| 173 public: | 200 public: |
| 174 bool IsMinMaxType() const override { return true; } | 201 bool IsMinMaxType() const override { return true; } |
| 175 | 202 |
| 176 private: | 203 private: |
| 177 friend AsmType; | 204 friend AsmType; |
| 178 | 205 |
| 179 AsmMinMaxType(Zone* zone, AsmType* type) : AsmFunctionType(zone, type) { | 206 AsmMinMaxType(Zone* zone, AsmType* dest, AsmType* src) |
| 180 AddArgument(type); | 207 : AsmFunctionType(zone, dest) { |
| 181 AddArgument(type); | 208 AddArgument(src); |
| 209 AddArgument(src); |
| 182 } | 210 } |
| 183 | 211 |
| 184 AsmType* ValidateCall(AsmType* function_type) override { | 212 AsmType* ValidateCall(AsmType* function_type) override { |
| 185 auto* callable = function_type->AsFunctionType(); | 213 auto* callable = function_type->AsFunctionType(); |
| 186 if (callable == nullptr) { | 214 if (callable == nullptr) { |
| 187 return nullptr; | 215 return nullptr; |
| 188 } | 216 } |
| 189 | 217 |
| 190 if (!ReturnType()->IsExactly(callable->ReturnType())) { | 218 if (!ReturnType()->IsExactly(callable->ReturnType())) { |
| 191 return AsmType::None(); | 219 return AsmType::None(); |
| 192 } | 220 } |
| 193 | 221 |
| 194 if (callable->Arguments().size() < 2) { | 222 if (callable->Arguments().size() < 2) { |
| 195 return AsmType::None(); | 223 return AsmType::None(); |
| 196 } | 224 } |
| 197 | 225 |
| 198 for (size_t ii = 0; ii < Arguments().size(); ++ii) { | 226 for (size_t ii = 0; ii < Arguments().size(); ++ii) { |
| 199 if (!Arguments()[0]->IsExactly(callable->Arguments()[ii])) { | 227 if (!Arguments()[0]->IsExactly(callable->Arguments()[ii])) { |
| 200 return AsmType::None(); | 228 return AsmType::None(); |
| 201 } | 229 } |
| 202 } | 230 } |
| 203 | 231 |
| 204 return ReturnType(); | 232 return ReturnType(); |
| 205 } | 233 } |
| 206 }; | 234 }; |
| 207 } // namespace | 235 } // namespace |
| 208 | 236 |
| 209 AsmType* AsmType::MinMaxType(Zone* zone, AsmType* type) { | 237 AsmType* AsmType::MinMaxType(Zone* zone, AsmType* dest, AsmType* src) { |
| 210 DCHECK(type->AsValueType() != nullptr); | 238 DCHECK(dest->AsValueType() != nullptr); |
| 211 auto* MinMax = new (zone) AsmMinMaxType(zone, type); | 239 DCHECK(src->AsValueType() != nullptr); |
| 240 auto* MinMax = new (zone) AsmMinMaxType(zone, dest, src); |
| 212 return reinterpret_cast<AsmType*>(MinMax); | 241 return reinterpret_cast<AsmType*>(MinMax); |
| 213 } | 242 } |
| 214 | 243 |
| 244 AsmType* AsmFFIType::ValidateCall(AsmType* function_type) { |
| 245 auto* callable = function_type->AsFunctionType(); |
| 246 if (callable == nullptr) { |
| 247 return nullptr; |
| 248 } |
| 249 |
| 250 for (int ii = 0; ii < callable->Arguments().size(); ++ii) { |
| 251 if (!callable->Arguments()[ii]->IsA(AsmType::Extern())) { |
| 252 return AsmType::None(); |
| 253 } |
| 254 } |
| 255 |
| 256 return callable->ReturnType(); |
| 257 } |
| 258 |
| 215 AsmType* AsmFunctionType::ValidateCall(AsmType* function_type) { | 259 AsmType* AsmFunctionType::ValidateCall(AsmType* function_type) { |
| 216 auto* callable = function_type->AsFunctionType(); | 260 auto* callable = function_type->AsFunctionType(); |
| 217 if (callable == nullptr) { | 261 if (callable == nullptr) { |
| 218 return nullptr; | 262 return nullptr; |
| 219 } | 263 } |
| 220 | 264 |
| 221 if (!return_type_->IsExactly(callable->return_type_)) { | 265 if (!return_type_->IsExactly(callable->return_type_)) { |
| 222 return AsmType::None(); | 266 return AsmType::None(); |
| 223 } | 267 } |
| 224 | 268 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 } | 307 } |
| 264 | 308 |
| 265 return AsmType::None(); | 309 return AsmType::None(); |
| 266 } | 310 } |
| 267 | 311 |
| 268 void AsmOverloadedFunctionType::AddOverload(AsmType* overload) { | 312 void AsmOverloadedFunctionType::AddOverload(AsmType* overload) { |
| 269 DCHECK(overload->AsFunctionType() != nullptr); | 313 DCHECK(overload->AsFunctionType() != nullptr); |
| 270 overloads_.push_back(overload); | 314 overloads_.push_back(overload); |
| 271 } | 315 } |
| 272 | 316 |
| 317 std::string AsmFunctionTableType::Name() { |
| 318 std::string base_name = |
| 319 signature_ == nullptr ? "[unbound]" : signature_->Name(); |
| 320 return base_name + "[" + std::to_string(length_) + "]"; |
| 321 } |
| 322 |
| 323 void AsmFunctionTableType::set_signature(AsmType* function_type) { |
| 324 CHECK(function_type->AsFunctionType() != nullptr); |
| 325 signature_ = function_type; |
| 326 } |
| 327 |
| 328 AsmType* AsmFunctionTableType::ValidateCall(AsmType* function_type) { |
| 329 if (signature_ == nullptr) { |
| 330 set_signature(function_type); |
| 331 } |
| 332 return signature_->AsCallableType()->ValidateCall(function_type); |
| 333 } |
| 334 |
| 273 } // namespace wasm | 335 } // namespace wasm |
| 274 } // namespace internal | 336 } // namespace internal |
| 275 } // namespace v8 | 337 } // namespace v8 |
| OLD | NEW |