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/factory.h"
9 #include "src/handles.h"
10 #include "src/ostreams.h"
17 // A simple type system for compiler-internal use. It is based entirely on
18 // union types, and all subtyping hence amounts to set inclusion. Besides the
19 // obvious primitive types and some predefined unions, the type language also
20 // can express class types (a.k.a. specific maps) and singleton types (i.e.,
21 // concrete constants).
23 // Types consist of two dimensions: semantic (value range) and representation.
24 // Both are related through subtyping.
28 // The following equations and inequations hold for the semantic axis:
33 // Number = Signed32 \/ Unsigned32 \/ Double
35 // Name = String \/ Symbol
36 // UniqueName = InternalizedString \/ Symbol
37 // InternalizedString < String
39 // Receiver = Object \/ Proxy
43 // Undetectable < Object
44 // Detectable = Receiver \/ Number \/ Name - Undetectable
46 // Class(map) < T iff instance_type(map) < T
47 // Constant(x) < T iff instance_type(map(x)) < T
49 // Function(R, S, T0, T1, ...) < Function
50 // Context(T) < Internal
52 // Both structural Array and Function types are invariant in all parameters;
53 // relaxing this would make Union and Intersect operations more involved.
54 // There is no subtyping relation between Array, Function, or Context types
55 // and respective Constant types, since these types cannot be reconstructed
56 // for arbitrary heap values.
57 // Note also that Constant(x) < Class(map(x)) does _not_ hold, since x's map can
58 // change! (Its instance type cannot, however.)
59 // TODO(rossberg): the latter is not currently true for proxies, because of fix,
60 // but will hold once we implement direct proxies.
61 // However, we also define a 'temporal' variant of the subtyping relation that
62 // considers the _current_ state only, i.e., Constant(x) <_now Class(map(x)).
64 // REPRESENTATIONAL DIMENSION
66 // For the representation axis, the following holds:
71 // UntaggedInt = UntaggedInt1 \/ UntaggedInt8 \/
72 // UntaggedInt16 \/ UntaggedInt32
73 // UntaggedFloat = UntaggedFloat32 \/ UntaggedFloat64
74 // UntaggedNumber = UntaggedInt \/ UntaggedFloat
75 // Untagged = UntaggedNumber \/ UntaggedPtr
76 // Tagged = TaggedInt \/ TaggedPtr
78 // Subtyping relates the two dimensions, for example:
80 // Number <= Tagged \/ UntaggedNumber
81 // Object <= TaggedPtr \/ UntaggedPtr
83 // That holds because the semantic type constructors defined by the API create
84 // types that allow for all possible representations, and dually, the ones for
85 // representation types initially include all semantic ranges. Representations
86 // can then e.g. be narrowed for a given semantic type using intersection:
88 // SignedSmall /\ TaggedInt (a 'smi')
89 // Number /\ TaggedPtr (a heap number)
93 // There are two main functions for testing types:
95 // T1->Is(T2) -- tests whether T1 is included in T2 (i.e., T1 <= T2)
96 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0)
98 // Typically, the former is to be used to select representations (e.g., via
99 // T->Is(SignedSmall())), and the latter to check whether a specific case needs
100 // handling (e.g., via T->Maybe(Number())).
102 // There is no functionality to discover whether a type is a leaf in the
103 // lattice. That is intentional. It should always be possible to refine the
104 // lattice (e.g., splitting up number types further) without invalidating any
105 // existing assumptions or tests.
106 // Consequently, do not normally use Equals for type tests, always use Is!
108 // The NowIs operator implements state-sensitive subtying, as described above.
109 // Any compilation decision based on such temporary properties requires runtime
114 // Various formal properties hold for constructors, operators, and predicates
115 // over types. For example, constructors are injective, subtyping is a complete
116 // partial order, union and intersection satisfy the usual algebraic properties.
118 // See test/cctest/test-types.cc for a comprehensive executable specification,
119 // especially with respect to the properties of the more exotic 'temporal'
120 // constructors and predicates (those prefixed 'Now').
124 // Internally, all 'primitive' types, and their unions, are represented as
125 // bitsets. Class is a heap pointer to the respective map. Only Constant's, or
126 // unions containing Class'es or Constant's, currently require allocation.
127 // Note that the bitset representation is closed under both Union and Intersect.
129 // There are two type representations, using different allocation:
131 // - class Type (zone-allocated, for compiler and concurrent compilation)
132 // - class HeapType (heap-allocated, for persistent types)
134 // Both provide the same API, and the Convert method can be used to interconvert
135 // them. For zone types, no query method touches the heap, only constructors do.
138 // -----------------------------------------------------------------------------
139 // Values for bitset types
141 #define MASK_BITSET_TYPE_LIST(V) \
142 V(Representation, static_cast<int>(0xffc00000)) \
143 V(Semantic, static_cast<int>(0x003fffff))
145 #define REPRESENTATION(k) ((k) & BitsetType::kRepresentation)
146 #define SEMANTIC(k) ((k) & BitsetType::kSemantic)
148 #define REPRESENTATION_BITSET_TYPE_LIST(V) \
150 V(UntaggedInt1, 1 << 22 | kSemantic) \
151 V(UntaggedInt8, 1 << 23 | kSemantic) \
152 V(UntaggedInt16, 1 << 24 | kSemantic) \
153 V(UntaggedInt32, 1 << 25 | kSemantic) \
154 V(UntaggedFloat32, 1 << 26 | kSemantic) \
155 V(UntaggedFloat64, 1 << 27 | kSemantic) \
156 V(UntaggedPtr, 1 << 28 | kSemantic) \
157 V(TaggedInt, 1 << 29 | kSemantic) \
158 /* MSB has to be sign-extended */ \
159 V(TaggedPtr, static_cast<int>(~0u << 30) | kSemantic) \
161 V(UntaggedInt, kUntaggedInt1 | kUntaggedInt8 | \
162 kUntaggedInt16 | kUntaggedInt32) \
163 V(UntaggedFloat, kUntaggedFloat32 | kUntaggedFloat64) \
164 V(UntaggedNumber, kUntaggedInt | kUntaggedFloat) \
165 V(Untagged, kUntaggedNumber | kUntaggedPtr) \
166 V(Tagged, kTaggedInt | kTaggedPtr)
168 #define SEMANTIC_BITSET_TYPE_LIST(V) \
169 V(Null, 1 << 0 | REPRESENTATION(kTaggedPtr)) \
170 V(Undefined, 1 << 1 | REPRESENTATION(kTaggedPtr)) \
171 V(Boolean, 1 << 2 | REPRESENTATION(kTaggedPtr)) \
172 V(UnsignedSmall, 1 << 3 | REPRESENTATION(kTagged | kUntaggedNumber)) \
173 V(OtherSignedSmall, 1 << 4 | REPRESENTATION(kTagged | kUntaggedNumber)) \
174 V(OtherUnsigned31, 1 << 5 | REPRESENTATION(kTagged | kUntaggedNumber)) \
175 V(OtherUnsigned32, 1 << 6 | REPRESENTATION(kTagged | kUntaggedNumber)) \
176 V(OtherSigned32, 1 << 7 | REPRESENTATION(kTagged | kUntaggedNumber)) \
177 V(MinusZero, 1 << 8 | REPRESENTATION(kTagged | kUntaggedNumber)) \
178 V(NaN, 1 << 9 | REPRESENTATION(kTagged | kUntaggedNumber)) \
179 V(OtherNumber, 1 << 10 | REPRESENTATION(kTagged | kUntaggedNumber)) \
180 V(Symbol, 1 << 11 | REPRESENTATION(kTaggedPtr)) \
181 V(InternalizedString, 1 << 12 | REPRESENTATION(kTaggedPtr)) \
182 V(OtherString, 1 << 13 | REPRESENTATION(kTaggedPtr)) \
183 V(Undetectable, 1 << 14 | REPRESENTATION(kTaggedPtr)) \
184 V(Array, 1 << 15 | REPRESENTATION(kTaggedPtr)) \
185 V(Buffer, 1 << 16 | REPRESENTATION(kTaggedPtr)) \
186 V(Function, 1 << 17 | REPRESENTATION(kTaggedPtr)) \
187 V(RegExp, 1 << 18 | REPRESENTATION(kTaggedPtr)) \
188 V(OtherObject, 1 << 19 | REPRESENTATION(kTaggedPtr)) \
189 V(Proxy, 1 << 20 | REPRESENTATION(kTaggedPtr)) \
190 V(Internal, 1 << 21 | REPRESENTATION(kTagged | kUntagged)) \
192 V(SignedSmall, kUnsignedSmall | kOtherSignedSmall) \
193 V(Signed32, kSignedSmall | kOtherUnsigned31 | kOtherSigned32) \
194 V(Unsigned32, kUnsignedSmall | kOtherUnsigned31 | kOtherUnsigned32) \
195 V(Integral32, kSigned32 | kUnsigned32) \
196 V(Number, kIntegral32 | kMinusZero | kNaN | kOtherNumber) \
197 V(String, kInternalizedString | kOtherString) \
198 V(UniqueName, kSymbol | kInternalizedString) \
199 V(Name, kSymbol | kString) \
200 V(NumberOrString, kNumber | kString) \
201 V(Primitive, kNumber | kName | kBoolean | kNull | kUndefined) \
202 V(DetectableObject, kArray | kFunction | kRegExp | kOtherObject) \
203 V(DetectableReceiver, kDetectableObject | kProxy) \
204 V(Detectable, kDetectableReceiver | kNumber | kName) \
205 V(Object, kDetectableObject | kUndetectable) \
206 V(Receiver, kObject | kProxy) \
207 V(NonNumber, kBoolean | kName | kNull | kReceiver | \
208 kUndefined | kInternal) \
211 #define BITSET_TYPE_LIST(V) \
212 MASK_BITSET_TYPE_LIST(V) \
213 REPRESENTATION_BITSET_TYPE_LIST(V) \
214 SEMANTIC_BITSET_TYPE_LIST(V)
217 // -----------------------------------------------------------------------------
218 // The abstract Type class, parameterized over the low-level representation.
221 // typedef TypeImpl<Config> Type;
225 // template<class> struct Handle { typedef type; } // No template typedefs...
226 // template<class T> static Handle<T>::type handle(T* t); // !is_bitset(t)
227 // template<class T> static Handle<T>::type cast(Handle<Type>::type);
228 // static bool is_bitset(Type*);
229 // static bool is_class(Type*);
230 // static bool is_struct(Type*, int tag);
231 // static int as_bitset(Type*);
232 // static i::Handle<i::Map> as_class(Type*);
233 // static Handle<Struct>::type as_struct(Type*);
234 // static Type* from_bitset(int bitset);
235 // static Handle<Type>::type from_bitset(int bitset, Region*);
236 // static Handle<Type>::type from_class(i::Handle<Map>, Region*);
237 // static Handle<Type>::type from_struct(Handle<Struct>::type, int tag);
238 // static Handle<Struct>::type struct_create(int tag, int length, Region*);
239 // static void struct_shrink(Handle<Struct>::type, int length);
240 // static int struct_tag(Handle<Struct>::type);
241 // static int struct_length(Handle<Struct>::type);
242 // static Handle<Type>::type struct_get(Handle<Struct>::type, int);
243 // static void struct_set(Handle<Struct>::type, int, Handle<Type>::type);
245 // static i::Handle<V> struct_get_value(Handle<Struct>::type, int);
247 // static void struct_set_value(Handle<Struct>::type, int, i::Handle<V>);
249 template<class Config>
250 class TypeImpl : public Config::Base {
254 class BitsetType; // Internal
255 class StructuralType; // Internal
256 class UnionType; // Internal
265 typedef typename Config::template Handle<TypeImpl>::type TypeHandle;
266 typedef typename Config::template Handle<ClassType>::type ClassHandle;
267 typedef typename Config::template Handle<ConstantType>::type ConstantHandle;
268 typedef typename Config::template Handle<RangeType>::type RangeHandle;
269 typedef typename Config::template Handle<ContextType>::type ContextHandle;
270 typedef typename Config::template Handle<ArrayType>::type ArrayHandle;
271 typedef typename Config::template Handle<FunctionType>::type FunctionHandle;
272 typedef typename Config::template Handle<UnionType>::type UnionHandle;
273 typedef typename Config::Region Region;
277 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \
278 static TypeImpl* type() { return BitsetType::New(BitsetType::k##type); } \
279 static TypeHandle type(Region* region) { \
280 return BitsetType::New(BitsetType::k##type, region); \
282 BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR)
283 #undef DEFINE_TYPE_CONSTRUCTOR
285 static TypeHandle Class(i::Handle<i::Map> map, Region* region) {
286 return ClassType::New(map, region);
288 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) {
289 // TODO(neis): Return RangeType for numerical values.
290 return ConstantType::New(value, region);
292 static TypeHandle Range(double min, double max, Region* region) {
293 return RangeType::New(min, max, region);
295 static TypeHandle Context(TypeHandle outer, Region* region) {
296 return ContextType::New(outer, region);
298 static TypeHandle Array(TypeHandle element, Region* region) {
299 return ArrayType::New(element, region);
301 static FunctionHandle Function(
302 TypeHandle result, TypeHandle receiver, int arity, Region* region) {
303 return FunctionType::New(result, receiver, arity, region);
305 static TypeHandle Function(TypeHandle result, Region* region) {
306 return Function(result, Any(region), 0, region);
308 static TypeHandle Function(
309 TypeHandle result, TypeHandle param0, Region* region) {
310 FunctionHandle function = Function(result, Any(region), 1, region);
311 function->InitParameter(0, param0);
314 static TypeHandle Function(
315 TypeHandle result, TypeHandle param0, TypeHandle param1, Region* region) {
316 FunctionHandle function = Function(result, Any(region), 2, region);
317 function->InitParameter(0, param0);
318 function->InitParameter(1, param1);
321 static TypeHandle Function(
322 TypeHandle result, TypeHandle param0, TypeHandle param1,
323 TypeHandle param2, Region* region) {
324 FunctionHandle function = Function(result, Any(region), 3, region);
325 function->InitParameter(0, param0);
326 function->InitParameter(1, param1);
327 function->InitParameter(2, param2);
331 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg);
332 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg);
334 static TypeHandle Of(double value, Region* region) {
335 return Config::from_bitset(BitsetType::Lub(value), region);
337 static TypeHandle Of(i::Object* value, Region* region) {
338 return Config::from_bitset(BitsetType::Lub(value), region);
340 static TypeHandle Of(i::Handle<i::Object> value, Region* region) {
341 return Of(*value, region);
346 bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); }
348 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); }
349 template<class TypeHandle>
350 bool Is(TypeHandle that) { return this->Is(*that); }
352 bool Maybe(TypeImpl* that);
353 template<class TypeHandle>
354 bool Maybe(TypeHandle that) { return this->Maybe(*that); }
356 bool Equals(TypeImpl* that) { return this->Is(that) && that->Is(this); }
357 template<class TypeHandle>
358 bool Equals(TypeHandle that) { return this->Equals(*that); }
360 // Equivalent to Constant(value)->Is(this), but avoiding allocation.
361 bool Contains(i::Object* val);
362 bool Contains(i::Handle<i::Object> val) { return this->Contains(*val); }
364 // State-dependent versions of the above that consider subtyping between
365 // a constant and its map class.
366 inline static TypeHandle NowOf(i::Object* value, Region* region);
367 static TypeHandle NowOf(i::Handle<i::Object> value, Region* region) {
368 return NowOf(*value, region);
370 bool NowIs(TypeImpl* that);
371 template<class TypeHandle>
372 bool NowIs(TypeHandle that) { return this->NowIs(*that); }
373 inline bool NowContains(i::Object* val);
374 bool NowContains(i::Handle<i::Object> val) { return this->NowContains(*val); }
381 return Config::is_class(this)
382 || Config::is_struct(this, StructuralType::kClassTag);
385 return Config::is_struct(this, StructuralType::kConstantTag);
388 return Config::is_struct(this, StructuralType::kRangeTag);
391 return Config::is_struct(this, StructuralType::kContextTag);
394 return Config::is_struct(this, StructuralType::kArrayTag);
397 return Config::is_struct(this, StructuralType::kFunctionTag);
400 ClassType* AsClass() { return ClassType::cast(this); }
401 ConstantType* AsConstant() { return ConstantType::cast(this); }
402 RangeType* AsRange() { return RangeType::cast(this); }
403 ContextType* AsContext() { return ContextType::cast(this); }
404 ArrayType* AsArray() { return ArrayType::cast(this); }
405 FunctionType* AsFunction() { return FunctionType::cast(this); }
410 template<class T> class Iterator;
411 Iterator<i::Map> Classes() {
412 if (this->IsBitset()) return Iterator<i::Map>();
413 return Iterator<i::Map>(Config::handle(this));
415 Iterator<i::Object> Constants() {
416 if (this->IsBitset()) return Iterator<i::Object>();
417 return Iterator<i::Object>(Config::handle(this));
420 // Casting and conversion.
422 static inline TypeImpl* cast(typename Config::Base* object);
424 template<class OtherTypeImpl>
425 static TypeHandle Convert(
426 typename OtherTypeImpl::TypeHandle type, Region* region);
430 enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM };
432 void PrintTo(OStream& os, PrintDimension dim = BOTH_DIMS); // NOLINT
441 template<class> friend class Iterator;
442 template<class> friend class TypeImpl;
444 // Handle conversion.
447 static typename Config::template Handle<T>::type handle(T* type) {
448 return Config::handle(type);
450 TypeImpl* unhandle() { return this; }
452 // Internal inspection.
454 bool IsNone() { return this == None(); }
455 bool IsAny() { return this == Any(); }
456 bool IsBitset() { return Config::is_bitset(this); }
457 bool IsUnion() { return Config::is_struct(this, StructuralType::kUnionTag); }
460 DCHECK(this->IsBitset());
461 return static_cast<BitsetType*>(this)->Bitset();
463 UnionType* AsUnion() { return UnionType::cast(this); }
465 // Auxiliary functions.
467 int BitsetGlb() { return BitsetType::Glb(this); }
468 int BitsetLub() { return BitsetType::Lub(this); }
469 int InherentBitsetLub() { return BitsetType::InherentLub(this); }
471 bool SlowIs(TypeImpl* that);
473 TypeHandle Rebound(int bitset, Region* region);
474 int BoundBy(TypeImpl* that);
475 int IndexInUnion(int bound, UnionHandle unioned, int current_size);
476 static int ExtendUnion(
477 UnionHandle unioned, int current_size, TypeHandle t,
478 TypeHandle other, bool is_intersect, Region* region);
482 // -----------------------------------------------------------------------------
483 // Bitset types (internal).
485 template<class Config>
486 class TypeImpl<Config>::BitsetType : public TypeImpl<Config> {
488 friend class TypeImpl<Config>;
491 #define DECLARE_TYPE(type, value) k##type = (value),
492 BITSET_TYPE_LIST(DECLARE_TYPE)
497 int Bitset() { return Config::as_bitset(this); }
499 static TypeImpl* New(int bitset) {
500 return static_cast<BitsetType*>(Config::from_bitset(bitset));
502 static TypeHandle New(int bitset, Region* region) {
503 return Config::from_bitset(bitset, region);
506 static bool IsInhabited(int bitset) {
507 return (bitset & kRepresentation) && (bitset & kSemantic);
510 static bool Is(int bitset1, int bitset2) {
511 return (bitset1 | bitset2) == bitset2;
514 static int Glb(TypeImpl* type); // greatest lower bound that's a bitset
515 static int Lub(TypeImpl* type); // least upper bound that's a bitset
516 static int Lub(i::Object* value);
517 static int Lub(double value);
518 static int Lub(int32_t value);
519 static int Lub(uint32_t value);
520 static int Lub(i::Map* map);
521 static int Lub(double min, double max);
522 static int InherentLub(TypeImpl* type);
524 static const char* Name(int bitset);
525 static void Print(OStream& os, int bitset); // NOLINT
526 using TypeImpl::PrintTo;
530 // -----------------------------------------------------------------------------
531 // Superclass for non-bitset types (internal).
532 // Contains a tag and a variable number of type or value fields.
534 template<class Config>
535 class TypeImpl<Config>::StructuralType : public TypeImpl<Config> {
537 template<class> friend class TypeImpl;
538 friend struct ZoneTypeConfig; // For tags.
539 friend struct HeapTypeConfig;
552 return Config::struct_length(Config::as_struct(this));
554 TypeHandle Get(int i) {
555 DCHECK(0 <= i && i < this->Length());
556 return Config::struct_get(Config::as_struct(this), i);
558 void Set(int i, TypeHandle type) {
559 DCHECK(0 <= i && i < this->Length());
560 Config::struct_set(Config::as_struct(this), i, type);
562 void Shrink(int length) {
563 DCHECK(2 <= length && length <= this->Length());
564 Config::struct_shrink(Config::as_struct(this), length);
566 template<class V> i::Handle<V> GetValue(int i) {
567 DCHECK(0 <= i && i < this->Length());
568 return Config::template struct_get_value<V>(Config::as_struct(this), i);
570 template<class V> void SetValue(int i, i::Handle<V> x) {
571 DCHECK(0 <= i && i < this->Length());
572 Config::struct_set_value(Config::as_struct(this), i, x);
575 static TypeHandle New(Tag tag, int length, Region* region) {
577 return Config::from_struct(Config::struct_create(tag, length, region));
582 // -----------------------------------------------------------------------------
583 // Union types (internal).
584 // A union is a structured type with the following invariants:
585 // - its length is at least 2
586 // - at most one field is a bitset, and it must go into index 0
587 // - no field is a union
588 // - no field is a subtype of any other field
589 template<class Config>
590 class TypeImpl<Config>::UnionType : public StructuralType {
592 static UnionHandle New(int length, Region* region) {
593 return Config::template cast<UnionType>(
594 StructuralType::New(StructuralType::kUnionTag, length, region));
597 static UnionType* cast(TypeImpl* type) {
598 DCHECK(type->IsUnion());
599 return static_cast<UnionType*>(type);
606 // -----------------------------------------------------------------------------
609 template<class Config>
610 class TypeImpl<Config>::ClassType : public StructuralType {
612 TypeHandle Bound(Region* region) {
613 return Config::is_class(this)
614 ? BitsetType::New(BitsetType::Lub(*Config::as_class(this)), region)
617 i::Handle<i::Map> Map() {
618 return Config::is_class(this)
619 ? Config::as_class(this)
620 : this->template GetValue<i::Map>(1);
623 static ClassHandle New(
624 i::Handle<i::Map> map, TypeHandle bound, Region* region) {
625 DCHECK(BitsetType::Is(bound->AsBitset(), BitsetType::Lub(*map)));
626 ClassHandle type = Config::template cast<ClassType>(
627 StructuralType::New(StructuralType::kClassTag, 2, region));
629 type->SetValue(1, map);
633 static ClassHandle New(i::Handle<i::Map> map, Region* region) {
635 Config::template cast<ClassType>(Config::from_class(map, region));
636 if (type->IsClass()) {
639 TypeHandle bound = BitsetType::New(BitsetType::Lub(*map), region);
640 return New(map, bound, region);
644 static ClassType* cast(TypeImpl* type) {
645 DCHECK(type->IsClass());
646 return static_cast<ClassType*>(type);
651 // -----------------------------------------------------------------------------
654 template<class Config>
655 class TypeImpl<Config>::ConstantType : public StructuralType {
657 TypeHandle Bound() { return this->Get(0); }
658 i::Handle<i::Object> Value() { return this->template GetValue<i::Object>(1); }
660 static ConstantHandle New(
661 i::Handle<i::Object> value, TypeHandle bound, Region* region) {
662 DCHECK(BitsetType::Is(bound->AsBitset(), BitsetType::Lub(*value)));
663 ConstantHandle type = Config::template cast<ConstantType>(
664 StructuralType::New(StructuralType::kConstantTag, 2, region));
666 type->SetValue(1, value);
670 static ConstantHandle New(i::Handle<i::Object> value, Region* region) {
671 TypeHandle bound = BitsetType::New(BitsetType::Lub(*value), region);
672 return New(value, bound, region);
675 static ConstantType* cast(TypeImpl* type) {
676 DCHECK(type->IsConstant());
677 return static_cast<ConstantType*>(type);
682 // -----------------------------------------------------------------------------
685 template<class Config>
686 class TypeImpl<Config>::RangeType : public StructuralType {
688 TypeHandle Bound() { return this->Get(0); }
689 double Min() { return this->template GetValue<i::HeapNumber>(1)->value(); }
690 double Max() { return this->template GetValue<i::HeapNumber>(2)->value(); }
692 static RangeHandle New(
693 double min, double max, TypeHandle bound, Region* region) {
694 DCHECK(BitsetType::Is(bound->AsBitset(), BitsetType::Lub(min, max)));
695 RangeHandle type = Config::template cast<RangeType>(
696 StructuralType::New(StructuralType::kRangeTag, 3, region));
698 Factory* factory = Config::isolate(region)->factory();
699 Handle<HeapNumber> minV = factory->NewHeapNumber(min);
700 Handle<HeapNumber> maxV = factory->NewHeapNumber(max);
701 type->SetValue(1, minV);
702 type->SetValue(2, maxV);
706 static RangeHandle New(double min, double max, Region* region) {
707 TypeHandle bound = BitsetType::New(BitsetType::Lub(min, max), region);
708 return New(min, max, bound, region);
711 static RangeType* cast(TypeImpl* type) {
712 DCHECK(type->IsRange());
713 return static_cast<RangeType*>(type);
718 // -----------------------------------------------------------------------------
721 template<class Config>
722 class TypeImpl<Config>::ContextType : public StructuralType {
724 TypeHandle Bound() { return this->Get(0); }
725 TypeHandle Outer() { return this->Get(1); }
727 static ContextHandle New(TypeHandle outer, TypeHandle bound, Region* region) {
728 DCHECK(BitsetType::Is(
729 bound->AsBitset(), BitsetType::kInternal & BitsetType::kTaggedPtr));
730 ContextHandle type = Config::template cast<ContextType>(
731 StructuralType::New(StructuralType::kContextTag, 2, region));
737 static ContextHandle New(TypeHandle outer, Region* region) {
738 TypeHandle bound = BitsetType::New(
739 BitsetType::kInternal & BitsetType::kTaggedPtr, region);
740 return New(outer, bound, region);
743 static ContextType* cast(TypeImpl* type) {
744 DCHECK(type->IsContext());
745 return static_cast<ContextType*>(type);
750 // -----------------------------------------------------------------------------
753 template<class Config>
754 class TypeImpl<Config>::ArrayType : public StructuralType {
756 TypeHandle Bound() { return this->Get(0); }
757 TypeHandle Element() { return this->Get(1); }
759 static ArrayHandle New(TypeHandle element, TypeHandle bound, Region* region) {
760 DCHECK(BitsetType::Is(bound->AsBitset(), BitsetType::kArray));
761 ArrayHandle type = Config::template cast<ArrayType>(
762 StructuralType::New(StructuralType::kArrayTag, 2, region));
764 type->Set(1, element);
768 static ArrayHandle New(TypeHandle element, Region* region) {
769 TypeHandle bound = BitsetType::New(BitsetType::kArray, region);
770 return New(element, bound, region);
773 static ArrayType* cast(TypeImpl* type) {
774 DCHECK(type->IsArray());
775 return static_cast<ArrayType*>(type);
780 // -----------------------------------------------------------------------------
783 template<class Config>
784 class TypeImpl<Config>::FunctionType : public StructuralType {
786 int Arity() { return this->Length() - 3; }
787 TypeHandle Bound() { return this->Get(0); }
788 TypeHandle Result() { return this->Get(1); }
789 TypeHandle Receiver() { return this->Get(2); }
790 TypeHandle Parameter(int i) { return this->Get(3 + i); }
792 void InitParameter(int i, TypeHandle type) { this->Set(3 + i, type); }
794 static FunctionHandle New(
795 TypeHandle result, TypeHandle receiver, TypeHandle bound,
796 int arity, Region* region) {
797 DCHECK(BitsetType::Is(bound->AsBitset(), BitsetType::kFunction));
798 FunctionHandle type = Config::template cast<FunctionType>(
799 StructuralType::New(StructuralType::kFunctionTag, 3 + arity, region));
801 type->Set(1, result);
802 type->Set(2, receiver);
806 static FunctionHandle New(
807 TypeHandle result, TypeHandle receiver, int arity, Region* region) {
808 TypeHandle bound = BitsetType::New(BitsetType::kFunction, region);
809 return New(result, receiver, bound, arity, region);
812 static FunctionType* cast(TypeImpl* type) {
813 DCHECK(type->IsFunction());
814 return static_cast<FunctionType*>(type);
819 // -----------------------------------------------------------------------------
822 template<class Config> template<class T>
823 class TypeImpl<Config>::Iterator {
825 bool Done() const { return index_ < 0; }
826 i::Handle<T> Current();
830 template<class> friend class TypeImpl;
832 Iterator() : index_(-1) {}
833 explicit Iterator(TypeHandle type) : type_(type), index_(-1) {
837 inline bool matches(TypeHandle type);
838 inline TypeHandle get_type();
845 // -----------------------------------------------------------------------------
846 // Zone-allocated types; they are either (odd) integers to represent bitsets, or
847 // (even) pointers to structures for everything else.
849 struct ZoneTypeConfig {
850 typedef TypeImpl<ZoneTypeConfig> Type;
852 typedef void* Struct;
853 typedef i::Zone Region;
854 template<class T> struct Handle { typedef T* type; };
856 // TODO(neis): This will be removed again once we have struct_get_double().
857 static inline i::Isolate* isolate(Region* region) {
858 return region->isolate();
861 template<class T> static inline T* handle(T* type);
862 template<class T> static inline T* cast(Type* type);
864 static inline bool is_bitset(Type* type);
865 static inline bool is_class(Type* type);
866 static inline bool is_struct(Type* type, int tag);
868 static inline int as_bitset(Type* type);
869 static inline i::Handle<i::Map> as_class(Type* type);
870 static inline Struct* as_struct(Type* type);
872 static inline Type* from_bitset(int bitset);
873 static inline Type* from_bitset(int bitset, Zone* zone);
874 static inline Type* from_class(i::Handle<i::Map> map, Zone* zone);
875 static inline Type* from_struct(Struct* structured);
877 static inline Struct* struct_create(int tag, int length, Zone* zone);
878 static inline void struct_shrink(Struct* structure, int length);
879 static inline int struct_tag(Struct* structure);
880 static inline int struct_length(Struct* structure);
881 static inline Type* struct_get(Struct* structure, int i);
882 static inline void struct_set(Struct* structure, int i, Type* type);
884 static inline i::Handle<V> struct_get_value(Struct* structure, int i);
885 template<class V> static inline void struct_set_value(
886 Struct* structure, int i, i::Handle<V> x);
889 typedef TypeImpl<ZoneTypeConfig> Type;
892 // -----------------------------------------------------------------------------
893 // Heap-allocated types; either smis for bitsets, maps for classes, boxes for
894 // constants, or fixed arrays for unions.
896 struct HeapTypeConfig {
897 typedef TypeImpl<HeapTypeConfig> Type;
898 typedef i::Object Base;
899 typedef i::FixedArray Struct;
900 typedef i::Isolate Region;
901 template<class T> struct Handle { typedef i::Handle<T> type; };
903 // TODO(neis): This will be removed again once we have struct_get_double().
904 static inline i::Isolate* isolate(Region* region) {
908 template<class T> static inline i::Handle<T> handle(T* type);
909 template<class T> static inline i::Handle<T> cast(i::Handle<Type> type);
911 static inline bool is_bitset(Type* type);
912 static inline bool is_class(Type* type);
913 static inline bool is_struct(Type* type, int tag);
915 static inline int as_bitset(Type* type);
916 static inline i::Handle<i::Map> as_class(Type* type);
917 static inline i::Handle<Struct> as_struct(Type* type);
919 static inline Type* from_bitset(int bitset);
920 static inline i::Handle<Type> from_bitset(int bitset, Isolate* isolate);
921 static inline i::Handle<Type> from_class(
922 i::Handle<i::Map> map, Isolate* isolate);
923 static inline i::Handle<Type> from_struct(i::Handle<Struct> structure);
925 static inline i::Handle<Struct> struct_create(
926 int tag, int length, Isolate* isolate);
927 static inline void struct_shrink(i::Handle<Struct> structure, int length);
928 static inline int struct_tag(i::Handle<Struct> structure);
929 static inline int struct_length(i::Handle<Struct> structure);
930 static inline i::Handle<Type> struct_get(i::Handle<Struct> structure, int i);
931 static inline void struct_set(
932 i::Handle<Struct> structure, int i, i::Handle<Type> type);
934 static inline i::Handle<V> struct_get_value(
935 i::Handle<Struct> structure, int i);
937 static inline void struct_set_value(
938 i::Handle<Struct> structure, int i, i::Handle<V> x);
941 typedef TypeImpl<HeapTypeConfig> HeapType;
944 // -----------------------------------------------------------------------------
945 // Type bounds. A simple struct to represent a pair of lower/upper types.
947 template<class Config>
949 typedef TypeImpl<Config> Type;
950 typedef typename Type::TypeHandle TypeHandle;
951 typedef typename Type::Region Region;
957 explicit BoundsImpl(TypeHandle t) : lower(t), upper(t) {}
958 BoundsImpl(TypeHandle l, TypeHandle u) : lower(l), upper(u) {
959 DCHECK(lower->Is(upper));
962 // Unrestricted bounds.
963 static BoundsImpl Unbounded(Region* region) {
964 return BoundsImpl(Type::None(region), Type::Any(region));
967 // Meet: both b1 and b2 are known to hold.
968 static BoundsImpl Both(BoundsImpl b1, BoundsImpl b2, Region* region) {
969 TypeHandle lower = Type::Union(b1.lower, b2.lower, region);
970 TypeHandle upper = Type::Intersect(b1.upper, b2.upper, region);
971 // Lower bounds are considered approximate, correct as necessary.
972 lower = Type::Intersect(lower, upper, region);
973 return BoundsImpl(lower, upper);
976 // Join: either b1 or b2 is known to hold.
977 static BoundsImpl Either(BoundsImpl b1, BoundsImpl b2, Region* region) {
978 TypeHandle lower = Type::Intersect(b1.lower, b2.lower, region);
979 TypeHandle upper = Type::Union(b1.upper, b2.upper, region);
980 return BoundsImpl(lower, upper);
983 static BoundsImpl NarrowLower(BoundsImpl b, TypeHandle t, Region* region) {
984 // Lower bounds are considered approximate, correct as necessary.
985 t = Type::Intersect(t, b.upper, region);
986 TypeHandle lower = Type::Union(b.lower, t, region);
987 return BoundsImpl(lower, b.upper);
989 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) {
990 TypeHandle lower = Type::Intersect(b.lower, t, region);
991 TypeHandle upper = Type::Intersect(b.upper, t, region);
992 return BoundsImpl(lower, upper);
995 bool Narrows(BoundsImpl that) {
996 return that.lower->Is(this->lower) && this->upper->Is(that.upper);
1000 typedef BoundsImpl<ZoneTypeConfig> Bounds;
1002 } } // namespace v8::internal
1004 #endif // V8_TYPES_H_