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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 V(NumberOrString, kNumber | kString) \ | 124 V(NumberOrString, kNumber | kString) \ |
125 V(Object, kUndetectable | kArray | kFunction | \ | 125 V(Object, kUndetectable | kArray | kFunction | \ |
126 kRegExp | kOtherObject) \ | 126 kRegExp | kOtherObject) \ |
127 V(Receiver, kObject | kProxy) \ | 127 V(Receiver, kObject | kProxy) \ |
128 V(Allocated, kDouble | kName | kReceiver) \ | 128 V(Allocated, kDouble | kName | kReceiver) \ |
129 V(Any, kOddball | kNumber | kAllocated | kInternal) \ | 129 V(Any, kOddball | kNumber | kAllocated | kInternal) \ |
130 V(NonNumber, kAny - kNumber) \ | 130 V(NonNumber, kAny - kNumber) \ |
131 V(Detectable, kAllocated - kUndetectable) | 131 V(Detectable, kAllocated - kUndetectable) |
132 | 132 |
133 | 133 |
134 // struct Config { | 134 class Type : public Object { |
135 // typedef Base; | |
136 // typedef Unioned; | |
137 // typedef Region; | |
138 // template<class> struct Handle { typedef type; } // No template typedefs... | |
139 // static Handle<Type>::type handle(Type* type); // !is_bitset(type) | |
140 // static bool is_bitset(Type* type); | |
141 // static bool is_class(Type* type); | |
142 // static bool is_constant(Type* type); | |
143 // static bool is_union(Type* type); | |
144 // static int as_bitset(Type* type); | |
145 // static i::Handle<i::Map> as_class(Type* type); | |
146 // static i::Handle<i::Object> as_constant(Type* type); | |
147 // static Handle<Unioned>::type as_union(Type* type); | |
148 // static Type* from_bitset(int bitset); | |
149 // static Handle<Type>::type from_bitset(int bitset, Region* region); | |
150 // static Handle<Type>::type from_class(i::Handle<i::Map> map, Region* region) | |
151 // static Handle<Type>::type from_constant( | |
152 // i::Handle<i::Object> value, Region* region); | |
153 // static Handle<Type>::type from_union(Handle<Unioned>::T unioned); | |
154 // static Handle<Unioned>::type union_create(int size, Region* region); | |
155 // static Handle<Type>::type union_get(Handle<Unioned>::T unioned, int i); | |
156 // } | |
157 template<class Config> | |
158 class TypeImpl : public Config::Base { | |
159 public: | 135 public: |
160 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; | 136 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ |
161 typedef typename Config::Region Region; | 137 static Type* type() { return from_bitset(k##type); } |
162 | |
163 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ | |
164 static TypeImpl* type() { return Config::from_bitset(k##type); } \ | |
165 static TypeHandle type(Region* region) { \ | |
166 return Config::from_bitset(k##type, region); \ | |
167 } | |
168 BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) | 138 BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) |
169 #undef DEFINE_TYPE_CONSTRUCTOR | 139 #undef DEFINE_TYPE_CONSTRUCTOR |
170 | 140 |
171 static TypeHandle Class(i::Handle<i::Map> map, Region* region) { | 141 static Type* Class(Handle<i::Map> map) { return from_handle(map); } |
172 return Config::from_class(map, region); | 142 static Type* Constant(Handle<i::HeapObject> value) { |
| 143 return Constant(value, value->GetIsolate()); |
173 } | 144 } |
174 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) { | 145 static Type* Constant(Handle<i::Object> value, Isolate* isolate) { |
175 return Config::from_constant(value, region); | 146 return from_handle(isolate->factory()->NewBox(value)); |
176 } | 147 } |
177 | 148 |
178 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); | 149 static Type* Union(Handle<Type> type1, Handle<Type> type2); |
179 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); | 150 static Type* Intersect(Handle<Type> type1, Handle<Type> type2); |
| 151 static Type* Optional(Handle<Type> type); // type \/ Undefined |
180 | 152 |
181 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { | 153 static Type* Of(Handle<i::Object> value) { |
182 return Config::from_bitset(LubBitset(*value), region); | 154 return from_bitset(LubBitset(*value)); |
183 } | 155 } |
184 | 156 |
185 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } | 157 bool Is(Type* that) { return this == that || SlowIs(that); } |
186 bool Is(TypeHandle that) { return this->Is(*that); } | 158 bool Is(Handle<Type> that) { return this->Is(*that); } |
187 bool Maybe(TypeImpl* that); | 159 bool Maybe(Type* that); |
188 bool Maybe(TypeHandle that) { return this->Maybe(*that); } | 160 bool Maybe(Handle<Type> that) { return this->Maybe(*that); } |
189 | 161 |
190 // State-dependent versions of Of and Is that consider subtyping between | 162 // State-dependent versions of Of and Is that consider subtyping between |
191 // a constant and its map class. | 163 // a constant and its map class. |
192 static TypeHandle OfCurrently(i::Handle<i::Object> value, Region* region); | 164 static Type* OfCurrently(Handle<i::Object> value); |
193 bool IsCurrently(TypeImpl* that); | 165 bool IsCurrently(Type* that); |
194 bool IsCurrently(TypeHandle that) { return this->IsCurrently(*that); } | 166 bool IsCurrently(Handle<Type> that) { return this->IsCurrently(*that); } |
195 | 167 |
196 bool IsClass() { return Config::is_class(this); } | 168 bool IsClass() { return is_class(); } |
197 bool IsConstant() { return Config::is_constant(this); } | 169 bool IsConstant() { return is_constant(); } |
198 i::Handle<i::Map> AsClass() { return Config::as_class(this); } | 170 Handle<i::Map> AsClass() { return as_class(); } |
199 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); } | 171 Handle<i::Object> AsConstant() { return as_constant(); } |
200 | 172 |
201 int NumClasses(); | 173 int NumClasses(); |
202 int NumConstants(); | 174 int NumConstants(); |
203 | 175 |
204 template<class T> | 176 template<class T> |
205 class Iterator { | 177 class Iterator { |
206 public: | 178 public: |
207 bool Done() const { return index_ < 0; } | 179 bool Done() const { return index_ < 0; } |
208 i::Handle<T> Current(); | 180 Handle<T> Current(); |
209 void Advance(); | 181 void Advance(); |
210 | 182 |
211 private: | 183 private: |
212 template<class> friend class TypeImpl; | 184 friend class Type; |
213 | 185 |
214 Iterator() : index_(-1) {} | 186 Iterator() : index_(-1) {} |
215 explicit Iterator(TypeHandle type) : type_(type), index_(-1) { | 187 explicit Iterator(Handle<Type> type) : type_(type), index_(-1) { |
216 Advance(); | 188 Advance(); |
217 } | 189 } |
218 | 190 |
219 inline bool matches(TypeHandle type); | 191 inline bool matches(Handle<Type> type); |
220 inline TypeHandle get_type(); | 192 inline Handle<Type> get_type(); |
221 | 193 |
222 TypeHandle type_; | 194 Handle<Type> type_; |
223 int index_; | 195 int index_; |
224 }; | 196 }; |
225 | 197 |
226 Iterator<i::Map> Classes() { | 198 Iterator<i::Map> Classes() { |
227 if (this->IsBitset()) return Iterator<i::Map>(); | 199 if (this->is_bitset()) return Iterator<i::Map>(); |
228 return Iterator<i::Map>(Config::handle(this)); | 200 return Iterator<i::Map>(this->handle()); |
229 } | 201 } |
230 Iterator<i::Object> Constants() { | 202 Iterator<i::Object> Constants() { |
231 if (this->IsBitset()) return Iterator<i::Object>(); | 203 if (this->is_bitset()) return Iterator<i::Object>(); |
232 return Iterator<i::Object>(Config::handle(this)); | 204 return Iterator<i::Object>(this->handle()); |
233 } | 205 } |
234 | 206 |
235 static TypeImpl* cast(i::Object* object) { | 207 static Type* cast(i::Object* object) { |
236 TypeImpl* t = static_cast<Type*>(object); | 208 Type* t = static_cast<Type*>(object); |
237 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion()); | 209 ASSERT(t->is_bitset() || t->is_class() || |
| 210 t->is_constant() || t->is_union()); |
238 return t; | 211 return t; |
239 } | 212 } |
240 | 213 |
241 #ifdef OBJECT_PRINT | 214 #ifdef OBJECT_PRINT |
242 void TypePrint(); | 215 void TypePrint(); |
243 void TypePrint(FILE* out); | 216 void TypePrint(FILE* out); |
244 #endif | 217 #endif |
245 | 218 |
246 private: | 219 private: |
247 template<class> friend class Iterator; | |
248 | |
249 // A union is a fixed array containing types. Invariants: | 220 // A union is a fixed array containing types. Invariants: |
250 // - its length is at least 2 | 221 // - its length is at least 2 |
251 // - at most one field is a bitset, and it must go into index 0 | 222 // - at most one field is a bitset, and it must go into index 0 |
252 // - no field is a union | 223 // - no field is a union |
253 typedef typename Config::Unioned Unioned; | 224 typedef FixedArray Unioned; |
254 typedef typename Config::template Handle<Unioned>::type UnionedHandle; | |
255 | 225 |
256 enum { | 226 enum { |
257 #define DECLARE_TYPE(type, value) k##type = (value), | 227 #define DECLARE_TYPE(type, value) k##type = (value), |
258 BITSET_TYPE_LIST(DECLARE_TYPE) | 228 BITSET_TYPE_LIST(DECLARE_TYPE) |
259 #undef DECLARE_TYPE | 229 #undef DECLARE_TYPE |
260 kUnusedEOL = 0 | 230 kUnusedEOL = 0 |
261 }; | 231 }; |
262 | 232 |
263 bool IsNone() { return this == None(); } | 233 bool is_none() { return this == None(); } |
264 bool IsAny() { return this == Any(); } | 234 bool is_bitset() { return this->IsSmi(); } |
265 bool IsBitset() { return Config::is_bitset(this); } | 235 bool is_class() { return this->IsMap(); } |
266 bool IsUnion() { return Config::is_union(this); } | 236 bool is_constant() { return this->IsBox(); } |
267 int AsBitset() { return Config::as_bitset(this); } | 237 bool is_union() { return this->IsFixedArray(); } |
268 UnionedHandle AsUnion() { return Config::as_union(this); } | |
269 | 238 |
270 bool SlowIs(TypeImpl* that); | 239 bool SlowIs(Type* that); |
| 240 |
| 241 int as_bitset() { return Smi::cast(this)->value(); } |
| 242 Handle<i::Map> as_class() { return Handle<i::Map>::cast(handle()); } |
| 243 Handle<i::Object> as_constant() { |
| 244 Handle<i::Box> box = Handle<i::Box>::cast(handle()); |
| 245 return i::handle(box->value(), box->GetIsolate()); |
| 246 } |
| 247 Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); } |
| 248 |
| 249 Handle<Type> handle() { return handle_via_isolate_of(this); } |
| 250 Handle<Type> handle_via_isolate_of(Type* type) { |
| 251 ASSERT(type->IsHeapObject()); |
| 252 return i::handle(this, i::HeapObject::cast(type)->GetIsolate()); |
| 253 } |
| 254 |
| 255 static Type* from_bitset(int bitset) { |
| 256 return static_cast<Type*>(i::Object::cast(i::Smi::FromInt(bitset))); |
| 257 } |
| 258 static Type* from_handle(Handle<i::HeapObject> handle) { |
| 259 return static_cast<Type*>(i::Object::cast(*handle)); |
| 260 } |
| 261 |
| 262 static Handle<Type> union_get(Handle<Unioned> unioned, int i) { |
| 263 Type* type = static_cast<Type*>(unioned->get(i)); |
| 264 ASSERT(!type->is_union()); |
| 265 return type->handle_via_isolate_of(from_handle(unioned)); |
| 266 } |
271 | 267 |
272 int LubBitset(); // least upper bound that's a bitset | 268 int LubBitset(); // least upper bound that's a bitset |
273 int GlbBitset(); // greatest lower bound that's a bitset | 269 int GlbBitset(); // greatest lower bound that's a bitset |
274 | 270 |
275 static int LubBitset(i::Object* value); | 271 static int LubBitset(i::Object* value); |
276 static int LubBitset(i::Map* map); | 272 static int LubBitset(i::Map* map); |
277 | 273 |
278 bool InUnion(UnionedHandle unioned, int current_size); | 274 bool InUnion(Handle<Unioned> unioned, int current_size); |
279 int ExtendUnion(UnionedHandle unioned, int current_size); | 275 int ExtendUnion(Handle<Unioned> unioned, int current_size); |
280 int ExtendIntersection( | 276 int ExtendIntersection( |
281 UnionedHandle unioned, TypeHandle type, int current_size); | 277 Handle<Unioned> unioned, Handle<Type> type, int current_size); |
282 | 278 |
283 static const char* bitset_name(int bitset); | 279 static const char* bitset_name(int bitset); |
284 }; | 280 }; |
285 | 281 |
286 | 282 |
287 struct HeapTypeConfig { | 283 // A simple struct to represent a pair of lower/upper type bounds. |
288 typedef TypeImpl<HeapTypeConfig> Type; | 284 struct Bounds { |
289 typedef i::Object Base; | 285 Handle<Type> lower; |
290 typedef i::FixedArray Unioned; | 286 Handle<Type> upper; |
291 typedef i::Isolate Region; | |
292 template<class T> struct Handle { typedef i::Handle<T> type; }; | |
293 | 287 |
294 static i::Handle<Type> handle(Type* type) { | 288 Bounds() {} |
295 return i::handle(type, i::HeapObject::cast(type)->GetIsolate()); | 289 Bounds(Handle<Type> l, Handle<Type> u) : lower(l), upper(u) { |
| 290 ASSERT(lower->Is(upper)); |
296 } | 291 } |
297 | 292 Bounds(Type* l, Type* u, Isolate* isl) : lower(l, isl), upper(u, isl) { |
298 static bool is_bitset(Type* type) { return type->IsSmi(); } | 293 ASSERT(lower->Is(upper)); |
299 static bool is_class(Type* type) { return type->IsMap(); } | |
300 static bool is_constant(Type* type) { return type->IsBox(); } | |
301 static bool is_union(Type* type) { return type->IsFixedArray(); } | |
302 | |
303 static int as_bitset(Type* type) { | |
304 return Smi::cast(type)->value(); | |
305 } | 294 } |
306 static i::Handle<i::Map> as_class(Type* type) { | 295 explicit Bounds(Handle<Type> t) : lower(t), upper(t) { |
307 return i::handle(i::Map::cast(type)); | 296 ASSERT(lower->Is(upper)); |
308 } | 297 } |
309 static i::Handle<i::Object> as_constant(Type* type) { | 298 Bounds(Type* t, Isolate* isl) : lower(t, isl), upper(t, isl) { |
310 i::Box* box = i::Box::cast(type); | |
311 return i::handle(box->value(), box->GetIsolate()); | |
312 } | |
313 static i::Handle<Unioned> as_union(Type* type) { | |
314 return i::handle(i::FixedArray::cast(type)); | |
315 } | |
316 | |
317 static Type* from_bitset(int bitset) { | |
318 return Type::cast(i::Smi::FromInt(bitset)); | |
319 } | |
320 static i::Handle<Type> from_bitset(int bitset, Isolate* isolate) { | |
321 return i::handle(from_bitset(bitset), isolate); | |
322 } | |
323 static i::Handle<Type> from_class(i::Handle<i::Map> map, Isolate* isolate) { | |
324 return i::Handle<Type>::cast(i::Handle<Object>::cast(map)); | |
325 } | |
326 static i::Handle<Type> from_constant( | |
327 i::Handle<i::Object> value, Isolate* isolate) { | |
328 ASSERT(isolate || value->IsHeapObject()); | |
329 if (!isolate) isolate = i::HeapObject::cast(*value)->GetIsolate(); | |
330 i::Handle<Box> box = isolate->factory()->NewBox(value); | |
331 return i::Handle<Type>::cast(i::Handle<Object>::cast(box)); | |
332 } | |
333 static i::Handle<Type> from_union(i::Handle<Unioned> unioned) { | |
334 return i::Handle<Type>::cast(i::Handle<Object>::cast(unioned)); | |
335 } | |
336 | |
337 static i::Handle<Unioned> union_create(int size, Isolate* isolate) { | |
338 return isolate->factory()->NewFixedArray(size); | |
339 } | |
340 static i::Handle<Type> union_get(i::Handle<Unioned> unioned, int i) { | |
341 Type* type = static_cast<Type*>(unioned->get(i)); | |
342 ASSERT(!is_union(type)); | |
343 return i::handle(type, unioned->GetIsolate()); | |
344 } | |
345 }; | |
346 | |
347 typedef TypeImpl<HeapTypeConfig> Type; | |
348 | |
349 | |
350 // A simple struct to represent a pair of lower/upper type bounds. | |
351 template<class Config> | |
352 struct BoundsImpl { | |
353 typedef TypeImpl<Config> Type; | |
354 typedef typename Type::TypeHandle TypeHandle; | |
355 typedef typename Type::Region Region; | |
356 | |
357 TypeHandle lower; | |
358 TypeHandle upper; | |
359 | |
360 BoundsImpl() {} | |
361 explicit BoundsImpl(TypeHandle t) : lower(t), upper(t) {} | |
362 BoundsImpl(TypeHandle l, TypeHandle u) : lower(l), upper(u) { | |
363 ASSERT(lower->Is(upper)); | 299 ASSERT(lower->Is(upper)); |
364 } | 300 } |
365 | 301 |
366 // Unrestricted bounds. | 302 // Unrestricted bounds. |
367 static BoundsImpl Unbounded(Region* region) { | 303 static Bounds Unbounded(Isolate* isl) { |
368 return BoundsImpl(Type::None(region), Type::Any(region)); | 304 return Bounds(Type::None(), Type::Any(), isl); |
369 } | 305 } |
370 | 306 |
371 // Meet: both b1 and b2 are known to hold. | 307 // Meet: both b1 and b2 are known to hold. |
372 static BoundsImpl Both(BoundsImpl b1, BoundsImpl b2, Region* region) { | 308 static Bounds Both(Bounds b1, Bounds b2, Isolate* isl) { |
373 TypeHandle lower = Type::Union(b1.lower, b2.lower, region); | 309 Handle<Type> lower(Type::Union(b1.lower, b2.lower), isl); |
374 TypeHandle upper = Type::Intersect(b1.upper, b2.upper, region); | 310 Handle<Type> upper(Type::Intersect(b1.upper, b2.upper), isl); |
375 // Lower bounds are considered approximate, correct as necessary. | 311 // Lower bounds are considered approximate, correct as necessary. |
376 lower = Type::Intersect(lower, upper, region); | 312 lower = handle(Type::Intersect(lower, upper), isl); |
377 return BoundsImpl(lower, upper); | 313 return Bounds(lower, upper); |
378 } | 314 } |
379 | 315 |
380 // Join: either b1 or b2 is known to hold. | 316 // Join: either b1 or b2 is known to hold. |
381 static BoundsImpl Either(BoundsImpl b1, BoundsImpl b2, Region* region) { | 317 static Bounds Either(Bounds b1, Bounds b2, Isolate* isl) { |
382 TypeHandle lower = Type::Intersect(b1.lower, b2.lower, region); | 318 return Bounds( |
383 TypeHandle upper = Type::Union(b1.upper, b2.upper, region); | 319 handle(Type::Intersect(b1.lower, b2.lower), isl), |
384 return BoundsImpl(lower, upper); | 320 handle(Type::Union(b1.upper, b2.upper), isl)); |
385 } | 321 } |
386 | 322 |
387 static BoundsImpl NarrowLower(BoundsImpl b, TypeHandle t, Region* region) { | 323 static Bounds NarrowLower(Bounds b, Handle<Type> t, Isolate* isl) { |
388 // Lower bounds are considered approximate, correct as necessary. | 324 // Lower bounds are considered approximate, correct as necessary. |
389 t = Type::Intersect(t, b.upper, region); | 325 t = handle(Type::Intersect(t, b.upper), isl); |
390 TypeHandle lower = Type::Union(b.lower, t, region); | 326 return Bounds(handle(Type::Union(b.lower, t), isl), b.upper); |
391 return BoundsImpl(lower, b.upper); | |
392 } | 327 } |
393 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) { | 328 static Bounds NarrowUpper(Bounds b, Handle<Type> t, Isolate* isl) { |
394 TypeHandle lower = Type::Intersect(b.lower, t, region); | 329 return Bounds( |
395 TypeHandle upper = Type::Intersect(b.upper, t, region); | 330 handle(Type::Intersect(b.lower, t), isl), |
396 return BoundsImpl(lower, upper); | 331 handle(Type::Intersect(b.upper, t), isl)); |
397 } | 332 } |
398 }; | 333 }; |
399 | 334 |
400 typedef BoundsImpl<HeapTypeConfig> Bounds; | |
401 | |
402 | |
403 } } // namespace v8::internal | 335 } } // namespace v8::internal |
404 | 336 |
405 #endif // V8_TYPES_H_ | 337 #endif // V8_TYPES_H_ |
OLD | NEW |