0aae0641710431626ebc991fc3ebb94609248e4c
[platform/upstream/nodejs.git] / deps / v8 / src / types.h
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 #ifndef V8_TYPES_H_
6 #define V8_TYPES_H_
7
8 #include "src/conversions.h"
9 #include "src/factory.h"
10 #include "src/handles.h"
11 #include "src/ostreams.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // SUMMARY
17 //
18 // A simple type system for compiler-internal use. It is based entirely on
19 // union types, and all subtyping hence amounts to set inclusion. Besides the
20 // obvious primitive types and some predefined unions, the type language also
21 // can express class types (a.k.a. specific maps) and singleton types (i.e.,
22 // concrete constants).
23 //
24 // Types consist of two dimensions: semantic (value range) and representation.
25 // Both are related through subtyping.
26 //
27 //
28 // SEMANTIC DIMENSION
29 //
30 // The following equations and inequations hold for the semantic axis:
31 //
32 //   None <= T
33 //   T <= Any
34 //
35 //   Number = Signed32 \/ Unsigned32 \/ Double
36 //   Smi <= Signed32
37 //   Name = String \/ Symbol
38 //   UniqueName = InternalizedString \/ Symbol
39 //   InternalizedString < String
40 //
41 //   Receiver = Object \/ Proxy
42 //   Array < Object
43 //   Function < Object
44 //   RegExp < Object
45 //   Undetectable < Object
46 //   Detectable = Receiver \/ Number \/ Name - Undetectable
47 //
48 //   Class(map) < T   iff instance_type(map) < T
49 //   Constant(x) < T  iff instance_type(map(x)) < T
50 //   Array(T) < Array
51 //   Function(R, S, T0, T1, ...) < Function
52 //   Context(T) < Internal
53 //
54 // Both structural Array and Function types are invariant in all parameters;
55 // relaxing this would make Union and Intersect operations more involved.
56 // There is no subtyping relation between Array, Function, or Context types
57 // and respective Constant types, since these types cannot be reconstructed
58 // for arbitrary heap values.
59 // Note also that Constant(x) < Class(map(x)) does _not_ hold, since x's map can
60 // change! (Its instance type cannot, however.)
61 // TODO(rossberg): the latter is not currently true for proxies, because of fix,
62 // but will hold once we implement direct proxies.
63 // However, we also define a 'temporal' variant of the subtyping relation that
64 // considers the _current_ state only, i.e., Constant(x) <_now Class(map(x)).
65 //
66 //
67 // REPRESENTATIONAL DIMENSION
68 //
69 // For the representation axis, the following holds:
70 //
71 //   None <= R
72 //   R <= Any
73 //
74 //   UntaggedInt = UntaggedInt1 \/ UntaggedInt8 \/
75 //                 UntaggedInt16 \/ UntaggedInt32
76 //   UntaggedFloat = UntaggedFloat32 \/ UntaggedFloat64
77 //   UntaggedNumber = UntaggedInt \/ UntaggedFloat
78 //   Untagged = UntaggedNumber \/ UntaggedPtr
79 //   Tagged = TaggedInt \/ TaggedPtr
80 //
81 // Subtyping relates the two dimensions, for example:
82 //
83 //   Number <= Tagged \/ UntaggedNumber
84 //   Object <= TaggedPtr \/ UntaggedPtr
85 //
86 // That holds because the semantic type constructors defined by the API create
87 // types that allow for all possible representations, and dually, the ones for
88 // representation types initially include all semantic ranges. Representations
89 // can then e.g. be narrowed for a given semantic type using intersection:
90 //
91 //   SignedSmall /\ TaggedInt       (a 'smi')
92 //   Number /\ TaggedPtr            (a heap number)
93 //
94 //
95 // RANGE TYPES
96 //
97 // A range type represents a continuous integer interval by its minimum and
98 // maximum value.  Either value might be an infinity.
99 //
100 // Constant(v) is considered a subtype of Range(x..y) if v happens to be an
101 // integer between x and y.
102 //
103 //
104 // PREDICATES
105 //
106 // There are two main functions for testing types:
107 //
108 //   T1->Is(T2)     -- tests whether T1 is included in T2 (i.e., T1 <= T2)
109 //   T1->Maybe(T2)  -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0)
110 //
111 // Typically, the former is to be used to select representations (e.g., via
112 // T->Is(SignedSmall())), and the latter to check whether a specific case needs
113 // handling (e.g., via T->Maybe(Number())).
114 //
115 // There is no functionality to discover whether a type is a leaf in the
116 // lattice. That is intentional. It should always be possible to refine the
117 // lattice (e.g., splitting up number types further) without invalidating any
118 // existing assumptions or tests.
119 // Consequently, do not normally use Equals for type tests, always use Is!
120 //
121 // The NowIs operator implements state-sensitive subtying, as described above.
122 // Any compilation decision based on such temporary properties requires runtime
123 // guarding!
124 //
125 //
126 // PROPERTIES
127 //
128 // Various formal properties hold for constructors, operators, and predicates
129 // over types. For example, constructors are injective and subtyping is a
130 // complete partial order.
131 //
132 // See test/cctest/test-types.cc for a comprehensive executable specification,
133 // especially with respect to the properties of the more exotic 'temporal'
134 // constructors and predicates (those prefixed 'Now').
135 //
136 //
137 // IMPLEMENTATION
138 //
139 // Internally, all 'primitive' types, and their unions, are represented as
140 // bitsets. Bit 0 is reserved for tagging. Class is a heap pointer to the
141 // respective map. Only structured types require allocation.
142 // Note that the bitset representation is closed under both Union and Intersect.
143 //
144 // There are two type representations, using different allocation:
145 //
146 // - class Type (zone-allocated, for compiler and concurrent compilation)
147 // - class HeapType (heap-allocated, for persistent types)
148 //
149 // Both provide the same API, and the Convert method can be used to interconvert
150 // them. For zone types, no query method touches the heap, only constructors do.
151
152
153 // -----------------------------------------------------------------------------
154 // Values for bitset types
155
156 // clang-format off
157
158 #define MASK_BITSET_TYPE_LIST(V) \
159   V(Representation, 0xfff00000u) \
160   V(Semantic,       0x000ffffeu)
161
162 #define REPRESENTATION(k) ((k) & BitsetType::kRepresentation)
163 #define SEMANTIC(k)       ((k) & BitsetType::kSemantic)
164
165 #define REPRESENTATION_BITSET_TYPE_LIST(V)    \
166   V(None,               0)                    \
167   V(UntaggedBit,        1u << 20 | kSemantic) \
168   V(UntaggedSigned8,    1u << 21 | kSemantic) \
169   V(UntaggedSigned16,   1u << 22 | kSemantic) \
170   V(UntaggedSigned32,   1u << 23 | kSemantic) \
171   V(UntaggedUnsigned8,  1u << 24 | kSemantic) \
172   V(UntaggedUnsigned16, 1u << 25 | kSemantic) \
173   V(UntaggedUnsigned32, 1u << 26 | kSemantic) \
174   V(UntaggedFloat32,    1u << 27 | kSemantic) \
175   V(UntaggedFloat64,    1u << 28 | kSemantic) \
176   V(UntaggedPointer,    1u << 29 | kSemantic) \
177   V(TaggedSigned,       1u << 30 | kSemantic) \
178   V(TaggedPointer,      1u << 31 | kSemantic) \
179   \
180   V(UntaggedSigned,     kUntaggedSigned8 | kUntaggedSigned16 |              \
181                         kUntaggedSigned32)                                  \
182   V(UntaggedUnsigned,   kUntaggedUnsigned8 | kUntaggedUnsigned16 |          \
183                         kUntaggedUnsigned32)                                \
184   V(UntaggedIntegral8,  kUntaggedSigned8 | kUntaggedUnsigned8)              \
185   V(UntaggedIntegral16, kUntaggedSigned16 | kUntaggedUnsigned16)            \
186   V(UntaggedIntegral32, kUntaggedSigned32 | kUntaggedUnsigned32)            \
187   V(UntaggedIntegral,   kUntaggedBit | kUntaggedSigned | kUntaggedUnsigned) \
188   V(UntaggedFloat,      kUntaggedFloat32 | kUntaggedFloat64)                \
189   V(UntaggedNumber,     kUntaggedIntegral | kUntaggedFloat)                 \
190   V(Untagged,           kUntaggedNumber | kUntaggedPointer)                 \
191   V(Tagged,             kTaggedSigned | kTaggedPointer)
192
193 #define INTERNAL_BITSET_TYPE_LIST(V)                                      \
194   V(OtherUnsigned31, 1u << 1 | REPRESENTATION(kTagged | kUntaggedNumber)) \
195   V(OtherUnsigned32, 1u << 2 | REPRESENTATION(kTagged | kUntaggedNumber)) \
196   V(OtherSigned32,   1u << 3 | REPRESENTATION(kTagged | kUntaggedNumber)) \
197   V(OtherNumber,     1u << 4 | REPRESENTATION(kTagged | kUntaggedNumber))
198
199 #define SEMANTIC_BITSET_TYPE_LIST(V) \
200   V(Negative31,          1u << 5  | REPRESENTATION(kTagged | kUntaggedNumber)) \
201   V(Null,                1u << 6  | REPRESENTATION(kTaggedPointer)) \
202   V(Undefined,           1u << 7  | REPRESENTATION(kTaggedPointer)) \
203   V(Boolean,             1u << 8  | REPRESENTATION(kTaggedPointer)) \
204   V(Unsigned30,          1u << 9  | REPRESENTATION(kTagged | kUntaggedNumber)) \
205   V(MinusZero,           1u << 10 | REPRESENTATION(kTagged | kUntaggedNumber)) \
206   V(NaN,                 1u << 11 | REPRESENTATION(kTagged | kUntaggedNumber)) \
207   V(Symbol,              1u << 12 | REPRESENTATION(kTaggedPointer)) \
208   V(InternalizedString,  1u << 13 | REPRESENTATION(kTaggedPointer)) \
209   V(OtherString,         1u << 14 | REPRESENTATION(kTaggedPointer)) \
210   V(Undetectable,        1u << 15 | REPRESENTATION(kTaggedPointer)) \
211   V(Array,               1u << 16 | REPRESENTATION(kTaggedPointer)) \
212   V(OtherObject,         1u << 17 | REPRESENTATION(kTaggedPointer)) \
213   V(Proxy,               1u << 18 | REPRESENTATION(kTaggedPointer)) \
214   V(Internal,            1u << 19 | REPRESENTATION(kTagged | kUntagged)) \
215   \
216   V(Signed31,            kUnsigned30 | kNegative31) \
217   V(Signed32,            kSigned31 | kOtherUnsigned31 | kOtherSigned32) \
218   V(Negative32,          kNegative31 | kOtherSigned32) \
219   V(Unsigned31,          kUnsigned30 | kOtherUnsigned31) \
220   V(Unsigned32,          kUnsigned30 | kOtherUnsigned31 | kOtherUnsigned32) \
221   V(Integral32,          kSigned32 | kUnsigned32) \
222   V(PlainNumber,         kIntegral32 | kOtherNumber) \
223   V(OrderedNumber,       kPlainNumber | kMinusZero) \
224   V(Number,              kOrderedNumber | kNaN) \
225   V(String,              kInternalizedString | kOtherString) \
226   V(UniqueName,          kSymbol | kInternalizedString) \
227   V(Name,                kSymbol | kString) \
228   V(NumberOrString,      kNumber | kString) \
229   V(PlainPrimitive,      kNumberOrString | kBoolean | kNull | kUndefined) \
230   V(Primitive,           kSymbol | kPlainPrimitive) \
231   V(DetectableObject,    kArray | kOtherObject) \
232   V(DetectableReceiver,  kDetectableObject | kProxy) \
233   V(Detectable,          kDetectableReceiver | kNumber | kName) \
234   V(Object,              kDetectableObject | kUndetectable) \
235   V(Receiver,            kObject | kProxy) \
236   V(StringOrReceiver,    kString | kReceiver) \
237   V(Unique,              kBoolean | kUniqueName | kNull | kUndefined | \
238                          kReceiver) \
239   V(NonNumber,           kUnique | kString | kInternal) \
240   V(Any,                 0xfffffffeu)
241
242 // clang-format on
243
244 /*
245  * The following diagrams show how integers (in the mathematical sense) are
246  * divided among the different atomic numerical types.
247  *
248  *   ON    OS32     N31     U30     OU31    OU32     ON
249  * ______[_______[_______[_______[_______[_______[_______
250  *     -2^31   -2^30     0      2^30    2^31    2^32
251  *
252  * E.g., OtherUnsigned32 (OU32) covers all integers from 2^31 to 2^32-1.
253  */
254
255 #define PROPER_BITSET_TYPE_LIST(V) \
256   REPRESENTATION_BITSET_TYPE_LIST(V) \
257   SEMANTIC_BITSET_TYPE_LIST(V)
258
259 #define BITSET_TYPE_LIST(V)          \
260   MASK_BITSET_TYPE_LIST(V)           \
261   REPRESENTATION_BITSET_TYPE_LIST(V) \
262   INTERNAL_BITSET_TYPE_LIST(V)       \
263   SEMANTIC_BITSET_TYPE_LIST(V)
264
265
266 // -----------------------------------------------------------------------------
267 // The abstract Type class, parameterized over the low-level representation.
268
269 // struct Config {
270 //   typedef TypeImpl<Config> Type;
271 //   typedef Base;
272 //   typedef Struct;
273 //   typedef Range;
274 //   typedef Region;
275 //   template<class> struct Handle { typedef type; }  // No template typedefs...
276 //   template<class T> static Handle<T>::type null_handle();
277 //   template<class T> static Handle<T>::type handle(T* t);  // !is_bitset(t)
278 //   template<class T> static Handle<T>::type cast(Handle<Type>::type);
279 //
280 //   static bool is_bitset(Type*);
281 //   static bool is_class(Type*);
282 //   static bool is_struct(Type*, int tag);
283 //   static bool is_range(Type*);
284 //
285 //   static bitset as_bitset(Type*);
286 //   static i::Handle<i::Map> as_class(Type*);
287 //   static Handle<Struct>::type as_struct(Type*);
288 //   static Handle<Range>::type as_range(Type*);
289 //
290 //   static Type* from_bitset(bitset);
291 //   static Handle<Type>::type from_bitset(bitset, Region*);
292 //   static Handle<Type>::type from_class(i::Handle<Map>, Region*);
293 //   static Handle<Type>::type from_struct(Handle<Struct>::type, int tag);
294 //   static Handle<Type>::type from_range(Handle<Range>::type);
295 //
296 //   static Handle<Struct>::type struct_create(int tag, int length, Region*);
297 //   static void struct_shrink(Handle<Struct>::type, int length);
298 //   static int struct_tag(Handle<Struct>::type);
299 //   static int struct_length(Handle<Struct>::type);
300 //   static Handle<Type>::type struct_get(Handle<Struct>::type, int);
301 //   static void struct_set(Handle<Struct>::type, int, Handle<Type>::type);
302 //   template<class V>
303 //   static i::Handle<V> struct_get_value(Handle<Struct>::type, int);
304 //   template<class V>
305 //   static void struct_set_value(Handle<Struct>::type, int, i::Handle<V>);
306 //
307 //   static Handle<Range>::type range_create(Region*);
308 //   static int range_get_bitset(Handle<Range>::type);
309 //   static void range_set_bitset(Handle<Range>::type, int);
310 //   static double range_get_double(Handle<Range>::type, int);
311 //   static void range_set_double(Handle<Range>::type, int, double, Region*);
312 // }
313 template<class Config>
314 class TypeImpl : public Config::Base {
315  public:
316   // Auxiliary types.
317
318   typedef uint32_t bitset;  // Internal
319   class BitsetType;         // Internal
320   class StructuralType;     // Internal
321   class UnionType;          // Internal
322
323   class ClassType;
324   class ConstantType;
325   class RangeType;
326   class ContextType;
327   class ArrayType;
328   class FunctionType;
329
330   typedef typename Config::template Handle<TypeImpl>::type TypeHandle;
331   typedef typename Config::template Handle<ClassType>::type ClassHandle;
332   typedef typename Config::template Handle<ConstantType>::type ConstantHandle;
333   typedef typename Config::template Handle<RangeType>::type RangeHandle;
334   typedef typename Config::template Handle<ContextType>::type ContextHandle;
335   typedef typename Config::template Handle<ArrayType>::type ArrayHandle;
336   typedef typename Config::template Handle<FunctionType>::type FunctionHandle;
337   typedef typename Config::template Handle<UnionType>::type UnionHandle;
338   typedef typename Config::Region Region;
339
340   // Constructors.
341
342   #define DEFINE_TYPE_CONSTRUCTOR(type, value)                                \
343     static TypeImpl* type() {                                                 \
344       return BitsetType::New(BitsetType::k##type);                            \
345     }                                                                         \
346     static TypeHandle type(Region* region) {                                  \
347       return BitsetType::New(BitsetType::k##type, region);                    \
348     }
349   PROPER_BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR)
350   #undef DEFINE_TYPE_CONSTRUCTOR
351
352   static TypeImpl* SignedSmall() {
353     return BitsetType::New(BitsetType::SignedSmall());
354   }
355   static TypeHandle SignedSmall(Region* region) {
356     return BitsetType::New(BitsetType::SignedSmall(), region);
357   }
358   static TypeImpl* UnsignedSmall() {
359     return BitsetType::New(BitsetType::UnsignedSmall());
360   }
361   static TypeHandle UnsignedSmall(Region* region) {
362     return BitsetType::New(BitsetType::UnsignedSmall(), region);
363   }
364
365   static TypeHandle Class(i::Handle<i::Map> map, Region* region) {
366     return ClassType::New(map, region);
367   }
368   static TypeHandle Constant(i::Handle<i::Object> value, Region* region) {
369     return ConstantType::New(value, region);
370   }
371   static TypeHandle Range(double min, double max, Region* region) {
372     return RangeType::New(
373         min, max, BitsetType::New(REPRESENTATION(BitsetType::kTagged |
374                                                  BitsetType::kUntaggedNumber),
375                                   region),
376         region);
377   }
378   static TypeHandle Context(TypeHandle outer, Region* region) {
379     return ContextType::New(outer, region);
380   }
381   static TypeHandle Array(TypeHandle element, Region* region) {
382     return ArrayType::New(element, region);
383   }
384   static FunctionHandle Function(
385       TypeHandle result, TypeHandle receiver, int arity, Region* region) {
386     return FunctionType::New(result, receiver, arity, region);
387   }
388   static TypeHandle Function(TypeHandle result, Region* region) {
389     return Function(result, Any(region), 0, region);
390   }
391   static TypeHandle Function(
392       TypeHandle result, TypeHandle param0, Region* region) {
393     FunctionHandle function = Function(result, Any(region), 1, region);
394     function->InitParameter(0, param0);
395     return function;
396   }
397   static TypeHandle Function(
398       TypeHandle result, TypeHandle param0, TypeHandle param1, Region* region) {
399     FunctionHandle function = Function(result, Any(region), 2, region);
400     function->InitParameter(0, param0);
401     function->InitParameter(1, param1);
402     return function;
403   }
404   static TypeHandle Function(
405       TypeHandle result, TypeHandle param0, TypeHandle param1,
406       TypeHandle param2, Region* region) {
407     FunctionHandle function = Function(result, Any(region), 3, region);
408     function->InitParameter(0, param0);
409     function->InitParameter(1, param1);
410     function->InitParameter(2, param2);
411     return function;
412   }
413
414   static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg);
415   static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg);
416   static TypeImpl* Union(TypeImpl* type1, TypeImpl* type2) {
417     return BitsetType::New(type1->AsBitset() | type2->AsBitset());
418   }
419   static TypeImpl* Intersect(TypeImpl* type1, TypeImpl* type2) {
420     return BitsetType::New(type1->AsBitset() & type2->AsBitset());
421   }
422
423   static TypeHandle Of(double value, Region* region) {
424     return Config::from_bitset(BitsetType::Lub(value), region);
425   }
426   static TypeHandle Of(i::Object* value, Region* region) {
427     return Config::from_bitset(BitsetType::Lub(value), region);
428   }
429   static TypeHandle Of(i::Handle<i::Object> value, Region* region) {
430     return Of(*value, region);
431   }
432
433   // Extraction of components.
434   static TypeHandle Representation(TypeHandle t, Region* region);
435   static TypeHandle Semantic(TypeHandle t, Region* region);
436
437   // Predicates.
438   bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); }
439
440   bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); }
441   template<class TypeHandle>
442   bool Is(TypeHandle that) { return this->Is(*that); }
443
444   bool Maybe(TypeImpl* that);
445   template<class TypeHandle>
446   bool Maybe(TypeHandle that) { return this->Maybe(*that); }
447
448   bool Equals(TypeImpl* that) { return this->Is(that) && that->Is(this); }
449   template<class TypeHandle>
450   bool Equals(TypeHandle that) { return this->Equals(*that); }
451
452   // Equivalent to Constant(val)->Is(this), but avoiding allocation.
453   bool Contains(i::Object* val);
454   bool Contains(i::Handle<i::Object> val) { return this->Contains(*val); }
455
456   // State-dependent versions of the above that consider subtyping between
457   // a constant and its map class.
458   inline static TypeHandle NowOf(i::Object* value, Region* region);
459   static TypeHandle NowOf(i::Handle<i::Object> value, Region* region) {
460     return NowOf(*value, region);
461   }
462   bool NowIs(TypeImpl* that);
463   template<class TypeHandle>
464   bool NowIs(TypeHandle that)  { return this->NowIs(*that); }
465   inline bool NowContains(i::Object* val);
466   bool NowContains(i::Handle<i::Object> val) { return this->NowContains(*val); }
467
468   bool NowStable();
469
470   // Inspection.
471
472   bool IsRange() { return Config::is_range(this); }
473   bool IsClass() {
474     return Config::is_class(this)
475         || Config::is_struct(this, StructuralType::kClassTag);
476   }
477   bool IsConstant() {
478     return Config::is_struct(this, StructuralType::kConstantTag);
479   }
480   bool IsContext() {
481     return Config::is_struct(this, StructuralType::kContextTag);
482   }
483   bool IsArray() {
484     return Config::is_struct(this, StructuralType::kArrayTag);
485   }
486   bool IsFunction() {
487     return Config::is_struct(this, StructuralType::kFunctionTag);
488   }
489
490   ClassType* AsClass() { return ClassType::cast(this); }
491   ConstantType* AsConstant() { return ConstantType::cast(this); }
492   RangeType* AsRange() { return RangeType::cast(this); }
493   ContextType* AsContext() { return ContextType::cast(this); }
494   ArrayType* AsArray() { return ArrayType::cast(this); }
495   FunctionType* AsFunction() { return FunctionType::cast(this); }
496
497   // Minimum and maximum of a numeric type.
498   // These functions do not distinguish between -0 and +0.  If the type equals
499   // kNaN, they return NaN; otherwise kNaN is ignored.  Only call these
500   // functions on subtypes of Number.
501   double Min();
502   double Max();
503
504   // Extracts a range from the type. If the type is a range, it just
505   // returns it; if it is a union, it returns the range component.
506   // Note that it does not contain range for constants.
507   RangeType* GetRange();
508
509   int NumClasses();
510   int NumConstants();
511
512   template<class T> class Iterator;
513   Iterator<i::Map> Classes() {
514     if (this->IsBitset()) return Iterator<i::Map>();
515     return Iterator<i::Map>(Config::handle(this));
516   }
517   Iterator<i::Object> Constants() {
518     if (this->IsBitset()) return Iterator<i::Object>();
519     return Iterator<i::Object>(Config::handle(this));
520   }
521
522   // Casting and conversion.
523
524   static inline TypeImpl* cast(typename Config::Base* object);
525
526   template<class OtherTypeImpl>
527   static TypeHandle Convert(
528       typename OtherTypeImpl::TypeHandle type, Region* region);
529
530   // Printing.
531
532   enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM };
533
534   void PrintTo(std::ostream& os, PrintDimension dim = BOTH_DIMS);  // NOLINT
535
536 #ifdef DEBUG
537   void Print();
538 #endif
539
540   bool IsUnionForTesting() { return IsUnion(); }
541
542  protected:
543   // Friends.
544
545   template<class> friend class Iterator;
546   template<class> friend class TypeImpl;
547
548   // Handle conversion.
549
550   template<class T>
551   static typename Config::template Handle<T>::type handle(T* type) {
552     return Config::handle(type);
553   }
554   TypeImpl* unhandle() { return this; }
555
556   // Internal inspection.
557
558   bool IsNone() { return this == None(); }
559   bool IsAny() { return this == Any(); }
560   bool IsBitset() { return Config::is_bitset(this); }
561   bool IsUnion() { return Config::is_struct(this, StructuralType::kUnionTag); }
562
563   bitset AsBitset() {
564     DCHECK(this->IsBitset());
565     return static_cast<BitsetType*>(this)->Bitset();
566   }
567   UnionType* AsUnion() { return UnionType::cast(this); }
568
569   bitset Representation();
570
571   // Auxiliary functions.
572   bool SemanticMaybe(TypeImpl* that);
573
574   bitset BitsetGlb() { return BitsetType::Glb(this); }
575   bitset BitsetLub() { return BitsetType::Lub(this); }
576
577   bool SlowIs(TypeImpl* that);
578   bool SemanticIs(TypeImpl* that);
579
580   static bool IsInteger(double x) {
581     return nearbyint(x) == x && !i::IsMinusZero(x);  // Allows for infinities.
582   }
583   static bool IsInteger(i::Object* x) {
584     return x->IsNumber() && IsInteger(x->Number());
585   }
586
587   struct Limits {
588     double min;
589     double max;
590     Limits(double min, double max) : min(min), max(max) {}
591     explicit Limits(RangeType* range) : min(range->Min()), max(range->Max()) {}
592     static Limits Empty(Region* region) { return Limits(1, 0); }
593   };
594
595   static bool IsEmpty(Limits lim);
596   static Limits Intersect(Limits lhs, Limits rhs);
597   static Limits Union(Limits lhs, Limits rhs);
598   static bool Overlap(RangeType* lhs, RangeType* rhs);
599   static bool Contains(RangeType* lhs, RangeType* rhs);
600   static bool Contains(RangeType* range, ConstantType* constant);
601   static bool Contains(RangeType* range, i::Object* val);
602
603   static int UpdateRange(
604       RangeHandle type, UnionHandle result, int size, Region* region);
605
606   static Limits IntersectRangeAndBitset(TypeHandle range, TypeHandle bits,
607                                         Region* region);
608   static Limits ToLimits(bitset bits, Region* region);
609
610   bool SimplyEquals(TypeImpl* that);
611   template<class TypeHandle>
612   bool SimplyEquals(TypeHandle that) { return this->SimplyEquals(*that); }
613
614   static int AddToUnion(
615       TypeHandle type, UnionHandle result, int size, Region* region);
616   static int IntersectAux(TypeHandle type, TypeHandle other, UnionHandle result,
617                           int size, Limits* limits, Region* region);
618   static TypeHandle NormalizeUnion(UnionHandle unioned, int size);
619   static TypeHandle NormalizeRangeAndBitset(RangeHandle range, bitset* bits,
620                                             Region* region);
621 };
622
623
624 // -----------------------------------------------------------------------------
625 // Bitset types (internal).
626
627 template<class Config>
628 class TypeImpl<Config>::BitsetType : public TypeImpl<Config> {
629  protected:
630   friend class TypeImpl<Config>;
631
632   enum {
633     #define DECLARE_TYPE(type, value) k##type = (value),
634     BITSET_TYPE_LIST(DECLARE_TYPE)
635     #undef DECLARE_TYPE
636     kUnusedEOL = 0
637   };
638
639   static bitset SignedSmall();
640   static bitset UnsignedSmall();
641
642   bitset Bitset() { return Config::as_bitset(this); }
643
644   static TypeImpl* New(bitset bits) {
645     if (FLAG_enable_slow_asserts) CheckNumberBits(bits);
646     return Config::from_bitset(bits);
647   }
648   static TypeHandle New(bitset bits, Region* region) {
649     if (FLAG_enable_slow_asserts) CheckNumberBits(bits);
650     return Config::from_bitset(bits, region);
651   }
652
653   static bool IsInhabited(bitset bits) {
654     return SEMANTIC(bits) != kNone && REPRESENTATION(bits) != kNone;
655   }
656
657   static bool SemanticIsInhabited(bitset bits) {
658     return SEMANTIC(bits) != kNone;
659   }
660
661   static bool Is(bitset bits1, bitset bits2) {
662     return (bits1 | bits2) == bits2;
663   }
664
665   static double Min(bitset);
666   static double Max(bitset);
667
668   static bitset Glb(TypeImpl* type);  // greatest lower bound that's a bitset
669   static bitset Glb(double min, double max);
670   static bitset Lub(TypeImpl* type);  // least upper bound that's a bitset
671   static bitset Lub(i::Map* map);
672   static bitset Lub(i::Object* value);
673   static bitset Lub(double value);
674   static bitset Lub(double min, double max);
675
676   static const char* Name(bitset);
677   static void Print(std::ostream& os, bitset);  // NOLINT
678 #ifdef DEBUG
679   static void Print(bitset);
680 #endif
681
682   static bitset NumberBits(bitset bits);
683
684  private:
685   struct Boundary {
686     bitset bits;
687     double min;
688   };
689   static const Boundary BoundariesArray[];
690   static inline const Boundary* Boundaries();
691   static inline size_t BoundariesSize();
692
693   static void CheckNumberBits(bitset bits);
694 };
695
696
697 // -----------------------------------------------------------------------------
698 // Superclass for non-bitset types (internal).
699 // Contains a tag and a variable number of type or value fields.
700
701 template<class Config>
702 class TypeImpl<Config>::StructuralType : public TypeImpl<Config> {
703  protected:
704   template<class> friend class TypeImpl;
705   friend struct ZoneTypeConfig;  // For tags.
706   friend struct HeapTypeConfig;
707
708   enum Tag {
709     kClassTag,
710     kConstantTag,
711     kContextTag,
712     kArrayTag,
713     kFunctionTag,
714     kUnionTag
715   };
716
717   int Length() {
718     return Config::struct_length(Config::as_struct(this));
719   }
720   TypeHandle Get(int i) {
721     DCHECK(0 <= i && i < this->Length());
722     return Config::struct_get(Config::as_struct(this), i);
723   }
724   void Set(int i, TypeHandle type) {
725     DCHECK(0 <= i && i < this->Length());
726     Config::struct_set(Config::as_struct(this), i, type);
727   }
728   void Shrink(int length) {
729     DCHECK(2 <= length && length <= this->Length());
730     Config::struct_shrink(Config::as_struct(this), length);
731   }
732   template<class V> i::Handle<V> GetValue(int i) {
733     DCHECK(0 <= i && i < this->Length());
734     return Config::template struct_get_value<V>(Config::as_struct(this), i);
735   }
736   template<class V> void SetValue(int i, i::Handle<V> x) {
737     DCHECK(0 <= i && i < this->Length());
738     Config::struct_set_value(Config::as_struct(this), i, x);
739   }
740
741   static TypeHandle New(Tag tag, int length, Region* region) {
742     DCHECK(1 <= length);
743     return Config::from_struct(Config::struct_create(tag, length, region));
744   }
745 };
746
747
748 // -----------------------------------------------------------------------------
749 // Union types (internal).
750 // A union is a structured type with the following invariants:
751 // - its length is at least 2
752 // - at most one field is a bitset, and it must go into index 0
753 // - no field is a union
754 // - no field is a subtype of any other field
755 template<class Config>
756 class TypeImpl<Config>::UnionType : public StructuralType {
757  public:
758   static UnionHandle New(int length, Region* region) {
759     return Config::template cast<UnionType>(
760         StructuralType::New(StructuralType::kUnionTag, length, region));
761   }
762
763   static UnionType* cast(TypeImpl* type) {
764     DCHECK(type->IsUnion());
765     return static_cast<UnionType*>(type);
766   }
767
768   bool Wellformed();
769 };
770
771
772 // -----------------------------------------------------------------------------
773 // Class types.
774
775 template<class Config>
776 class TypeImpl<Config>::ClassType : public StructuralType {
777  public:
778   TypeHandle Bound(Region* region) {
779     return Config::is_class(this) ?
780         BitsetType::New(BitsetType::Lub(*Config::as_class(this)), region) :
781         this->Get(0);
782   }
783   i::Handle<i::Map> Map() {
784     return Config::is_class(this) ? Config::as_class(this) :
785         this->template GetValue<i::Map>(1);
786   }
787
788   static ClassHandle New(i::Handle<i::Map> map, Region* region) {
789     ClassHandle type =
790         Config::template cast<ClassType>(Config::from_class(map, region));
791     if (!type->IsClass()) {
792       type = Config::template cast<ClassType>(
793           StructuralType::New(StructuralType::kClassTag, 2, region));
794       type->Set(0, BitsetType::New(BitsetType::Lub(*map), region));
795       type->SetValue(1, map);
796     }
797     return type;
798   }
799
800   static ClassType* cast(TypeImpl* type) {
801     DCHECK(type->IsClass());
802     return static_cast<ClassType*>(type);
803   }
804 };
805
806
807 // -----------------------------------------------------------------------------
808 // Constant types.
809
810 template<class Config>
811 class TypeImpl<Config>::ConstantType : public StructuralType {
812  public:
813   TypeHandle Bound() { return this->Get(0); }
814   i::Handle<i::Object> Value() { return this->template GetValue<i::Object>(1); }
815
816   static ConstantHandle New(i::Handle<i::Object> value, Region* region) {
817     ConstantHandle type = Config::template cast<ConstantType>(
818         StructuralType::New(StructuralType::kConstantTag, 2, region));
819     type->Set(0, BitsetType::New(BitsetType::Lub(*value), region));
820     type->SetValue(1, value);
821     return type;
822   }
823
824   static ConstantType* cast(TypeImpl* type) {
825     DCHECK(type->IsConstant());
826     return static_cast<ConstantType*>(type);
827   }
828 };
829 // TODO(neis): Also cache value if numerical.
830 // TODO(neis): Allow restricting the representation.
831
832
833 // -----------------------------------------------------------------------------
834 // Range types.
835
836 template <class Config>
837 class TypeImpl<Config>::RangeType : public TypeImpl<Config> {
838  public:
839   bitset Bound() { return Config::range_get_bitset(Config::as_range(this)); }
840   double Min() { return Config::range_get_double(Config::as_range(this), 0); }
841   double Max() { return Config::range_get_double(Config::as_range(this), 1); }
842
843   static RangeHandle New(double min, double max, TypeHandle representation,
844                          Region* region) {
845     DCHECK(IsInteger(min) && IsInteger(max));
846     DCHECK(min <= max);
847     bitset representation_bits = representation->AsBitset();
848     DCHECK(REPRESENTATION(representation_bits) == representation_bits);
849
850     typename Config::template Handle<typename Config::Range>::type range =
851         Config::range_create(region);
852
853     bitset bits = SEMANTIC(BitsetType::Lub(min, max)) | representation_bits;
854     Config::range_set_bitset(range, bits);
855     Config::range_set_double(range, 0, min, region);
856     Config::range_set_double(range, 1, max, region);
857     return Config::template cast<RangeType>(Config::from_range(range));
858   }
859
860   static RangeHandle New(Limits lim, bitset representation, Region* region) {
861     return New(lim.min, lim.max, BitsetType::New(representation, region),
862                region);
863   }
864
865   static RangeType* cast(TypeImpl* type) {
866     DCHECK(type->IsRange());
867     return static_cast<RangeType*>(type);
868   }
869 };
870 // TODO(neis): Also cache min and max values.
871
872
873 // -----------------------------------------------------------------------------
874 // Context types.
875
876 template<class Config>
877 class TypeImpl<Config>::ContextType : public StructuralType {
878  public:
879   TypeHandle Outer() { return this->Get(0); }
880
881   static ContextHandle New(TypeHandle outer, Region* region) {
882     ContextHandle type = Config::template cast<ContextType>(
883         StructuralType::New(StructuralType::kContextTag, 1, region));
884     type->Set(0, outer);
885     return type;
886   }
887
888   static ContextType* cast(TypeImpl* type) {
889     DCHECK(type->IsContext());
890     return static_cast<ContextType*>(type);
891   }
892 };
893
894
895 // -----------------------------------------------------------------------------
896 // Array types.
897
898 template<class Config>
899 class TypeImpl<Config>::ArrayType : public StructuralType {
900  public:
901   TypeHandle Element() { return this->Get(0); }
902
903   static ArrayHandle New(TypeHandle element, Region* region) {
904     ArrayHandle type = Config::template cast<ArrayType>(
905         StructuralType::New(StructuralType::kArrayTag, 1, region));
906     type->Set(0, element);
907     return type;
908   }
909
910   static ArrayType* cast(TypeImpl* type) {
911     DCHECK(type->IsArray());
912     return static_cast<ArrayType*>(type);
913   }
914 };
915
916
917 // -----------------------------------------------------------------------------
918 // Function types.
919
920 template<class Config>
921 class TypeImpl<Config>::FunctionType : public StructuralType {
922  public:
923   int Arity() { return this->Length() - 2; }
924   TypeHandle Result() { return this->Get(0); }
925   TypeHandle Receiver() { return this->Get(1); }
926   TypeHandle Parameter(int i) { return this->Get(2 + i); }
927
928   void InitParameter(int i, TypeHandle type) { this->Set(2 + i, type); }
929
930   static FunctionHandle New(
931       TypeHandle result, TypeHandle receiver, int arity, Region* region) {
932     FunctionHandle type = Config::template cast<FunctionType>(
933         StructuralType::New(StructuralType::kFunctionTag, 2 + arity, region));
934     type->Set(0, result);
935     type->Set(1, receiver);
936     return type;
937   }
938
939   static FunctionType* cast(TypeImpl* type) {
940     DCHECK(type->IsFunction());
941     return static_cast<FunctionType*>(type);
942   }
943 };
944
945
946 // -----------------------------------------------------------------------------
947 // Type iterators.
948
949 template<class Config> template<class T>
950 class TypeImpl<Config>::Iterator {
951  public:
952   bool Done() const { return index_ < 0; }
953   i::Handle<T> Current();
954   void Advance();
955
956  private:
957   template<class> friend class TypeImpl;
958
959   Iterator() : index_(-1) {}
960   explicit Iterator(TypeHandle type) : type_(type), index_(-1) {
961     Advance();
962   }
963
964   inline bool matches(TypeHandle type);
965   inline TypeHandle get_type();
966
967   TypeHandle type_;
968   int index_;
969 };
970
971
972 // -----------------------------------------------------------------------------
973 // Zone-allocated types; they are either (odd) integers to represent bitsets, or
974 // (even) pointers to structures for everything else.
975
976 struct ZoneTypeConfig {
977   typedef TypeImpl<ZoneTypeConfig> Type;
978   class Base {};
979   typedef void* Struct;
980   // Hack: the Struct and Range types can be aliased in memory, the first
981   // pointer word of each both must be the tag (kRangeStructTag for Range,
982   // anything else for Struct) so that we can differentiate them.
983   struct Range {
984     void* tag;
985     int bitset;
986     double limits[2];
987   };
988   typedef i::Zone Region;
989   template<class T> struct Handle { typedef T* type; };
990
991   static const int kRangeStructTag = 0x1000;
992
993   template<class T> static inline T* null_handle();
994   template<class T> static inline T* handle(T* type);
995   template<class T> static inline T* cast(Type* type);
996
997   static inline bool is_bitset(Type* type);
998   static inline bool is_class(Type* type);
999   static inline bool is_struct(Type* type, int tag);
1000   static inline bool is_range(Type* type);
1001
1002   static inline Type::bitset as_bitset(Type* type);
1003   static inline i::Handle<i::Map> as_class(Type* type);
1004   static inline Struct* as_struct(Type* type);
1005   static inline Range* as_range(Type* type);
1006
1007   static inline Type* from_bitset(Type::bitset);
1008   static inline Type* from_bitset(Type::bitset, Zone* zone);
1009   static inline Type* from_class(i::Handle<i::Map> map, Zone* zone);
1010   static inline Type* from_struct(Struct* structured);
1011   static inline Type* from_range(Range* range);
1012
1013   static inline Struct* struct_create(int tag, int length, Zone* zone);
1014   static inline void struct_shrink(Struct* structure, int length);
1015   static inline int struct_tag(Struct* structure);
1016   static inline int struct_length(Struct* structure);
1017   static inline Type* struct_get(Struct* structure, int i);
1018   static inline void struct_set(Struct* structure, int i, Type* type);
1019   template<class V>
1020   static inline i::Handle<V> struct_get_value(Struct* structure, int i);
1021   template<class V> static inline void struct_set_value(
1022       Struct* structure, int i, i::Handle<V> x);
1023
1024   static inline Range* range_create(Zone* zone);
1025   static inline int range_get_bitset(Range* range);
1026   static inline void range_set_bitset(Range* range, int);
1027   static inline double range_get_double(Range*, int index);
1028   static inline void range_set_double(Range*, int index, double value, Zone*);
1029 };
1030
1031 typedef TypeImpl<ZoneTypeConfig> Type;
1032
1033
1034 // -----------------------------------------------------------------------------
1035 // Heap-allocated types; either smis for bitsets, maps for classes, boxes for
1036 // constants, or fixed arrays for unions.
1037
1038 struct HeapTypeConfig {
1039   typedef TypeImpl<HeapTypeConfig> Type;
1040   typedef i::Object Base;
1041   typedef i::FixedArray Struct;
1042   typedef i::FixedArray Range;
1043   typedef i::Isolate Region;
1044   template<class T> struct Handle { typedef i::Handle<T> type; };
1045
1046   static const int kRangeStructTag = 0xffff;
1047
1048   template<class T> static inline i::Handle<T> null_handle();
1049   template<class T> static inline i::Handle<T> handle(T* type);
1050   template<class T> static inline i::Handle<T> cast(i::Handle<Type> type);
1051
1052   static inline bool is_bitset(Type* type);
1053   static inline bool is_class(Type* type);
1054   static inline bool is_struct(Type* type, int tag);
1055   static inline bool is_range(Type* type);
1056
1057   static inline Type::bitset as_bitset(Type* type);
1058   static inline i::Handle<i::Map> as_class(Type* type);
1059   static inline i::Handle<Struct> as_struct(Type* type);
1060   static inline i::Handle<Range> as_range(Type* type);
1061
1062   static inline Type* from_bitset(Type::bitset);
1063   static inline i::Handle<Type> from_bitset(Type::bitset, Isolate* isolate);
1064   static inline i::Handle<Type> from_class(
1065       i::Handle<i::Map> map, Isolate* isolate);
1066   static inline i::Handle<Type> from_struct(i::Handle<Struct> structure);
1067   static inline i::Handle<Type> from_range(i::Handle<Range> range);
1068
1069   static inline i::Handle<Struct> struct_create(
1070       int tag, int length, Isolate* isolate);
1071   static inline void struct_shrink(i::Handle<Struct> structure, int length);
1072   static inline int struct_tag(i::Handle<Struct> structure);
1073   static inline int struct_length(i::Handle<Struct> structure);
1074   static inline i::Handle<Type> struct_get(i::Handle<Struct> structure, int i);
1075   static inline void struct_set(
1076       i::Handle<Struct> structure, int i, i::Handle<Type> type);
1077   template<class V>
1078   static inline i::Handle<V> struct_get_value(
1079       i::Handle<Struct> structure, int i);
1080   template<class V>
1081   static inline void struct_set_value(
1082       i::Handle<Struct> structure, int i, i::Handle<V> x);
1083
1084   static inline i::Handle<Range> range_create(Isolate* isolate);
1085   static inline int range_get_bitset(i::Handle<Range> range);
1086   static inline void range_set_bitset(i::Handle<Range> range, int value);
1087   static inline double range_get_double(i::Handle<Range> range, int index);
1088   static inline void range_set_double(i::Handle<Range> range, int index,
1089                                       double value, Isolate* isolate);
1090 };
1091
1092 typedef TypeImpl<HeapTypeConfig> HeapType;
1093
1094
1095 // -----------------------------------------------------------------------------
1096 // Type bounds. A simple struct to represent a pair of lower/upper types.
1097
1098 template<class Config>
1099 struct BoundsImpl {
1100   typedef TypeImpl<Config> Type;
1101   typedef typename Type::TypeHandle TypeHandle;
1102   typedef typename Type::Region Region;
1103
1104   TypeHandle lower;
1105   TypeHandle upper;
1106
1107   BoundsImpl() :  // Make sure accessing uninitialized bounds crashes big-time.
1108     lower(Config::template null_handle<Type>()),
1109     upper(Config::template null_handle<Type>()) {}
1110   explicit BoundsImpl(TypeHandle t) : lower(t), upper(t) {}
1111   BoundsImpl(TypeHandle l, TypeHandle u) : lower(l), upper(u) {
1112     DCHECK(lower->Is(upper));
1113   }
1114
1115   // Unrestricted bounds.
1116   static BoundsImpl Unbounded(Region* region) {
1117     return BoundsImpl(Type::None(region), Type::Any(region));
1118   }
1119
1120   // Meet: both b1 and b2 are known to hold.
1121   static BoundsImpl Both(BoundsImpl b1, BoundsImpl b2, Region* region) {
1122     TypeHandle lower = Type::Union(b1.lower, b2.lower, region);
1123     TypeHandle upper = Type::Intersect(b1.upper, b2.upper, region);
1124     // Lower bounds are considered approximate, correct as necessary.
1125     if (!lower->Is(upper)) lower = upper;
1126     return BoundsImpl(lower, upper);
1127   }
1128
1129   // Join: either b1 or b2 is known to hold.
1130   static BoundsImpl Either(BoundsImpl b1, BoundsImpl b2, Region* region) {
1131     TypeHandle lower = Type::Intersect(b1.lower, b2.lower, region);
1132     TypeHandle upper = Type::Union(b1.upper, b2.upper, region);
1133     return BoundsImpl(lower, upper);
1134   }
1135
1136   static BoundsImpl NarrowLower(BoundsImpl b, TypeHandle t, Region* region) {
1137     TypeHandle lower = Type::Union(b.lower, t, region);
1138     // Lower bounds are considered approximate, correct as necessary.
1139     if (!lower->Is(b.upper)) lower = b.upper;
1140     return BoundsImpl(lower, b.upper);
1141   }
1142   static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) {
1143     TypeHandle lower = b.lower;
1144     TypeHandle upper = Type::Intersect(b.upper, t, region);
1145     // Lower bounds are considered approximate, correct as necessary.
1146     if (!lower->Is(upper)) lower = upper;
1147     return BoundsImpl(lower, upper);
1148   }
1149
1150   bool Narrows(BoundsImpl that) {
1151     return that.lower->Is(this->lower) && this->upper->Is(that.upper);
1152   }
1153 };
1154
1155 typedef BoundsImpl<ZoneTypeConfig> Bounds;
1156
1157 } }  // namespace v8::internal
1158
1159 #endif  // V8_TYPES_H_