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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 // struct Config { |
135 // typedef Base; | 135 // typedef Base; |
136 // typedef Unioned; | 136 // typedef Unioned; |
137 // typedef Region; | 137 // typedef Region; |
138 // template<class> struct Handle { typedef type; } // No template typedefs... | 138 // template<class> struct Handle { typedef type; } // No template typedefs... |
139 // static Handle<Type>::type handle(Type* type); // !is_bitset(type) | 139 // static Handle<Type>::type handle(Type* type); // !is_bitset(type) |
140 // static bool is_bitset(Type* type); | 140 // static bool is_bitset(Type*); |
141 // static bool is_class(Type* type); | 141 // static bool is_class(Type*); |
142 // static bool is_constant(Type* type); | 142 // static bool is_constant(Type*); |
143 // static bool is_union(Type* type); | 143 // static bool is_union(Type*); |
144 // static int as_bitset(Type* type); | 144 // static int as_bitset(Type*); |
145 // static i::Handle<i::Map> as_class(Type* type); | 145 // static i::Handle<i::Map> as_class(Type*); |
146 // static i::Handle<i::Object> as_constant(Type* type); | 146 // static i::Handle<i::Object> as_constant(Type*); |
147 // static Handle<Unioned>::type as_union(Type* type); | 147 // static Handle<Unioned>::type as_union(Type*); |
148 // static Type* from_bitset(int bitset); | 148 // static Type* from_bitset(int bitset); |
149 // static Handle<Type>::type from_bitset(int bitset, Region* region); | 149 // static Handle<Type>::type from_bitset(int bitset, Region*); |
150 // static Handle<Type>::type from_class(i::Handle<i::Map> map, Region* region) | 150 // static Handle<Type>::type from_class(i::Handle<i::Map>, Region*) |
151 // static Handle<Type>::type from_constant( | 151 // static Handle<Type>::type from_constant(i::Handle<i::Object>, Region*); |
152 // i::Handle<i::Object> value, Region* region); | 152 // static Handle<Type>::type from_union(Handle<Unioned>::type); |
153 // static Handle<Type>::type from_union(Handle<Unioned>::T unioned); | 153 // static Handle<Unioned>::type union_create(int size, Region*); |
154 // static Handle<Unioned>::type union_create(int size, Region* region); | 154 // static void union_shrink(Handle<Unioned>::type, int size); |
155 // static Handle<Type>::type union_get(Handle<Unioned>::T unioned, int i); | 155 // static Handle<Type>::type union_get(Handle<Unioned>::type, int); |
| 156 // static void union_set(Handle<Unioned>::type, int, Handle<Type>::type); |
| 157 // static int union_length(Handle<Unioned>::type); |
156 // } | 158 // } |
157 template<class Config> | 159 template<class Config> |
158 class TypeImpl : public Config::Base { | 160 class TypeImpl : public Config::Base { |
159 public: | 161 public: |
160 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; | 162 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; |
161 typedef typename Config::Region Region; | 163 typedef typename Config::Region Region; |
162 | 164 |
163 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ | 165 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ |
164 static TypeImpl* type() { return Config::from_bitset(k##type); } \ | 166 static TypeImpl* type() { return Config::from_bitset(k##type); } \ |
165 static TypeHandle type(Region* region) { \ | 167 static TypeHandle type(Region* region) { \ |
(...skipping 10 matching lines...) Expand all Loading... |
176 } | 178 } |
177 | 179 |
178 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); | 180 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); |
179 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); | 181 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); |
180 | 182 |
181 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { | 183 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { |
182 return Config::from_bitset(LubBitset(*value), region); | 184 return Config::from_bitset(LubBitset(*value), region); |
183 } | 185 } |
184 | 186 |
185 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } | 187 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } |
| 188 template<class TypeHandle> |
186 bool Is(TypeHandle that) { return this->Is(*that); } | 189 bool Is(TypeHandle that) { return this->Is(*that); } |
187 bool Maybe(TypeImpl* that); | 190 bool Maybe(TypeImpl* that); |
| 191 template<class TypeHandle> |
188 bool Maybe(TypeHandle that) { return this->Maybe(*that); } | 192 bool Maybe(TypeHandle that) { return this->Maybe(*that); } |
189 | 193 |
190 // State-dependent versions of Of and Is that consider subtyping between | 194 // State-dependent versions of Of and Is that consider subtyping between |
191 // a constant and its map class. | 195 // a constant and its map class. |
192 static TypeHandle OfCurrently(i::Handle<i::Object> value, Region* region); | 196 static TypeHandle OfCurrently(i::Handle<i::Object> value, Region* region); |
193 bool IsCurrently(TypeImpl* that); | 197 bool IsCurrently(TypeImpl* that); |
| 198 template<class TypeHandle> |
194 bool IsCurrently(TypeHandle that) { return this->IsCurrently(*that); } | 199 bool IsCurrently(TypeHandle that) { return this->IsCurrently(*that); } |
195 | 200 |
196 bool IsClass() { return Config::is_class(this); } | 201 bool IsClass() { return Config::is_class(this); } |
197 bool IsConstant() { return Config::is_constant(this); } | 202 bool IsConstant() { return Config::is_constant(this); } |
198 i::Handle<i::Map> AsClass() { return Config::as_class(this); } | 203 i::Handle<i::Map> AsClass() { return Config::as_class(this); } |
199 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); } | 204 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); } |
200 | 205 |
201 int NumClasses(); | 206 int NumClasses(); |
202 int NumConstants(); | 207 int NumConstants(); |
203 | 208 |
(...skipping 21 matching lines...) Expand all Loading... |
225 | 230 |
226 Iterator<i::Map> Classes() { | 231 Iterator<i::Map> Classes() { |
227 if (this->IsBitset()) return Iterator<i::Map>(); | 232 if (this->IsBitset()) return Iterator<i::Map>(); |
228 return Iterator<i::Map>(Config::handle(this)); | 233 return Iterator<i::Map>(Config::handle(this)); |
229 } | 234 } |
230 Iterator<i::Object> Constants() { | 235 Iterator<i::Object> Constants() { |
231 if (this->IsBitset()) return Iterator<i::Object>(); | 236 if (this->IsBitset()) return Iterator<i::Object>(); |
232 return Iterator<i::Object>(Config::handle(this)); | 237 return Iterator<i::Object>(Config::handle(this)); |
233 } | 238 } |
234 | 239 |
235 static TypeImpl* cast(i::Object* object) { | 240 static TypeImpl* cast(typename Config::Base* object) { |
236 TypeImpl* t = static_cast<TypeImpl*>(object); | 241 TypeImpl* t = static_cast<TypeImpl*>(object); |
237 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion()); | 242 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion()); |
238 return t; | 243 return t; |
239 } | 244 } |
240 | 245 |
241 #ifdef OBJECT_PRINT | 246 #ifdef OBJECT_PRINT |
242 void TypePrint(); | 247 void TypePrint(); |
243 void TypePrint(FILE* out); | 248 void TypePrint(FILE* out); |
244 #endif | 249 #endif |
245 | 250 |
(...skipping 23 matching lines...) Expand all Loading... |
269 | 274 |
270 bool SlowIs(TypeImpl* that); | 275 bool SlowIs(TypeImpl* that); |
271 | 276 |
272 int LubBitset(); // least upper bound that's a bitset | 277 int LubBitset(); // least upper bound that's a bitset |
273 int GlbBitset(); // greatest lower bound that's a bitset | 278 int GlbBitset(); // greatest lower bound that's a bitset |
274 | 279 |
275 static int LubBitset(i::Object* value); | 280 static int LubBitset(i::Object* value); |
276 static int LubBitset(i::Map* map); | 281 static int LubBitset(i::Map* map); |
277 | 282 |
278 bool InUnion(UnionedHandle unioned, int current_size); | 283 bool InUnion(UnionedHandle unioned, int current_size); |
279 int ExtendUnion(UnionedHandle unioned, int current_size); | 284 static int ExtendUnion( |
280 int ExtendIntersection( | 285 UnionedHandle unioned, TypeHandle t, int current_size); |
281 UnionedHandle unioned, TypeHandle type, int current_size); | 286 static int ExtendIntersection( |
| 287 UnionedHandle unioned, TypeHandle t, TypeHandle other, int current_size); |
282 | 288 |
283 #ifdef OBJECT_PRINT | 289 #ifdef OBJECT_PRINT |
284 static const char* bitset_name(int bitset); | 290 static const char* bitset_name(int bitset); |
285 #endif | 291 #endif |
286 }; | 292 }; |
287 | 293 |
288 | 294 |
| 295 // Zone-allocated types are either (odd) integers to represent bitsets, or |
| 296 // (even) pointers to zone lists for everything else. The first slot of every |
| 297 // list is an explicit tag value to distinguish representation. |
| 298 struct ZoneTypeConfig { |
| 299 private: |
| 300 typedef i::ZoneList<void*> Tagged; |
| 301 |
| 302 enum Tag { |
| 303 kClassTag, |
| 304 kConstantTag, |
| 305 kUnionTag |
| 306 }; |
| 307 |
| 308 static Tagged* tagged_create(Tag tag, int size, Zone* zone) { |
| 309 Tagged* tagged = new(zone) Tagged(size + 1, zone); |
| 310 tagged->Add(reinterpret_cast<void*>(tag), zone); |
| 311 tagged->AddBlock(NULL, size, zone); |
| 312 return tagged; |
| 313 } |
| 314 static void tagged_shrink(Tagged* tagged, int size) { |
| 315 tagged->Rewind(size + 1); |
| 316 } |
| 317 static Tag tagged_tag(Tagged* tagged) { |
| 318 return static_cast<Tag>(reinterpret_cast<intptr_t>(tagged->at(0))); |
| 319 } |
| 320 template<class T> |
| 321 static T tagged_get(Tagged* tagged, int i) { |
| 322 return reinterpret_cast<T>(tagged->at(i + 1)); |
| 323 } |
| 324 template<class T> |
| 325 static void tagged_set(Tagged* tagged, int i, T value) { |
| 326 tagged->at(i + 1) = reinterpret_cast<T>(value); |
| 327 } |
| 328 static int tagged_length(Tagged* tagged) { |
| 329 return tagged->length() - 1; |
| 330 } |
| 331 |
| 332 public: |
| 333 typedef TypeImpl<ZoneTypeConfig> Type; |
| 334 class Base {}; |
| 335 typedef i::ZoneList<Type*> Unioned; |
| 336 typedef i::Zone Region; |
| 337 template<class T> struct Handle { typedef T* type; }; |
| 338 |
| 339 static Type* handle(Type* type) { return type; } |
| 340 |
| 341 static bool is(Type* type, Tag tag) { |
| 342 return is_tagged(type) && tagged_tag(as_tagged(type)) == tag; |
| 343 } |
| 344 |
| 345 static bool is_bitset(Type* type) { |
| 346 return reinterpret_cast<intptr_t>(type) & 1; |
| 347 } |
| 348 static bool is_tagged(Type* type) { return !is_bitset(type); } |
| 349 static bool is_class(Type* type) { return is(type, kClassTag); } |
| 350 static bool is_constant(Type* type) { return is(type, kConstantTag); } |
| 351 static bool is_union(Type* type) { return is(type, kUnionTag); } |
| 352 static bool tagged_is_union(Tagged* tagged) { |
| 353 return is(from_tagged(tagged), kUnionTag); |
| 354 } |
| 355 |
| 356 static int as_bitset(Type* type) { |
| 357 ASSERT(is_bitset(type)); |
| 358 return reinterpret_cast<intptr_t>(type) >> 1; |
| 359 } |
| 360 static Tagged* as_tagged(Type* type) { |
| 361 ASSERT(is_tagged(type)); |
| 362 return reinterpret_cast<Tagged*>(type); |
| 363 } |
| 364 static i::Handle<i::Map> as_class(Type* type) { |
| 365 ASSERT(is_class(type)); |
| 366 return i::Handle<i::Map>(tagged_get<i::Map**>(as_tagged(type), 0)); |
| 367 } |
| 368 static i::Handle<i::Object> as_constant(Type* type) { |
| 369 ASSERT(is_constant(type)); |
| 370 return i::Handle<i::Object>(tagged_get<i::Object**>(as_tagged(type), 0)); |
| 371 } |
| 372 static Unioned* as_union(Type* type) { |
| 373 ASSERT(is_union(type)); |
| 374 return tagged_as_union(as_tagged(type)); |
| 375 } |
| 376 static Unioned* tagged_as_union(Tagged* tagged) { |
| 377 ASSERT(tagged_is_union(tagged)); |
| 378 return reinterpret_cast<Unioned*>(tagged); |
| 379 } |
| 380 |
| 381 static Type* from_bitset(int bitset) { |
| 382 return reinterpret_cast<Type*>((bitset << 1) | 1); |
| 383 } |
| 384 static Type* from_bitset(int bitset, Zone* Zone) { |
| 385 return from_bitset(bitset); |
| 386 } |
| 387 static Type* from_tagged(Tagged* tagged) { |
| 388 return reinterpret_cast<Type*>(tagged); |
| 389 } |
| 390 static Type* from_class(i::Handle<i::Map> map, Zone* zone) { |
| 391 Tagged* tagged = tagged_create(kClassTag, 1, zone); |
| 392 tagged_set(tagged, 0, map.location()); |
| 393 return from_tagged(tagged); |
| 394 } |
| 395 static Type* from_constant(i::Handle<i::Object> value, Zone* zone) { |
| 396 Tagged* tagged = tagged_create(kConstantTag, 1, zone); |
| 397 tagged_set(tagged, 0, value.location()); |
| 398 return from_tagged(tagged); |
| 399 } |
| 400 static Type* from_union(Unioned* unioned) { |
| 401 return from_tagged(tagged_from_union(unioned)); |
| 402 } |
| 403 static Tagged* tagged_from_union(Unioned* unioned) { |
| 404 return reinterpret_cast<Tagged*>(unioned); |
| 405 } |
| 406 |
| 407 static Unioned* union_create(int size, Zone* zone) { |
| 408 return tagged_as_union(tagged_create(kUnionTag, size, zone)); |
| 409 } |
| 410 static void union_shrink(Unioned* unioned, int size) { |
| 411 tagged_shrink(tagged_from_union(unioned), size); |
| 412 } |
| 413 static Type* union_get(Unioned* unioned, int i) { |
| 414 Type* type = tagged_get<Type*>(tagged_from_union(unioned), i); |
| 415 ASSERT(!is_union(type)); |
| 416 return type; |
| 417 } |
| 418 static void union_set(Unioned* unioned, int i, Type* type) { |
| 419 ASSERT(!is_union(type)); |
| 420 tagged_set(tagged_from_union(unioned), i, type); |
| 421 } |
| 422 static int union_length(Unioned* unioned) { |
| 423 return tagged_length(tagged_from_union(unioned)); |
| 424 } |
| 425 }; |
| 426 |
| 427 |
| 428 // Heap-allocated types are either smis for bitsets, maps for classes, boxes for |
| 429 // constants, or fixed arrays for unions. |
289 struct HeapTypeConfig { | 430 struct HeapTypeConfig { |
290 typedef TypeImpl<HeapTypeConfig> Type; | 431 typedef TypeImpl<HeapTypeConfig> Type; |
291 typedef i::Object Base; | 432 typedef i::Object Base; |
292 typedef i::FixedArray Unioned; | 433 typedef i::FixedArray Unioned; |
293 typedef i::Isolate Region; | 434 typedef i::Isolate Region; |
294 template<class T> struct Handle { typedef i::Handle<T> type; }; | 435 template<class T> struct Handle { typedef i::Handle<T> type; }; |
295 | 436 |
296 static i::Handle<Type> handle(Type* type) { | 437 static i::Handle<Type> handle(Type* type) { |
297 return i::handle(type, i::HeapObject::cast(type)->GetIsolate()); | 438 return i::handle(type, i::HeapObject::cast(type)->GetIsolate()); |
298 } | 439 } |
(...skipping 21 matching lines...) Expand all Loading... |
320 return Type::cast(i::Smi::FromInt(bitset)); | 461 return Type::cast(i::Smi::FromInt(bitset)); |
321 } | 462 } |
322 static i::Handle<Type> from_bitset(int bitset, Isolate* isolate) { | 463 static i::Handle<Type> from_bitset(int bitset, Isolate* isolate) { |
323 return i::handle(from_bitset(bitset), isolate); | 464 return i::handle(from_bitset(bitset), isolate); |
324 } | 465 } |
325 static i::Handle<Type> from_class(i::Handle<i::Map> map, Isolate* isolate) { | 466 static i::Handle<Type> from_class(i::Handle<i::Map> map, Isolate* isolate) { |
326 return i::Handle<Type>::cast(i::Handle<Object>::cast(map)); | 467 return i::Handle<Type>::cast(i::Handle<Object>::cast(map)); |
327 } | 468 } |
328 static i::Handle<Type> from_constant( | 469 static i::Handle<Type> from_constant( |
329 i::Handle<i::Object> value, Isolate* isolate) { | 470 i::Handle<i::Object> value, Isolate* isolate) { |
330 ASSERT(isolate || value->IsHeapObject()); | |
331 if (!isolate) isolate = i::HeapObject::cast(*value)->GetIsolate(); | |
332 i::Handle<Box> box = isolate->factory()->NewBox(value); | 471 i::Handle<Box> box = isolate->factory()->NewBox(value); |
333 return i::Handle<Type>::cast(i::Handle<Object>::cast(box)); | 472 return i::Handle<Type>::cast(i::Handle<Object>::cast(box)); |
334 } | 473 } |
335 static i::Handle<Type> from_union(i::Handle<Unioned> unioned) { | 474 static i::Handle<Type> from_union(i::Handle<Unioned> unioned) { |
336 return i::Handle<Type>::cast(i::Handle<Object>::cast(unioned)); | 475 return i::Handle<Type>::cast(i::Handle<Object>::cast(unioned)); |
337 } | 476 } |
338 | 477 |
339 static i::Handle<Unioned> union_create(int size, Isolate* isolate) { | 478 static i::Handle<Unioned> union_create(int size, Isolate* isolate) { |
340 return isolate->factory()->NewFixedArray(size); | 479 return isolate->factory()->NewFixedArray(size); |
341 } | 480 } |
| 481 static void union_shrink(i::Handle<Unioned> unioned, int size) { |
| 482 unioned->Shrink(size); |
| 483 } |
342 static i::Handle<Type> union_get(i::Handle<Unioned> unioned, int i) { | 484 static i::Handle<Type> union_get(i::Handle<Unioned> unioned, int i) { |
343 Type* type = static_cast<Type*>(unioned->get(i)); | 485 Type* type = static_cast<Type*>(unioned->get(i)); |
344 ASSERT(!is_union(type)); | 486 ASSERT(!is_union(type)); |
345 return i::handle(type, unioned->GetIsolate()); | 487 return i::handle(type, unioned->GetIsolate()); |
346 } | 488 } |
| 489 static void union_set( |
| 490 i::Handle<Unioned> unioned, int i, i::Handle<Type> type) { |
| 491 ASSERT(!is_union(*type)); |
| 492 unioned->set(i, *type); |
| 493 } |
| 494 static int union_length(i::Handle<Unioned> unioned) { |
| 495 return unioned->length(); |
| 496 } |
347 }; | 497 }; |
348 | 498 |
| 499 typedef TypeImpl<ZoneTypeConfig> ZoneType; |
349 typedef TypeImpl<HeapTypeConfig> Type; | 500 typedef TypeImpl<HeapTypeConfig> Type; |
350 | 501 |
351 | 502 |
352 // A simple struct to represent a pair of lower/upper type bounds. | 503 // A simple struct to represent a pair of lower/upper type bounds. |
353 template<class Config> | 504 template<class Config> |
354 struct BoundsImpl { | 505 struct BoundsImpl { |
355 typedef TypeImpl<Config> Type; | 506 typedef TypeImpl<Config> Type; |
356 typedef typename Type::TypeHandle TypeHandle; | 507 typedef typename Type::TypeHandle TypeHandle; |
357 typedef typename Type::Region Region; | 508 typedef typename Type::Region Region; |
358 | 509 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 TypeHandle lower = Type::Union(b.lower, t, region); | 543 TypeHandle lower = Type::Union(b.lower, t, region); |
393 return BoundsImpl(lower, b.upper); | 544 return BoundsImpl(lower, b.upper); |
394 } | 545 } |
395 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) { | 546 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) { |
396 TypeHandle lower = Type::Intersect(b.lower, t, region); | 547 TypeHandle lower = Type::Intersect(b.lower, t, region); |
397 TypeHandle upper = Type::Intersect(b.upper, t, region); | 548 TypeHandle upper = Type::Intersect(b.upper, t, region); |
398 return BoundsImpl(lower, upper); | 549 return BoundsImpl(lower, upper); |
399 } | 550 } |
400 }; | 551 }; |
401 | 552 |
| 553 typedef BoundsImpl<ZoneTypeConfig> ZoneBounds; |
402 typedef BoundsImpl<HeapTypeConfig> Bounds; | 554 typedef BoundsImpl<HeapTypeConfig> Bounds; |
403 | 555 |
404 | 556 |
405 } } // namespace v8::internal | 557 } } // namespace v8::internal |
406 | 558 |
407 #endif // V8_TYPES_H_ | 559 #endif // V8_TYPES_H_ |
OLD | NEW |