type-cache.h 6.65 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"
9
#include "src/date.h"
10
#include "src/objects/code.h"
11
#include "src/objects/string.h"
12 13 14

namespace v8 {
namespace internal {
15
namespace compiler {
16

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

 public:
24 25
  static TypeCache const& Get();

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

28 29 30 31
  Type const kInt8 = CreateRange<int8_t>();
  Type const kUint8 = CreateRange<uint8_t>();
  Type const kUint8Clamped = kUint8;
  Type const kUint8OrMinusZeroOrNaN =
32
      Type::Union(kUint8, Type::MinusZeroOrNaN(), zone());
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  Type const kInt16 = CreateRange<int16_t>();
  Type const kUint16 = CreateRange<uint16_t>();
  Type const kInt32 = Type::Signed32();
  Type const kUint32 = Type::Unsigned32();
  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 =
49
      Type::Union(kSingletonZero, Type::MinusZero(), zone());
50
  Type const kZeroOrUndefined =
51
      Type::Union(kSingletonZero, Type::Undefined(), zone());
52 53
  Type const kTenOrUndefined =
      Type::Union(kSingletonTen, Type::Undefined(), zone());
54 55
  Type const kMinusOneOrZero = CreateRange(-1.0, 0.0);
  Type const kMinusOneToOneOrMinusZeroOrNaN = Type::Union(
56 57
      Type::Union(CreateRange(-1.0, 1.0), Type::MinusZero(), zone()),
      Type::NaN(), zone());
58 59 60 61 62
  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 =
63
      Type::Union(kSingletonZero, Type::MinusZeroOrNaN(), zone());
64 65
  Type const kInteger = CreateRange(-V8_INFINITY, V8_INFINITY);
  Type const kIntegerOrMinusZero =
66
      Type::Union(kInteger, Type::MinusZero(), zone());
67
  Type const kIntegerOrMinusZeroOrNaN =
68
      Type::Union(kIntegerOrMinusZero, Type::NaN(), zone());
69 70
  Type const kPositiveInteger = CreateRange(0.0, V8_INFINITY);
  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 80
  Type const kSafeInteger = CreateRange(-kMaxSafeInteger, kMaxSafeInteger);
  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 87
  // The FixedArray::length property always containts a smi in the range
  // [0, FixedArray::kMaxLength].
88
  Type const kFixedArrayLengthType = CreateRange(0.0, FixedArray::kMaxLength);
89 90 91

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

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

99 100
  // The JSTypedArray::length property always contains a tagged number in the
  // range [0, kMaxSmiValue].
101
  Type const kJSTypedArrayLengthType = Type::UnsignedSmall();
102

103 104
  // The String::length property always contains a smi in the range
  // [0, String::kMaxLength].
105
  Type const kStringLengthType = CreateRange(0.0, String::kMaxLength);
106

107 108
  // A time value always contains a tagged number in the range
  // [-kMaxTimeInMs, kMaxTimeInMs].
109
  Type const kTimeValueType =
110 111
      CreateRange(-DateCache::kMaxTimeInMs, DateCache::kMaxTimeInMs);

112 113
  // The JSDate::day property always contains a tagged number in the range
  // [1, 31] or NaN.
114
  Type const kJSDateDayType =
115 116 117 118
      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.
119
  Type const kJSDateHourType =
120 121 122 123
      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.
124
  Type const kJSDateMinuteType =
125 126 127 128
      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.
129
  Type const kJSDateMonthType =
130 131 132 133
      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.
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 142
  // The JSDate::weekday property always contains a tagged number in the range
  // [0, 6] or NaN.
143
  Type const kJSDateWeekdayType =
144 145 146 147
      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.
148
  Type const kJSDateYearType =
149 150
      Type::Union(Type::SignedSmall(), Type::NaN(), zone());

151
  // The valid number of arguments for JavaScript functions.
152
  Type const kArgumentsLengthType =
153 154
      Type::Range(0.0, Code::kMaxArguments, zone());

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

159 160
 private:
  template <typename T>
161
  Type CreateRange() {
162 163 164 165
    return CreateRange(std::numeric_limits<T>::min(),
                       std::numeric_limits<T>::max());
  }

166
  Type CreateRange(double min, double max) {
167 168 169 170 171 172
    return Type::Range(min, max, zone());
  }

  Zone* zone() { return &zone_; }
};

173
}  // namespace compiler
174 175 176
}  // namespace internal
}  // namespace v8

177
#endif  // V8_COMPILER_TYPE_CACHE_H_