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:
227 if (map->is_undetectable()) return kUndetectable;
231 case JS_FUNCTION_TYPE:
236 case JS_FUNCTION_PROXY_TYPE:
239 // When compiling stub templates, the meta map is used as a place holder
240 // for the actual map with which the template is later instantiated.
241 // We treat it as a kind of type variable whose upper bound is Any.
242 // TODO(rossberg): for caching of CompareNilIC stubs to work correctly,
243 // we must exclude Undetectable here. This makes no sense, really,
244 // because it means that the template isn't actually parametric.
245 // Also, it doesn't apply elsewhere. 8-(
246 // We ought to find a cleaner solution for compiling stubs parameterised
247 // over type or class variables, esp ones with bounds...
249 case DECLARED_ACCESSOR_INFO_TYPE:
250 case EXECUTABLE_ACCESSOR_INFO_TYPE:
251 case SHARED_FUNCTION_INFO_TYPE:
252 case ACCESSOR_PAIR_TYPE:
253 case FIXED_ARRAY_TYPE:
256 return kInternal & kTaggedPtr;
264 // -----------------------------------------------------------------------------
267 // Check this <= that.
268 template<class Config>
269 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
270 DisallowHeapAllocation no_allocation;
272 // Fast path for bitsets.
273 if (this->IsNone()) return true;
274 if (that->IsBitset()) {
275 return BitsetType::Is(BitsetType::Lub(this), that->AsBitset());
278 if (that->IsClass()) {
279 return this->IsClass()
280 && *this->AsClass()->Map() == *that->AsClass()->Map()
281 && ((Config::is_class(that) && Config::is_class(this)) ||
282 BitsetType::New(this->BitsetLub())->Is(
283 BitsetType::New(that->BitsetLub())));
285 if (that->IsConstant()) {
286 return this->IsConstant()
287 && *this->AsConstant()->Value() == *that->AsConstant()->Value()
288 && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound());
290 if (that->IsRange()) {
291 return this->IsRange()
292 && this->AsRange()->Bound()->Is(that->AsRange()->Bound())
293 && dle(that->AsRange()->Min(), this->AsRange()->Min())
294 && dle(this->AsRange()->Max(), that->AsRange()->Max());
296 if (that->IsContext()) {
297 return this->IsContext()
298 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
300 if (that->IsArray()) {
301 return this->IsArray()
302 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
304 if (that->IsFunction()) {
305 // We currently do not allow for any variance here, in order to keep
306 // Union and Intersect operations simple.
307 if (!this->IsFunction()) return false;
308 FunctionType* this_fun = this->AsFunction();
309 FunctionType* that_fun = that->AsFunction();
310 if (this_fun->Arity() != that_fun->Arity() ||
311 !this_fun->Result()->Equals(that_fun->Result()) ||
312 !that_fun->Receiver()->Equals(this_fun->Receiver())) {
315 for (int i = 0; i < this_fun->Arity(); ++i) {
316 if (!that_fun->Parameter(i)->Equals(this_fun->Parameter(i))) return false;
321 // (T1 \/ ... \/ Tn) <= T <=> (T1 <= T) /\ ... /\ (Tn <= T)
322 if (this->IsUnion()) {
323 UnionHandle unioned = handle(this->AsUnion());
324 for (int i = 0; i < unioned->Length(); ++i) {
325 if (!unioned->Get(i)->Is(that)) return false;
330 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn)
331 // (iff T is not a union)
332 DCHECK(!this->IsUnion() && that->IsUnion());
333 UnionHandle unioned = handle(that->AsUnion());
334 for (int i = 0; i < unioned->Length(); ++i) {
335 if (this->Is(unioned->Get(i))) return true;
336 if (this->IsBitset()) break; // Fast fail, only first field is a bitset.
342 template<class Config>
343 bool TypeImpl<Config>::NowIs(TypeImpl* that) {
344 DisallowHeapAllocation no_allocation;
346 // TODO(rossberg): this is incorrect for
347 // Union(Constant(V), T)->NowIs(Class(M))
348 // but fuzzing does not cover that!
349 if (this->IsConstant()) {
350 i::Object* object = *this->AsConstant()->Value();
351 if (object->IsHeapObject()) {
352 i::Map* map = i::HeapObject::cast(object)->map();
353 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
354 if (*it.Current() == map) return true;
358 return this->Is(that);
362 // Check if this contains only (currently) stable classes.
363 template<class Config>
364 bool TypeImpl<Config>::NowStable() {
365 DisallowHeapAllocation no_allocation;
366 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
367 if (!it.Current()->is_stable()) return false;
373 // Check this overlaps that.
374 template<class Config>
375 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
376 DisallowHeapAllocation no_allocation;
378 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T)
379 if (this->IsUnion()) {
380 UnionHandle unioned = handle(this->AsUnion());
381 for (int i = 0; i < unioned->Length(); ++i) {
382 if (unioned->Get(i)->Maybe(that)) return true;
387 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn)
388 if (that->IsUnion()) {
389 UnionHandle unioned = handle(that->AsUnion());
390 for (int i = 0; i < unioned->Length(); ++i) {
391 if (this->Maybe(unioned->Get(i))) return true;
396 DCHECK(!this->IsUnion() && !that->IsUnion());
397 if (this->IsBitset() || that->IsBitset()) {
398 return BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub());
400 if (this->IsClass()) {
401 return that->IsClass()
402 && *this->AsClass()->Map() == *that->AsClass()->Map();
404 if (this->IsConstant()) {
405 return that->IsConstant()
406 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
408 if (this->IsContext()) {
409 return this->Equals(that);
411 if (this->IsArray()) {
412 // There is no variance!
413 return this->Equals(that);
415 if (this->IsFunction()) {
416 // There is no variance!
417 return this->Equals(that);
424 // Check if value is contained in (inhabits) type.
425 template<class Config>
426 bool TypeImpl<Config>::Contains(i::Object* value) {
427 DisallowHeapAllocation no_allocation;
428 if (this->IsRange()) {
429 return value->IsNumber() &&
430 dle(this->AsRange()->Min(), value->Number()) &&
431 dle(value->Number(), this->AsRange()->Max()) &&
432 BitsetType::Is(BitsetType::Lub(value), this->BitsetLub());
434 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
435 if (*it.Current() == value) return true;
437 return BitsetType::New(BitsetType::Lub(value))->Is(this);
441 template<class Config>
442 bool TypeImpl<Config>::UnionType::Wellformed() {
443 DCHECK(this->Length() >= 2);
444 for (int i = 0; i < this->Length(); ++i) {
445 DCHECK(!this->Get(i)->IsUnion());
446 if (i > 0) DCHECK(!this->Get(i)->IsBitset());
447 for (int j = 0; j < this->Length(); ++j) {
448 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j)));
455 // -----------------------------------------------------------------------------
456 // Union and intersection
458 template<class Config>
459 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound(
460 int bitset, Region* region) {
461 TypeHandle bound = BitsetType::New(bitset, region);
462 if (this->IsClass()) {
463 return ClassType::New(this->AsClass()->Map(), bound, region);
464 } else if (this->IsConstant()) {
465 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()) {
470 return ContextType::New(this->AsContext()->Outer(), bound, region);
471 } else if (this->IsArray()) {
472 return ArrayType::New(this->AsArray()->Element(), bound, region);
473 } else if (this->IsFunction()) {
474 FunctionHandle function = Config::handle(this->AsFunction());
475 int arity = function->Arity();
476 FunctionHandle type = FunctionType::New(
477 function->Result(), function->Receiver(), bound, arity, region);
478 for (int i = 0; i < arity; ++i) {
479 type->InitParameter(i, function->Parameter(i));
488 template<class Config>
489 int TypeImpl<Config>::BoundBy(TypeImpl* that) {
490 DCHECK(!this->IsUnion());
491 if (that->IsUnion()) {
492 UnionType* unioned = that->AsUnion();
493 int length = unioned->Length();
494 int bitset = BitsetType::kNone;
495 for (int i = 0; i < length; ++i) {
496 bitset |= BoundBy(unioned->Get(i)->unhandle());
499 } else if (that->IsClass() && this->IsClass() &&
500 *this->AsClass()->Map() == *that->AsClass()->Map()) {
501 return that->BitsetLub();
502 } else if (that->IsConstant() && this->IsConstant() &&
503 *this->AsConstant()->Value() == *that->AsConstant()->Value()) {
504 return that->AsConstant()->Bound()->AsBitset();
505 } else if (that->IsContext() && this->IsContext() && this->Is(that)) {
506 return that->AsContext()->Bound()->AsBitset();
507 } else if (that->IsArray() && this->IsArray() && this->Is(that)) {
508 return that->AsArray()->Bound()->AsBitset();
509 } else if (that->IsFunction() && this->IsFunction() && this->Is(that)) {
510 return that->AsFunction()->Bound()->AsBitset();
512 return that->BitsetGlb();
516 template<class Config>
517 int TypeImpl<Config>::IndexInUnion(
518 int bound, UnionHandle unioned, int current_size) {
519 DCHECK(!this->IsUnion());
520 for (int i = 0; i < current_size; ++i) {
521 TypeHandle that = unioned->Get(i);
522 if (that->IsBitset()) {
523 if (BitsetType::Is(bound, that->AsBitset())) return i;
524 } else if (that->IsClass() && this->IsClass()) {
525 if (*this->AsClass()->Map() == *that->AsClass()->Map()) return i;
526 } else if (that->IsConstant() && this->IsConstant()) {
527 if (*this->AsConstant()->Value() == *that->AsConstant()->Value())
529 } else if (that->IsContext() && this->IsContext()) {
530 if (this->Is(that)) return i;
531 } else if (that->IsArray() && this->IsArray()) {
532 if (this->Is(that)) return i;
533 } else if (that->IsFunction() && this->IsFunction()) {
534 if (this->Is(that)) return i;
541 // Get non-bitsets from type, bounded by upper.
542 // Store at result starting at index. Returns updated index.
543 template<class Config>
544 int TypeImpl<Config>::ExtendUnion(
545 UnionHandle result, int size, TypeHandle type,
546 TypeHandle other, bool is_intersect, Region* region) {
547 if (type->IsUnion()) {
548 UnionHandle unioned = handle(type->AsUnion());
549 for (int i = 0; i < unioned->Length(); ++i) {
550 TypeHandle type_i = unioned->Get(i);
551 DCHECK(i == 0 || !(type_i->IsBitset() || type_i->Is(unioned->Get(0))));
552 if (!type_i->IsBitset()) {
553 size = ExtendUnion(result, size, type_i, other, is_intersect, region);
556 } else if (!type->IsBitset()) {
557 DCHECK(type->IsClass() || type->IsConstant() || type->IsRange() ||
558 type->IsContext() || type->IsArray() || type->IsFunction());
559 int inherent_bound = type->InherentBitsetLub();
560 int old_bound = type->BitsetLub();
561 int other_bound = type->BoundBy(other->unhandle()) & inherent_bound;
563 is_intersect ? (old_bound & other_bound) : (old_bound | other_bound);
564 if (new_bound != BitsetType::kNone) {
565 int i = type->IndexInUnion(new_bound, result, size);
568 } else if (result->Get(i)->IsBitset()) {
569 return size; // Already fully subsumed.
571 int type_i_bound = result->Get(i)->BitsetLub();
572 new_bound |= type_i_bound;
573 if (new_bound == type_i_bound) return size;
575 if (new_bound != old_bound) type = type->Rebound(new_bound, region);
576 result->Set(i, type);
583 // Union is O(1) on simple bitsets, but O(n*m) on structured unions.
584 template<class Config>
585 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union(
586 TypeHandle type1, TypeHandle type2, Region* region) {
587 // Fast case: bit sets.
588 if (type1->IsBitset() && type2->IsBitset()) {
589 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region);
592 // Fast case: top or bottom types.
593 if (type1->IsAny() || type2->IsNone()) return type1;
594 if (type2->IsAny() || type1->IsNone()) return type2;
596 // Semi-fast case: Unioned objects are neither involved nor produced.
597 if (!(type1->IsUnion() || type2->IsUnion())) {
598 if (type1->Is(type2)) return type2;
599 if (type2->Is(type1)) return type1;
602 // Slow case: may need to produce a Unioned object.
604 if (!type1->IsBitset()) {
605 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
607 if (!type2->IsBitset()) {
608 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
610 int bitset = type1->BitsetGlb() | type2->BitsetGlb();
611 if (bitset != BitsetType::kNone) ++size;
614 UnionHandle unioned = UnionType::New(size, region);
616 if (bitset != BitsetType::kNone) {
617 unioned->Set(size++, BitsetType::New(bitset, region));
619 size = ExtendUnion(unioned, size, type1, type2, false, region);
620 size = ExtendUnion(unioned, size, type2, type1, false, region);
623 return unioned->Get(0);
625 unioned->Shrink(size);
626 DCHECK(unioned->Wellformed());
632 // Intersection is O(1) on simple bitsets, but O(n*m) on structured unions.
633 template<class Config>
634 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
635 TypeHandle type1, TypeHandle type2, Region* region) {
636 // Fast case: bit sets.
637 if (type1->IsBitset() && type2->IsBitset()) {
638 return BitsetType::New(type1->AsBitset() & type2->AsBitset(), region);
641 // Fast case: top or bottom types.
642 if (type1->IsNone() || type2->IsAny()) return type1;
643 if (type2->IsNone() || type1->IsAny()) return type2;
645 // Semi-fast case: Unioned objects are neither involved nor produced.
646 if (!(type1->IsUnion() || type2->IsUnion())) {
647 if (type1->Is(type2)) return type1;
648 if (type2->Is(type1)) return type2;
651 // Slow case: may need to produce a Unioned object.
653 if (!type1->IsBitset()) {
654 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
656 if (!type2->IsBitset()) {
657 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
659 int bitset = type1->BitsetGlb() & type2->BitsetGlb();
660 if (bitset != BitsetType::kNone) ++size;
663 UnionHandle unioned = UnionType::New(size, region);
665 if (bitset != BitsetType::kNone) {
666 unioned->Set(size++, BitsetType::New(bitset, region));
668 size = ExtendUnion(unioned, size, type1, type2, true, region);
669 size = ExtendUnion(unioned, size, type2, type1, true, region);
673 } else if (size == 1) {
674 return unioned->Get(0);
676 unioned->Shrink(size);
677 DCHECK(unioned->Wellformed());
683 // -----------------------------------------------------------------------------
686 template<class Config>
687 int TypeImpl<Config>::NumClasses() {
688 DisallowHeapAllocation no_allocation;
689 if (this->IsClass()) {
691 } else if (this->IsUnion()) {
692 UnionHandle unioned = handle(this->AsUnion());
694 for (int i = 0; i < unioned->Length(); ++i) {
695 if (unioned->Get(i)->IsClass()) ++result;
704 template<class Config>
705 int TypeImpl<Config>::NumConstants() {
706 DisallowHeapAllocation no_allocation;
707 if (this->IsConstant()) {
709 } else if (this->IsUnion()) {
710 UnionHandle unioned = handle(this->AsUnion());
712 for (int i = 0; i < unioned->Length(); ++i) {
713 if (unioned->Get(i)->IsConstant()) ++result;
722 template<class Config> template<class T>
723 typename TypeImpl<Config>::TypeHandle
724 TypeImpl<Config>::Iterator<T>::get_type() {
726 return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_;
730 // C++ cannot specialise nested templates, so we have to go through this
731 // contortion with an auxiliary template to simulate it.
732 template<class Config, class T>
733 struct TypeImplIteratorAux {
734 static bool matches(typename TypeImpl<Config>::TypeHandle type);
735 static i::Handle<T> current(typename TypeImpl<Config>::TypeHandle type);
738 template<class Config>
739 struct TypeImplIteratorAux<Config, i::Map> {
740 static bool matches(typename TypeImpl<Config>::TypeHandle type) {
741 return type->IsClass();
743 static i::Handle<i::Map> current(typename TypeImpl<Config>::TypeHandle type) {
744 return type->AsClass()->Map();
748 template<class Config>
749 struct TypeImplIteratorAux<Config, i::Object> {
750 static bool matches(typename TypeImpl<Config>::TypeHandle type) {
751 return type->IsConstant();
753 static i::Handle<i::Object> current(
754 typename TypeImpl<Config>::TypeHandle type) {
755 return type->AsConstant()->Value();
759 template<class Config> template<class T>
760 bool TypeImpl<Config>::Iterator<T>::matches(TypeHandle type) {
761 return TypeImplIteratorAux<Config, T>::matches(type);
764 template<class Config> template<class T>
765 i::Handle<T> TypeImpl<Config>::Iterator<T>::Current() {
766 return TypeImplIteratorAux<Config, T>::current(get_type());
770 template<class Config> template<class T>
771 void TypeImpl<Config>::Iterator<T>::Advance() {
772 DisallowHeapAllocation no_allocation;
774 if (type_->IsUnion()) {
775 UnionHandle unioned = handle(type_->AsUnion());
776 for (; index_ < unioned->Length(); ++index_) {
777 if (matches(unioned->Get(index_))) return;
779 } else if (index_ == 0 && matches(type_)) {
786 // -----------------------------------------------------------------------------
787 // Conversion between low-level representations.
789 template<class Config>
790 template<class OtherType>
791 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
792 typename OtherType::TypeHandle type, Region* region) {
793 if (type->IsBitset()) {
794 return BitsetType::New(type->AsBitset(), region);
795 } else if (type->IsClass()) {
796 TypeHandle bound = BitsetType::New(type->BitsetLub(), region);
797 return ClassType::New(type->AsClass()->Map(), bound, region);
798 } else if (type->IsConstant()) {
799 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region);
800 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()) {
806 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region);
807 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region);
808 return ContextType::New(outer, bound, region);
809 } else if (type->IsUnion()) {
810 int length = type->AsUnion()->Length();
811 UnionHandle unioned = UnionType::New(length, region);
812 for (int i = 0; i < length; ++i) {
813 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region);
817 } else if (type->IsArray()) {
818 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region);
819 TypeHandle bound = Convert<OtherType>(type->AsArray()->Bound(), region);
820 return ArrayType::New(element, bound, region);
821 } else if (type->IsFunction()) {
822 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region);
823 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region);
824 TypeHandle bound = Convert<OtherType>(type->AsFunction()->Bound(), region);
825 FunctionHandle function = FunctionType::New(
826 res, rcv, bound, type->AsFunction()->Arity(), region);
827 for (int i = 0; i < function->Arity(); ++i) {
828 TypeHandle param = Convert<OtherType>(
829 type->AsFunction()->Parameter(i), region);
830 function->InitParameter(i, param);
840 // -----------------------------------------------------------------------------
843 template<class Config>
844 const char* TypeImpl<Config>::BitsetType::Name(int bitset) {
846 case REPRESENTATION(kAny): return "Any";
847 #define RETURN_NAMED_REPRESENTATION_TYPE(type, value) \
848 case REPRESENTATION(k##type): return #type;
849 REPRESENTATION_BITSET_TYPE_LIST(RETURN_NAMED_REPRESENTATION_TYPE)
850 #undef RETURN_NAMED_REPRESENTATION_TYPE
852 #define RETURN_NAMED_SEMANTIC_TYPE(type, value) \
853 case SEMANTIC(k##type): return #type;
854 SEMANTIC_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
855 #undef RETURN_NAMED_SEMANTIC_TYPE
863 template <class Config>
864 void TypeImpl<Config>::BitsetType::Print(OStream& os, // NOLINT
866 DisallowHeapAllocation no_allocation;
867 const char* name = Name(bitset);
873 static const int named_bitsets[] = {
874 #define BITSET_CONSTANT(type, value) REPRESENTATION(k##type),
875 REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT)
876 #undef BITSET_CONSTANT
878 #define BITSET_CONSTANT(type, value) SEMANTIC(k##type),
879 SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
880 #undef BITSET_CONSTANT
883 bool is_first = true;
885 for (int i(ARRAY_SIZE(named_bitsets) - 1); bitset != 0 && i >= 0; --i) {
886 int subset = named_bitsets[i];
887 if ((bitset & subset) == subset) {
888 if (!is_first) os << " | ";
899 template <class Config>
900 void TypeImpl<Config>::PrintTo(OStream& os, PrintDimension dim) { // NOLINT
901 DisallowHeapAllocation no_allocation;
902 if (dim != REPRESENTATION_DIM) {
903 if (this->IsBitset()) {
904 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
905 } else if (this->IsClass()) {
906 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
907 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
909 } else if (this->IsConstant()) {
910 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value())
912 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
914 } else if (this->IsRange()) {
915 os << "Range(" << this->AsRange()->Min()
916 << ".." << this->AsRange()->Max() << " : ";
917 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
919 } else if (this->IsContext()) {
921 this->AsContext()->Outer()->PrintTo(os, dim);
923 } else if (this->IsUnion()) {
925 UnionHandle unioned = handle(this->AsUnion());
926 for (int i = 0; i < unioned->Length(); ++i) {
927 TypeHandle type_i = unioned->Get(i);
928 if (i > 0) os << " | ";
929 type_i->PrintTo(os, dim);
932 } else if (this->IsArray()) {
934 AsArray()->Element()->PrintTo(os, dim);
936 } else if (this->IsFunction()) {
937 if (!this->AsFunction()->Receiver()->IsAny()) {
938 this->AsFunction()->Receiver()->PrintTo(os, dim);
942 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
943 if (i > 0) os << ", ";
944 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
947 this->AsFunction()->Result()->PrintTo(os, dim);
952 if (dim == BOTH_DIMS) os << "/";
953 if (dim != SEMANTIC_DIM) {
954 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
960 template <class Config>
961 void TypeImpl<Config>::Print() {
969 // -----------------------------------------------------------------------------
972 template class TypeImpl<ZoneTypeConfig>;
973 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
974 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
976 template class TypeImpl<HeapTypeConfig>;
977 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
978 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
980 template TypeImpl<ZoneTypeConfig>::TypeHandle
981 TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
982 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
983 template TypeImpl<HeapTypeConfig>::TypeHandle
984 TypeImpl<HeapTypeConfig>::Convert<Type>(
985 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
987 } } // namespace v8::internal