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
3 // found in the LICENSE file.
4 
5 #ifndef V8_COMPILER_TYPE_CACHE_H_
6 #define V8_COMPILER_TYPE_CACHE_H_
7 
8 #include "src/compiler/types.h"
9 #include "src/date.h"
10 
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14 
15 class TypeCache final {
16  private:
17   // This has to be first for the initialization magic to work.
18   AccountingAllocator allocator;
19   Zone zone_;
20 
21  public:
22   static TypeCache const& Get();
23 
TypeCache()24   TypeCache() : zone_(&allocator, ZONE_NAME) {}
25 
26   Type* const kInt8 = CreateRange<int8_t>();
27   Type* const kUint8 = CreateRange<uint8_t>();
28   Type* const kUint8Clamped = kUint8;
29   Type* const kUint8OrMinusZeroOrNaN =
30       Type::Union(kUint8, Type::MinusZeroOrNaN(), zone());
31   Type* const kInt16 = CreateRange<int16_t>();
32   Type* const kUint16 = CreateRange<uint16_t>();
33   Type* const kInt32 = Type::Signed32();
34   Type* const kUint32 = Type::Unsigned32();
35   Type* const kFloat32 = Type::Number();
36   Type* const kFloat64 = Type::Number();
37 
38   Type* const kHoleySmi =
39       Type::Union(Type::SignedSmall(), Type::Hole(), zone());
40 
41   Type* const kSingletonZero = CreateRange(0.0, 0.0);
42   Type* const kSingletonOne = CreateRange(1.0, 1.0);
43   Type* const kSingletonTen = CreateRange(10.0, 10.0);
44   Type* const kSingletonMinusOne = CreateRange(-1.0, -1.0);
45   Type* const kZeroOrUndefined =
46       Type::Union(kSingletonZero, Type::Undefined(), zone());
47   Type* const kTenOrUndefined =
48       Type::Union(kSingletonTen, Type::Undefined(), zone());
49   Type* const kMinusOneOrZero = CreateRange(-1.0, 0.0);
50   Type* const kMinusOneToOneOrMinusZeroOrNaN = Type::Union(
51       Type::Union(CreateRange(-1.0, 1.0), Type::MinusZero(), zone()),
52       Type::NaN(), zone());
53   Type* const kZeroOrOne = CreateRange(0.0, 1.0);
54   Type* const kZeroOrOneOrNaN = Type::Union(kZeroOrOne, Type::NaN(), zone());
55   Type* const kZeroToThirtyOne = CreateRange(0.0, 31.0);
56   Type* const kZeroToThirtyTwo = CreateRange(0.0, 32.0);
57   Type* const kZeroish =
58       Type::Union(kSingletonZero, Type::MinusZeroOrNaN(), zone());
59   Type* const kInteger = CreateRange(-V8_INFINITY, V8_INFINITY);
60   Type* const kIntegerOrMinusZero =
61       Type::Union(kInteger, Type::MinusZero(), zone());
62   Type* const kIntegerOrMinusZeroOrNaN =
63       Type::Union(kIntegerOrMinusZero, Type::NaN(), zone());
64   Type* const kPositiveInteger = CreateRange(0.0, V8_INFINITY);
65   Type* const kPositiveIntegerOrMinusZero =
66       Type::Union(kPositiveInteger, Type::MinusZero(), zone());
67   Type* const kPositiveIntegerOrMinusZeroOrNaN =
68       Type::Union(kPositiveIntegerOrMinusZero, Type::NaN(), zone());
69 
70   Type* const kAdditiveSafeInteger =
71       CreateRange(-4503599627370496.0, 4503599627370496.0);
72   Type* const kSafeInteger = CreateRange(-kMaxSafeInteger, kMaxSafeInteger);
73   Type* const kAdditiveSafeIntegerOrMinusZero =
74       Type::Union(kAdditiveSafeInteger, Type::MinusZero(), zone());
75   Type* const kSafeIntegerOrMinusZero =
76       Type::Union(kSafeInteger, Type::MinusZero(), zone());
77   Type* const kPositiveSafeInteger = CreateRange(0.0, kMaxSafeInteger);
78 
79   // The FixedArray::length property always containts a smi in the range
80   // [0, FixedArray::kMaxLength].
81   Type* const kFixedArrayLengthType = CreateRange(0.0, FixedArray::kMaxLength);
82 
83   // The FixedDoubleArray::length property always containts a smi in the range
84   // [0, FixedDoubleArray::kMaxLength].
85   Type* const kFixedDoubleArrayLengthType =
86       CreateRange(0.0, FixedDoubleArray::kMaxLength);
87 
88   // The JSArray::length property always contains a tagged number in the range
89   // [0, kMaxUInt32].
90   Type* const kJSArrayLengthType = Type::Unsigned32();
91 
92   // The JSTyped::length property always contains a tagged number in the range
93   // [0, kMaxSmiValue].
94   Type* const kJSTypedArrayLengthType = Type::UnsignedSmall();
95 
96   // The String::length property always contains a smi in the range
97   // [0, String::kMaxLength].
98   Type* const kStringLengthType = CreateRange(0.0, String::kMaxLength);
99 
100   // The JSDate::day property always contains a tagged number in the range
101   // [1, 31] or NaN.
102   Type* const kJSDateDayType =
103       Type::Union(CreateRange(1, 31.0), Type::NaN(), zone());
104 
105   // The JSDate::hour property always contains a tagged number in the range
106   // [0, 23] or NaN.
107   Type* const kJSDateHourType =
108       Type::Union(CreateRange(0, 23.0), Type::NaN(), zone());
109 
110   // The JSDate::minute property always contains a tagged number in the range
111   // [0, 59] or NaN.
112   Type* const kJSDateMinuteType =
113       Type::Union(CreateRange(0, 59.0), Type::NaN(), zone());
114 
115   // The JSDate::month property always contains a tagged number in the range
116   // [0, 11] or NaN.
117   Type* const kJSDateMonthType =
118       Type::Union(CreateRange(0, 11.0), Type::NaN(), zone());
119 
120   // The JSDate::second property always contains a tagged number in the range
121   // [0, 59] or NaN.
122   Type* const kJSDateSecondType = kJSDateMinuteType;
123 
124   // The JSDate::value property always contains a tagged number in the range
125   // [-kMaxTimeInMs, kMaxTimeInMs] or NaN.
126   Type* const kJSDateValueType = Type::Union(
127       CreateRange(-DateCache::kMaxTimeInMs, DateCache::kMaxTimeInMs),
128       Type::NaN(), zone());
129 
130   // The JSDate::weekday property always contains a tagged number in the range
131   // [0, 6] or NaN.
132   Type* const kJSDateWeekdayType =
133       Type::Union(CreateRange(0, 6.0), Type::NaN(), zone());
134 
135   // The JSDate::year property always contains a tagged number in the signed
136   // small range or NaN.
137   Type* const kJSDateYearType =
138       Type::Union(Type::SignedSmall(), Type::NaN(), zone());
139 
140  private:
141   template <typename T>
CreateRange()142   Type* CreateRange() {
143     return CreateRange(std::numeric_limits<T>::min(),
144                        std::numeric_limits<T>::max());
145   }
146 
CreateRange(double min,double max)147   Type* CreateRange(double min, double max) {
148     return Type::Range(min, max, zone());
149   }
150 
zone()151   Zone* zone() { return &zone_; }
152 };
153 
154 }  // namespace compiler
155 }  // namespace internal
156 }  // namespace v8
157 
158 #endif  // V8_COMPILER_TYPE_CACHE_H_
159