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