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.
8 #include "src/conversions.h"
9 #include "src/factory.h"
10 #include "src/handles.h"
11 #include "src/ostreams.h"
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).
24 // Types consist of two dimensions: semantic (value range) and representation.
25 // Both are related through subtyping.
30 // The following equations and inequations hold for the semantic axis:
35 // Number = Signed32 \/ Unsigned32 \/ Double
37 // Name = String \/ Symbol
38 // UniqueName = InternalizedString \/ Symbol
39 // InternalizedString < String
41 // Receiver = Object \/ Proxy
45 // Undetectable < Object
46 // Detectable = Receiver \/ Number \/ Name - Undetectable
48 // Class(map) < T iff instance_type(map) < T
49 // Constant(x) < T iff instance_type(map(x)) < T
51 // Function(R, S, T0, T1, ...) < Function
52 // Context(T) < Internal
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)).
67 // REPRESENTATIONAL DIMENSION
69 // For the representation axis, the following holds:
74 // UntaggedInt = UntaggedInt1 \/ UntaggedInt8 \/
75 // UntaggedInt16 \/ UntaggedInt32
76 // UntaggedFloat = UntaggedFloat32 \/ UntaggedFloat64
77 // UntaggedNumber = UntaggedInt \/ UntaggedFloat
78 // Untagged = UntaggedNumber \/ UntaggedPtr
79 // Tagged = TaggedInt \/ TaggedPtr
81 // Subtyping relates the two dimensions, for example:
83 // Number <= Tagged \/ UntaggedNumber
84 // Object <= TaggedPtr \/ UntaggedPtr
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:
91 // SignedSmall /\ TaggedInt (a 'smi')
92 // Number /\ TaggedPtr (a heap number)
97 // A range type represents a continuous integer interval by its minimum and
98 // maximum value. Either value might be an infinity.
100 // Constant(v) is considered a subtype of Range(x..y) if v happens to be an
101 // integer between x and y.
106 // There are two main functions for testing types:
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)
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())).
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!
121 // The NowIs operator implements state-sensitive subtying, as described above.
122 // Any compilation decision based on such temporary properties requires runtime
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.
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').
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.
144 // There are two type representations, using different allocation:
146 // - class Type (zone-allocated, for compiler and concurrent compilation)
147 // - class HeapType (heap-allocated, for persistent types)
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.
153 // -----------------------------------------------------------------------------
154 // Values for bitset types
158 #define MASK_BITSET_TYPE_LIST(V) \
159 V(Representation, 0xfff00000u) \
160 V(Semantic, 0x000ffffeu)
162 #define REPRESENTATION(k) ((k) & BitsetType::kRepresentation)
163 #define SEMANTIC(k) ((k) & BitsetType::kSemantic)
165 #define REPRESENTATION_BITSET_TYPE_LIST(V) \
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) \
180 V(UntaggedSigned, kUntaggedSigned8 | kUntaggedSigned16 | \
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)
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))
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)) \
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 | \
239 V(NonNumber, kUnique | kString | kInternal) \
245 * The following diagrams show how integers (in the mathematical sense) are
246 * divided among the different atomic numerical types.
248 * ON OS32 N31 U30 OU31 OU32 ON
249 * ______[_______[_______[_______[_______[_______[_______
250 * -2^31 -2^30 0 2^30 2^31 2^32
252 * E.g., OtherUnsigned32 (OU32) covers all integers from 2^31 to 2^32-1.
255 #define PROPER_BITSET_TYPE_LIST(V) \
256 REPRESENTATION_BITSET_TYPE_LIST(V) \
257 SEMANTIC_BITSET_TYPE_LIST(V)
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)
266 // -----------------------------------------------------------------------------
267 // The abstract Type class, parameterized over the low-level representation.
270 // typedef TypeImpl<Config> Type;
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);
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*);
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*);
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);
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);
303 // static i::Handle<V> struct_get_value(Handle<Struct>::type, int);
305 // static void struct_set_value(Handle<Struct>::type, int, i::Handle<V>);
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*);
313 template<class Config>
314 class TypeImpl : public Config::Base {
318 typedef uint32_t bitset; // Internal
319 class BitsetType; // Internal
320 class StructuralType; // Internal
321 class UnionType; // Internal
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;
342 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \
343 static TypeImpl* type() { \
344 return BitsetType::New(BitsetType::k##type); \
346 static TypeHandle type(Region* region) { \
347 return BitsetType::New(BitsetType::k##type, region); \
349 PROPER_BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR)
350 #undef DEFINE_TYPE_CONSTRUCTOR
352 static TypeImpl* SignedSmall() {
353 return BitsetType::New(BitsetType::SignedSmall());
355 static TypeHandle SignedSmall(Region* region) {
356 return BitsetType::New(BitsetType::SignedSmall(), region);
358 static TypeImpl* UnsignedSmall() {
359 return BitsetType::New(BitsetType::UnsignedSmall());
361 static TypeHandle UnsignedSmall(Region* region) {
362 return BitsetType::New(BitsetType::UnsignedSmall(), region);
365 static TypeHandle Class(i::Handle<i::Map> map, Region* region) {
366 return ClassType::New(map, region);
368 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) {
369 return ConstantType::New(value, region);
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),
378 static TypeHandle Context(TypeHandle outer, Region* region) {
379 return ContextType::New(outer, region);
381 static TypeHandle Array(TypeHandle element, Region* region) {
382 return ArrayType::New(element, region);
384 static FunctionHandle Function(
385 TypeHandle result, TypeHandle receiver, int arity, Region* region) {
386 return FunctionType::New(result, receiver, arity, region);
388 static TypeHandle Function(TypeHandle result, Region* region) {
389 return Function(result, Any(region), 0, region);
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);
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);
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);
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());
419 static TypeImpl* Intersect(TypeImpl* type1, TypeImpl* type2) {
420 return BitsetType::New(type1->AsBitset() & type2->AsBitset());
423 static TypeHandle Of(double value, Region* region) {
424 return Config::from_bitset(BitsetType::Lub(value), region);
426 static TypeHandle Of(i::Object* value, Region* region) {
427 return Config::from_bitset(BitsetType::Lub(value), region);
429 static TypeHandle Of(i::Handle<i::Object> value, Region* region) {
430 return Of(*value, region);
433 // Extraction of components.
434 static TypeHandle Representation(TypeHandle t, Region* region);
435 static TypeHandle Semantic(TypeHandle t, Region* region);
438 bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); }
440 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); }
441 template<class TypeHandle>
442 bool Is(TypeHandle that) { return this->Is(*that); }
444 bool Maybe(TypeImpl* that);
445 template<class TypeHandle>
446 bool Maybe(TypeHandle that) { return this->Maybe(*that); }
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); }
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); }
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);
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); }
472 bool IsRange() { return Config::is_range(this); }
474 return Config::is_class(this)
475 || Config::is_struct(this, StructuralType::kClassTag);
478 return Config::is_struct(this, StructuralType::kConstantTag);
481 return Config::is_struct(this, StructuralType::kContextTag);
484 return Config::is_struct(this, StructuralType::kArrayTag);
487 return Config::is_struct(this, StructuralType::kFunctionTag);
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); }
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.
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();
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));
517 Iterator<i::Object> Constants() {
518 if (this->IsBitset()) return Iterator<i::Object>();
519 return Iterator<i::Object>(Config::handle(this));
522 // Casting and conversion.
524 static inline TypeImpl* cast(typename Config::Base* object);
526 template<class OtherTypeImpl>
527 static TypeHandle Convert(
528 typename OtherTypeImpl::TypeHandle type, Region* region);
532 enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM };
534 void PrintTo(std::ostream& os, PrintDimension dim = BOTH_DIMS); // NOLINT
540 bool IsUnionForTesting() { return IsUnion(); }
545 template<class> friend class Iterator;
546 template<class> friend class TypeImpl;
548 // Handle conversion.
551 static typename Config::template Handle<T>::type handle(T* type) {
552 return Config::handle(type);
554 TypeImpl* unhandle() { return this; }
556 // Internal inspection.
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); }
564 DCHECK(this->IsBitset());
565 return static_cast<BitsetType*>(this)->Bitset();
567 UnionType* AsUnion() { return UnionType::cast(this); }
569 bitset Representation();
571 // Auxiliary functions.
572 bool SemanticMaybe(TypeImpl* that);
574 bitset BitsetGlb() { return BitsetType::Glb(this); }
575 bitset BitsetLub() { return BitsetType::Lub(this); }
577 bool SlowIs(TypeImpl* that);
578 bool SemanticIs(TypeImpl* that);
580 static bool IsInteger(double x) {
581 return nearbyint(x) == x && !i::IsMinusZero(x); // Allows for infinities.
583 static bool IsInteger(i::Object* x) {
584 return x->IsNumber() && IsInteger(x->Number());
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); }
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);
603 static int UpdateRange(
604 RangeHandle type, UnionHandle result, int size, Region* region);
606 static Limits IntersectRangeAndBitset(TypeHandle range, TypeHandle bits,
608 static Limits ToLimits(bitset bits, Region* region);
610 bool SimplyEquals(TypeImpl* that);
611 template<class TypeHandle>
612 bool SimplyEquals(TypeHandle that) { return this->SimplyEquals(*that); }
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,
624 // -----------------------------------------------------------------------------
625 // Bitset types (internal).
627 template<class Config>
628 class TypeImpl<Config>::BitsetType : public TypeImpl<Config> {
630 friend class TypeImpl<Config>;
633 #define DECLARE_TYPE(type, value) k##type = (value),
634 BITSET_TYPE_LIST(DECLARE_TYPE)
639 static bitset SignedSmall();
640 static bitset UnsignedSmall();
642 bitset Bitset() { return Config::as_bitset(this); }
644 static TypeImpl* New(bitset bits) {
645 if (FLAG_enable_slow_asserts) CheckNumberBits(bits);
646 return Config::from_bitset(bits);
648 static TypeHandle New(bitset bits, Region* region) {
649 if (FLAG_enable_slow_asserts) CheckNumberBits(bits);
650 return Config::from_bitset(bits, region);
653 static bool IsInhabited(bitset bits) {
654 return SEMANTIC(bits) != kNone && REPRESENTATION(bits) != kNone;
657 static bool SemanticIsInhabited(bitset bits) {
658 return SEMANTIC(bits) != kNone;
661 static bool Is(bitset bits1, bitset bits2) {
662 return (bits1 | bits2) == bits2;
665 static double Min(bitset);
666 static double Max(bitset);
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);
676 static const char* Name(bitset);
677 static void Print(std::ostream& os, bitset); // NOLINT
679 static void Print(bitset);
682 static bitset NumberBits(bitset bits);
689 static const Boundary BoundariesArray[];
690 static inline const Boundary* Boundaries();
691 static inline size_t BoundariesSize();
693 static void CheckNumberBits(bitset bits);
697 // -----------------------------------------------------------------------------
698 // Superclass for non-bitset types (internal).
699 // Contains a tag and a variable number of type or value fields.
701 template<class Config>
702 class TypeImpl<Config>::StructuralType : public TypeImpl<Config> {
704 template<class> friend class TypeImpl;
705 friend struct ZoneTypeConfig; // For tags.
706 friend struct HeapTypeConfig;
718 return Config::struct_length(Config::as_struct(this));
720 TypeHandle Get(int i) {
721 DCHECK(0 <= i && i < this->Length());
722 return Config::struct_get(Config::as_struct(this), i);
724 void Set(int i, TypeHandle type) {
725 DCHECK(0 <= i && i < this->Length());
726 Config::struct_set(Config::as_struct(this), i, type);
728 void Shrink(int length) {
729 DCHECK(2 <= length && length <= this->Length());
730 Config::struct_shrink(Config::as_struct(this), length);
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);
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);
741 static TypeHandle New(Tag tag, int length, Region* region) {
743 return Config::from_struct(Config::struct_create(tag, length, region));
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 {
758 static UnionHandle New(int length, Region* region) {
759 return Config::template cast<UnionType>(
760 StructuralType::New(StructuralType::kUnionTag, length, region));
763 static UnionType* cast(TypeImpl* type) {
764 DCHECK(type->IsUnion());
765 return static_cast<UnionType*>(type);
772 // -----------------------------------------------------------------------------
775 template<class Config>
776 class TypeImpl<Config>::ClassType : public StructuralType {
778 TypeHandle Bound(Region* region) {
779 return Config::is_class(this) ?
780 BitsetType::New(BitsetType::Lub(*Config::as_class(this)), region) :
783 i::Handle<i::Map> Map() {
784 return Config::is_class(this) ? Config::as_class(this) :
785 this->template GetValue<i::Map>(1);
788 static ClassHandle New(i::Handle<i::Map> map, Region* region) {
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);
800 static ClassType* cast(TypeImpl* type) {
801 DCHECK(type->IsClass());
802 return static_cast<ClassType*>(type);
807 // -----------------------------------------------------------------------------
810 template<class Config>
811 class TypeImpl<Config>::ConstantType : public StructuralType {
813 TypeHandle Bound() { return this->Get(0); }
814 i::Handle<i::Object> Value() { return this->template GetValue<i::Object>(1); }
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);
824 static ConstantType* cast(TypeImpl* type) {
825 DCHECK(type->IsConstant());
826 return static_cast<ConstantType*>(type);
829 // TODO(neis): Also cache value if numerical.
830 // TODO(neis): Allow restricting the representation.
833 // -----------------------------------------------------------------------------
836 template <class Config>
837 class TypeImpl<Config>::RangeType : public TypeImpl<Config> {
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); }
843 static RangeHandle New(double min, double max, TypeHandle representation,
845 DCHECK(IsInteger(min) && IsInteger(max));
847 bitset representation_bits = representation->AsBitset();
848 DCHECK(REPRESENTATION(representation_bits) == representation_bits);
850 typename Config::template Handle<typename Config::Range>::type range =
851 Config::range_create(region);
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));
860 static RangeHandle New(Limits lim, bitset representation, Region* region) {
861 return New(lim.min, lim.max, BitsetType::New(representation, region),
865 static RangeType* cast(TypeImpl* type) {
866 DCHECK(type->IsRange());
867 return static_cast<RangeType*>(type);
870 // TODO(neis): Also cache min and max values.
873 // -----------------------------------------------------------------------------
876 template<class Config>
877 class TypeImpl<Config>::ContextType : public StructuralType {
879 TypeHandle Outer() { return this->Get(0); }
881 static ContextHandle New(TypeHandle outer, Region* region) {
882 ContextHandle type = Config::template cast<ContextType>(
883 StructuralType::New(StructuralType::kContextTag, 1, region));
888 static ContextType* cast(TypeImpl* type) {
889 DCHECK(type->IsContext());
890 return static_cast<ContextType*>(type);
895 // -----------------------------------------------------------------------------
898 template<class Config>
899 class TypeImpl<Config>::ArrayType : public StructuralType {
901 TypeHandle Element() { return this->Get(0); }
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);
910 static ArrayType* cast(TypeImpl* type) {
911 DCHECK(type->IsArray());
912 return static_cast<ArrayType*>(type);
917 // -----------------------------------------------------------------------------
920 template<class Config>
921 class TypeImpl<Config>::FunctionType : public StructuralType {
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); }
928 void InitParameter(int i, TypeHandle type) { this->Set(2 + i, type); }
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);
939 static FunctionType* cast(TypeImpl* type) {
940 DCHECK(type->IsFunction());
941 return static_cast<FunctionType*>(type);
946 // -----------------------------------------------------------------------------
949 template<class Config> template<class T>
950 class TypeImpl<Config>::Iterator {
952 bool Done() const { return index_ < 0; }
953 i::Handle<T> Current();
957 template<class> friend class TypeImpl;
959 Iterator() : index_(-1) {}
960 explicit Iterator(TypeHandle type) : type_(type), index_(-1) {
964 inline bool matches(TypeHandle type);
965 inline TypeHandle get_type();
972 // -----------------------------------------------------------------------------
973 // Zone-allocated types; they are either (odd) integers to represent bitsets, or
974 // (even) pointers to structures for everything else.
976 struct ZoneTypeConfig {
977 typedef TypeImpl<ZoneTypeConfig> Type;
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.
988 typedef i::Zone Region;
989 template<class T> struct Handle { typedef T* type; };
991 static const int kRangeStructTag = 0x1000;
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);
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);
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);
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);
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);
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);
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*);
1031 typedef TypeImpl<ZoneTypeConfig> Type;
1034 // -----------------------------------------------------------------------------
1035 // Heap-allocated types; either smis for bitsets, maps for classes, boxes for
1036 // constants, or fixed arrays for unions.
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; };
1046 static const int kRangeStructTag = 0xffff;
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);
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);
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);
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);
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);
1078 static inline i::Handle<V> struct_get_value(
1079 i::Handle<Struct> structure, int i);
1081 static inline void struct_set_value(
1082 i::Handle<Struct> structure, int i, i::Handle<V> x);
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);
1092 typedef TypeImpl<HeapTypeConfig> HeapType;
1095 // -----------------------------------------------------------------------------
1096 // Type bounds. A simple struct to represent a pair of lower/upper types.
1098 template<class Config>
1100 typedef TypeImpl<Config> Type;
1101 typedef typename Type::TypeHandle TypeHandle;
1102 typedef typename Type::Region Region;
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));
1115 // Unrestricted bounds.
1116 static BoundsImpl Unbounded(Region* region) {
1117 return BoundsImpl(Type::None(region), Type::Any(region));
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);
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);
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);
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);
1150 bool Narrows(BoundsImpl that) {
1151 return that.lower->Is(this->lower) && this->upper->Is(that.upper);
1155 typedef BoundsImpl<ZoneTypeConfig> Bounds;
1157 } } // namespace v8::internal
1159 #endif // V8_TYPES_H_