blob: 4b7d8ba9795b9919a6733a299a82d66cb6a11165 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
13namespace v8 {
14namespace 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#define MASK_BITSET_TYPE_LIST(V) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400157 V(Representation, 0xfff00000u) \
158 V(Semantic, 0x000ffffeu)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159
160#define REPRESENTATION(k) ((k) & BitsetType::kRepresentation)
161#define SEMANTIC(k) ((k) & BitsetType::kSemantic)
162
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163#define REPRESENTATION_BITSET_TYPE_LIST(V) \
164 V(None, 0) \
165 V(UntaggedBit, 1u << 20 | kSemantic) \
166 V(UntaggedSigned8, 1u << 21 | kSemantic) \
167 V(UntaggedSigned16, 1u << 22 | kSemantic) \
168 V(UntaggedSigned32, 1u << 23 | kSemantic) \
169 V(UntaggedUnsigned8, 1u << 24 | kSemantic) \
170 V(UntaggedUnsigned16, 1u << 25 | kSemantic) \
171 V(UntaggedUnsigned32, 1u << 26 | kSemantic) \
172 V(UntaggedFloat32, 1u << 27 | kSemantic) \
173 V(UntaggedFloat64, 1u << 28 | kSemantic) \
174 V(UntaggedPointer, 1u << 29 | kSemantic) \
175 V(TaggedSigned, 1u << 30 | kSemantic) \
176 V(TaggedPointer, 1u << 31 | kSemantic) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400178 V(UntaggedSigned, kUntaggedSigned8 | kUntaggedSigned16 | \
179 kUntaggedSigned32) \
180 V(UntaggedUnsigned, kUntaggedUnsigned8 | kUntaggedUnsigned16 | \
181 kUntaggedUnsigned32) \
182 V(UntaggedIntegral8, kUntaggedSigned8 | kUntaggedUnsigned8) \
183 V(UntaggedIntegral16, kUntaggedSigned16 | kUntaggedUnsigned16) \
184 V(UntaggedIntegral32, kUntaggedSigned32 | kUntaggedUnsigned32) \
185 V(UntaggedIntegral, kUntaggedBit | kUntaggedSigned | kUntaggedUnsigned) \
186 V(UntaggedFloat, kUntaggedFloat32 | kUntaggedFloat64) \
187 V(UntaggedNumber, kUntaggedIntegral | kUntaggedFloat) \
188 V(Untagged, kUntaggedNumber | kUntaggedPointer) \
189 V(Tagged, kTaggedSigned | kTaggedPointer)
190
191#define INTERNAL_BITSET_TYPE_LIST(V) \
192 V(OtherUnsigned31, 1u << 1 | REPRESENTATION(kTagged | kUntaggedNumber)) \
193 V(OtherUnsigned32, 1u << 2 | REPRESENTATION(kTagged | kUntaggedNumber)) \
194 V(OtherSigned32, 1u << 3 | REPRESENTATION(kTagged | kUntaggedNumber)) \
195 V(OtherNumber, 1u << 4 | REPRESENTATION(kTagged | kUntaggedNumber))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196
197#define SEMANTIC_BITSET_TYPE_LIST(V) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400198 V(NegativeSignedSmall, 1u << 5 | REPRESENTATION(kTagged | kUntaggedNumber)) \
199 V(Null, 1u << 6 | REPRESENTATION(kTaggedPointer)) \
200 V(Undefined, 1u << 7 | REPRESENTATION(kTaggedPointer)) \
201 V(Boolean, 1u << 8 | REPRESENTATION(kTaggedPointer)) \
202 V(UnsignedSmall, 1u << 9 | REPRESENTATION(kTagged | kUntaggedNumber)) \
203 V(MinusZero, 1u << 10 | REPRESENTATION(kTagged | kUntaggedNumber)) \
204 V(NaN, 1u << 11 | REPRESENTATION(kTagged | kUntaggedNumber)) \
205 V(Symbol, 1u << 12 | REPRESENTATION(kTaggedPointer)) \
206 V(InternalizedString, 1u << 13 | REPRESENTATION(kTaggedPointer)) \
207 V(OtherString, 1u << 14 | REPRESENTATION(kTaggedPointer)) \
208 V(Undetectable, 1u << 15 | REPRESENTATION(kTaggedPointer)) \
209 V(Array, 1u << 16 | REPRESENTATION(kTaggedPointer)) \
210 V(OtherObject, 1u << 17 | REPRESENTATION(kTaggedPointer)) \
211 V(Proxy, 1u << 18 | REPRESENTATION(kTaggedPointer)) \
212 V(Internal, 1u << 19 | REPRESENTATION(kTagged | kUntagged)) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400214 V(SignedSmall, kUnsignedSmall | kNegativeSignedSmall) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 V(Signed32, kSignedSmall | kOtherUnsigned31 | kOtherSigned32) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400216 V(NegativeSigned32, kNegativeSignedSmall | kOtherSigned32) \
217 V(NonNegativeSigned32, kUnsignedSmall | kOtherUnsigned31) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 V(Unsigned32, kUnsignedSmall | kOtherUnsigned31 | kOtherUnsigned32) \
219 V(Integral32, kSigned32 | kUnsigned32) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400220 V(PlainNumber, kIntegral32 | kOtherNumber) \
221 V(OrderedNumber, kPlainNumber | kMinusZero) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 V(Number, kOrderedNumber | kNaN) \
223 V(String, kInternalizedString | kOtherString) \
224 V(UniqueName, kSymbol | kInternalizedString) \
225 V(Name, kSymbol | kString) \
226 V(NumberOrString, kNumber | kString) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400227 V(PlainPrimitive, kNumberOrString | kBoolean | kNull | kUndefined) \
228 V(Primitive, kSymbol | kPlainPrimitive) \
229 V(DetectableObject, kArray | kOtherObject) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 V(DetectableReceiver, kDetectableObject | kProxy) \
231 V(Detectable, kDetectableReceiver | kNumber | kName) \
232 V(Object, kDetectableObject | kUndetectable) \
233 V(Receiver, kObject | kProxy) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234 V(StringOrReceiver, kString | kReceiver) \
235 V(Unique, kBoolean | kUniqueName | kNull | kUndefined | \
236 kReceiver) \
237 V(NonNumber, kUnique | kString | kInternal) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 V(Any, 0xfffffffeu)
239
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400240
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241/*
242 * The following diagrams show how integers (in the mathematical sense) are
243 * divided among the different atomic numerical types.
244 *
245 * If SmiValuesAre31Bits():
246 *
247 * ON OS32 OSS US OU31 OU32 ON
248 * ______[_______[_______[_______[_______[_______[_______
249 * -2^31 -2^30 0 2^30 2^31 2^32
250 *
251 * Otherwise:
252 *
253 * ON OSS US OU32 ON
254 * ______[_______________[_______________[_______[_______
255 * -2^31 0 2^31 2^32
256 *
257 *
258 * E.g., OtherUnsigned32 (OU32) covers all integers from 2^31 to 2^32-1.
259 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400260 * NOTE: OtherSigned32 (OS32) and OU31 (OtherUnsigned31) are empty if Smis are
261 * 32-bit wide. They should thus never be used directly, only indirectly
262 * via e.g. Number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 */
264
265#define PROPER_BITSET_TYPE_LIST(V) \
266 REPRESENTATION_BITSET_TYPE_LIST(V) \
267 SEMANTIC_BITSET_TYPE_LIST(V)
268
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400269#define BITSET_TYPE_LIST(V) \
270 MASK_BITSET_TYPE_LIST(V) \
271 REPRESENTATION_BITSET_TYPE_LIST(V) \
272 INTERNAL_BITSET_TYPE_LIST(V) \
273 SEMANTIC_BITSET_TYPE_LIST(V)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274
275
276// -----------------------------------------------------------------------------
277// The abstract Type class, parameterized over the low-level representation.
278
279// struct Config {
280// typedef TypeImpl<Config> Type;
281// typedef Base;
282// typedef Struct;
283// typedef Region;
284// template<class> struct Handle { typedef type; } // No template typedefs...
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400285// template<class T> static Handle<T>::type null_handle();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286// template<class T> static Handle<T>::type handle(T* t); // !is_bitset(t)
287// template<class T> static Handle<T>::type cast(Handle<Type>::type);
288// static bool is_bitset(Type*);
289// static bool is_class(Type*);
290// static bool is_struct(Type*, int tag);
291// static bitset as_bitset(Type*);
292// static i::Handle<i::Map> as_class(Type*);
293// static Handle<Struct>::type as_struct(Type*);
294// static Type* from_bitset(bitset);
295// static Handle<Type>::type from_bitset(bitset, Region*);
296// static Handle<Type>::type from_class(i::Handle<Map>, Region*);
297// static Handle<Type>::type from_struct(Handle<Struct>::type, int tag);
298// static Handle<Struct>::type struct_create(int tag, int length, Region*);
299// static void struct_shrink(Handle<Struct>::type, int length);
300// static int struct_tag(Handle<Struct>::type);
301// static int struct_length(Handle<Struct>::type);
302// static Handle<Type>::type struct_get(Handle<Struct>::type, int);
303// static void struct_set(Handle<Struct>::type, int, Handle<Type>::type);
304// template<class V>
305// static i::Handle<V> struct_get_value(Handle<Struct>::type, int);
306// template<class V>
307// static void struct_set_value(Handle<Struct>::type, int, i::Handle<V>);
308// }
309template<class Config>
310class TypeImpl : public Config::Base {
311 public:
312 // Auxiliary types.
313
314 typedef uint32_t bitset; // Internal
315 class BitsetType; // Internal
316 class StructuralType; // Internal
317 class UnionType; // Internal
318
319 class ClassType;
320 class ConstantType;
321 class RangeType;
322 class ContextType;
323 class ArrayType;
324 class FunctionType;
325
326 typedef typename Config::template Handle<TypeImpl>::type TypeHandle;
327 typedef typename Config::template Handle<ClassType>::type ClassHandle;
328 typedef typename Config::template Handle<ConstantType>::type ConstantHandle;
329 typedef typename Config::template Handle<RangeType>::type RangeHandle;
330 typedef typename Config::template Handle<ContextType>::type ContextHandle;
331 typedef typename Config::template Handle<ArrayType>::type ArrayHandle;
332 typedef typename Config::template Handle<FunctionType>::type FunctionHandle;
333 typedef typename Config::template Handle<UnionType>::type UnionHandle;
334 typedef typename Config::Region Region;
335
336 // Constructors.
337
338 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \
339 static TypeImpl* type() { \
340 return BitsetType::New(BitsetType::k##type); \
341 } \
342 static TypeHandle type(Region* region) { \
343 return BitsetType::New(BitsetType::k##type, region); \
344 }
345 PROPER_BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR)
346 #undef DEFINE_TYPE_CONSTRUCTOR
347
348 static TypeHandle Class(i::Handle<i::Map> map, Region* region) {
349 return ClassType::New(map, region);
350 }
351 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) {
352 return ConstantType::New(value, region);
353 }
354 static TypeHandle Range(
355 i::Handle<i::Object> min, i::Handle<i::Object> max, Region* region) {
356 return RangeType::New(min, max, region);
357 }
358 static TypeHandle Context(TypeHandle outer, Region* region) {
359 return ContextType::New(outer, region);
360 }
361 static TypeHandle Array(TypeHandle element, Region* region) {
362 return ArrayType::New(element, region);
363 }
364 static FunctionHandle Function(
365 TypeHandle result, TypeHandle receiver, int arity, Region* region) {
366 return FunctionType::New(result, receiver, arity, region);
367 }
368 static TypeHandle Function(TypeHandle result, Region* region) {
369 return Function(result, Any(region), 0, region);
370 }
371 static TypeHandle Function(
372 TypeHandle result, TypeHandle param0, Region* region) {
373 FunctionHandle function = Function(result, Any(region), 1, region);
374 function->InitParameter(0, param0);
375 return function;
376 }
377 static TypeHandle Function(
378 TypeHandle result, TypeHandle param0, TypeHandle param1, Region* region) {
379 FunctionHandle function = Function(result, Any(region), 2, region);
380 function->InitParameter(0, param0);
381 function->InitParameter(1, param1);
382 return function;
383 }
384 static TypeHandle Function(
385 TypeHandle result, TypeHandle param0, TypeHandle param1,
386 TypeHandle param2, Region* region) {
387 FunctionHandle function = Function(result, Any(region), 3, region);
388 function->InitParameter(0, param0);
389 function->InitParameter(1, param1);
390 function->InitParameter(2, param2);
391 return function;
392 }
393
394 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg);
395 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400396 static TypeImpl* Union(TypeImpl* type1, TypeImpl* type2) {
397 return BitsetType::New(type1->AsBitset() | type2->AsBitset());
398 }
399 static TypeImpl* Intersect(TypeImpl* type1, TypeImpl* type2) {
400 return BitsetType::New(type1->AsBitset() & type2->AsBitset());
401 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402
403 static TypeHandle Of(double value, Region* region) {
404 return Config::from_bitset(BitsetType::Lub(value), region);
405 }
406 static TypeHandle Of(i::Object* value, Region* region) {
407 return Config::from_bitset(BitsetType::Lub(value), region);
408 }
409 static TypeHandle Of(i::Handle<i::Object> value, Region* region) {
410 return Of(*value, region);
411 }
412
413 // Predicates.
414
415 bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); }
416
417 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); }
418 template<class TypeHandle>
419 bool Is(TypeHandle that) { return this->Is(*that); }
420
421 bool Maybe(TypeImpl* that);
422 template<class TypeHandle>
423 bool Maybe(TypeHandle that) { return this->Maybe(*that); }
424
425 bool Equals(TypeImpl* that) { return this->Is(that) && that->Is(this); }
426 template<class TypeHandle>
427 bool Equals(TypeHandle that) { return this->Equals(*that); }
428
429 // Equivalent to Constant(val)->Is(this), but avoiding allocation.
430 bool Contains(i::Object* val);
431 bool Contains(i::Handle<i::Object> val) { return this->Contains(*val); }
432
433 // State-dependent versions of the above that consider subtyping between
434 // a constant and its map class.
435 inline static TypeHandle NowOf(i::Object* value, Region* region);
436 static TypeHandle NowOf(i::Handle<i::Object> value, Region* region) {
437 return NowOf(*value, region);
438 }
439 bool NowIs(TypeImpl* that);
440 template<class TypeHandle>
441 bool NowIs(TypeHandle that) { return this->NowIs(*that); }
442 inline bool NowContains(i::Object* val);
443 bool NowContains(i::Handle<i::Object> val) { return this->NowContains(*val); }
444
445 bool NowStable();
446
447 // Inspection.
448
449 bool IsClass() {
450 return Config::is_class(this)
451 || Config::is_struct(this, StructuralType::kClassTag);
452 }
453 bool IsConstant() {
454 return Config::is_struct(this, StructuralType::kConstantTag);
455 }
456 bool IsRange() {
457 return Config::is_struct(this, StructuralType::kRangeTag);
458 }
459 bool IsContext() {
460 return Config::is_struct(this, StructuralType::kContextTag);
461 }
462 bool IsArray() {
463 return Config::is_struct(this, StructuralType::kArrayTag);
464 }
465 bool IsFunction() {
466 return Config::is_struct(this, StructuralType::kFunctionTag);
467 }
468
469 ClassType* AsClass() { return ClassType::cast(this); }
470 ConstantType* AsConstant() { return ConstantType::cast(this); }
471 RangeType* AsRange() { return RangeType::cast(this); }
472 ContextType* AsContext() { return ContextType::cast(this); }
473 ArrayType* AsArray() { return ArrayType::cast(this); }
474 FunctionType* AsFunction() { return FunctionType::cast(this); }
475
476 // Minimum and maximum of a numeric type.
477 // These functions do not distinguish between -0 and +0. If the type equals
478 // kNaN, they return NaN; otherwise kNaN is ignored. Only call these
479 // functions on subtypes of Number.
480 double Min();
481 double Max();
482
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400483 // Extracts a range from the type. If the type is a range, it just
484 // returns it; if it is a union, it returns the range component.
485 // Note that it does not contain range for constants.
486 RangeType* GetRange();
487
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488 int NumClasses();
489 int NumConstants();
490
491 template<class T> class Iterator;
492 Iterator<i::Map> Classes() {
493 if (this->IsBitset()) return Iterator<i::Map>();
494 return Iterator<i::Map>(Config::handle(this));
495 }
496 Iterator<i::Object> Constants() {
497 if (this->IsBitset()) return Iterator<i::Object>();
498 return Iterator<i::Object>(Config::handle(this));
499 }
500
501 // Casting and conversion.
502
503 static inline TypeImpl* cast(typename Config::Base* object);
504
505 template<class OtherTypeImpl>
506 static TypeHandle Convert(
507 typename OtherTypeImpl::TypeHandle type, Region* region);
508
509 // Printing.
510
511 enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM };
512
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400513 void PrintTo(std::ostream& os, PrintDimension dim = BOTH_DIMS); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514
515#ifdef DEBUG
516 void Print();
517#endif
518
519 protected:
520 // Friends.
521
522 template<class> friend class Iterator;
523 template<class> friend class TypeImpl;
524
525 // Handle conversion.
526
527 template<class T>
528 static typename Config::template Handle<T>::type handle(T* type) {
529 return Config::handle(type);
530 }
531 TypeImpl* unhandle() { return this; }
532
533 // Internal inspection.
534
535 bool IsNone() { return this == None(); }
536 bool IsAny() { return this == Any(); }
537 bool IsBitset() { return Config::is_bitset(this); }
538 bool IsUnion() { return Config::is_struct(this, StructuralType::kUnionTag); }
539
540 bitset AsBitset() {
541 DCHECK(this->IsBitset());
542 return static_cast<BitsetType*>(this)->Bitset();
543 }
544 UnionType* AsUnion() { return UnionType::cast(this); }
545
546 // Auxiliary functions.
547
548 bitset BitsetGlb() { return BitsetType::Glb(this); }
549 bitset BitsetLub() { return BitsetType::Lub(this); }
550
551 bool SlowIs(TypeImpl* that);
552
553 static bool IsInteger(double x) {
554 return nearbyint(x) == x && !i::IsMinusZero(x); // Allows for infinities.
555 }
556 static bool IsInteger(i::Object* x) {
557 return x->IsNumber() && IsInteger(x->Number());
558 }
559
560 struct Limits {
561 i::Handle<i::Object> min;
562 i::Handle<i::Object> max;
563 Limits(i::Handle<i::Object> min, i::Handle<i::Object> max) :
564 min(min), max(max) {}
565 explicit Limits(RangeType* range) :
566 min(range->Min()), max(range->Max()) {}
567 };
568
569 static Limits Intersect(Limits lhs, Limits rhs);
570 static Limits Union(Limits lhs, Limits rhs);
571 static bool Overlap(RangeType* lhs, RangeType* rhs);
572 static bool Contains(RangeType* lhs, RangeType* rhs);
573 static bool Contains(RangeType* range, i::Object* val);
574
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 static int UpdateRange(
576 RangeHandle type, UnionHandle result, int size, Region* region);
577
578 bool SimplyEquals(TypeImpl* that);
579 template<class TypeHandle>
580 bool SimplyEquals(TypeHandle that) { return this->SimplyEquals(*that); }
581
582 static int AddToUnion(
583 TypeHandle type, UnionHandle result, int size, Region* region);
584 static int IntersectAux(
585 TypeHandle type, TypeHandle other,
586 UnionHandle result, int size, Region* region);
587 static TypeHandle NormalizeUnion(UnionHandle unioned, int size);
588};
589
590
591// -----------------------------------------------------------------------------
592// Bitset types (internal).
593
594template<class Config>
595class TypeImpl<Config>::BitsetType : public TypeImpl<Config> {
596 protected:
597 friend class TypeImpl<Config>;
598
599 enum {
600 #define DECLARE_TYPE(type, value) k##type = (value),
601 BITSET_TYPE_LIST(DECLARE_TYPE)
602 #undef DECLARE_TYPE
603 kUnusedEOL = 0
604 };
605
606 bitset Bitset() { return Config::as_bitset(this); }
607
608 static TypeImpl* New(bitset bits) {
609 DCHECK(bits == kNone || IsInhabited(bits));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400610
611 if (FLAG_enable_slow_asserts) {
612 // Check that the bitset does not contain any holes in number ranges.
613 bitset mask = kSemantic;
614 if (!i::SmiValuesAre31Bits()) {
615 mask &= ~(kOtherUnsigned31 | kOtherSigned32);
616 }
617 bitset number_bits = bits & kPlainNumber & mask;
618 if (number_bits != 0) {
619 bitset lub = Lub(Min(number_bits), Max(number_bits)) & mask;
620 CHECK(lub == number_bits);
621 }
622 }
623
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 return Config::from_bitset(bits);
625 }
626 static TypeHandle New(bitset bits, Region* region) {
627 DCHECK(bits == kNone || IsInhabited(bits));
628 return Config::from_bitset(bits, region);
629 }
630 // TODO(neis): Eventually allow again for types with empty semantics
631 // part and modify intersection and possibly subtyping accordingly.
632
633 static bool IsInhabited(bitset bits) {
634 return bits & kSemantic;
635 }
636
637 static bool Is(bitset bits1, bitset bits2) {
638 return (bits1 | bits2) == bits2;
639 }
640
641 static double Min(bitset);
642 static double Max(bitset);
643
644 static bitset Glb(TypeImpl* type); // greatest lower bound that's a bitset
645 static bitset Lub(TypeImpl* type); // least upper bound that's a bitset
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400646 static bitset Lub(i::Map* map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 static bitset Lub(i::Object* value);
648 static bitset Lub(double value);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400649 static bitset Lub(double min, double max);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650
651 static const char* Name(bitset);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400652 static void Print(std::ostream& os, bitset); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653#ifdef DEBUG
654 static void Print(bitset);
655#endif
656
657 private:
658 struct BitsetMin{
659 bitset bits;
660 double min;
661 };
662 static const BitsetMin BitsetMins31[];
663 static const BitsetMin BitsetMins32[];
664 static const BitsetMin* BitsetMins() {
665 return i::SmiValuesAre31Bits() ? BitsetMins31 : BitsetMins32;
666 }
667 static size_t BitsetMinsSize() {
668 return i::SmiValuesAre31Bits() ? 7 : 5;
669 /* arraysize(BitsetMins31) : arraysize(BitsetMins32); */
670 // Using arraysize here doesn't compile on Windows.
671 }
672};
673
674
675// -----------------------------------------------------------------------------
676// Superclass for non-bitset types (internal).
677// Contains a tag and a variable number of type or value fields.
678
679template<class Config>
680class TypeImpl<Config>::StructuralType : public TypeImpl<Config> {
681 protected:
682 template<class> friend class TypeImpl;
683 friend struct ZoneTypeConfig; // For tags.
684 friend struct HeapTypeConfig;
685
686 enum Tag {
687 kClassTag,
688 kConstantTag,
689 kRangeTag,
690 kContextTag,
691 kArrayTag,
692 kFunctionTag,
693 kUnionTag
694 };
695
696 int Length() {
697 return Config::struct_length(Config::as_struct(this));
698 }
699 TypeHandle Get(int i) {
700 DCHECK(0 <= i && i < this->Length());
701 return Config::struct_get(Config::as_struct(this), i);
702 }
703 void Set(int i, TypeHandle type) {
704 DCHECK(0 <= i && i < this->Length());
705 Config::struct_set(Config::as_struct(this), i, type);
706 }
707 void Shrink(int length) {
708 DCHECK(2 <= length && length <= this->Length());
709 Config::struct_shrink(Config::as_struct(this), length);
710 }
711 template<class V> i::Handle<V> GetValue(int i) {
712 DCHECK(0 <= i && i < this->Length());
713 return Config::template struct_get_value<V>(Config::as_struct(this), i);
714 }
715 template<class V> void SetValue(int i, i::Handle<V> x) {
716 DCHECK(0 <= i && i < this->Length());
717 Config::struct_set_value(Config::as_struct(this), i, x);
718 }
719
720 static TypeHandle New(Tag tag, int length, Region* region) {
721 DCHECK(1 <= length);
722 return Config::from_struct(Config::struct_create(tag, length, region));
723 }
724};
725
726
727// -----------------------------------------------------------------------------
728// Union types (internal).
729// A union is a structured type with the following invariants:
730// - its length is at least 2
731// - at most one field is a bitset, and it must go into index 0
732// - no field is a union
733// - no field is a subtype of any other field
734template<class Config>
735class TypeImpl<Config>::UnionType : public StructuralType {
736 public:
737 static UnionHandle New(int length, Region* region) {
738 return Config::template cast<UnionType>(
739 StructuralType::New(StructuralType::kUnionTag, length, region));
740 }
741
742 static UnionType* cast(TypeImpl* type) {
743 DCHECK(type->IsUnion());
744 return static_cast<UnionType*>(type);
745 }
746
747 bool Wellformed();
748};
749
750
751// -----------------------------------------------------------------------------
752// Class types.
753
754template<class Config>
755class TypeImpl<Config>::ClassType : public StructuralType {
756 public:
757 TypeHandle Bound(Region* region) {
758 return Config::is_class(this) ?
759 BitsetType::New(BitsetType::Lub(*Config::as_class(this)), region) :
760 this->Get(0);
761 }
762 i::Handle<i::Map> Map() {
763 return Config::is_class(this) ? Config::as_class(this) :
764 this->template GetValue<i::Map>(1);
765 }
766
767 static ClassHandle New(i::Handle<i::Map> map, Region* region) {
768 ClassHandle type =
769 Config::template cast<ClassType>(Config::from_class(map, region));
770 if (!type->IsClass()) {
771 type = Config::template cast<ClassType>(
772 StructuralType::New(StructuralType::kClassTag, 2, region));
773 type->Set(0, BitsetType::New(BitsetType::Lub(*map), region));
774 type->SetValue(1, map);
775 }
776 return type;
777 }
778
779 static ClassType* cast(TypeImpl* type) {
780 DCHECK(type->IsClass());
781 return static_cast<ClassType*>(type);
782 }
783};
784
785
786// -----------------------------------------------------------------------------
787// Constant types.
788
789template<class Config>
790class TypeImpl<Config>::ConstantType : public StructuralType {
791 public:
792 TypeHandle Bound() { return this->Get(0); }
793 i::Handle<i::Object> Value() { return this->template GetValue<i::Object>(1); }
794
795 static ConstantHandle New(i::Handle<i::Object> value, Region* region) {
796 ConstantHandle type = Config::template cast<ConstantType>(
797 StructuralType::New(StructuralType::kConstantTag, 2, region));
798 type->Set(0, BitsetType::New(BitsetType::Lub(*value), region));
799 type->SetValue(1, value);
800 return type;
801 }
802
803 static ConstantType* cast(TypeImpl* type) {
804 DCHECK(type->IsConstant());
805 return static_cast<ConstantType*>(type);
806 }
807};
808// TODO(neis): Also cache value if numerical.
809// TODO(neis): Allow restricting the representation.
810
811
812// -----------------------------------------------------------------------------
813// Range types.
814
815template<class Config>
816class TypeImpl<Config>::RangeType : public StructuralType {
817 public:
818 int BitsetLub() { return this->Get(0)->AsBitset(); }
819 i::Handle<i::Object> Min() { return this->template GetValue<i::Object>(1); }
820 i::Handle<i::Object> Max() { return this->template GetValue<i::Object>(2); }
821
822 static RangeHandle New(
823 i::Handle<i::Object> min, i::Handle<i::Object> max, Region* region) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400824 DCHECK(IsInteger(min->Number()) && IsInteger(max->Number()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 DCHECK(min->Number() <= max->Number());
826 RangeHandle type = Config::template cast<RangeType>(
827 StructuralType::New(StructuralType::kRangeTag, 3, region));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400828 type->Set(0, BitsetType::New(
829 BitsetType::Lub(min->Number(), max->Number()), region));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000830 type->SetValue(1, min);
831 type->SetValue(2, max);
832 return type;
833 }
834
835 static RangeHandle New(Limits lim, Region* region) {
836 return New(lim.min, lim.max, region);
837 }
838
839 static RangeType* cast(TypeImpl* type) {
840 DCHECK(type->IsRange());
841 return static_cast<RangeType*>(type);
842 }
843};
844// TODO(neis): Also cache min and max values.
845// TODO(neis): Allow restricting the representation.
846
847
848// -----------------------------------------------------------------------------
849// Context types.
850
851template<class Config>
852class TypeImpl<Config>::ContextType : public StructuralType {
853 public:
854 TypeHandle Outer() { return this->Get(0); }
855
856 static ContextHandle New(TypeHandle outer, Region* region) {
857 ContextHandle type = Config::template cast<ContextType>(
858 StructuralType::New(StructuralType::kContextTag, 1, region));
859 type->Set(0, outer);
860 return type;
861 }
862
863 static ContextType* cast(TypeImpl* type) {
864 DCHECK(type->IsContext());
865 return static_cast<ContextType*>(type);
866 }
867};
868
869
870// -----------------------------------------------------------------------------
871// Array types.
872
873template<class Config>
874class TypeImpl<Config>::ArrayType : public StructuralType {
875 public:
876 TypeHandle Element() { return this->Get(0); }
877
878 static ArrayHandle New(TypeHandle element, Region* region) {
879 ArrayHandle type = Config::template cast<ArrayType>(
880 StructuralType::New(StructuralType::kArrayTag, 1, region));
881 type->Set(0, element);
882 return type;
883 }
884
885 static ArrayType* cast(TypeImpl* type) {
886 DCHECK(type->IsArray());
887 return static_cast<ArrayType*>(type);
888 }
889};
890
891
892// -----------------------------------------------------------------------------
893// Function types.
894
895template<class Config>
896class TypeImpl<Config>::FunctionType : public StructuralType {
897 public:
898 int Arity() { return this->Length() - 2; }
899 TypeHandle Result() { return this->Get(0); }
900 TypeHandle Receiver() { return this->Get(1); }
901 TypeHandle Parameter(int i) { return this->Get(2 + i); }
902
903 void InitParameter(int i, TypeHandle type) { this->Set(2 + i, type); }
904
905 static FunctionHandle New(
906 TypeHandle result, TypeHandle receiver, int arity, Region* region) {
907 FunctionHandle type = Config::template cast<FunctionType>(
908 StructuralType::New(StructuralType::kFunctionTag, 2 + arity, region));
909 type->Set(0, result);
910 type->Set(1, receiver);
911 return type;
912 }
913
914 static FunctionType* cast(TypeImpl* type) {
915 DCHECK(type->IsFunction());
916 return static_cast<FunctionType*>(type);
917 }
918};
919
920
921// -----------------------------------------------------------------------------
922// Type iterators.
923
924template<class Config> template<class T>
925class TypeImpl<Config>::Iterator {
926 public:
927 bool Done() const { return index_ < 0; }
928 i::Handle<T> Current();
929 void Advance();
930
931 private:
932 template<class> friend class TypeImpl;
933
934 Iterator() : index_(-1) {}
935 explicit Iterator(TypeHandle type) : type_(type), index_(-1) {
936 Advance();
937 }
938
939 inline bool matches(TypeHandle type);
940 inline TypeHandle get_type();
941
942 TypeHandle type_;
943 int index_;
944};
945
946
947// -----------------------------------------------------------------------------
948// Zone-allocated types; they are either (odd) integers to represent bitsets, or
949// (even) pointers to structures for everything else.
950
951struct ZoneTypeConfig {
952 typedef TypeImpl<ZoneTypeConfig> Type;
953 class Base {};
954 typedef void* Struct;
955 typedef i::Zone Region;
956 template<class T> struct Handle { typedef T* type; };
957
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400958 template<class T> static inline T* null_handle();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000959 template<class T> static inline T* handle(T* type);
960 template<class T> static inline T* cast(Type* type);
961
962 static inline bool is_bitset(Type* type);
963 static inline bool is_class(Type* type);
964 static inline bool is_struct(Type* type, int tag);
965
966 static inline Type::bitset as_bitset(Type* type);
967 static inline i::Handle<i::Map> as_class(Type* type);
968 static inline Struct* as_struct(Type* type);
969
970 static inline Type* from_bitset(Type::bitset);
971 static inline Type* from_bitset(Type::bitset, Zone* zone);
972 static inline Type* from_class(i::Handle<i::Map> map, Zone* zone);
973 static inline Type* from_struct(Struct* structured);
974
975 static inline Struct* struct_create(int tag, int length, Zone* zone);
976 static inline void struct_shrink(Struct* structure, int length);
977 static inline int struct_tag(Struct* structure);
978 static inline int struct_length(Struct* structure);
979 static inline Type* struct_get(Struct* structure, int i);
980 static inline void struct_set(Struct* structure, int i, Type* type);
981 template<class V>
982 static inline i::Handle<V> struct_get_value(Struct* structure, int i);
983 template<class V> static inline void struct_set_value(
984 Struct* structure, int i, i::Handle<V> x);
985};
986
987typedef TypeImpl<ZoneTypeConfig> Type;
988
989
990// -----------------------------------------------------------------------------
991// Heap-allocated types; either smis for bitsets, maps for classes, boxes for
992// constants, or fixed arrays for unions.
993
994struct HeapTypeConfig {
995 typedef TypeImpl<HeapTypeConfig> Type;
996 typedef i::Object Base;
997 typedef i::FixedArray Struct;
998 typedef i::Isolate Region;
999 template<class T> struct Handle { typedef i::Handle<T> type; };
1000
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001001 template<class T> static inline i::Handle<T> null_handle();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 template<class T> static inline i::Handle<T> handle(T* type);
1003 template<class T> static inline i::Handle<T> cast(i::Handle<Type> type);
1004
1005 static inline bool is_bitset(Type* type);
1006 static inline bool is_class(Type* type);
1007 static inline bool is_struct(Type* type, int tag);
1008
1009 static inline Type::bitset as_bitset(Type* type);
1010 static inline i::Handle<i::Map> as_class(Type* type);
1011 static inline i::Handle<Struct> as_struct(Type* type);
1012
1013 static inline Type* from_bitset(Type::bitset);
1014 static inline i::Handle<Type> from_bitset(Type::bitset, Isolate* isolate);
1015 static inline i::Handle<Type> from_class(
1016 i::Handle<i::Map> map, Isolate* isolate);
1017 static inline i::Handle<Type> from_struct(i::Handle<Struct> structure);
1018
1019 static inline i::Handle<Struct> struct_create(
1020 int tag, int length, Isolate* isolate);
1021 static inline void struct_shrink(i::Handle<Struct> structure, int length);
1022 static inline int struct_tag(i::Handle<Struct> structure);
1023 static inline int struct_length(i::Handle<Struct> structure);
1024 static inline i::Handle<Type> struct_get(i::Handle<Struct> structure, int i);
1025 static inline void struct_set(
1026 i::Handle<Struct> structure, int i, i::Handle<Type> type);
1027 template<class V>
1028 static inline i::Handle<V> struct_get_value(
1029 i::Handle<Struct> structure, int i);
1030 template<class V>
1031 static inline void struct_set_value(
1032 i::Handle<Struct> structure, int i, i::Handle<V> x);
1033};
1034
1035typedef TypeImpl<HeapTypeConfig> HeapType;
1036
1037
1038// -----------------------------------------------------------------------------
1039// Type bounds. A simple struct to represent a pair of lower/upper types.
1040
1041template<class Config>
1042struct BoundsImpl {
1043 typedef TypeImpl<Config> Type;
1044 typedef typename Type::TypeHandle TypeHandle;
1045 typedef typename Type::Region Region;
1046
1047 TypeHandle lower;
1048 TypeHandle upper;
1049
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001050 BoundsImpl() : // Make sure accessing uninitialized bounds crashes big-time.
1051 lower(Config::template null_handle<Type>()),
1052 upper(Config::template null_handle<Type>()) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053 explicit BoundsImpl(TypeHandle t) : lower(t), upper(t) {}
1054 BoundsImpl(TypeHandle l, TypeHandle u) : lower(l), upper(u) {
1055 DCHECK(lower->Is(upper));
1056 }
1057
1058 // Unrestricted bounds.
1059 static BoundsImpl Unbounded(Region* region) {
1060 return BoundsImpl(Type::None(region), Type::Any(region));
1061 }
1062
1063 // Meet: both b1 and b2 are known to hold.
1064 static BoundsImpl Both(BoundsImpl b1, BoundsImpl b2, Region* region) {
1065 TypeHandle lower = Type::Union(b1.lower, b2.lower, region);
1066 TypeHandle upper = Type::Intersect(b1.upper, b2.upper, region);
1067 // Lower bounds are considered approximate, correct as necessary.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001068 if (!lower->Is(upper)) lower = upper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069 return BoundsImpl(lower, upper);
1070 }
1071
1072 // Join: either b1 or b2 is known to hold.
1073 static BoundsImpl Either(BoundsImpl b1, BoundsImpl b2, Region* region) {
1074 TypeHandle lower = Type::Intersect(b1.lower, b2.lower, region);
1075 TypeHandle upper = Type::Union(b1.upper, b2.upper, region);
1076 return BoundsImpl(lower, upper);
1077 }
1078
1079 static BoundsImpl NarrowLower(BoundsImpl b, TypeHandle t, Region* region) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001080 TypeHandle lower = Type::Union(b.lower, t, region);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001081 // Lower bounds are considered approximate, correct as necessary.
1082 if (!lower->Is(b.upper)) lower = b.upper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 return BoundsImpl(lower, b.upper);
1084 }
1085 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001086 TypeHandle lower = b.lower;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001087 TypeHandle upper = Type::Intersect(b.upper, t, region);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001088 // Lower bounds are considered approximate, correct as necessary.
1089 if (!lower->Is(upper)) lower = upper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001090 return BoundsImpl(lower, upper);
1091 }
1092
1093 bool Narrows(BoundsImpl that) {
1094 return that.lower->Is(this->lower) && this->upper->Is(that.upper);
1095 }
1096};
1097
1098typedef BoundsImpl<ZoneTypeConfig> Bounds;
1099
1100} } // namespace v8::internal
1101
1102#endif // V8_TYPES_H_