17a7ce7647ae95b32259b566bb2b4b3fa22ad96e
[platform/framework/web/crosswalk.git] / src / v8 / src / types.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "types.h"
29 #include "string-stream.h"
30
31 namespace v8 {
32 namespace internal {
33
34 template<class Config>
35 int TypeImpl<Config>::NumClasses() {
36   if (this->IsClass()) {
37     return 1;
38   } else if (this->IsUnion()) {
39     UnionedHandle unioned = this->AsUnion();
40     int result = 0;
41     for (int i = 0; i < Config::union_length(unioned); ++i) {
42       if (Config::union_get(unioned, i)->IsClass()) ++result;
43     }
44     return result;
45   } else {
46     return 0;
47   }
48 }
49
50
51 template<class Config>
52 int TypeImpl<Config>::NumConstants() {
53   if (this->IsConstant()) {
54     return 1;
55   } else if (this->IsUnion()) {
56     UnionedHandle unioned = this->AsUnion();
57     int result = 0;
58     for (int i = 0; i < Config::union_length(unioned); ++i) {
59       if (Config::union_get(unioned, i)->IsConstant()) ++result;
60     }
61     return result;
62   } else {
63     return 0;
64   }
65 }
66
67
68 template<class Config> template<class T>
69 typename TypeImpl<Config>::TypeHandle
70 TypeImpl<Config>::Iterator<T>::get_type() {
71   ASSERT(!Done());
72   return type_->IsUnion() ? Config::union_get(type_->AsUnion(), index_) : type_;
73 }
74
75
76 // C++ cannot specialise nested templates, so we have to go through this
77 // contortion with an auxiliary template to simulate it.
78 template<class Config, class T>
79 struct TypeImplIteratorAux {
80   static bool matches(typename TypeImpl<Config>::TypeHandle type);
81   static i::Handle<T> current(typename TypeImpl<Config>::TypeHandle type);
82 };
83
84 template<class Config>
85 struct TypeImplIteratorAux<Config, i::Map> {
86   static bool matches(typename TypeImpl<Config>::TypeHandle type) {
87     return type->IsClass();
88   }
89   static i::Handle<i::Map> current(typename TypeImpl<Config>::TypeHandle type) {
90     return type->AsClass();
91   }
92 };
93
94 template<class Config>
95 struct TypeImplIteratorAux<Config, i::Object> {
96   static bool matches(typename TypeImpl<Config>::TypeHandle type) {
97     return type->IsConstant();
98   }
99   static i::Handle<i::Object> current(
100       typename TypeImpl<Config>::TypeHandle type) {
101     return type->AsConstant();
102   }
103 };
104
105 template<class Config> template<class T>
106 bool TypeImpl<Config>::Iterator<T>::matches(TypeHandle type) {
107   return TypeImplIteratorAux<Config, T>::matches(type);
108 }
109
110 template<class Config> template<class T>
111 i::Handle<T> TypeImpl<Config>::Iterator<T>::Current() {
112   return TypeImplIteratorAux<Config, T>::current(get_type());
113 }
114
115
116 template<class Config> template<class T>
117 void TypeImpl<Config>::Iterator<T>::Advance() {
118   ++index_;
119   if (type_->IsUnion()) {
120     UnionedHandle unioned = type_->AsUnion();
121     for (; index_ < Config::union_length(unioned); ++index_) {
122       if (matches(Config::union_get(unioned, index_))) return;
123     }
124   } else if (index_ == 0 && matches(type_)) {
125     return;
126   }
127   index_ = -1;
128 }
129
130
131 // Get the smallest bitset subsuming this type.
132 template<class Config>
133 int TypeImpl<Config>::LubBitset() {
134   if (this->IsBitset()) {
135     return this->AsBitset();
136   } else if (this->IsUnion()) {
137     UnionedHandle unioned = this->AsUnion();
138     int bitset = kNone;
139     for (int i = 0; i < Config::union_length(unioned); ++i) {
140       bitset |= Config::union_get(unioned, i)->LubBitset();
141     }
142     return bitset;
143   } else if (this->IsClass()) {
144     int bitset = Config::lub_bitset(this);
145     return bitset ? bitset : LubBitset(*this->AsClass());
146   } else {
147     int bitset = Config::lub_bitset(this);
148     return bitset ? bitset : LubBitset(*this->AsConstant());
149   }
150 }
151
152
153 template<class Config>
154 int TypeImpl<Config>::LubBitset(i::Object* value) {
155   if (value->IsSmi()) return kSignedSmall & kTaggedInt;
156   i::Map* map = i::HeapObject::cast(value)->map();
157   if (map->instance_type() == HEAP_NUMBER_TYPE) {
158     int32_t i;
159     uint32_t u;
160     return kTaggedPtr & (
161         value->ToInt32(&i) ? (Smi::IsValid(i) ? kSignedSmall : kOtherSigned32) :
162         value->ToUint32(&u) ? kUnsigned32 : kFloat);
163   }
164   if (map->instance_type() == ODDBALL_TYPE) {
165     if (value->IsUndefined()) return kUndefined;
166     if (value->IsNull()) return kNull;
167     if (value->IsBoolean()) return kBoolean;
168     if (value->IsTheHole()) return kAny;  // TODO(rossberg): kNone?
169     if (value->IsUninitialized()) return kNone;
170     UNREACHABLE();
171   }
172   return LubBitset(map);
173 }
174
175
176 template<class Config>
177 int TypeImpl<Config>::LubBitset(i::Map* map) {
178   switch (map->instance_type()) {
179     case STRING_TYPE:
180     case ASCII_STRING_TYPE:
181     case CONS_STRING_TYPE:
182     case CONS_ASCII_STRING_TYPE:
183     case SLICED_STRING_TYPE:
184     case SLICED_ASCII_STRING_TYPE:
185     case EXTERNAL_STRING_TYPE:
186     case EXTERNAL_ASCII_STRING_TYPE:
187     case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
188     case SHORT_EXTERNAL_STRING_TYPE:
189     case SHORT_EXTERNAL_ASCII_STRING_TYPE:
190     case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
191     case INTERNALIZED_STRING_TYPE:
192     case ASCII_INTERNALIZED_STRING_TYPE:
193     case CONS_INTERNALIZED_STRING_TYPE:
194     case CONS_ASCII_INTERNALIZED_STRING_TYPE:
195     case EXTERNAL_INTERNALIZED_STRING_TYPE:
196     case EXTERNAL_ASCII_INTERNALIZED_STRING_TYPE:
197     case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
198     case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
199     case SHORT_EXTERNAL_ASCII_INTERNALIZED_STRING_TYPE:
200     case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
201       return kString;
202     case SYMBOL_TYPE:
203       return kSymbol;
204     case ODDBALL_TYPE:
205       return kOddball;
206     case HEAP_NUMBER_TYPE:
207       return kFloat & kTaggedPtr;
208     case FLOAT32x4_TYPE:
209       return kFloat32x4 & kTaggedPtr;
210     case INT32x4_TYPE:
211       return kInt32x4 & kTaggedPtr;
212     case JS_VALUE_TYPE:
213     case JS_DATE_TYPE:
214     case JS_OBJECT_TYPE:
215     case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
216     case JS_GENERATOR_OBJECT_TYPE:
217     case JS_MODULE_TYPE:
218     case JS_GLOBAL_OBJECT_TYPE:
219     case JS_BUILTINS_OBJECT_TYPE:
220     case JS_GLOBAL_PROXY_TYPE:
221     case JS_ARRAY_BUFFER_TYPE:
222     case JS_TYPED_ARRAY_TYPE:
223     case JS_DATA_VIEW_TYPE:
224     case JS_SET_TYPE:
225     case JS_MAP_TYPE:
226     case JS_WEAK_MAP_TYPE:
227     case JS_WEAK_SET_TYPE:
228       if (map->is_undetectable()) return kUndetectable;
229       return kOtherObject;
230     case JS_ARRAY_TYPE:
231       return kArray;
232     case JS_FUNCTION_TYPE:
233       return kFunction;
234     case JS_REGEXP_TYPE:
235       return kRegExp;
236     case JS_PROXY_TYPE:
237     case JS_FUNCTION_PROXY_TYPE:
238       return kProxy;
239     case MAP_TYPE:
240       // When compiling stub templates, the meta map is used as a place holder
241       // for the actual map with which the template is later instantiated.
242       // We treat it as a kind of type variable whose upper bound is Any.
243       // TODO(rossberg): for caching of CompareNilIC stubs to work correctly,
244       // we must exclude Undetectable here. This makes no sense, really,
245       // because it means that the template isn't actually parametric.
246       // Also, it doesn't apply elsewhere. 8-(
247       // We ought to find a cleaner solution for compiling stubs parameterised
248       // over type or class variables, esp ones with bounds...
249       return kDetectable;
250     case DECLARED_ACCESSOR_INFO_TYPE:
251     case EXECUTABLE_ACCESSOR_INFO_TYPE:
252     case ACCESSOR_PAIR_TYPE:
253     case FIXED_ARRAY_TYPE:
254       return kInternal & kTaggedPtr;
255     default:
256       UNREACHABLE();
257       return kNone;
258   }
259 }
260
261
262 // Get the largest bitset subsumed by this type.
263 template<class Config>
264 int TypeImpl<Config>::GlbBitset() {
265   if (this->IsBitset()) {
266     return this->AsBitset();
267   } else if (this->IsUnion()) {
268     // All but the first are non-bitsets and thus would yield kNone anyway.
269     return Config::union_get(this->AsUnion(), 0)->GlbBitset();
270   } else {
271     return kNone;
272   }
273 }
274
275
276 // Most precise _current_ type of a value (usually its class).
277 template<class Config>
278 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::OfCurrently(
279     i::Handle<i::Object> value, Region* region) {
280   if (value->IsSmi() ||
281       i::HeapObject::cast(*value)->map()->instance_type() == HEAP_NUMBER_TYPE ||
282       i::HeapObject::cast(*value)->map()->instance_type() == ODDBALL_TYPE) {
283     return Of(value, region);
284   }
285   return Class(i::handle(i::HeapObject::cast(*value)->map()), region);
286 }
287
288
289 // Check this <= that.
290 template<class Config>
291 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
292   // Fast path for bitsets.
293   if (this->IsNone()) return true;
294   if (that->IsBitset()) {
295     return (this->LubBitset() | that->AsBitset()) == that->AsBitset();
296   }
297
298   if (that->IsClass()) {
299     return this->IsClass() && *this->AsClass() == *that->AsClass();
300   }
301   if (that->IsConstant()) {
302     return this->IsConstant() && *this->AsConstant() == *that->AsConstant();
303   }
304
305   // (T1 \/ ... \/ Tn) <= T  <=>  (T1 <= T) /\ ... /\ (Tn <= T)
306   if (this->IsUnion()) {
307     UnionedHandle unioned = this->AsUnion();
308     for (int i = 0; i < Config::union_length(unioned); ++i) {
309       TypeHandle this_i = Config::union_get(unioned, i);
310       if (!this_i->Is(that)) return false;
311     }
312     return true;
313   }
314
315   // T <= (T1 \/ ... \/ Tn)  <=>  (T <= T1) \/ ... \/ (T <= Tn)
316   // (iff T is not a union)
317   ASSERT(!this->IsUnion());
318   if (that->IsUnion()) {
319     UnionedHandle unioned = that->AsUnion();
320     for (int i = 0; i < Config::union_length(unioned); ++i) {
321       TypeHandle that_i = Config::union_get(unioned, i);
322       if (this->Is(that_i)) return true;
323       if (this->IsBitset()) break;  // Fast fail, only first field is a bitset.
324     }
325     return false;
326   }
327
328   return false;
329 }
330
331
332 template<class Config>
333 bool TypeImpl<Config>::IsCurrently(TypeImpl* that) {
334   return this->Is(that) ||
335       (this->IsConstant() && that->IsClass() &&
336        this->AsConstant()->IsHeapObject() &&
337        i::HeapObject::cast(*this->AsConstant())->map() == *that->AsClass());
338 }
339
340
341 // Check this overlaps that.
342 template<class Config>
343 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
344   // Fast path for bitsets.
345   if (this->IsBitset()) {
346     return IsInhabited(this->AsBitset() & that->LubBitset());
347   }
348   if (that->IsBitset()) {
349     return IsInhabited(this->LubBitset() & that->AsBitset());
350   }
351
352   // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T)
353   if (this->IsUnion()) {
354     UnionedHandle unioned = this->AsUnion();
355     for (int i = 0; i < Config::union_length(unioned); ++i) {
356       TypeHandle this_i = Config::union_get(unioned, i);
357       if (this_i->Maybe(that)) return true;
358     }
359     return false;
360   }
361
362   // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn)
363   if (that->IsUnion()) {
364     UnionedHandle unioned = that->AsUnion();
365     for (int i = 0; i < Config::union_length(unioned); ++i) {
366       TypeHandle that_i = Config::union_get(unioned, i);
367       if (this->Maybe(that_i)) return true;
368     }
369     return false;
370   }
371
372   ASSERT(!this->IsUnion() && !that->IsUnion());
373   if (this->IsClass()) {
374     return that->IsClass() && *this->AsClass() == *that->AsClass();
375   }
376   if (this->IsConstant()) {
377     return that->IsConstant() && *this->AsConstant() == *that->AsConstant();
378   }
379
380   return false;
381 }
382
383
384 template<class Config>
385 bool TypeImpl<Config>::InUnion(UnionedHandle unioned, int current_size) {
386   ASSERT(!this->IsUnion());
387   for (int i = 0; i < current_size; ++i) {
388     TypeHandle type = Config::union_get(unioned, i);
389     if (this->Is(type)) return true;
390   }
391   return false;
392 }
393
394
395 // Get non-bitsets from this which are not subsumed by union, store at unioned,
396 // starting at index. Returns updated index.
397 template<class Config>
398 int TypeImpl<Config>::ExtendUnion(
399     UnionedHandle result, TypeHandle type, int current_size) {
400   int old_size = current_size;
401   if (type->IsClass() || type->IsConstant()) {
402     if (!type->InUnion(result, old_size)) {
403       Config::union_set(result, current_size++, type);
404     }
405   } else if (type->IsUnion()) {
406     UnionedHandle unioned = type->AsUnion();
407     for (int i = 0; i < Config::union_length(unioned); ++i) {
408       TypeHandle type = Config::union_get(unioned, i);
409       ASSERT(i == 0 ||
410              !(type->IsBitset() || type->Is(Config::union_get(unioned, 0))));
411       if (!type->IsBitset() && !type->InUnion(result, old_size)) {
412         Config::union_set(result, current_size++, type);
413       }
414     }
415   }
416   return current_size;
417 }
418
419
420 // Union is O(1) on simple bit unions, but O(n*m) on structured unions.
421 // TODO(rossberg): Should we use object sets somehow? Is it worth it?
422 template<class Config>
423 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union(
424     TypeHandle type1, TypeHandle type2, Region* region) {
425   // Fast case: bit sets.
426   if (type1->IsBitset() && type2->IsBitset()) {
427     return Config::from_bitset(type1->AsBitset() | type2->AsBitset(), region);
428   }
429
430   // Fast case: top or bottom types.
431   if (type1->IsAny()) return type1;
432   if (type2->IsAny()) return type2;
433   if (type1->IsNone()) return type2;
434   if (type2->IsNone()) return type1;
435
436   // Semi-fast case: Unioned objects are neither involved nor produced.
437   if (!(type1->IsUnion() || type2->IsUnion())) {
438     if (type1->Is(type2)) return type2;
439     if (type2->Is(type1)) return type1;
440   }
441
442   // Slow case: may need to produce a Unioned object.
443   int size = type1->IsBitset() || type2->IsBitset() ? 1 : 0;
444   if (!type1->IsBitset()) {
445     size += (type1->IsUnion() ? Config::union_length(type1->AsUnion()) : 1);
446   }
447   if (!type2->IsBitset()) {
448     size += (type2->IsUnion() ? Config::union_length(type2->AsUnion()) : 1);
449   }
450   ASSERT(size >= 2);
451   UnionedHandle unioned = Config::union_create(size, region);
452   size = 0;
453
454   int bitset = type1->GlbBitset() | type2->GlbBitset();
455   if (bitset != kNone) {
456     Config::union_set(unioned, size++, Config::from_bitset(bitset, region));
457   }
458   size = ExtendUnion(unioned, type1, size);
459   size = ExtendUnion(unioned, type2, size);
460
461   if (size == 1) {
462     return Config::union_get(unioned, 0);
463   } else {
464     Config::union_shrink(unioned, size);
465     return Config::from_union(unioned);
466   }
467 }
468
469
470 // Get non-bitsets from type which are also in other, store at unioned,
471 // starting at index. Returns updated index.
472 template<class Config>
473 int TypeImpl<Config>::ExtendIntersection(
474     UnionedHandle result, TypeHandle type, TypeHandle other, int current_size) {
475   int old_size = current_size;
476   if (type->IsClass() || type->IsConstant()) {
477     if (type->Is(other) && !type->InUnion(result, old_size)) {
478       Config::union_set(result, current_size++, type);
479     }
480   } else if (type->IsUnion()) {
481     UnionedHandle unioned = type->AsUnion();
482     for (int i = 0; i < Config::union_length(unioned); ++i) {
483       TypeHandle type = Config::union_get(unioned, i);
484       ASSERT(i == 0 ||
485              !(type->IsBitset() || type->Is(Config::union_get(unioned, 0))));
486       if (!type->IsBitset() && type->Is(other) &&
487           !type->InUnion(result, old_size)) {
488         Config::union_set(result, current_size++, type);
489       }
490     }
491   }
492   return current_size;
493 }
494
495
496 // Intersection is O(1) on simple bit unions, but O(n*m) on structured unions.
497 // TODO(rossberg): Should we use object sets somehow? Is it worth it?
498 template<class Config>
499 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
500     TypeHandle type1, TypeHandle type2, Region* region) {
501   // Fast case: bit sets.
502   if (type1->IsBitset() && type2->IsBitset()) {
503     return Config::from_bitset(type1->AsBitset() & type2->AsBitset(), region);
504   }
505
506   // Fast case: top or bottom types.
507   if (type1->IsNone()) return type1;
508   if (type2->IsNone()) return type2;
509   if (type1->IsAny()) return type2;
510   if (type2->IsAny()) return type1;
511
512   // Semi-fast case: Unioned objects are neither involved nor produced.
513   if (!(type1->IsUnion() || type2->IsUnion())) {
514     if (type1->Is(type2)) return type1;
515     if (type2->Is(type1)) return type2;
516   }
517
518   // Slow case: may need to produce a Unioned object.
519   int size = 0;
520   if (!type1->IsBitset()) {
521     size = (type1->IsUnion() ? Config::union_length(type1->AsUnion()) : 2);
522   }
523   if (!type2->IsBitset()) {
524     int size2 = (type2->IsUnion() ? Config::union_length(type2->AsUnion()) : 2);
525     size = (size == 0 ? size2 : Min(size, size2));
526   }
527   ASSERT(size >= 2);
528   UnionedHandle unioned = Config::union_create(size, region);
529   size = 0;
530
531   int bitset = type1->GlbBitset() & type2->GlbBitset();
532   if (bitset != kNone) {
533     Config::union_set(unioned, size++, Config::from_bitset(bitset, region));
534   }
535   size = ExtendIntersection(unioned, type1, type2, size);
536   size = ExtendIntersection(unioned, type2, type1, size);
537
538   if (size == 0) {
539     return None(region);
540   } else if (size == 1) {
541     return Config::union_get(unioned, 0);
542   } else {
543     Config::union_shrink(unioned, size);
544     return Config::from_union(unioned);
545   }
546 }
547
548
549 template<class Config>
550 template<class OtherType>
551 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
552     typename OtherType::TypeHandle type, Region* region) {
553   if (type->IsBitset()) {
554     return Config::from_bitset(type->AsBitset(), region);
555   } else if (type->IsClass()) {
556     return Config::from_class(type->AsClass(), type->LubBitset(), region);
557   } else if (type->IsConstant()) {
558     return Config::from_constant(type->AsConstant(), type->LubBitset(), region);
559   } else {
560     ASSERT(type->IsUnion());
561     typename OtherType::UnionedHandle unioned = type->AsUnion();
562     int length = OtherType::UnionLength(unioned);
563     UnionedHandle new_unioned = Config::union_create(length, region);
564     for (int i = 0; i < length; ++i) {
565       Config::union_set(new_unioned, i,
566           Convert<OtherType>(OtherType::UnionGet(unioned, i), region));
567     }
568     return Config::from_union(new_unioned);
569   }
570 }
571
572
573 // TODO(rossberg): this does not belong here.
574 Representation Representation::FromType(Type* type) {
575   if (type->Is(Type::None())) return Representation::None();
576   if (type->Is(Type::SignedSmall())) return Representation::Smi();
577   if (type->Is(Type::Signed32())) return Representation::Integer32();
578   if (type->Is(Type::Number())) return Representation::Double();
579   if (type->Is(Type::Float32x4())) return Representation::Float32x4();
580   if (type->Is(Type::Int32x4())) return Representation::Int32x4();
581   return Representation::Tagged();
582 }
583
584
585 #ifdef OBJECT_PRINT
586 template<class Config>
587 void TypeImpl<Config>::TypePrint(PrintDimension dim) {
588   TypePrint(stdout, dim);
589   PrintF(stdout, "\n");
590   Flush(stdout);
591 }
592
593
594 template<class Config>
595 const char* TypeImpl<Config>::bitset_name(int bitset) {
596   switch (bitset) {
597     case kAny & kRepresentation: return "Any";
598     #define PRINT_COMPOSED_TYPE(type, value) \
599     case k##type & kRepresentation: return #type;
600     REPRESENTATION_BITSET_TYPE_LIST(PRINT_COMPOSED_TYPE)
601     #undef PRINT_COMPOSED_TYPE
602
603     #define PRINT_COMPOSED_TYPE(type, value) \
604     case k##type & kSemantic: return #type;
605     SEMANTIC_BITSET_TYPE_LIST(PRINT_COMPOSED_TYPE)
606     #undef PRINT_COMPOSED_TYPE
607
608     default:
609       return NULL;
610   }
611 }
612
613
614 template<class Config>
615 void TypeImpl<Config>::BitsetTypePrint(FILE* out, int bitset) {
616   const char* name = bitset_name(bitset);
617   if (name != NULL) {
618     PrintF(out, "%s", name);
619   } else {
620     static const int named_bitsets[] = {
621       #define BITSET_CONSTANT(type, value) k##type & kRepresentation,
622       REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT)
623       #undef BITSET_CONSTANT
624
625       #define BITSET_CONSTANT(type, value) k##type & kSemantic,
626       SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
627       #undef BITSET_CONSTANT
628     };
629
630     bool is_first = true;
631     PrintF(out, "(");
632     for (int i(ARRAY_SIZE(named_bitsets) - 1); bitset != 0 && i >= 0; --i) {
633       int subset = named_bitsets[i];
634       if ((bitset & subset) == subset) {
635         if (!is_first) PrintF(out, " | ");
636         is_first = false;
637         PrintF(out, "%s", bitset_name(subset));
638         bitset -= subset;
639       }
640     }
641     ASSERT(bitset == 0);
642     PrintF(out, ")");
643   }
644 }
645
646
647 template<class Config>
648 void TypeImpl<Config>::TypePrint(FILE* out, PrintDimension dim) {
649   if (this->IsBitset()) {
650     int bitset = this->AsBitset();
651     switch (dim) {
652       case BOTH_DIMS:
653         BitsetTypePrint(out, bitset & kSemantic);
654         PrintF("/");
655         BitsetTypePrint(out, bitset & kRepresentation);
656         break;
657       case SEMANTIC_DIM:
658         BitsetTypePrint(out, bitset & kSemantic);
659         break;
660       case REPRESENTATION_DIM:
661         BitsetTypePrint(out, bitset & kRepresentation);
662         break;
663     }
664   } else if (this->IsConstant()) {
665     PrintF(out, "Constant(%p : ", static_cast<void*>(*this->AsConstant()));
666     Config::from_bitset(this->LubBitset())->TypePrint(out);
667     PrintF(")");
668   } else if (this->IsClass()) {
669     PrintF(out, "Class(%p < ", static_cast<void*>(*this->AsClass()));
670     Config::from_bitset(this->LubBitset())->TypePrint(out);
671     PrintF(")");
672   } else if (this->IsUnion()) {
673     PrintF(out, "(");
674     UnionedHandle unioned = this->AsUnion();
675     for (int i = 0; i < Config::union_length(unioned); ++i) {
676       TypeHandle type_i = Config::union_get(unioned, i);
677       if (i > 0) PrintF(out, " | ");
678       type_i->TypePrint(out);
679     }
680     PrintF(out, ")");
681   }
682 }
683 #endif
684
685
686 template class TypeImpl<ZoneTypeConfig>;
687 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
688 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
689
690 template class TypeImpl<HeapTypeConfig>;
691 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
692 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
693
694 template TypeImpl<ZoneTypeConfig>::TypeHandle
695   TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
696     TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
697 template TypeImpl<HeapTypeConfig>::TypeHandle
698   TypeImpl<HeapTypeConfig>::Convert<Type>(
699     TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
700
701
702 } }  // namespace v8::internal