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