OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_TYPES_H_ | 5 #ifndef V8_COMPILER_TYPES_H_ |
6 #define V8_COMPILER_TYPES_H_ | 6 #define V8_COMPILER_TYPES_H_ |
7 | 7 |
8 #include "src/conversions.h" | 8 #include "src/conversions.h" |
9 #include "src/handles.h" | 9 #include "src/handles.h" |
10 #include "src/objects.h" | 10 #include "src/objects.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 static inline const Boundary* Boundaries(); | 256 static inline const Boundary* Boundaries(); |
257 static inline size_t BoundariesSize(); | 257 static inline size_t BoundariesSize(); |
258 }; | 258 }; |
259 | 259 |
260 // ----------------------------------------------------------------------------- | 260 // ----------------------------------------------------------------------------- |
261 // Superclass for non-bitset types (internal). | 261 // Superclass for non-bitset types (internal). |
262 class TypeBase { | 262 class TypeBase { |
263 protected: | 263 protected: |
264 friend class Type; | 264 friend class Type; |
265 | 265 |
266 enum Kind { kConstant, kTuple, kUnion, kRange }; | 266 enum Kind { kHeapConstant, kOtherNumberConstant, kTuple, kUnion, kRange }; |
267 | 267 |
268 Kind kind() const { return kind_; } | 268 Kind kind() const { return kind_; } |
269 explicit TypeBase(Kind kind) : kind_(kind) {} | 269 explicit TypeBase(Kind kind) : kind_(kind) {} |
270 | 270 |
271 static bool IsKind(Type* type, Kind kind) { | 271 static bool IsKind(Type* type, Kind kind) { |
272 if (BitsetType::IsBitset(type)) return false; | 272 if (BitsetType::IsBitset(type)) return false; |
273 TypeBase* base = reinterpret_cast<TypeBase*>(type); | 273 TypeBase* base = reinterpret_cast<TypeBase*>(type); |
274 return base->kind() == kind; | 274 return base->kind() == kind; |
275 } | 275 } |
276 | 276 |
277 // The hacky conversion to/from Type*. | 277 // The hacky conversion to/from Type*. |
278 static Type* AsType(TypeBase* type) { return reinterpret_cast<Type*>(type); } | 278 static Type* AsType(TypeBase* type) { return reinterpret_cast<Type*>(type); } |
279 static TypeBase* FromType(Type* type) { | 279 static TypeBase* FromType(Type* type) { |
280 return reinterpret_cast<TypeBase*>(type); | 280 return reinterpret_cast<TypeBase*>(type); |
281 } | 281 } |
282 | 282 |
283 private: | 283 private: |
284 Kind kind_; | 284 Kind kind_; |
285 }; | 285 }; |
286 | 286 |
287 // ----------------------------------------------------------------------------- | 287 // ----------------------------------------------------------------------------- |
288 // Constant types. | 288 // Constant types. |
289 | 289 |
290 class ConstantType : public TypeBase { | 290 class OtherNumberConstantType : public TypeBase { |
| 291 public: |
| 292 double Value() { return value_; } |
| 293 |
| 294 static bool IsOtherNumberConstant(double value); |
| 295 static bool IsOtherNumberConstant(Object* value); |
| 296 |
| 297 private: |
| 298 friend class Type; |
| 299 friend class BitsetType; |
| 300 |
| 301 static Type* New(double value, Zone* zone) { |
| 302 return AsType(new (zone->New(sizeof(OtherNumberConstantType))) |
| 303 OtherNumberConstantType(value)); // NOLINT |
| 304 } |
| 305 |
| 306 static OtherNumberConstantType* cast(Type* type) { |
| 307 DCHECK(IsKind(type, kOtherNumberConstant)); |
| 308 return static_cast<OtherNumberConstantType*>(FromType(type)); |
| 309 } |
| 310 |
| 311 explicit OtherNumberConstantType(double value) |
| 312 : TypeBase(kOtherNumberConstant), value_(value) { |
| 313 CHECK(IsOtherNumberConstant(value)); |
| 314 } |
| 315 |
| 316 BitsetType::bitset Lub() { return BitsetType::kOtherNumber; } |
| 317 |
| 318 double value_; |
| 319 }; |
| 320 |
| 321 class HeapConstantType : public TypeBase { |
291 public: | 322 public: |
292 i::Handle<i::Object> Value() { return object_; } | 323 i::Handle<i::Object> Value() { return object_; } |
293 | 324 |
294 private: | 325 private: |
295 friend class Type; | 326 friend class Type; |
296 friend class BitsetType; | 327 friend class BitsetType; |
297 | 328 |
298 static Type* New(i::Handle<i::Object> value, Zone* zone) { | 329 static Type* New(i::Handle<i::Object> value, Zone* zone) { |
299 BitsetType::bitset bitset = BitsetType::Lub(*value); | 330 BitsetType::bitset bitset = BitsetType::Lub(*value); |
300 return AsType(new (zone->New(sizeof(ConstantType))) | 331 return AsType(new (zone->New(sizeof(HeapConstantType))) |
301 ConstantType(bitset, value)); | 332 HeapConstantType(bitset, value)); |
302 } | 333 } |
303 | 334 |
304 static ConstantType* cast(Type* type) { | 335 static HeapConstantType* cast(Type* type) { |
305 DCHECK(IsKind(type, kConstant)); | 336 DCHECK(IsKind(type, kHeapConstant)); |
306 return static_cast<ConstantType*>(FromType(type)); | 337 return static_cast<HeapConstantType*>(FromType(type)); |
307 } | 338 } |
308 | 339 |
309 ConstantType(BitsetType::bitset bitset, i::Handle<i::Object> object) | 340 HeapConstantType(BitsetType::bitset bitset, i::Handle<i::Object> object); |
310 : TypeBase(kConstant), bitset_(bitset), object_(object) {} | |
311 | 341 |
312 BitsetType::bitset Lub() { return bitset_; } | 342 BitsetType::bitset Lub() { return bitset_; } |
313 | 343 |
314 BitsetType::bitset bitset_; | 344 BitsetType::bitset bitset_; |
315 Handle<i::Object> object_; | 345 Handle<i::Object> object_; |
316 }; | 346 }; |
317 // TODO(neis): Also cache value if numerical. | |
318 | 347 |
319 // ----------------------------------------------------------------------------- | 348 // ----------------------------------------------------------------------------- |
320 // Range types. | 349 // Range types. |
321 | 350 |
322 class RangeType : public TypeBase { | 351 class RangeType : public TypeBase { |
323 public: | 352 public: |
324 struct Limits { | 353 struct Limits { |
325 double min; | 354 double min; |
326 double max; | 355 double max; |
327 Limits(double min, double max) : min(min), max(max) {} | 356 Limits(double min, double max) : min(min), max(max) {} |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 PROPER_BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) | 496 PROPER_BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) |
468 #undef DEFINE_TYPE_CONSTRUCTOR | 497 #undef DEFINE_TYPE_CONSTRUCTOR |
469 | 498 |
470 static Type* SignedSmall() { | 499 static Type* SignedSmall() { |
471 return BitsetType::New(BitsetType::SignedSmall()); | 500 return BitsetType::New(BitsetType::SignedSmall()); |
472 } | 501 } |
473 static Type* UnsignedSmall() { | 502 static Type* UnsignedSmall() { |
474 return BitsetType::New(BitsetType::UnsignedSmall()); | 503 return BitsetType::New(BitsetType::UnsignedSmall()); |
475 } | 504 } |
476 | 505 |
477 static Type* Constant(i::Handle<i::Object> value, Zone* zone) { | 506 static Type* OtherNumberConstant(double value, Zone* zone) { |
478 return ConstantType::New(value, zone); | 507 return OtherNumberConstantType::New(value, zone); |
| 508 } |
| 509 static Type* HeapConstant(i::Handle<i::Object> value, Zone* zone) { |
| 510 return HeapConstantType::New(value, zone); |
479 } | 511 } |
480 static Type* Range(double min, double max, Zone* zone) { | 512 static Type* Range(double min, double max, Zone* zone) { |
481 return RangeType::New(min, max, zone); | 513 return RangeType::New(min, max, zone); |
482 } | 514 } |
483 static Type* Tuple(Type* first, Type* second, Type* third, Zone* zone) { | 515 static Type* Tuple(Type* first, Type* second, Type* third, Zone* zone) { |
484 Type* tuple = TupleType::New(3, zone); | 516 Type* tuple = TupleType::New(3, zone); |
485 tuple->AsTuple()->InitElement(0, first); | 517 tuple->AsTuple()->InitElement(0, first); |
486 tuple->AsTuple()->InitElement(1, second); | 518 tuple->AsTuple()->InitElement(1, second); |
487 tuple->AsTuple()->InitElement(2, third); | 519 tuple->AsTuple()->InitElement(2, third); |
488 return tuple; | 520 return tuple; |
489 } | 521 } |
490 | 522 |
| 523 // NewConstant is a factory that returns Constant, Range or Number. |
| 524 static Type* NewConstant(i::Handle<i::Object> value, Zone* zone); |
| 525 static Type* NewConstant(double value, Zone* zone); |
| 526 |
491 static Type* Union(Type* type1, Type* type2, Zone* zone); | 527 static Type* Union(Type* type1, Type* type2, Zone* zone); |
492 static Type* Intersect(Type* type1, Type* type2, Zone* zone); | 528 static Type* Intersect(Type* type1, Type* type2, Zone* zone); |
493 | 529 |
494 static Type* Of(double value, Zone* zone) { | 530 static Type* Of(double value, Zone* zone) { |
495 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(value))); | 531 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(value))); |
496 } | 532 } |
497 static Type* Of(i::Object* value, Zone* zone) { | 533 static Type* Of(i::Object* value, Zone* zone) { |
498 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(value))); | 534 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(value))); |
499 } | 535 } |
500 static Type* Of(i::Handle<i::Object> value, Zone* zone) { | 536 static Type* Of(i::Handle<i::Object> value, Zone* zone) { |
501 return Of(*value, zone); | 537 return Of(*value, zone); |
502 } | 538 } |
503 | 539 |
504 static Type* For(i::Map* map) { | 540 static Type* For(i::Map* map) { |
505 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(map))); | 541 return BitsetType::New(BitsetType::ExpandInternals(BitsetType::Lub(map))); |
506 } | 542 } |
507 static Type* For(i::Handle<i::Map> map) { return For(*map); } | 543 static Type* For(i::Handle<i::Map> map) { return For(*map); } |
508 | 544 |
509 // Predicates. | 545 // Predicates. |
510 bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); } | 546 bool IsInhabited() { return BitsetType::IsInhabited(this->BitsetLub()); } |
511 | 547 |
512 bool Is(Type* that) { return this == that || this->SlowIs(that); } | 548 bool Is(Type* that) { return this == that || this->SlowIs(that); } |
513 bool Maybe(Type* that); | 549 bool Maybe(Type* that); |
514 bool Equals(Type* that) { return this->Is(that) && that->Is(this); } | 550 bool Equals(Type* that) { return this->Is(that) && that->Is(this); } |
515 | 551 |
516 // Inspection. | 552 // Inspection. |
517 bool IsRange() { return IsKind(TypeBase::kRange); } | 553 bool IsRange() { return IsKind(TypeBase::kRange); } |
518 bool IsConstant() { return IsKind(TypeBase::kConstant); } | 554 bool IsHeapConstant() { return IsKind(TypeBase::kHeapConstant); } |
| 555 bool IsOtherNumberConstant() { |
| 556 return IsKind(TypeBase::kOtherNumberConstant); |
| 557 } |
519 bool IsTuple() { return IsKind(TypeBase::kTuple); } | 558 bool IsTuple() { return IsKind(TypeBase::kTuple); } |
520 | 559 |
521 ConstantType* AsConstant() { return ConstantType::cast(this); } | 560 HeapConstantType* AsHeapConstant() { return HeapConstantType::cast(this); } |
| 561 OtherNumberConstantType* AsOtherNumberConstant() { |
| 562 return OtherNumberConstantType::cast(this); |
| 563 } |
522 RangeType* AsRange() { return RangeType::cast(this); } | 564 RangeType* AsRange() { return RangeType::cast(this); } |
523 TupleType* AsTuple() { return TupleType::cast(this); } | 565 TupleType* AsTuple() { return TupleType::cast(this); } |
524 | 566 |
525 // Minimum and maximum of a numeric type. | 567 // Minimum and maximum of a numeric type. |
526 // These functions do not distinguish between -0 and +0. If the type equals | 568 // These functions do not distinguish between -0 and +0. If the type equals |
527 // kNaN, they return NaN; otherwise kNaN is ignored. Only call these | 569 // kNaN, they return NaN; otherwise kNaN is ignored. Only call these |
528 // functions on subtypes of Number. | 570 // functions on subtypes of Number. |
529 double Min(); | 571 double Min(); |
530 double Max(); | 572 double Max(); |
531 | 573 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 } | 617 } |
576 UnionType* AsUnion() { return UnionType::cast(this); } | 618 UnionType* AsUnion() { return UnionType::cast(this); } |
577 | 619 |
578 bitset BitsetGlb() { return BitsetType::Glb(this); } | 620 bitset BitsetGlb() { return BitsetType::Glb(this); } |
579 bitset BitsetLub() { return BitsetType::Lub(this); } | 621 bitset BitsetLub() { return BitsetType::Lub(this); } |
580 | 622 |
581 bool SlowIs(Type* that); | 623 bool SlowIs(Type* that); |
582 | 624 |
583 static bool Overlap(RangeType* lhs, RangeType* rhs); | 625 static bool Overlap(RangeType* lhs, RangeType* rhs); |
584 static bool Contains(RangeType* lhs, RangeType* rhs); | 626 static bool Contains(RangeType* lhs, RangeType* rhs); |
585 static bool Contains(RangeType* range, ConstantType* constant); | |
586 static bool Contains(RangeType* range, i::Object* val); | 627 static bool Contains(RangeType* range, i::Object* val); |
587 | 628 |
588 static int UpdateRange(Type* type, UnionType* result, int size, Zone* zone); | 629 static int UpdateRange(Type* type, UnionType* result, int size, Zone* zone); |
589 | 630 |
590 static RangeType::Limits IntersectRangeAndBitset(Type* range, Type* bits, | 631 static RangeType::Limits IntersectRangeAndBitset(Type* range, Type* bits, |
591 Zone* zone); | 632 Zone* zone); |
592 static RangeType::Limits ToLimits(bitset bits, Zone* zone); | 633 static RangeType::Limits ToLimits(bitset bits, Zone* zone); |
593 | 634 |
594 bool SimplyEquals(Type* that); | 635 bool SimplyEquals(Type* that); |
595 | 636 |
596 static int AddToUnion(Type* type, UnionType* result, int size, Zone* zone); | 637 static int AddToUnion(Type* type, UnionType* result, int size, Zone* zone); |
597 static int IntersectAux(Type* type, Type* other, UnionType* result, int size, | 638 static int IntersectAux(Type* type, Type* other, UnionType* result, int size, |
598 RangeType::Limits* limits, Zone* zone); | 639 RangeType::Limits* limits, Zone* zone); |
599 static Type* NormalizeUnion(Type* unioned, int size, Zone* zone); | 640 static Type* NormalizeUnion(Type* unioned, int size, Zone* zone); |
600 static Type* NormalizeRangeAndBitset(Type* range, bitset* bits, Zone* zone); | 641 static Type* NormalizeRangeAndBitset(Type* range, bitset* bits, Zone* zone); |
601 }; | 642 }; |
602 | 643 |
603 } // namespace compiler | 644 } // namespace compiler |
604 } // namespace internal | 645 } // namespace internal |
605 } // namespace v8 | 646 } // namespace v8 |
606 | 647 |
607 #endif // V8_COMPILER_TYPES_H_ | 648 #endif // V8_COMPILER_TYPES_H_ |
OLD | NEW |