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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 Handle<Unioned> unioned = this->as_union(); | 236 Handle<Unioned> unioned = this->as_union(); |
237 for (int i = 0; i < unioned->length(); ++i) { | 237 for (int i = 0; i < unioned->length(); ++i) { |
238 Handle<Type> this_i = union_get(unioned, i); | 238 Handle<Type> this_i = union_get(unioned, i); |
239 if (!this_i->Is(that)) return false; | 239 if (!this_i->Is(that)) return false; |
240 } | 240 } |
241 return true; | 241 return true; |
242 } | 242 } |
243 | 243 |
244 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn) | 244 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn) |
245 // (iff T is not a union) | 245 // (iff T is not a union) |
| 246 ASSERT(!this->is_union()); |
246 if (that->is_union()) { | 247 if (that->is_union()) { |
247 Handle<Unioned> unioned = that->as_union(); | 248 Handle<Unioned> unioned = that->as_union(); |
248 for (int i = 0; i < unioned->length(); ++i) { | 249 for (int i = 0; i < unioned->length(); ++i) { |
249 Handle<Type> that_i = union_get(unioned, i); | 250 Handle<Type> that_i = union_get(unioned, i); |
250 if (this->Is(that_i)) return true; | 251 if (this->Is(that_i)) return true; |
251 if (this->is_bitset()) break; // Fast fail, no other field is a bitset. | 252 if (this->is_bitset()) break; // Fast fail, no other field is a bitset. |
252 } | 253 } |
253 return false; | 254 return false; |
254 } | 255 } |
255 | 256 |
256 return false; | 257 return false; |
257 } | 258 } |
258 | 259 |
259 | 260 |
260 // Check this overlaps that. | 261 // Check this overlaps that. |
261 bool Type::Maybe(Type* that) { | 262 bool Type::Maybe(Type* that) { |
262 // Fast path for bitsets. | 263 // Fast path for bitsets. |
263 if (this->is_bitset()) { | 264 if (this->is_bitset()) { |
264 return (this->as_bitset() & that->LubBitset()) != 0; | 265 return (this->as_bitset() & that->LubBitset()) != 0; |
265 } | 266 } |
266 if (that->is_bitset()) { | 267 if (that->is_bitset()) { |
267 return (this->LubBitset() & that->as_bitset()) != 0; | 268 return (this->LubBitset() & that->as_bitset()) != 0; |
268 } | 269 } |
269 | 270 |
270 if (this->is_class()) { | |
271 return that->is_class() && *this->as_class() == *that->as_class(); | |
272 } | |
273 if (this->is_constant()) { | |
274 return that->is_constant() && *this->as_constant() == *that->as_constant(); | |
275 } | |
276 | |
277 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T) | 271 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T) |
278 if (this->is_union()) { | 272 if (this->is_union()) { |
279 Handle<Unioned> unioned = this->as_union(); | 273 Handle<Unioned> unioned = this->as_union(); |
280 for (int i = 0; i < unioned->length(); ++i) { | 274 for (int i = 0; i < unioned->length(); ++i) { |
281 Handle<Type> this_i = union_get(unioned, i); | 275 Handle<Type> this_i = union_get(unioned, i); |
282 if (this_i->Maybe(that)) return true; | 276 if (this_i->Maybe(that)) return true; |
283 } | 277 } |
284 return false; | 278 return false; |
285 } | 279 } |
286 | 280 |
287 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn) | 281 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn) |
288 if (that->is_union()) { | 282 if (that->is_union()) { |
289 Handle<Unioned> unioned = that->as_union(); | 283 Handle<Unioned> unioned = that->as_union(); |
290 for (int i = 0; i < unioned->length(); ++i) { | 284 for (int i = 0; i < unioned->length(); ++i) { |
291 Handle<Type> that_i = union_get(unioned, i); | 285 Handle<Type> that_i = union_get(unioned, i); |
292 if (this->Maybe(that_i)) return true; | 286 if (this->Maybe(that_i)) return true; |
293 } | 287 } |
294 return false; | 288 return false; |
295 } | 289 } |
296 | 290 |
| 291 ASSERT(!that->is_union()); |
| 292 if (this->is_class()) { |
| 293 return that->is_class() && *this->as_class() == *that->as_class(); |
| 294 } |
| 295 if (this->is_constant()) { |
| 296 return that->is_constant() && *this->as_constant() == *that->as_constant(); |
| 297 } |
| 298 |
297 return false; | 299 return false; |
298 } | 300 } |
299 | 301 |
300 | 302 |
301 bool Type::InUnion(Handle<Unioned> unioned, int current_size) { | 303 bool Type::InUnion(Handle<Unioned> unioned, int current_size) { |
302 ASSERT(!this->is_union()); | 304 ASSERT(!this->is_union()); |
303 for (int i = 0; i < current_size; ++i) { | 305 for (int i = 0; i < current_size; ++i) { |
304 Handle<Type> type = union_get(unioned, i); | 306 Handle<Type> type = union_get(unioned, i); |
305 if (type->is_bitset() ? this->Is(type) : this == *type) return true; | 307 if (this->Is(type)) return true; |
306 } | 308 } |
307 return false; | 309 return false; |
308 } | 310 } |
309 | 311 |
310 // Get non-bitsets from this which are not subsumed by that, store at unioned, | 312 // Get non-bitsets from this which are not subsumed by union, store at unioned, |
311 // starting at index. Returns updated index. | 313 // starting at index. Returns updated index. |
312 int Type::ExtendUnion(Handle<Unioned> result, int current_size) { | 314 int Type::ExtendUnion(Handle<Unioned> result, int current_size) { |
313 int old_size = current_size; | 315 int old_size = current_size; |
314 if (this->is_class() || this->is_constant()) { | 316 if (this->is_class() || this->is_constant()) { |
315 if (!this->InUnion(result, old_size)) result->set(current_size++, this); | 317 if (!this->InUnion(result, old_size)) result->set(current_size++, this); |
316 } else if (this->is_union()) { | 318 } else if (this->is_union()) { |
317 Handle<Unioned> unioned = this->as_union(); | 319 Handle<Unioned> unioned = this->as_union(); |
318 for (int i = 0; i < unioned->length(); ++i) { | 320 for (int i = 0; i < unioned->length(); ++i) { |
319 Handle<Type> type = union_get(unioned, i); | 321 Handle<Type> type = union_get(unioned, i); |
320 ASSERT(i == 0 || !(type->is_bitset() || type->Is(union_get(unioned, 0)))); | 322 ASSERT(i == 0 || !(type->is_bitset() || type->Is(union_get(unioned, 0)))); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 return from_handle(unioned); | 369 return from_handle(unioned); |
368 } | 370 } |
369 | 371 |
370 // There was an overlap. Copy to smaller union. | 372 // There was an overlap. Copy to smaller union. |
371 Handle<Unioned> result = isolate->factory()->NewFixedArray(size); | 373 Handle<Unioned> result = isolate->factory()->NewFixedArray(size); |
372 for (int i = 0; i < size; ++i) result->set(i, unioned->get(i)); | 374 for (int i = 0; i < size; ++i) result->set(i, unioned->get(i)); |
373 return from_handle(result); | 375 return from_handle(result); |
374 } | 376 } |
375 | 377 |
376 | 378 |
| 379 // Get non-bitsets from this which are also in that, store at unioned, |
| 380 // starting at index. Returns updated index. |
| 381 int Type::ExtendIntersection( |
| 382 Handle<Unioned> result, Handle<Type> that, int current_size) { |
| 383 int old_size = current_size; |
| 384 if (this->is_class() || this->is_constant()) { |
| 385 if (this->Is(that) && !this->InUnion(result, old_size)) |
| 386 result->set(current_size++, this); |
| 387 } else if (this->is_union()) { |
| 388 Handle<Unioned> unioned = this->as_union(); |
| 389 for (int i = 0; i < unioned->length(); ++i) { |
| 390 Handle<Type> type = union_get(unioned, i); |
| 391 ASSERT(i == 0 || !(type->is_bitset() || type->Is(union_get(unioned, 0)))); |
| 392 if (type->is_bitset()) continue; |
| 393 if (type->Is(that) && !type->InUnion(result, old_size)) |
| 394 result->set(current_size++, *type); |
| 395 } |
| 396 } |
| 397 return current_size; |
| 398 } |
| 399 |
| 400 |
| 401 // Intersection is O(1) on simple bit unions, but O(n*m) on structured unions. |
| 402 // TODO(rossberg): Should we use object sets somehow? Is it worth it? |
| 403 Type* Type::Intersect(Handle<Type> type1, Handle<Type> type2) { |
| 404 // Fast case: bit sets. |
| 405 if (type1->is_bitset() && type2->is_bitset()) { |
| 406 return from_bitset(type1->as_bitset() & type2->as_bitset()); |
| 407 } |
| 408 |
| 409 // Semi-fast case: Unioned objects are neither involved nor produced. |
| 410 if (!(type1->is_union() || type2->is_union())) { |
| 411 if (type1->Is(type2)) return *type1; |
| 412 if (type2->Is(type1)) return *type2; |
| 413 } |
| 414 |
| 415 // Slow case: may need to produce a Unioned object. |
| 416 Isolate* isolate = NULL; |
| 417 int size = 0; |
| 418 if (!type1->is_bitset()) { |
| 419 isolate = HeapObject::cast(*type1)->GetIsolate(); |
| 420 size = (type1->is_union() ? type1->as_union()->length() : 2); |
| 421 } |
| 422 if (!type2->is_bitset()) { |
| 423 isolate = HeapObject::cast(*type2)->GetIsolate(); |
| 424 int size2 = (type2->is_union() ? type2->as_union()->length() : 2); |
| 425 size = (size == 0 ? size2 : Min(size, size2)); |
| 426 } |
| 427 ASSERT(isolate != NULL); |
| 428 ASSERT(size >= 2); |
| 429 Handle<Unioned> unioned = isolate->factory()->NewFixedArray(size); |
| 430 size = 0; |
| 431 |
| 432 int bitset = type1->GlbBitset() & type2->GlbBitset(); |
| 433 if (bitset != kNone) unioned->set(size++, from_bitset(bitset)); |
| 434 size = type1->ExtendIntersection(unioned, type2, size); |
| 435 size = type2->ExtendIntersection(unioned, type1, size); |
| 436 |
| 437 if (size == 0) { |
| 438 return None(); |
| 439 } else if (size == 1) { |
| 440 return *union_get(unioned, 0); |
| 441 } else if (size == unioned->length()) { |
| 442 return from_handle(unioned); |
| 443 } |
| 444 |
| 445 // There were dropped cases. Copy to smaller union. |
| 446 Handle<Unioned> result = isolate->factory()->NewFixedArray(size); |
| 447 for (int i = 0; i < size; ++i) result->set(i, unioned->get(i)); |
| 448 return from_handle(result); |
| 449 } |
| 450 |
| 451 |
377 Type* Type::Optional(Handle<Type> type) { | 452 Type* Type::Optional(Handle<Type> type) { |
378 return type->is_bitset() | 453 return type->is_bitset() |
379 ? from_bitset(type->as_bitset() | kUndefined) | 454 ? from_bitset(type->as_bitset() | kUndefined) |
380 : Union(type, Undefined()->handle_via_isolate_of(*type)); | 455 : Union(type, Undefined()->handle_via_isolate_of(*type)); |
381 } | 456 } |
382 | 457 |
383 } } // namespace v8::internal | 458 } } // namespace v8::internal |
OLD | NEW |