OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 30 matching lines...) Expand all Loading... | |
41 // obvious primitive types and some predefined unions, the type language also | 41 // obvious primitive types and some predefined unions, the type language also |
42 // can express class types (a.k.a. specific maps) and singleton types (i.e., | 42 // can express class types (a.k.a. specific maps) and singleton types (i.e., |
43 // concrete constants). | 43 // concrete constants). |
44 // | 44 // |
45 // The following equations and inequations hold: | 45 // The following equations and inequations hold: |
46 // | 46 // |
47 // None <= T | 47 // None <= T |
48 // T <= Any | 48 // T <= Any |
49 // | 49 // |
50 // Oddball = Boolean \/ Null \/ Undefined | 50 // Oddball = Boolean \/ Null \/ Undefined |
51 // Number = Smi \/ Double | 51 // Number = Integer32 \/ Double |
52 // Integer31 < Integer32 | |
52 // Name = String \/ Symbol | 53 // Name = String \/ Symbol |
53 // UniqueName = InternalizedString \/ Symbol | 54 // UniqueName = InternalizedString \/ Symbol |
54 // InternalizedString < String | 55 // InternalizedString < String |
55 // | 56 // |
56 // Receiver = Object \/ Proxy | 57 // Receiver = Object \/ Proxy |
58 // Undetectable < Object | |
57 // Array < Object | 59 // Array < Object |
58 // Function < Object | 60 // Function < Object |
59 // | 61 // |
60 // Class(map) < T iff instance_type(map) < T | 62 // Class(map) < T iff instance_type(map) < T |
61 // Constant(x) < T iff instance_type(map(x)) < T | 63 // Constant(x) < T iff instance_type(map(x)) < T |
62 // | 64 // |
63 // Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can | 65 // Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can |
64 // change! (Its instance type cannot, however.) | 66 // change! (Its instance type cannot, however.) |
65 // TODO(rossberg): the latter is not currently true for proxies, because of fix, | 67 // TODO(rossberg): the latter is not currently true for proxies, because of fix, |
66 // but will hold once we implement direct proxies. | 68 // but will hold once we implement direct proxies. |
67 // | 69 // |
68 // There are two main functions for testing types: | 70 // There are two main functions for testing types: |
69 // | 71 // |
70 // T1->Is(T2) -- tests whether T1 is included in T2 (i.e., T1 <= T2) | 72 // T1->Is(T2) -- tests whether T1 is included in T2 (i.e., T1 <= T2) |
71 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0) | 73 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0) |
72 // | 74 // |
73 // Typically, the latter should be used to check whether a specific case needs | 75 // Typically, the former is to be used to select representations (e.g., via |
74 // handling (e.g., via T->Maybe(Number)). | 76 // T->Is(Integer31())), and the to check whether a specific case needs handling |
77 // (e.g., via T->Maybe(Number())). | |
75 // | 78 // |
76 // There is no functionality to discover whether a type is a leaf in the | 79 // There is no functionality to discover whether a type is a leaf in the |
77 // lattice. That is intentional. It should always be possible to refine the | 80 // lattice. That is intentional. It should always be possible to refine the |
78 // lattice (e.g., splitting up number types further) without invalidating any | 81 // lattice (e.g., splitting up number types further) without invalidating any |
79 // existing assumptions or tests. | 82 // existing assumptions or tests. |
80 // | 83 // |
81 // Internally, all 'primitive' types, and their unions, are represented as | 84 // Internally, all 'primitive' types, and their unions, are represented as |
82 // bitsets via smis. Class is a heap pointer to the respective map. Only | 85 // bitsets via smis. Class is a heap pointer to the respective map. Only |
83 // Constant's, or unions containing Class'es or Constant's, require allocation. | 86 // Constant's, or unions containing Class'es or Constant's, require allocation. |
84 // | 87 // |
85 // The type representation is heap-allocated, so cannot (currently) be used in | 88 // The type representation is heap-allocated, so cannot (currently) be used in |
86 // a parallel compilation context. | 89 // a parallel compilation context. |
87 | 90 |
88 class Type : public Object { | 91 class Type : public Object { |
89 public: | 92 public: |
90 static Type* None() { return from_bitset(kNone); } | 93 static Type* None() { return from_bitset(kNone); } |
91 static Type* Any() { return from_bitset(kAny); } | 94 static Type* Any() { return from_bitset(kAny); } |
95 static Type* Boxed() { return from_bitset(kBoxed); } | |
Jakob Kummerow
2013/06/11 17:58:07
Why not "HeapObject"? I don't care much either way
rossberg
2013/06/12 17:21:32
Produces annoying name clashes between type and fu
| |
96 static Type* Detectable() { return from_bitset(kDetectable); } | |
92 | 97 |
93 static Type* Oddball() { return from_bitset(kOddball); } | 98 static Type* Oddball() { return from_bitset(kOddball); } |
94 static Type* Boolean() { return from_bitset(kBoolean); } | 99 static Type* Boolean() { return from_bitset(kBoolean); } |
95 static Type* Null() { return from_bitset(kNull); } | 100 static Type* Null() { return from_bitset(kNull); } |
96 static Type* Undefined() { return from_bitset(kUndefined); } | 101 static Type* Undefined() { return from_bitset(kUndefined); } |
97 | 102 |
98 static Type* Number() { return from_bitset(kNumber); } | 103 static Type* Number() { return from_bitset(kNumber); } |
99 static Type* Smi() { return from_bitset(kSmi); } | 104 static Type* Integer31() { return from_bitset(kInteger31); } |
105 static Type* Integer32() { return from_bitset(kInteger32); } | |
100 static Type* Double() { return from_bitset(kDouble); } | 106 static Type* Double() { return from_bitset(kDouble); } |
101 | 107 |
102 static Type* Name() { return from_bitset(kName); } | 108 static Type* Name() { return from_bitset(kName); } |
103 static Type* UniqueName() { return from_bitset(kUniqueName); } | 109 static Type* UniqueName() { return from_bitset(kUniqueName); } |
104 static Type* String() { return from_bitset(kString); } | 110 static Type* String() { return from_bitset(kString); } |
105 static Type* InternalizedString() { return from_bitset(kInternalizedString); } | 111 static Type* InternalizedString() { return from_bitset(kInternalizedString); } |
106 static Type* Symbol() { return from_bitset(kSymbol); } | 112 static Type* Symbol() { return from_bitset(kSymbol); } |
107 | 113 |
108 static Type* Receiver() { return from_bitset(kReceiver); } | 114 static Type* Receiver() { return from_bitset(kReceiver); } |
109 static Type* Object() { return from_bitset(kObject); } | 115 static Type* Object() { return from_bitset(kObject); } |
116 static Type* Undetectable() { return from_bitset(kUndetectable); } | |
110 static Type* Array() { return from_bitset(kArray); } | 117 static Type* Array() { return from_bitset(kArray); } |
111 static Type* Function() { return from_bitset(kFunction); } | 118 static Type* Function() { return from_bitset(kFunction); } |
112 static Type* Proxy() { return from_bitset(kProxy); } | 119 static Type* Proxy() { return from_bitset(kProxy); } |
113 | 120 |
114 static Type* Class(Handle<Map> map) { return from_handle(map); } | 121 static Type* Class(Handle<Map> map) { return from_handle(map); } |
115 static Type* Constant(Handle<HeapObject> value) { | 122 static Type* Constant(Handle<HeapObject> value) { |
116 return Constant(value, value->GetIsolate()); | 123 return Constant(value, value->GetIsolate()); |
117 } | 124 } |
118 static Type* Constant(Handle<v8::internal::Object> value, Isolate* isolate) { | 125 static Type* Constant(Handle<v8::internal::Object> value, Isolate* isolate) { |
119 return from_handle(isolate->factory()->NewBox(value)); | 126 return from_handle(isolate->factory()->NewBox(value)); |
120 } | 127 } |
121 | 128 |
122 static Type* Union(Handle<Type> type1, Handle<Type> type2); | 129 static Type* Union(Handle<Type> type1, Handle<Type> type2); |
123 static Type* Optional(Handle<Type> type); // type \/ Undefined | 130 static Type* Optional(Handle<Type> type); // type \/ Undefined |
124 | 131 |
125 bool Is(Handle<Type> that); | 132 bool Is(Type* that); |
126 bool Maybe(Handle<Type> that); | 133 bool Is(Handle<Type> that) { return this->Is(*that); } |
134 bool Maybe(Type* that); | |
135 bool Maybe(Handle<Type> that) { return this->Maybe(*that); } | |
127 | 136 |
128 // TODO(rossberg): method to iterate unions? | 137 bool IsClass() { return is_class(); } |
138 bool IsConstant() { return is_constant(); } | |
139 Handle<Map> AsClass() { return as_class(); } | |
140 Handle<v8::internal::Object> AsConstant() { return as_constant(); } | |
141 | |
142 int NumClasses(); | |
143 int NumConstants(); | |
144 | |
145 template<class T> | |
146 class Iterator { | |
147 public: | |
148 bool Done() const { return index_ < 0; } | |
149 Handle<T> Get(); | |
Jakob Kummerow
2013/06/11 17:58:07
nit: most of our iterators use the names "Current(
rossberg
2013/06/12 17:21:32
Done.
| |
150 void Next(); | |
151 | |
152 private: | |
153 friend class Type; | |
154 | |
155 Iterator() : index_(-1) {} | |
156 explicit Iterator(Handle<Type> type) : type_(type), index_(-1) { | |
157 Next(); | |
158 } | |
159 | |
160 inline bool matches(Handle<Type> type); | |
161 inline Handle<Type> get_type(); | |
162 | |
163 Handle<Type> type_; | |
164 int index_; | |
165 }; | |
166 | |
167 Iterator<Map> Classes() { | |
168 if (this->is_bitset()) return Iterator<Map>(); | |
169 return Iterator<Map>(this->handle()); | |
170 } | |
171 Iterator<v8::internal::Object> Constants() { | |
172 if (this->is_bitset()) return Iterator<v8::internal::Object>(); | |
173 return Iterator<v8::internal::Object>(this->handle()); | |
174 } | |
129 | 175 |
130 private: | 176 private: |
131 // A union is a fixed array containing types. Invariants: | 177 // A union is a fixed array containing types. Invariants: |
132 // - its length is at least 2 | 178 // - its length is at least 2 |
133 // - at most one field is a bitset, and it must go into index 0 | 179 // - at most one field is a bitset, and it must go into index 0 |
134 // - no field is a union | 180 // - no field is a union |
135 typedef FixedArray Unioned; | 181 typedef FixedArray Unioned; |
136 | 182 |
137 enum { | 183 enum { |
138 kNull = 1 << 0, | 184 kNull = 1 << 0, |
139 kUndefined = 1 << 1, | 185 kUndefined = 1 << 1, |
140 kBoolean = 1 << 2, | 186 kBoolean = 1 << 2, |
141 kSmi = 1 << 3, | 187 kInteger31 = 1 << 3, |
142 kDouble = 1 << 4, | 188 kOtherInteger = 1 << 4, |
143 kSymbol = 1 << 5, | 189 kDouble = 1 << 5, |
144 kInternalizedString = 1 << 6, | 190 kSymbol = 1 << 6, |
145 kOtherString = 1 << 7, | 191 kInternalizedString = 1 << 7, |
146 kArray = 1 << 8, | 192 kOtherString = 1 << 8, |
147 kFunction = 1 << 9, | 193 kUndetectable = 1 << 9, |
148 kOtherObject = 1 << 10, | 194 kArray = 1 << 10, |
149 kProxy = 1 << 11, | 195 kFunction = 1 << 11, |
196 kOtherObject = 1 << 12, | |
197 kProxy = 1 << 13, | |
150 | 198 |
151 kOddball = kBoolean | kNull | kUndefined, | 199 kOddball = kBoolean | kNull | kUndefined, |
152 kNumber = kSmi | kDouble, | 200 kInteger32 = kInteger31 | kOtherInteger, |
201 kNumber = kInteger32 | kDouble, | |
153 kString = kInternalizedString | kOtherString, | 202 kString = kInternalizedString | kOtherString, |
154 kUniqueName = kSymbol | kInternalizedString, | 203 kUniqueName = kSymbol | kInternalizedString, |
155 kName = kSymbol | kString, | 204 kName = kSymbol | kString, |
156 kObject = kArray | kFunction | kOtherObject, | 205 kObject = kUndetectable | kArray | kFunction | kOtherObject, |
157 kReceiver = kObject | kProxy, | 206 kReceiver = kObject | kProxy, |
158 kAny = kOddball | kNumber | kName | kReceiver, | 207 kBoxed = kDouble | kName | kReceiver, |
208 kAny = kOddball | kNumber | kBoxed, | |
209 kDetectable = kBoxed - kUndetectable, | |
159 kNone = 0 | 210 kNone = 0 |
160 }; | 211 }; |
161 | 212 |
162 bool is_bitset() { return this->IsSmi(); } | 213 bool is_bitset() { return this->IsSmi(); } |
163 bool is_class() { return this->IsMap(); } | 214 bool is_class() { return this->IsMap(); } |
164 bool is_constant() { return this->IsBox(); } | 215 bool is_constant() { return this->IsBox(); } |
165 bool is_union() { return this->IsFixedArray(); } | 216 bool is_union() { return this->IsFixedArray(); } |
166 | 217 |
167 int as_bitset() { return Smi::cast(this)->value(); } | 218 int as_bitset() { return Smi::cast(this)->value(); } |
168 Handle<Map> as_class() { return Handle<Map>::cast(handle()); } | 219 Handle<Map> as_class() { return Handle<Map>::cast(handle()); } |
169 Handle<Box> as_constant() { return Handle<Box>::cast(handle()); } | 220 Handle<v8::internal::Object> as_constant() { |
221 Handle<Box> box = Handle<Box>::cast(handle()); | |
222 return v8::internal::handle(box->value(), box->GetIsolate()); | |
223 } | |
170 Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); } | 224 Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); } |
171 | 225 |
172 Handle<Type> handle() { return handle_via_isolate_of(this); } | 226 Handle<Type> handle() { return handle_via_isolate_of(this); } |
173 Handle<Type> handle_via_isolate_of(Type* type) { | 227 Handle<Type> handle_via_isolate_of(Type* type) { |
174 ASSERT(type->IsHeapObject()); | 228 ASSERT(type->IsHeapObject()); |
175 return v8::internal::handle(this, HeapObject::cast(type)->GetIsolate()); | 229 return v8::internal::handle(this, HeapObject::cast(type)->GetIsolate()); |
176 } | 230 } |
177 | 231 |
178 static Type* from_bitset(int bitset) { | 232 static Type* from_bitset(int bitset) { |
179 return static_cast<Type*>(Object::cast(Smi::FromInt(bitset))); | 233 return static_cast<Type*>(Object::cast(Smi::FromInt(bitset))); |
(...skipping 10 matching lines...) Expand all Loading... | |
190 | 244 |
191 int LubBitset(); // least upper bound that's a bitset | 245 int LubBitset(); // least upper bound that's a bitset |
192 int GlbBitset(); // greatest lower bound that's a bitset | 246 int GlbBitset(); // greatest lower bound that's a bitset |
193 bool InUnion(Handle<Unioned> unioned, int current_size); | 247 bool InUnion(Handle<Unioned> unioned, int current_size); |
194 int ExtendUnion(Handle<Unioned> unioned, int current_size); | 248 int ExtendUnion(Handle<Unioned> unioned, int current_size); |
195 }; | 249 }; |
196 | 250 |
197 } } // namespace v8::internal | 251 } } // namespace v8::internal |
198 | 252 |
199 #endif // V8_TYPES_H_ | 253 #endif // V8_TYPES_H_ |
OLD | NEW |