type-cache.h 8.62 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_COMPILER_TYPE_CACHE_H_
#define V8_COMPILER_TYPE_CACHE_H_
7

8
#include "src/compiler/types.h"
Yang Guo's avatar
Yang Guo committed
9
#include "src/date/date.h"
10
#include "src/objects/code.h"
11
#include "src/objects/js-array-buffer.h"
12
#include "src/objects/string.h"
13 14 15

namespace v8 {
namespace internal {
16
namespace compiler {
17

18
class V8_EXPORT_PRIVATE TypeCache final {
19 20
 private:
  // This has to be first for the initialization magic to work.
21
  AccountingAllocator allocator;
22 23 24
  Zone zone_;

 public:
25
  static TypeCache const* Get();
26

27
  TypeCache() : zone_(&allocator, ZONE_NAME) {}
28

29 30 31 32
  Type const kInt8 = CreateRange<int8_t>();
  Type const kUint8 = CreateRange<uint8_t>();
  Type const kUint8Clamped = kUint8;
  Type const kUint8OrMinusZeroOrNaN =
33
      Type::Union(kUint8, Type::MinusZeroOrNaN(), zone());
34 35
  Type const kInt16 = CreateRange<int16_t>();
  Type const kUint16 = CreateRange<uint16_t>();
36
  Type const kUnsigned31 = Type::Unsigned31();
37 38
  Type const kInt32 = Type::Signed32();
  Type const kUint32 = Type::Unsigned32();
39 40 41 42
  Type const kDoubleRepresentableInt64 = CreateRange(
      std::numeric_limits<int64_t>::min(), kMaxDoubleRepresentableInt64);
  Type const kDoubleRepresentableUint64 = CreateRange(
      std::numeric_limits<uint64_t>::min(), kMaxDoubleRepresentableUint64);
43 44 45 46 47 48 49 50 51 52 53 54
  Type const kFloat32 = Type::Number();
  Type const kFloat64 = Type::Number();
  Type const kBigInt64 = Type::BigInt();
  Type const kBigUint64 = Type::BigInt();

  Type const kHoleySmi = Type::Union(Type::SignedSmall(), Type::Hole(), zone());

  Type const kSingletonZero = CreateRange(0.0, 0.0);
  Type const kSingletonOne = CreateRange(1.0, 1.0);
  Type const kSingletonTen = CreateRange(10.0, 10.0);
  Type const kSingletonMinusOne = CreateRange(-1.0, -1.0);
  Type const kZeroOrMinusZero =
55
      Type::Union(kSingletonZero, Type::MinusZero(), zone());
56
  Type const kZeroOrUndefined =
57
      Type::Union(kSingletonZero, Type::Undefined(), zone());
58 59
  Type const kTenOrUndefined =
      Type::Union(kSingletonTen, Type::Undefined(), zone());
60 61
  Type const kMinusOneOrZero = CreateRange(-1.0, 0.0);
  Type const kMinusOneToOneOrMinusZeroOrNaN = Type::Union(
62 63
      Type::Union(CreateRange(-1.0, 1.0), Type::MinusZero(), zone()),
      Type::NaN(), zone());
64 65 66 67 68
  Type const kZeroOrOne = CreateRange(0.0, 1.0);
  Type const kZeroOrOneOrNaN = Type::Union(kZeroOrOne, Type::NaN(), zone());
  Type const kZeroToThirtyOne = CreateRange(0.0, 31.0);
  Type const kZeroToThirtyTwo = CreateRange(0.0, 32.0);
  Type const kZeroish =
69
      Type::Union(kSingletonZero, Type::MinusZeroOrNaN(), zone());
70 71
  Type const kInteger = CreateRange(-V8_INFINITY, V8_INFINITY);
  Type const kIntegerOrMinusZero =
72
      Type::Union(kInteger, Type::MinusZero(), zone());
73
  Type const kIntegerOrMinusZeroOrNaN =
74
      Type::Union(kIntegerOrMinusZero, Type::NaN(), zone());
75 76
  Type const kPositiveInteger = CreateRange(0.0, V8_INFINITY);
  Type const kPositiveIntegerOrMinusZero =
77
      Type::Union(kPositiveInteger, Type::MinusZero(), zone());
78
  Type const kPositiveIntegerOrNaN =
79
      Type::Union(kPositiveInteger, Type::NaN(), zone());
80
  Type const kPositiveIntegerOrMinusZeroOrNaN =
81
      Type::Union(kPositiveIntegerOrMinusZero, Type::NaN(), zone());
82

83
  Type const kAdditiveSafeInteger =
84
      CreateRange(-4503599627370495.0, 4503599627370495.0);
85 86
  Type const kSafeInteger = CreateRange(-kMaxSafeInteger, kMaxSafeInteger);
  Type const kAdditiveSafeIntegerOrMinusZero =
87
      Type::Union(kAdditiveSafeInteger, Type::MinusZero(), zone());
88
  Type const kSafeIntegerOrMinusZero =
89
      Type::Union(kSafeInteger, Type::MinusZero(), zone());
90
  Type const kPositiveSafeInteger = CreateRange(0.0, kMaxSafeInteger);
91

92 93
  // The FixedArray::length property always containts a smi in the range
  // [0, FixedArray::kMaxLength].
94
  Type const kFixedArrayLengthType = CreateRange(0.0, FixedArray::kMaxLength);
95

96 97 98 99 100
  // The WeakFixedArray::length property always containts a smi in the range
  // [0, WeakFixedArray::kMaxLength].
  Type const kWeakFixedArrayLengthType =
      CreateRange(0.0, WeakFixedArray::kMaxLength);

101 102
  // The FixedDoubleArray::length property always containts a smi in the range
  // [0, FixedDoubleArray::kMaxLength].
103
  Type const kFixedDoubleArrayLengthType =
104
      CreateRange(0.0, FixedDoubleArray::kMaxLength);
105 106 107

