| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 <math.h> |
| 6 |
| 5 #include "src/types.h" | 7 #include "src/types.h" |
| 6 | 8 |
| 7 #include "src/ostreams.h" | 9 #include "src/ostreams.h" |
| 8 #include "src/types-inl.h" | 10 #include "src/types-inl.h" |
| 9 | 11 |
| 10 namespace v8 { | 12 namespace v8 { |
| 11 namespace internal { | 13 namespace internal { |
| 12 | 14 |
| 15 |
| 16 // ----------------------------------------------------------------------------- |
| 17 // Range-related custom order on doubles. |
| 18 // We want -0 to be less than +0. |
| 19 |
| 20 static bool dle(double x, double y) { |
| 21 return x <= y && copysign(1, x) <= copysign(1, y); |
| 22 } |
| 23 |
| 24 |
| 25 static bool deq(double x, double y) { |
| 26 return dle(x, y) && dle(y, x); |
| 27 } |
| 28 |
| 29 |
| 13 // ----------------------------------------------------------------------------- | 30 // ----------------------------------------------------------------------------- |
| 14 // Glb and lub computation. | 31 // Glb and lub computation. |
| 15 | 32 |
| 16 // The largest bitset subsumed by this type. | 33 // The largest bitset subsumed by this type. |
| 17 template<class Config> | 34 template<class Config> |
| 18 int TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) { | 35 int TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) { |
| 19 DisallowHeapAllocation no_allocation; | 36 DisallowHeapAllocation no_allocation; |
| 20 if (type->IsBitset()) { | 37 if (type->IsBitset()) { |
| 21 return type->AsBitset(); | 38 return type->AsBitset(); |
| 22 } else if (type->IsUnion()) { | 39 } else if (type->IsUnion()) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 41 for (int i = 0; i < unioned->Length(); ++i) { | 58 for (int i = 0; i < unioned->Length(); ++i) { |
| 42 bitset |= unioned->Get(i)->BitsetLub(); | 59 bitset |= unioned->Get(i)->BitsetLub(); |
| 43 } | 60 } |
| 44 return bitset; | 61 return bitset; |
| 45 } else if (type->IsClass()) { | 62 } else if (type->IsClass()) { |
| 46 // Little hack to avoid the need for a region for handlification here... | 63 // Little hack to avoid the need for a region for handlification here... |
| 47 return Config::is_class(type) ? Lub(*Config::as_class(type)) : | 64 return Config::is_class(type) ? Lub(*Config::as_class(type)) : |
| 48 type->AsClass()->Bound(NULL)->AsBitset(); | 65 type->AsClass()->Bound(NULL)->AsBitset(); |
| 49 } else if (type->IsConstant()) { | 66 } else if (type->IsConstant()) { |
| 50 return type->AsConstant()->Bound()->AsBitset(); | 67 return type->AsConstant()->Bound()->AsBitset(); |
| 68 } else if (type->IsRange()) { |
| 69 return type->AsRange()->Bound()->AsBitset(); |
| 51 } else if (type->IsContext()) { | 70 } else if (type->IsContext()) { |
| 52 return type->AsContext()->Bound()->AsBitset(); | 71 return type->AsContext()->Bound()->AsBitset(); |
| 53 } else if (type->IsArray()) { | 72 } else if (type->IsArray()) { |
| 54 return type->AsArray()->Bound()->AsBitset(); | 73 return type->AsArray()->Bound()->AsBitset(); |
| 55 } else if (type->IsFunction()) { | 74 } else if (type->IsFunction()) { |
| 56 return type->AsFunction()->Bound()->AsBitset(); | 75 return type->AsFunction()->Bound()->AsBitset(); |
| 57 } else { | 76 } else { |
| 58 UNREACHABLE(); | 77 UNREACHABLE(); |
| 59 return kNone; | 78 return kNone; |
| 60 } | 79 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 71 UnionHandle unioned = handle(type->AsUnion()); | 90 UnionHandle unioned = handle(type->AsUnion()); |
| 72 int bitset = kNone; | 91 int bitset = kNone; |
| 73 for (int i = 0; i < unioned->Length(); ++i) { | 92 for (int i = 0; i < unioned->Length(); ++i) { |
| 74 bitset |= unioned->Get(i)->InherentBitsetLub(); | 93 bitset |= unioned->Get(i)->InherentBitsetLub(); |
| 75 } | 94 } |
| 76 return bitset; | 95 return bitset; |
| 77 } else if (type->IsClass()) { | 96 } else if (type->IsClass()) { |
| 78 return Lub(*type->AsClass()->Map()); | 97 return Lub(*type->AsClass()->Map()); |
| 79 } else if (type->IsConstant()) { | 98 } else if (type->IsConstant()) { |
| 80 return Lub(*type->AsConstant()->Value()); | 99 return Lub(*type->AsConstant()->Value()); |
| 100 } else if (type->IsRange()) { |
| 101 return Lub(type->AsRange()->Min(), type->AsRange()->Max()); |
| 81 } else if (type->IsContext()) { | 102 } else if (type->IsContext()) { |
| 82 return kInternal & kTaggedPtr; | 103 return kInternal & kTaggedPtr; |
| 83 } else if (type->IsArray()) { | 104 } else if (type->IsArray()) { |
| 84 return kArray; | 105 return kArray; |
| 85 } else if (type->IsFunction()) { | 106 } else if (type->IsFunction()) { |
| 86 return kFunction; | 107 return kFunction; |
| 87 } else { | 108 } else { |
| 88 UNREACHABLE(); | 109 UNREACHABLE(); |
| 89 return kNone; | 110 return kNone; |
| 90 } | 111 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 106 DisallowHeapAllocation no_allocation; | 127 DisallowHeapAllocation no_allocation; |
| 107 if (i::IsMinusZero(value)) return kMinusZero; | 128 if (i::IsMinusZero(value)) return kMinusZero; |
| 108 if (std::isnan(value)) return kNaN; | 129 if (std::isnan(value)) return kNaN; |
| 109 if (IsUint32Double(value)) return Lub(FastD2UI(value)); | 130 if (IsUint32Double(value)) return Lub(FastD2UI(value)); |
| 110 if (IsInt32Double(value)) return Lub(FastD2I(value)); | 131 if (IsInt32Double(value)) return Lub(FastD2I(value)); |
| 111 return kOtherNumber; | 132 return kOtherNumber; |
| 112 } | 133 } |
| 113 | 134 |
| 114 | 135 |
| 115 template<class Config> | 136 template<class Config> |
| 137 int TypeImpl<Config>::BitsetType::Lub(double min, double max) { |
| 138 DisallowHeapAllocation no_allocation; |
| 139 DCHECK(dle(min, max)); |
| 140 if (deq(min, max)) return BitsetType::Lub(min); // Singleton range. |
| 141 int bitset = BitsetType::kNumber ^ SEMANTIC(BitsetType::kNaN); |
| 142 if (dle(0, min) || max < 0) bitset ^= SEMANTIC(BitsetType::kMinusZero); |
| 143 return bitset; |
| 144 // TODO(neis): Could refine this further by doing more checks on min/max. |
| 145 } |
| 146 |
| 147 |
| 148 template<class Config> |
| 116 int TypeImpl<Config>::BitsetType::Lub(int32_t value) { | 149 int TypeImpl<Config>::BitsetType::Lub(int32_t value) { |
| 117 if (value >= 0x40000000) { | 150 if (value >= 0x40000000) { |
| 118 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall; | 151 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall; |
| 119 } | 152 } |
| 120 if (value >= 0) return kUnsignedSmall; | 153 if (value >= 0) return kUnsignedSmall; |
| 121 if (value >= -0x40000000) return kOtherSignedSmall; | 154 if (value >= -0x40000000) return kOtherSignedSmall; |
| 122 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall; | 155 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall; |
| 123 } | 156 } |
| 124 | 157 |
| 125 | 158 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 && *this->AsClass()->Map() == *that->AsClass()->Map() | 282 && *this->AsClass()->Map() == *that->AsClass()->Map() |
| 250 && ((Config::is_class(that) && Config::is_class(this)) || | 283 && ((Config::is_class(that) && Config::is_class(this)) || |
| 251 BitsetType::New(this->BitsetLub())->Is( | 284 BitsetType::New(this->BitsetLub())->Is( |
| 252 BitsetType::New(that->BitsetLub()))); | 285 BitsetType::New(that->BitsetLub()))); |
| 253 } | 286 } |
| 254 if (that->IsConstant()) { | 287 if (that->IsConstant()) { |
| 255 return this->IsConstant() | 288 return this->IsConstant() |
| 256 && *this->AsConstant()->Value() == *that->AsConstant()->Value() | 289 && *this->AsConstant()->Value() == *that->AsConstant()->Value() |
| 257 && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound()); | 290 && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound()); |
| 258 } | 291 } |
| 292 if (that->IsRange()) { |
| 293 return this->IsRange() |
| 294 && this->AsRange()->Bound()->Is(that->AsRange()->Bound()) |
| 295 && dle(that->AsRange()->Min(), this->AsRange()->Min()) |
| 296 && dle(this->AsRange()->Max(), that->AsRange()->Max()); |
| 297 } |
| 259 if (that->IsContext()) { | 298 if (that->IsContext()) { |
| 260 return this->IsContext() | 299 return this->IsContext() |
| 261 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer()); | 300 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer()); |
| 262 } | 301 } |
| 263 if (that->IsArray()) { | 302 if (that->IsArray()) { |
| 264 return this->IsArray() | 303 return this->IsArray() |
| 265 && this->AsArray()->Element()->Equals(that->AsArray()->Element()); | 304 && this->AsArray()->Element()->Equals(that->AsArray()->Element()); |
| 266 } | 305 } |
| 267 if (that->IsFunction()) { | 306 if (that->IsFunction()) { |
| 268 // We currently do not allow for any variance here, in order to keep | 307 // We currently do not allow for any variance here, in order to keep |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 } | 420 } |
| 382 | 421 |
| 383 return false; | 422 return false; |
| 384 } | 423 } |
| 385 | 424 |
| 386 | 425 |
| 387 // Check if value is contained in (inhabits) type. | 426 // Check if value is contained in (inhabits) type. |
| 388 template<class Config> | 427 template<class Config> |
| 389 bool TypeImpl<Config>::Contains(i::Object* value) { | 428 bool TypeImpl<Config>::Contains(i::Object* value) { |
| 390 DisallowHeapAllocation no_allocation; | 429 DisallowHeapAllocation no_allocation; |
| 430 if (this->IsRange()) { |
| 431 return value->IsNumber() && |
| 432 dle(this->AsRange()->Min(), value->Number()) && |
| 433 dle(value->Number(), this->AsRange()->Max()) && |
| 434 BitsetType::Is(BitsetType::Lub(value), this->BitsetLub()); |
| 435 } |
| 391 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) { | 436 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) { |
| 392 if (*it.Current() == value) return true; | 437 if (*it.Current() == value) return true; |
| 393 } | 438 } |
| 394 return BitsetType::New(BitsetType::Lub(value))->Is(this); | 439 return BitsetType::New(BitsetType::Lub(value))->Is(this); |
| 395 } | 440 } |
| 396 | 441 |
| 397 | 442 |
| 398 template<class Config> | 443 template<class Config> |
| 399 bool TypeImpl<Config>::UnionType::Wellformed() { | 444 bool TypeImpl<Config>::UnionType::Wellformed() { |
| 400 DCHECK(this->Length() >= 2); | 445 DCHECK(this->Length() >= 2); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 413 // Union and intersection | 458 // Union and intersection |
| 414 | 459 |
| 415 template<class Config> | 460 template<class Config> |
| 416 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound( | 461 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound( |
| 417 int bitset, Region* region) { | 462 int bitset, Region* region) { |
| 418 TypeHandle bound = BitsetType::New(bitset, region); | 463 TypeHandle bound = BitsetType::New(bitset, region); |
| 419 if (this->IsClass()) { | 464 if (this->IsClass()) { |
| 420 return ClassType::New(this->AsClass()->Map(), bound, region); | 465 return ClassType::New(this->AsClass()->Map(), bound, region); |
| 421 } else if (this->IsConstant()) { | 466 } else if (this->IsConstant()) { |
| 422 return ConstantType::New(this->AsConstant()->Value(), bound, region); | 467 return ConstantType::New(this->AsConstant()->Value(), bound, region); |
| 468 } else if (this->IsRange()) { |
| 469 return RangeType::New( |
| 470 this->AsRange()->Min(), this->AsRange()->Max(), bound, region); |
| 423 } else if (this->IsContext()) { | 471 } else if (this->IsContext()) { |
| 424 return ContextType::New(this->AsContext()->Outer(), bound, region); | 472 return ContextType::New(this->AsContext()->Outer(), bound, region); |
| 425 } else if (this->IsArray()) { | 473 } else if (this->IsArray()) { |
| 426 return ArrayType::New(this->AsArray()->Element(), bound, region); | 474 return ArrayType::New(this->AsArray()->Element(), bound, region); |
| 427 } else if (this->IsFunction()) { | 475 } else if (this->IsFunction()) { |
| 428 FunctionHandle function = Config::handle(this->AsFunction()); | 476 FunctionHandle function = Config::handle(this->AsFunction()); |
| 429 int arity = function->Arity(); | 477 int arity = function->Arity(); |
| 430 FunctionHandle type = FunctionType::New( | 478 FunctionHandle type = FunctionType::New( |
| 431 function->Result(), function->Receiver(), bound, arity, region); | 479 function->Result(), function->Receiver(), bound, arity, region); |
| 432 for (int i = 0; i < arity; ++i) { | 480 for (int i = 0; i < arity; ++i) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 if (type->IsUnion()) { | 549 if (type->IsUnion()) { |
| 502 UnionHandle unioned = handle(type->AsUnion()); | 550 UnionHandle unioned = handle(type->AsUnion()); |
| 503 for (int i = 0; i < unioned->Length(); ++i) { | 551 for (int i = 0; i < unioned->Length(); ++i) { |
| 504 TypeHandle type_i = unioned->Get(i); | 552 TypeHandle type_i = unioned->Get(i); |
| 505 DCHECK(i == 0 || !(type_i->IsBitset() || type_i->Is(unioned->Get(0)))); | 553 DCHECK(i == 0 || !(type_i->IsBitset() || type_i->Is(unioned->Get(0)))); |
| 506 if (!type_i->IsBitset()) { | 554 if (!type_i->IsBitset()) { |
| 507 size = ExtendUnion(result, size, type_i, other, is_intersect, region); | 555 size = ExtendUnion(result, size, type_i, other, is_intersect, region); |
| 508 } | 556 } |
| 509 } | 557 } |
| 510 } else if (!type->IsBitset()) { | 558 } else if (!type->IsBitset()) { |
| 511 DCHECK(type->IsClass() || type->IsConstant() || | 559 DCHECK(type->IsClass() || type->IsConstant() || type->IsRange() || |
| 512 type->IsArray() || type->IsFunction() || type->IsContext()); | 560 type->IsContext() || type->IsArray() || type->IsFunction()); |
| 513 int inherent_bound = type->InherentBitsetLub(); | 561 int inherent_bound = type->InherentBitsetLub(); |
| 514 int old_bound = type->BitsetLub(); | 562 int old_bound = type->BitsetLub(); |
| 515 int other_bound = type->BoundBy(other->unhandle()) & inherent_bound; | 563 int other_bound = type->BoundBy(other->unhandle()) & inherent_bound; |
| 516 int new_bound = | 564 int new_bound = |
| 517 is_intersect ? (old_bound & other_bound) : (old_bound | other_bound); | 565 is_intersect ? (old_bound & other_bound) : (old_bound | other_bound); |
| 518 if (new_bound != BitsetType::kNone) { | 566 if (new_bound != BitsetType::kNone) { |
| 519 int i = type->IndexInUnion(new_bound, result, size); | 567 int i = type->IndexInUnion(new_bound, result, size); |
| 520 if (i == -1) { | 568 if (i == -1) { |
| 521 i = size++; | 569 i = size++; |
| 522 } else if (result->Get(i)->IsBitset()) { | 570 } else if (result->Get(i)->IsBitset()) { |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert( | 793 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert( |
| 746 typename OtherType::TypeHandle type, Region* region) { | 794 typename OtherType::TypeHandle type, Region* region) { |
| 747 if (type->IsBitset()) { | 795 if (type->IsBitset()) { |
| 748 return BitsetType::New(type->AsBitset(), region); | 796 return BitsetType::New(type->AsBitset(), region); |
| 749 } else if (type->IsClass()) { | 797 } else if (type->IsClass()) { |
| 750 TypeHandle bound = BitsetType::New(type->BitsetLub(), region); | 798 TypeHandle bound = BitsetType::New(type->BitsetLub(), region); |
| 751 return ClassType::New(type->AsClass()->Map(), bound, region); | 799 return ClassType::New(type->AsClass()->Map(), bound, region); |
| 752 } else if (type->IsConstant()) { | 800 } else if (type->IsConstant()) { |
| 753 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region); | 801 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region); |
| 754 return ConstantType::New(type->AsConstant()->Value(), bound, region); | 802 return ConstantType::New(type->AsConstant()->Value(), bound, region); |
| 803 } else if (type->IsRange()) { |
| 804 TypeHandle bound = Convert<OtherType>(type->AsRange()->Bound(), region); |
| 805 return RangeType::New( |
| 806 type->AsRange()->Min(), type->AsRange()->Max(), bound, region); |
| 755 } else if (type->IsContext()) { | 807 } else if (type->IsContext()) { |
| 756 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region); | 808 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region); |
| 757 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region); | 809 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region); |
| 758 return ContextType::New(outer, bound, region); | 810 return ContextType::New(outer, bound, region); |
| 759 } else if (type->IsUnion()) { | 811 } else if (type->IsUnion()) { |
| 760 int length = type->AsUnion()->Length(); | 812 int length = type->AsUnion()->Length(); |
| 761 UnionHandle unioned = UnionType::New(length, region); | 813 UnionHandle unioned = UnionType::New(length, region); |
| 762 for (int i = 0; i < length; ++i) { | 814 for (int i = 0; i < length; ++i) { |
| 763 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region); | 815 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region); |
| 764 unioned->Set(i, t); | 816 unioned->Set(i, t); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 928 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; | 980 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; |
| 929 | 981 |
| 930 template TypeImpl<ZoneTypeConfig>::TypeHandle | 982 template TypeImpl<ZoneTypeConfig>::TypeHandle |
| 931 TypeImpl<ZoneTypeConfig>::Convert<HeapType>( | 983 TypeImpl<ZoneTypeConfig>::Convert<HeapType>( |
| 932 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*); | 984 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*); |
| 933 template TypeImpl<HeapTypeConfig>::TypeHandle | 985 template TypeImpl<HeapTypeConfig>::TypeHandle |
| 934 TypeImpl<HeapTypeConfig>::Convert<Type>( | 986 TypeImpl<HeapTypeConfig>::Convert<Type>( |
| 935 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*); | 987 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*); |
| 936 | 988 |
| 937 } } // namespace v8::internal | 989 } } // namespace v8::internal |
| OLD | NEW |