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
3 // found in the LICENSE file.
7 #include "src/ostreams.h"
8 #include "src/types-inl.h"
14 // -----------------------------------------------------------------------------
15 // Range-related custom order on doubles.
16 // We want -0 to be less than +0.
18 static bool dle(double x, double y) {
19 return x <= y && (x != 0 || IsMinusZero(x) || !IsMinusZero(y));
23 static bool deq(double x, double y) {
24 return dle(x, y) && dle(y, x);
28 // -----------------------------------------------------------------------------
29 // Glb and lub computation.
31 // The largest bitset subsumed by this type.
32 template<class Config>
33 int TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) {
34 DisallowHeapAllocation no_allocation;
35 if (type->IsBitset()) {
36 return type->AsBitset();
37 } else if (type->IsUnion()) {
38 UnionHandle unioned = handle(type->AsUnion());
39 DCHECK(unioned->Wellformed());
40 return unioned->Get(0)->BitsetGlb(); // Other BitsetGlb's are kNone anyway.
47 // The smallest bitset subsuming this type.
48 template<class Config>
49 int TypeImpl<Config>::BitsetType::Lub(TypeImpl* type) {
50 DisallowHeapAllocation no_allocation;
51 if (type->IsBitset()) {
52 return type->AsBitset();
53 } else if (type->IsUnion()) {
54 UnionHandle unioned = handle(type->AsUnion());
56 for (int i = 0; i < unioned->Length(); ++i) {
57 bitset |= unioned->Get(i)->BitsetLub();
60 } else if (type->IsClass()) {
61 // Little hack to avoid the need for a region for handlification here...
62 return Config::is_class(type) ? Lub(*Config::as_class(type)) :
63 type->AsClass()->Bound(NULL)->AsBitset();
64 } else if (type->IsConstant()) {
65 return type->AsConstant()->Bound()->AsBitset();
66 } else if (type->IsRange()) {
67 return type->AsRange()->Bound()->AsBitset();
68 } else if (type->IsContext()) {
69 return type->AsContext()->Bound()->AsBitset();
70 } else if (type->IsArray()) {
71 return type->AsArray()->Bound()->AsBitset();
72 } else if (type->IsFunction()) {
73 return type->AsFunction()->Bound()->AsBitset();
81 // The smallest bitset subsuming this type, ignoring explicit bounds.
82 template<class Config>
83 int TypeImpl<Config>::BitsetType::InherentLub(TypeImpl* type) {
84 DisallowHeapAllocation no_allocation;
85 if (type->IsBitset()) {
86 return type->AsBitset();
87 } else if (type->IsUnion()) {
88 UnionHandle unioned = handle(type->AsUnion());
90 for (int i = 0; i < unioned->Length(); ++i) {
91 bitset |= unioned->Get(i)->InherentBitsetLub();
94 } else if (type->IsClass()) {
95 return Lub(*type->AsClass()->Map());
96 } else if (type->IsConstant()) {
97 return Lub(*type->AsConstant()->Value());
98 } else if (type->IsRange()) {
99 return Lub(type->AsRange()->Min(), type->AsRange()->Max());
100 } else if (type->IsContext()) {
101 return kInternal & kTaggedPtr;
102 } else if (type->IsArray()) {
104 } else if (type->IsFunction()) {
113 template<class Config>
114 int TypeImpl<Config>::BitsetType::Lub(i::Object* value) {
115 DisallowHeapAllocation no_allocation;
116 if (value->IsNumber()) {
117 return Lub(value->Number()) & (value->IsSmi() ? kTaggedInt : kTaggedPtr);
119 return Lub(i::HeapObject::cast(value)->map());
123 template<class Config>
124 int TypeImpl<Config>::BitsetType::Lub(double value) {
125 DisallowHeapAllocation no_allocation;
126 if (i::IsMinusZero(value)) return kMinusZero;
127 if (std::isnan(value)) return kNaN;
128 if (IsUint32Double(value)) return Lub(FastD2UI(value));
129 if (IsInt32Double(value)) return Lub(FastD2I(value));
134 template<class Config>
135 int TypeImpl<Config>::BitsetType::Lub(double min, double max) {
136 DisallowHeapAllocation no_allocation;
137 DCHECK(dle(min, max));
138 if (deq(min, max)) return BitsetType::Lub(min); // Singleton range.
139 int bitset = BitsetType::kNumber ^ SEMANTIC(BitsetType::kNaN);
140 if (dle(0, min) || max < 0) bitset ^= SEMANTIC(BitsetType::kMinusZero);
142 // TODO(neis): Could refine this further by doing more checks on min/max.
146 template<class Config>
147 int TypeImpl<Config>::BitsetType::Lub(int32_t value) {
148 if (value >= 0x40000000) {
149 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
151 if (value >= 0) return kUnsignedSmall;
152 if (value >= -0x40000000) return kOtherSignedSmall;
153 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall;
157 template<class Config>
158 int TypeImpl<Config>::BitsetType::Lub(uint32_t value) {
159 DisallowHeapAllocation no_allocation;
160 if (value >= 0x80000000u) return kOtherUnsigned32;
161 if (value >= 0x40000000u) {
162 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
164 return kUnsignedSmall;
168 template<class Config>
169 int TypeImpl<Config>::BitsetType::Lub(i::Map* map) {
170 DisallowHeapAllocation no_allocation;
171 switch (map->instance_type()) {
173 case ASCII_STRING_TYPE:
174 case CONS_STRING_TYPE:
175 case CONS_ASCII_STRING_TYPE:
176 case SLICED_STRING_TYPE:
177 case SLICED_ASCII_STRING_TYPE:
178 case EXTERNAL_STRING_TYPE:
179 case EXTERNAL_ASCII_STRING_TYPE:
180 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
181 case SHORT_EXTERNAL_STRING_TYPE:
182 case SHORT_EXTERNAL_ASCII_STRING_TYPE:
183 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
184 case INTERNALIZED_STRING_TYPE:
185 case ASCII_INTERNALIZED_STRING_TYPE:
186 case EXTERNAL_INTERNALIZED_STRING_TYPE:
187 case EXTERNAL_ASCII_INTERNALIZED_STRING_TYPE:
188 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
189 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
190 case SHORT_EXTERNAL_ASCII_INTERNALIZED_STRING_TYPE:
191 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
196 Heap* heap = map->GetHeap();
197 if (map == heap->undefined_map()) return kUndefined;
198 if (map == heap->null_map()) return kNull;
199 if (map == heap->boolean_map()) return kBoolean;
200 DCHECK(map == heap->the_hole_map() ||
201 map == heap->uninitialized_map() ||
202 map == heap->no_interceptor_result_sentinel_map() ||
203 map == heap->termination_exception_map() ||
204 map == heap->arguments_marker_map());
205 return kInternal & kTaggedPtr;
207 case HEAP_NUMBER_TYPE:
208 return kNumber & kTaggedPtr;
212 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
213 case JS_GENERATOR_OBJECT_TYPE:
215 case JS_GLOBAL_OBJECT_TYPE:
216 case JS_BUILTINS_OBJECT_TYPE:
217 case JS_GLOBAL_PROXY_TYPE:
218 case JS_ARRAY_BUFFER_TYPE:
219 case JS_TYPED_ARRAY_TYPE:
220 case JS_DATA_VIEW_TYPE:
223 case JS_SET_ITERATOR_TYPE:
224 case JS_MAP_ITERATOR_TYPE:
225 case JS_WEAK_MAP_TYPE:
226 case JS_WEAK_SET_TYPE:
230 if (map->is_undetectable()) return kUndetectable;
234 case JS_FUNCTION_TYPE:
239 case JS_FUNCTION_PROXY_TYPE:
242 // When compiling stub templates, the meta map is used as a place holder
243 // for the actual map with which the template is later instantiated.
244 // We treat it as a kind of type variable whose upper bound is Any.
245 // TODO(rossberg): for caching of CompareNilIC stubs to work correctly,
246 // we must exclude Undetectable here. This makes no sense, really,
247 // because it means that the template isn't actually parametric.
248 // Also, it doesn't apply elsewhere. 8-(
249 // We ought to find a cleaner solution for compiling stubs parameterised
250 // over type or class variables, esp ones with bounds...
252 case DECLARED_ACCESSOR_INFO_TYPE:
253 case EXECUTABLE_ACCESSOR_INFO_TYPE:
254 case SHARED_FUNCTION_INFO_TYPE:
255 case ACCESSOR_PAIR_TYPE:
256 case FIXED_ARRAY_TYPE:
259 return kInternal & kTaggedPtr;
267 // -----------------------------------------------------------------------------
270 // Check this <= that.
271 template<class Config>
272 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
273 DisallowHeapAllocation no_allocation;
275 // Fast path for bitsets.
276 if (this->IsNone()) return true;
277 if (that->IsBitset()) {
278 return BitsetType::Is(BitsetType::Lub(this), that->AsBitset());
281 if (that->IsClass()) {
282 return this->IsClass()
283 && *this->AsClass()->Map() == *that->AsClass()->Map()
284 && ((Config::is_class(that) && Config::is_class(this)) ||
285 BitsetType::New(this->BitsetLub())->Is(
286 BitsetType::New(that->BitsetLub())));
288 if (that->IsConstant()) {
289 return this->IsConstant()
290 && *this->AsConstant()->Value() == *that->AsConstant()->Value()
291 && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound());
293 if (that->IsRange()) {
294 return this->IsRange()
295 && this->AsRange()->Bound()->Is(that->AsRange()->Bound())
296 && dle(that->AsRange()->Min(), this->AsRange()->Min())
297 && dle(this->AsRange()->Max(), that->AsRange()->Max());
299 if (that->IsContext()) {
300 return this->IsContext()
301 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
303 if (that->IsArray()) {
304 return this->IsArray()
305 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
307 if (that->IsFunction()) {
308 // We currently do not allow for any variance here, in order to keep
309 // Union and Intersect operations simple.
310 if (!this->IsFunction()) return false;
311 FunctionType* this_fun = this->AsFunction();
312 FunctionType* that_fun = that->AsFunction();
313 if (this_fun->Arity() != that_fun->Arity() ||
314 !this_fun->Result()->Equals(that_fun->Result()) ||
315 !that_fun->Receiver()->Equals(this_fun->Receiver())) {
318 for (int i = 0; i < this_fun->Arity(); ++i) {
319 if (!that_fun->Parameter(i)->Equals(this_fun->Parameter(i))) return false;
324 // (T1 \/ ... \/ Tn) <= T <=> (T1 <= T) /\ ... /\ (Tn <= T)
325 if (this->IsUnion()) {
326 UnionHandle unioned = handle(this->AsUnion());
327 for (int i = 0; i < unioned->Length(); ++i) {
328 if (!unioned->Get(i)->Is(that)) return false;
333 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn)
334 // (iff T is not a union)
335 DCHECK(!this->IsUnion() && that->IsUnion());
336 UnionHandle unioned = handle(that->AsUnion());
337 for (int i = 0; i < unioned->Length(); ++i) {
338 if (this->Is(unioned->Get(i))) return true;
339 if (this->IsBitset()) break; // Fast fail, only first field is a bitset.
345 template<class Config>
346 bool TypeImpl<Config>::NowIs(TypeImpl* that) {
347 DisallowHeapAllocation no_allocation;
349 // TODO(rossberg): this is incorrect for
350 // Union(Constant(V), T)->NowIs(Class(M))
351 // but fuzzing does not cover that!
352 if (this->IsConstant()) {
353 i::Object* object = *this->AsConstant()->Value();
354 if (object->IsHeapObject()) {
355 i::Map* map = i::HeapObject::cast(object)->map();
356 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
357 if (*it.Current() == map) return true;
361 return this->Is(that);
365 // Check if this contains only (currently) stable classes.
366 template<class Config>
367 bool TypeImpl<Config>::NowStable() {
368 DisallowHeapAllocation no_allocation;
369 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
370 if (!it.Current()->is_stable()) return false;
376 // Check this overlaps that.
377 template<class Config>
378 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
379 DisallowHeapAllocation no_allocation;
381 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T)
382 if (this->IsUnion()) {
383 UnionHandle unioned = handle(this->AsUnion());
384 for (int i = 0; i < unioned->Length(); ++i) {
385 if (unioned->Get(i)->Maybe(that)) return true;
390 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn)
391 if (that->IsUnion()) {
392 UnionHandle unioned = handle(that->AsUnion());
393 for (int i = 0; i < unioned->Length(); ++i) {
394 if (this->Maybe(unioned->Get(i))) return true;
399 DCHECK(!this->IsUnion() && !that->IsUnion());
400 if (this->IsBitset() || that->IsBitset()) {
401 return BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub());
403 if (this->IsClass()) {
404 return that->IsClass()
405 && *this->AsClass()->Map() == *that->AsClass()->Map();
407 if (this->IsConstant()) {
408 return that->IsConstant()
409 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
411 if (this->IsContext()) {
412 return this->Equals(that);
414 if (this->IsArray()) {
415 // There is no variance!
416 return this->Equals(that);
418 if (this->IsFunction()) {
419 // There is no variance!
420 return this->Equals(that);
427 // Check if value is contained in (inhabits) type.
428 template<class Config>
429 bool TypeImpl<Config>::Contains(i::Object* value) {
430 DisallowHeapAllocation no_allocation;
431 if (this->IsRange()) {
432 return value->IsNumber() &&
433 dle(this->AsRange()->Min(), value->Number()) &&
434 dle(value->Number(), this->AsRange()->Max()) &&
435 BitsetType::Is(BitsetType::Lub(value), this->BitsetLub());
437 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
438 if (*it.Current() == value) return true;
440 return BitsetType::New(BitsetType::Lub(value))->Is(this);
444 template<class Config>
445 bool TypeImpl<Config>::UnionType::Wellformed() {
446 DCHECK(this->Length() >= 2);
447 for (int i = 0; i < this->Length(); ++i) {
448 DCHECK(!this->Get(i)->IsUnion());
449 if (i > 0) DCHECK(!this->Get(i)->IsBitset());
450 for (int j = 0; j < this->Length(); ++j) {
451 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j)));
458 // -----------------------------------------------------------------------------
459 // Union and intersection
461 template<class Config>
462 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound(
463 int bitset, Region* region) {
464 TypeHandle bound = BitsetType::New(bitset, region);
465 if (this->IsClass()) {
466 return ClassType::New(this->AsClass()->Map(), bound, region);
467 } else if (this->IsConstant()) {
468 return ConstantType::New(this->AsConstant()->Value(), bound, region);
469 } else if (this->IsRange()) {
470 return RangeType::New(
471 this->AsRange()->Min(), this->AsRange()->Max(), bound, region);
472 } else if (this->IsContext()) {
473 return ContextType::New(this->AsContext()->Outer(), bound, region);
474 } else if (this->IsArray()) {
475 return ArrayType::New(this->AsArray()->Element(), bound, region);
476 } else if (this->IsFunction()) {
477 FunctionHandle function = Config::handle(this->AsFunction());
478 int arity = function->Arity();
479 FunctionHandle type = FunctionType::New(
480 function->Result(), function->Receiver(), bound, arity, region);
481 for (int i = 0; i < arity; ++i) {
482 type->InitParameter(i, function->Parameter(i));
491 template<class Config>
492 int TypeImpl<Config>::BoundBy(TypeImpl* that) {
493 DCHECK(!this->IsUnion());
494 if (that->IsUnion()) {
495 UnionType* unioned = that->AsUnion();
496 int length = unioned->Length();
497 int bitset = BitsetType::kNone;
498 for (int i = 0; i < length; ++i) {
499 bitset |= BoundBy(unioned->Get(i)->unhandle());
502 } else if (that->IsClass() && this->IsClass() &&
503 *this->AsClass()->Map() == *that->AsClass()->Map()) {
504 return that->BitsetLub();
505 } else if (that->IsConstant() && this->IsConstant() &&
506 *this->AsConstant()->Value() == *that->AsConstant()->Value()) {
507 return that->AsConstant()->Bound()->AsBitset();
508 } else if (that->IsContext() && this->IsContext() && this->Is(that)) {
509 return that->AsContext()->Bound()->AsBitset();
510 } else if (that->IsArray() && this->IsArray() && this->Is(that)) {
511 return that->AsArray()->Bound()->AsBitset();
512 } else if (that->IsFunction() && this->IsFunction() && this->Is(that)) {
513 return that->AsFunction()->Bound()->AsBitset();
515 return that->BitsetGlb();
519 template<class Config>
520 int TypeImpl<Config>::IndexInUnion(
521 int bound, UnionHandle unioned, int current_size) {
522 DCHECK(!this->IsUnion());
523 for (int i = 0; i < current_size; ++i) {
524 TypeHandle that = unioned->Get(i);
525 if (that->IsBitset()) {
526 if (BitsetType::Is(bound, that->AsBitset())) return i;
527 } else if (that->IsClass() && this->IsClass()) {
528 if (*this->AsClass()->Map() == *that->AsClass()->Map()) return i;
529 } else if (that->IsConstant() && this->IsConstant()) {
530 if (*this->AsConstant()->Value() == *that->AsConstant()->Value())
532 } else if (that->IsContext() && this->IsContext()) {
533 if (this->Is(that)) return i;
534 } else if (that->IsArray() && this->IsArray()) {
535 if (this->Is(that)) return i;
536 } else if (that->IsFunction() && this->IsFunction()) {
537 if (this->Is(that)) return i;
544 // Get non-bitsets from type, bounded by upper.
545 // Store at result starting at index. Returns updated index.
546 template<class Config>
547 int TypeImpl<Config>::ExtendUnion(
548 UnionHandle result, int size, TypeHandle type,
549 TypeHandle other, bool is_intersect, Region* region) {
550 if (type->IsUnion()) {
551 UnionHandle unioned = handle(type->AsUnion());
552 for (int i = 0; i < unioned->Length(); ++i) {
553 TypeHandle type_i = unioned->Get(i);
554 DCHECK(i == 0 || !(type_i->IsBitset() || type_i->Is(unioned->Get(0))));
555 if (!type_i->IsBitset()) {
556 size = ExtendUnion(result, size, type_i, other, is_intersect, region);
559 } else if (!type->IsBitset()) {
560 DCHECK(type->IsClass() || type->IsConstant() || type->IsRange() ||
561 type->IsContext() || type->IsArray() || type->IsFunction());
562 int inherent_bound = type->InherentBitsetLub();
563 int old_bound = type->BitsetLub();
564 int other_bound = type->BoundBy(other->unhandle()) & inherent_bound;
566 is_intersect ? (old_bound & other_bound) : (old_bound | other_bound);
567 if (new_bound != BitsetType::kNone) {
568 int i = type->IndexInUnion(new_bound, result, size);
571 } else if (result->Get(i)->IsBitset()) {
572 return size; // Already fully subsumed.
574 int type_i_bound = result->Get(i)->BitsetLub();
575 new_bound |= type_i_bound;
576 if (new_bound == type_i_bound) return size;
578 if (new_bound != old_bound) type = type->Rebound(new_bound, region);
579 result->Set(i, type);
586 // Union is O(1) on simple bitsets, but O(n*m) on structured unions.
587 template<class Config>
588 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union(
589 TypeHandle type1, TypeHandle type2, Region* region) {
590 // Fast case: bit sets.
591 if (type1->IsBitset() && type2->IsBitset()) {
592 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region);
595 // Fast case: top or bottom types.
596 if (type1->IsAny() || type2->IsNone()) return type1;
597 if (type2->IsAny() || type1->IsNone()) return type2;
599 // Semi-fast case: Unioned objects are neither involved nor produced.
600 if (!(type1->IsUnion() || type2->IsUnion())) {
601 if (type1->Is(type2)) return type2;
602 if (type2->Is(type1)) return type1;
605 // Slow case: may need to produce a Unioned object.
607 if (!type1->IsBitset()) {
608 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
610 if (!type2->IsBitset()) {
611 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
613 int bitset = type1->BitsetGlb() | type2->BitsetGlb();
614 if (bitset != BitsetType::kNone) ++size;
617 UnionHandle unioned = UnionType::New(size, region);
619 if (bitset != BitsetType::kNone) {
620 unioned->Set(size++, BitsetType::New(bitset, region));
622 size = ExtendUnion(unioned, size, type1, type2, false, region);
623 size = ExtendUnion(unioned, size, type2, type1, false, region);
626 return unioned->Get(0);
628 unioned->Shrink(size);
629 DCHECK(unioned->Wellformed());
635 // Intersection is O(1) on simple bitsets, but O(n*m) on structured unions.
636 template<class Config>
637 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
638 TypeHandle type1, TypeHandle type2, Region* region) {
639 // Fast case: bit sets.
640 if (type1->IsBitset() && type2->IsBitset()) {
641 return BitsetType::New(type1->AsBitset() & type2->AsBitset(), region);
644 // Fast case: top or bottom types.
645 if (type1->IsNone() || type2->IsAny()) return type1;
646 if (type2->IsNone() || type1->IsAny()) return type2;
648 // Semi-fast case: Unioned objects are neither involved nor produced.
649 if (!(type1->IsUnion() || type2->IsUnion())) {
650 if (type1->Is(type2)) return type1;
651 if (type2->Is(type1)) return type2;
654 // Slow case: may need to produce a Unioned object.
656 if (!type1->IsBitset()) {
657 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
659 if (!type2->IsBitset()) {
660 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
662 int bitset = type1->BitsetGlb() & type2->BitsetGlb();
663 if (bitset != BitsetType::kNone) ++size;
666 UnionHandle unioned = UnionType::New(size, region);
668 if (bitset != BitsetType::kNone) {
669 unioned->Set(size++, BitsetType::New(bitset, region));
671 size = ExtendUnion(unioned, size, type1, type2, true, region);
672 size = ExtendUnion(unioned, size, type2, type1, true, region);
676 } else if (size == 1) {
677 return unioned->Get(0);
679 unioned->Shrink(size);
680 DCHECK(unioned->Wellformed());
686 // -----------------------------------------------------------------------------
689 template<class Config>
690 int TypeImpl<Config>::NumClasses() {
691 DisallowHeapAllocation no_allocation;
692 if (this->IsClass()) {
694 } else if (this->IsUnion()) {
695 UnionHandle unioned = handle(this->AsUnion());
697 for (int i = 0; i < unioned->Length(); ++i) {
698 if (unioned->Get(i)->IsClass()) ++result;
707 template<class Config>
708 int TypeImpl<Config>::NumConstants() {
709 DisallowHeapAllocation no_allocation;
710 if (this->IsConstant()) {
712 } else if (this->IsUnion()) {
713 UnionHandle unioned = handle(this->AsUnion());
715 for (int i = 0; i < unioned->Length(); ++i) {
716 if (unioned->Get(i)->IsConstant()) ++result;
725 template<class Config> template<class T>
726 typename TypeImpl<Config>::TypeHandle
727 TypeImpl<Config>::Iterator<T>::get_type() {
729 return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_;
733 // C++ cannot specialise nested templates, so we have to go through this
734 // contortion with an auxiliary template to simulate it.
735 template<class Config, class T>
736 struct TypeImplIteratorAux {
737 static bool matches(typename TypeImpl<Config>::TypeHandle type);
738 static i::Handle<T> current(typename TypeImpl<Config>::TypeHandle type);
741 template<class Config>
742 struct TypeImplIteratorAux<Config, i::Map> {
743 static bool matches(typename TypeImpl<Config>::TypeHandle type) {
744 return type->IsClass();
746 static i::Handle<i::Map> current(typename TypeImpl<Config>::TypeHandle type) {
747 return type->AsClass()->Map();
751 template<class Config>
752 struct TypeImplIteratorAux<Config, i::Object> {
753 static bool matches(typename TypeImpl<Config>::TypeHandle type) {
754 return type->IsConstant();
756 static i::Handle<i::Object> current(
757 typename TypeImpl<Config>::TypeHandle type) {
758 return type->AsConstant()->Value();
762 template<class Config> template<class T>
763 bool TypeImpl<Config>::Iterator<T>::matches(TypeHandle type) {
764 return TypeImplIteratorAux<Config, T>::matches(type);
767 template<class Config> template<class T>
768 i::Handle<T> TypeImpl<Config>::Iterator<T>::Current() {
769 return TypeImplIteratorAux<Config, T>::current(get_type());
773 template<class Config> template<class T>
774 void TypeImpl<Config>::Iterator<T>::Advance() {
775 DisallowHeapAllocation no_allocation;
777 if (type_->IsUnion()) {
778 UnionHandle unioned = handle(type_->AsUnion());
779 for (; index_ < unioned->Length(); ++index_) {
780 if (matches(unioned->Get(index_))) return;
782 } else if (index_ == 0 && matches(type_)) {
789 // -----------------------------------------------------------------------------
790 // Conversion between low-level representations.
792 template<class Config>
793 template<class OtherType>
794 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
795 typename OtherType::TypeHandle type, Region* region) {
796 if (type->IsBitset()) {
797 return BitsetType::New(type->AsBitset(), region);
798 } else if (type->IsClass()) {
799 TypeHandle bound = BitsetType::New(type->BitsetLub(), region);
800 return ClassType::New(type->AsClass()->Map(), bound, region);
801 } else if (type->IsConstant()) {
802 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region);
803 return ConstantType::New(type->AsConstant()->Value(), bound, region);
804 } else if (type->IsRange()) {
805 TypeHandle bound = Convert<OtherType>(type->AsRange()->Bound(), region);
806 return RangeType::New(
807 type->AsRange()->Min(), type->AsRange()->Max(), bound, region);
808 } else if (type->IsContext()) {
809 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region);
810 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region);
811 return ContextType::New(outer, bound, region);
812 } else if (type->IsUnion()) {
813 int length = type->AsUnion()->Length();
814 UnionHandle unioned = UnionType::New(length, region);
815 for (int i = 0; i < length; ++i) {
816 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region);
820 } else if (type->IsArray()) {
821 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region);
822 TypeHandle bound = Convert<OtherType>(type->AsArray()->Bound(), region);
823 return ArrayType::New(element, bound, region);
824 } else if (type->IsFunction()) {
825 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region);
826 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region);
827 TypeHandle bound = Convert<OtherType>(type->AsFunction()->Bound(), region);
828 FunctionHandle function = FunctionType::New(
829 res, rcv, bound, type->AsFunction()->Arity(), region);
830 for (int i = 0; i < function->Arity(); ++i) {
831 TypeHandle param = Convert<OtherType>(
832 type->AsFunction()->Parameter(i), region);
833 function->InitParameter(i, param);
843 // -----------------------------------------------------------------------------
846 template<class Config>
847 const char* TypeImpl<Config>::BitsetType::Name(int bitset) {
849 case REPRESENTATION(kAny): return "Any";
850 #define RETURN_NAMED_REPRESENTATION_TYPE(type, value) \
851 case REPRESENTATION(k##type): return #type;
852 REPRESENTATION_BITSET_TYPE_LIST(RETURN_NAMED_REPRESENTATION_TYPE)
853 #undef RETURN_NAMED_REPRESENTATION_TYPE
855 #define RETURN_NAMED_SEMANTIC_TYPE(type, value) \
856 case SEMANTIC(k##type): return #type;
857 SEMANTIC_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
858 #undef RETURN_NAMED_SEMANTIC_TYPE
866 template <class Config>
867 void TypeImpl<Config>::BitsetType::Print(OStream& os, // NOLINT
869 DisallowHeapAllocation no_allocation;
870 const char* name = Name(bitset);
876 static const int named_bitsets[] = {
877 #define BITSET_CONSTANT(type, value) REPRESENTATION(k##type),
878 REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT)
879 #undef BITSET_CONSTANT
881 #define BITSET_CONSTANT(type, value) SEMANTIC(k##type),
882 SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
883 #undef BITSET_CONSTANT
886 bool is_first = true;
888 for (int i(ARRAY_SIZE(named_bitsets) - 1); bitset != 0 && i >= 0; --i) {
889 int subset = named_bitsets[i];
890 if ((bitset & subset) == subset) {
891 if (!is_first) os << " | ";
902 template <class Config>
903 void TypeImpl<Config>::PrintTo(OStream& os, PrintDimension dim) { // NOLINT
904 DisallowHeapAllocation no_allocation;
905 if (dim != REPRESENTATION_DIM) {
906 if (this->IsBitset()) {
907 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
908 } else if (this->IsClass()) {
909 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
910 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
912 } else if (this->IsConstant()) {
913 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value())
915 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
917 } else if (this->IsRange()) {
918 os << "Range(" << this->AsRange()->Min()
919 << ".." << this->AsRange()->Max() << " : ";
920 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
922 } else if (this->IsContext()) {
924 this->AsContext()->Outer()->PrintTo(os, dim);
926 } else if (this->IsUnion()) {
928 UnionHandle unioned = handle(this->AsUnion());
929 for (int i = 0; i < unioned->Length(); ++i) {
930 TypeHandle type_i = unioned->Get(i);
931 if (i > 0) os << " | ";
932 type_i->PrintTo(os, dim);
935 } else if (this->IsArray()) {
937 AsArray()->Element()->PrintTo(os, dim);
939 } else if (this->IsFunction()) {
940 if (!this->AsFunction()->Receiver()->IsAny()) {
941 this->AsFunction()->Receiver()->PrintTo(os, dim);
945 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
946 if (i > 0) os << ", ";
947 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
950 this->AsFunction()->Result()->PrintTo(os, dim);
955 if (dim == BOTH_DIMS) os << "/";
956 if (dim != SEMANTIC_DIM) {
957 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
963 template <class Config>
964 void TypeImpl<Config>::Print() {
972 // -----------------------------------------------------------------------------
975 template class TypeImpl<ZoneTypeConfig>;
976 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
977 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
979 template class TypeImpl<HeapTypeConfig>;
980 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
981 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
983 template TypeImpl<ZoneTypeConfig>::TypeHandle
984 TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
985 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
986 template TypeImpl<HeapTypeConfig>::TypeHandle
987 TypeImpl<HeapTypeConfig>::Convert<Type>(
988 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
990 } } // namespace v8::internal