Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / src / types.cc
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.
4
5 #include "src/types.h"
6
7 #include "src/ostreams.h"
8 #include "src/types-inl.h"
9
10 namespace v8 {
11 namespace internal {
12
13
14 // -----------------------------------------------------------------------------
15 // Range-related custom order on doubles.
16 // We want -0 to be less than +0.
17
18 static bool dle(double x, double y) {
19   return x <= y && (x != 0 || IsMinusZero(x) || !IsMinusZero(y));
20 }
21
22
23 static bool deq(double x, double y) {
24   return dle(x, y) && dle(y, x);
25 }
26
27
28 // -----------------------------------------------------------------------------
29 // Glb and lub computation.
30
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.
41   } else {
42     return kNone;
43   }
44 }
45
46
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());
55     int bitset = kNone;
56     for (int i = 0; i < unioned->Length(); ++i) {
57       bitset |= unioned->Get(i)->BitsetLub();
58     }
59     return bitset;
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();
74   } else {
75     UNREACHABLE();
76     return kNone;
77   }
78 }
79
80
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());
89     int bitset = kNone;
90     for (int i = 0; i < unioned->Length(); ++i) {
91       bitset |= unioned->Get(i)->InherentBitsetLub();
92     }
93     return bitset;
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()) {
103     return kArray;
104   } else if (type->IsFunction()) {
105     return kFunction;
106   } else {
107     UNREACHABLE();
108     return kNone;
109   }
110 }
111
112
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);
118   }
119   return Lub(i::HeapObject::cast(value)->map());
120 }
121
122
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));
130   return kOtherNumber;
131 }
132
133
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);
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) {
148   if (value >= 0x40000000) {
149     return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
150   }
151   if (value >= 0) return kUnsignedSmall;
152   if (value >= -0x40000000) return kOtherSignedSmall;
153   return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall;
154 }
155
156
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;
163   }
164   return kUnsignedSmall;
165 }
166
167
168 template<class Config>
169 int TypeImpl<Config>::BitsetType::Lub(i::Map* map) {
170   DisallowHeapAllocation no_allocation;
171   switch (map->instance_type()) {
172     case STRING_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:
192       return kString;
193     case SYMBOL_TYPE:
194       return kSymbol;
195     case ODDBALL_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;
206     }
207     case HEAP_NUMBER_TYPE:
208       return kNumber & kTaggedPtr;
209     case JS_VALUE_TYPE:
210     case JS_DATE_TYPE:
211     case JS_OBJECT_TYPE:
212     case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
213     case JS_GENERATOR_OBJECT_TYPE:
214     case JS_MODULE_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:
221     case JS_SET_TYPE:
222     case JS_MAP_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;
228       return kOtherObject;
229     case JS_ARRAY_TYPE:
230       return kArray;
231     case JS_FUNCTION_TYPE:
232       return kFunction;
233     case JS_REGEXP_TYPE:
234       return kRegExp;
235     case JS_PROXY_TYPE:
236     case JS_FUNCTION_PROXY_TYPE:
237       return kProxy;
238     case MAP_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...
248       return kDetectable;
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:
254     case FOREIGN_TYPE:
255     case CODE_TYPE:
256       return kInternal & kTaggedPtr;
257     default:
258       UNREACHABLE();
259       return kNone;
260   }
261 }
262
263
264 // -----------------------------------------------------------------------------
265 // Predicates.
266
267 // Check this <= that.
268 template<class Config>
269 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
270   DisallowHeapAllocation no_allocation;
271
272   // Fast path for bitsets.
273   if (this->IsNone()) return true;
274   if (that->IsBitset()) {
275     return BitsetType::Is(BitsetType::Lub(this), that->AsBitset());
276   }
277
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())));
284   }
285   if (that->IsConstant()) {
286     return this->IsConstant()
287         && *this->AsConstant()->Value() == *that->AsConstant()->Value()
288         && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound());
289   }
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());
295   }
296   if (that->IsContext()) {
297     return this->IsContext()
298         && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
299   }
300   if (that->IsArray()) {
301     return this->IsArray()
302         && this->AsArray()->Element()->Equals(that->AsArray()->Element());
303   }
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())) {
313       return false;
314     }
315     for (int i = 0; i < this_fun->Arity(); ++i) {
316       if (!that_fun->Parameter(i)->Equals(this_fun->Parameter(i))) return false;
317     }
318     return true;
319   }
320
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;
326     }
327     return true;
328   }
329
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.
337   }
338   return false;
339 }
340
341
342 template<class Config>
343 bool TypeImpl<Config>::NowIs(TypeImpl* that) {
344   DisallowHeapAllocation no_allocation;
345
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;
355       }
356     }
357   }
358   return this->Is(that);
359 }
360
361
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;
368   }
369   return true;
370 }
371
372
373 // Check this overlaps that.
374 template<class Config>
375 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
376   DisallowHeapAllocation no_allocation;
377
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;
383     }
384     return false;
385   }
386
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;
392     }
393     return false;
394   }
395
396   DCHECK(!this->IsUnion() && !that->IsUnion());
397   if (this->IsBitset() || that->IsBitset()) {
398     return BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub());
399   }
400   if (this->IsClass()) {
401     return that->IsClass()
402         && *this->AsClass()->Map() == *that->AsClass()->Map();
403   }
404   if (this->IsConstant()) {
405     return that->IsConstant()
406         && *this->AsConstant()->Value() == *that->AsConstant()->Value();
407   }
408   if (this->IsContext()) {
409     return this->Equals(that);
410   }
411   if (this->IsArray()) {
412     // There is no variance!
413     return this->Equals(that);
414   }
415   if (this->IsFunction()) {
416     // There is no variance!
417     return this->Equals(that);
418   }
419
420   return false;
421 }
422
423
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());
433   }
434   for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
435     if (*it.Current() == value) return true;
436   }
437   return BitsetType::New(BitsetType::Lub(value))->Is(this);
438 }
439
440
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)));
449     }
450   }
451   return true;
452 }
453
454
455 // -----------------------------------------------------------------------------
456 // Union and intersection
457
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));
480     }
481     return type;
482   }
483   UNREACHABLE();
484   return TypeHandle();
485 }
486
487
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());
497     }
498     return bitset;
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();
511   }
512   return that->BitsetGlb();
513 }
514
515
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())
528         return i;
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;
535     }
536   }
537   return -1;
538 }
539
540
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);
554       }
555     }
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;
562     int new_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);
566       if (i == -1) {
567         i = size++;
568       } else if (result->Get(i)->IsBitset()) {
569         return size;  // Already fully subsumed.
570       } else {
571         int type_i_bound = result->Get(i)->BitsetLub();
572         new_bound |= type_i_bound;
573         if (new_bound == type_i_bound) return size;
574       }
575       if (new_bound != old_bound) type = type->Rebound(new_bound, region);
576       result->Set(i, type);
577     }
578   }
579   return size;
580 }
581
582
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);
590   }
591
592   // Fast case: top or bottom types.
593   if (type1->IsAny() || type2->IsNone()) return type1;
594   if (type2->IsAny() || type1->IsNone()) return type2;
595
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;
600   }
601
602   // Slow case: may need to produce a Unioned object.
603   int size = 0;
604   if (!type1->IsBitset()) {
605     size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
606   }
607   if (!type2->IsBitset()) {
608     size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
609   }
610   int bitset = type1->BitsetGlb() | type2->BitsetGlb();
611   if (bitset != BitsetType::kNone) ++size;
612   DCHECK(size >= 1);
613
614   UnionHandle unioned = UnionType::New(size, region);
615   size = 0;
616   if (bitset != BitsetType::kNone) {
617     unioned->Set(size++, BitsetType::New(bitset, region));
618   }
619   size = ExtendUnion(unioned, size, type1, type2, false, region);
620   size = ExtendUnion(unioned, size, type2, type1, false, region);
621
622   if (size == 1) {
623     return unioned->Get(0);
624   } else {
625     unioned->Shrink(size);
626     DCHECK(unioned->Wellformed());
627     return unioned;
628   }
629 }
630
631
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);
639   }
640
641   // Fast case: top or bottom types.
642   if (type1->IsNone() || type2->IsAny()) return type1;
643   if (type2->IsNone() || type1->IsAny()) return type2;
644
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;
649   }
650
651   // Slow case: may need to produce a Unioned object.
652   int size = 0;
653   if (!type1->IsBitset()) {
654     size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
655   }
656   if (!type2->IsBitset()) {
657     size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
658   }
659   int bitset = type1->BitsetGlb() & type2->BitsetGlb();
660   if (bitset != BitsetType::kNone) ++size;
661   DCHECK(size >= 1);
662
663   UnionHandle unioned = UnionType::New(size, region);
664   size = 0;
665   if (bitset != BitsetType::kNone) {
666     unioned->Set(size++, BitsetType::New(bitset, region));
667   }
668   size = ExtendUnion(unioned, size, type1, type2, true, region);
669   size = ExtendUnion(unioned, size, type2, type1, true, region);
670
671   if (size == 0) {
672     return None(region);
673   } else if (size == 1) {
674     return unioned->Get(0);
675   } else {
676     unioned->Shrink(size);
677     DCHECK(unioned->Wellformed());
678     return unioned;
679   }
680 }
681
682
683 // -----------------------------------------------------------------------------
684 // Iteration.
685
686 template<class Config>
687 int TypeImpl<Config>::NumClasses() {
688   DisallowHeapAllocation no_allocation;
689   if (this->IsClass()) {
690     return 1;
691   } else if (this->IsUnion()) {
692     UnionHandle unioned = handle(this->AsUnion());
693     int result = 0;
694     for (int i = 0; i < unioned->Length(); ++i) {
695       if (unioned->Get(i)->IsClass()) ++result;
696     }
697     return result;
698   } else {
699     return 0;
700   }
701 }
702
703
704 template<class Config>
705 int TypeImpl<Config>::NumConstants() {
706   DisallowHeapAllocation no_allocation;
707   if (this->IsConstant()) {
708     return 1;
709   } else if (this->IsUnion()) {
710     UnionHandle unioned = handle(this->AsUnion());
711     int result = 0;
712     for (int i = 0; i < unioned->Length(); ++i) {
713       if (unioned->Get(i)->IsConstant()) ++result;
714     }
715     return result;
716   } else {
717     return 0;
718   }
719 }
720
721
722 template<class Config> template<class T>
723 typename TypeImpl<Config>::TypeHandle
724 TypeImpl<Config>::Iterator<T>::get_type() {
725   DCHECK(!Done());
726   return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_;
727 }
728
729
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);
736 };
737
738 template<class Config>
739 struct TypeImplIteratorAux<Config, i::Map> {
740   static bool matches(typename TypeImpl<Config>::TypeHandle type) {
741     return type->IsClass();
742   }
743   static i::Handle<i::Map> current(typename TypeImpl<Config>::TypeHandle type) {
744     return type->AsClass()->Map();
745   }
746 };
747
748 template<class Config>
749 struct TypeImplIteratorAux<Config, i::Object> {
750   static bool matches(typename TypeImpl<Config>::TypeHandle type) {
751     return type->IsConstant();
752   }
753   static i::Handle<i::Object> current(
754       typename TypeImpl<Config>::TypeHandle type) {
755     return type->AsConstant()->Value();
756   }
757 };
758
759 template<class Config> template<class T>
760 bool TypeImpl<Config>::Iterator<T>::matches(TypeHandle type) {
761   return TypeImplIteratorAux<Config, T>::matches(type);
762 }
763
764 template<class Config> template<class T>
765 i::Handle<T> TypeImpl<Config>::Iterator<T>::Current() {
766   return TypeImplIteratorAux<Config, T>::current(get_type());
767 }
768
769
770 template<class Config> template<class T>
771 void TypeImpl<Config>::Iterator<T>::Advance() {
772   DisallowHeapAllocation no_allocation;
773   ++index_;
774   if (type_->IsUnion()) {
775     UnionHandle unioned = handle(type_->AsUnion());
776     for (; index_ < unioned->Length(); ++index_) {
777       if (matches(unioned->Get(index_))) return;
778     }
779   } else if (index_ == 0 && matches(type_)) {
780     return;
781   }
782   index_ = -1;
783 }
784
785
786 // -----------------------------------------------------------------------------
787 // Conversion between low-level representations.
788
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);
814       unioned->Set(i, t);
815     }
816     return unioned;
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);
831     }
832     return function;
833   } else {
834     UNREACHABLE();
835     return None(region);
836   }
837 }
838
839
840 // -----------------------------------------------------------------------------
841 // Printing.
842
843 template<class Config>
844 const char* TypeImpl<Config>::BitsetType::Name(int bitset) {
845   switch (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
851
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
856
857     default:
858       return NULL;
859   }
860 }
861
862
863 template <class Config>
864 void TypeImpl<Config>::BitsetType::Print(OStream& os,  // NOLINT
865                                          int bitset) {
866   DisallowHeapAllocation no_allocation;
867   const char* name = Name(bitset);
868   if (name != NULL) {
869     os << name;
870     return;
871   }
872
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
877
878 #define BITSET_CONSTANT(type, value) SEMANTIC(k##type),
879       SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
880 #undef BITSET_CONSTANT
881   };
882
883   bool is_first = true;
884   os << "(";
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 << " | ";
889       is_first = false;
890       os << Name(subset);
891       bitset -= subset;
892     }
893   }
894   DCHECK(bitset == 0);
895   os << ")";
896 }
897
898
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);
908       os << ")";
909     } else if (this->IsConstant()) {
910       os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value())
911          << " : ";
912       BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
913       os << ")";
914     } else if (this->IsRange()) {
915       os << "Range(" << this->AsRange()->Min()
916          << ".." << this->AsRange()->Max() << " : ";
917       BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
918       os << ")";
919     } else if (this->IsContext()) {
920       os << "Context(";
921       this->AsContext()->Outer()->PrintTo(os, dim);
922       os << ")";
923     } else if (this->IsUnion()) {
924       os << "(";
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);
930       }
931       os << ")";
932     } else if (this->IsArray()) {
933       os << "Array(";
934       AsArray()->Element()->PrintTo(os, dim);
935       os << ")";
936     } else if (this->IsFunction()) {
937       if (!this->AsFunction()->Receiver()->IsAny()) {
938         this->AsFunction()->Receiver()->PrintTo(os, dim);
939         os << ".";
940       }
941       os << "(";
942       for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
943         if (i > 0) os << ", ";
944         this->AsFunction()->Parameter(i)->PrintTo(os, dim);
945       }
946       os << ")->";
947       this->AsFunction()->Result()->PrintTo(os, dim);
948     } else {
949       UNREACHABLE();
950     }
951   }
952   if (dim == BOTH_DIMS) os << "/";
953   if (dim != SEMANTIC_DIM) {
954     BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
955   }
956 }
957
958
959 #ifdef DEBUG
960 template <class Config>
961 void TypeImpl<Config>::Print() {
962   OFStream os(stdout);
963   PrintTo(os);
964   os << endl;
965 }
966 #endif
967
968
969 // -----------------------------------------------------------------------------
970 // Instantiations.
971
972 template class TypeImpl<ZoneTypeConfig>;
973 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
974 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
975
976 template class TypeImpl<HeapTypeConfig>;
977 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
978 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
979
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*);
986
987 } }  // namespace v8::internal