  // The JSArray::length property always contains a tagged number in the range
  // [0, kMaxUInt32].
108
  Type const kJSArrayLengthType = Type::Unsigned32();
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123
  // The JSArrayBuffer::byte_length property is limited to safe integer range
  // per specification, but on 32-bit architectures is implemented as uint32_t
  // field, so it's in the [0, kMaxUInt32] range in that case.
  Type const kJSArrayBufferByteLengthType =
      CreateRange(0.0, JSArrayBuffer::kMaxByteLength);

  // The type for the JSArrayBufferView::byte_length property is the same as
  // JSArrayBuffer::byte_length above.
  Type const kJSArrayBufferViewByteLengthType = kJSArrayBufferByteLengthType;

  // The type for the JSArrayBufferView::byte_offset property is the same as
  // JSArrayBuffer::byte_length above.
  Type const kJSArrayBufferViewByteOffsetType = kJSArrayBufferByteLengthType;

124
  // The JSTypedArray::length property always contains an untagged number in
125
  // the range [0, JSTypedArray::kMaxLength].
126 127
  Type const kJSTypedArrayLengthType =
      CreateRange(0.0, JSTypedArray::kMaxLength);
128

129 130
  // The String::length property always contains a smi in the range
  // [0, String::kMaxLength].
131
  Type const kStringLengthType = CreateRange(0.0, String::kMaxLength);
132

133 134
  // A time value always contains a tagged number in the range
  // [-kMaxTimeInMs, kMaxTimeInMs].
135
  Type const kTimeValueType =
136 137
      CreateRange(-DateCache::kMaxTimeInMs, DateCache::kMaxTimeInMs);

138 139
  // The JSDate::day property always contains a tagged number in the range
  // [1, 31] or NaN.
140
  Type const kJSDateDayType =
141 142 143 144
      Type::Union(CreateRange(1, 31.0), Type::NaN(), zone());

  // The JSDate::hour property always contains a tagged number in the range
  // [0, 23] or NaN.
145
  Type const kJSDateHourType =
146 147 148 149
      Type::Union(CreateRange(0, 23.0), Type::NaN(), zone());

  // The JSDate::minute property always contains a tagged number in the range
  // [0, 59] or NaN.
150
  Type const kJSDateMinuteType =
151 152 153 154
      Type::Union(CreateRange(0, 59.0), Type::NaN(), zone());

  // The JSDate::month property always contains a tagged number in the range
  // [0, 11] or NaN.
155
  Type const kJSDateMonthType =
156 157 158 159
      Type::Union(CreateRange(0, 11.0), Type::NaN(), zone());

  // The JSDate::second property always contains a tagged number in the range
  // [0, 59] or NaN.
160
  Type const kJSDateSecondType = kJSDateMinuteType;
161 162

  // The JSDate::value property always contains a tagged number in the range
163
  // [-kMaxTimeInMs, kMaxTimeInMs] or NaN.
164
  Type const kJSDateValueType =
165
      Type::Union(kTimeValueType, Type::NaN(), zone());
166

167 168
  // The JSDate::weekday property always contains a tagged number in the range
  // [0, 6] or NaN.
169
  Type const kJSDateWeekdayType =
170 171 172 173
      Type::Union(CreateRange(0, 6.0), Type::NaN(), zone());

  // The JSDate::year property always contains a tagged number in the signed
  // small range or NaN.
174
  Type const kJSDateYearType =
175 176
      Type::Union(Type::SignedSmall(), Type::NaN(), zone());

177 178 179 180
  // The valid number of arguments for JavaScript functions. We can never
  // materialize more than the max size of a fixed array, because we require a
  // fixed array in spread/apply calls.
  Type const kArgumentsLengthType = CreateRange(0.0, FixedArray::kMaxLength);
181

182 183 184 185 186
  // The valid number of arguments for rest parameters. We can never
  // materialize more than the max size of a fixed array, because we require a
  // fixed array in spread/apply calls.
  Type const kRestLengthType = CreateRange(0.0, FixedArray::kMaxLength);

187 188
  // The JSArrayIterator::kind property always contains an integer in the
  // range [0, 2], representing the possible IterationKinds.
189
  Type const kJSArrayIteratorKindType = CreateRange(0.0, 2.0);
190

191 192
 private:
  template <typename T>
193
  Type CreateRange() {
194 195 196 197 198
    T min = std::numeric_limits<T>::min();
    T max = std::numeric_limits<T>::max();
    DCHECK_EQ(min, static_cast<T>(static_cast<double>(min)));
    DCHECK_EQ(max, static_cast<T>(static_cast<double>(max)));
    return CreateRange(min, max);
199 200
  }

201
  Type CreateRange(double min, double max) {
202 203 204 205
    return Type::Range(min, max, zone());
  }

  Zone* zone() { return &zone_; }
206 207 208 209

  static constexpr double kMaxDoubleRepresentableInt64 = 9223372036854774784.0;
  static constexpr double kMaxDoubleRepresentableUint64 =
      18446744073709549568.0;
210 211
};

212
}  // namespace compiler
213 214 215
}  // namespace internal
}  // namespace v8

216
#endif  // V8_COMPILER_TYPE_CACHE_H_