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/date.h" 10 #include "src/objects/code.h" 11 #include "src/objects/js-array-buffer.h" 12 #include "src/objects/string.h" 13 14 namespace v8 { 15 namespace internal { 16 namespace compiler { 17 18 class V8_EXPORT_PRIVATE TypeCache final { 19 private: 20 // This has to be first for the initialization magic to work. 21 AccountingAllocator allocator; 22 Zone zone_; 23 24 public: 25 static TypeCache const* Get(); 26 TypeCache()27 TypeCache() : zone_(&allocator, ZONE_NAME) {} 28 29 Type const kInt8 = CreateRange<int8_t>(); 30 Type const kUint8 = CreateRange<uint8_t>(); 31 Type const kUint8Clamped = kUint8; 32 Type const kUint8OrMinusZeroOrNaN = 33 Type::Union(kUint8, Type::MinusZeroOrNaN(), zone()); 34 Type const kInt16 = CreateRange<int16_t>(); 35 Type const kUint16 = CreateRange<uint16_t>(); 36 Type const kUnsigned31 = Type::Unsigned31(); 37 Type const kInt32 = Type::Signed32(); 38 Type const kUint32 = Type::Unsigned32(); 39 Type const kInt64 = CreateRange<int64_t>(); 40 Type const kUint64 = CreateRange<uint64_t>(); 41 Type const kIntPtr = CreateRange<intptr_t>(); 42 Type const kFloat32 = Type::Number(); 43 Type const kFloat64 = Type::Number(); 44 Type const kBigInt64 = Type::BigInt(); 45 Type const kBigUint64 = Type::BigInt(); 46 47 Type const kHoleySmi = Type::Union(Type::SignedSmall(), Type::Hole(), zone()); 48 49 Type const kSingletonZero = CreateRange(0.0, 0.0); 50 Type const kSingletonOne = CreateRange(1.0, 1.0); 51 Type const kSingletonTen = CreateRange(10.0, 10.0); 52 Type const kSingletonMinusOne = CreateRange(-1.0, -1.0); 53 Type const kZeroOrMinusZero = 54 Type::Union(kSingletonZero, Type::MinusZero(), zone()); 55 Type const kZeroOrUndefined = 56 Type::Union(kSingletonZero, Type::Undefined(), zone()); 57 Type const kTenOrUndefined = 58 Type::Union(kSingletonTen, Type::Undefined(), zone()); 59 Type const kMinusOneOrZero = CreateRange(-1.0, 0.0); 60 Type const kMinusOneToOneOrMinusZeroOrNaN = Type::Union( 61 Type::Union(CreateRange(-1.0, 1.0), Type::MinusZero(), zone()), 62 Type::NaN(), zone()); 63 Type const kZeroOrOne = CreateRange(0.0, 1.0); 64 Type const kZeroOrOneOrNaN = Type::Union(kZeroOrOne, Type::NaN(), zone()); 65 Type const kZeroToThirtyOne = CreateRange(0.0, 31.0); 66 Type const kZeroToThirtyTwo = CreateRange(0.0, 32.0); 67 Type const kZeroish = 68 Type::Union(kSingletonZero, Type::MinusZeroOrNaN(), zone()); 69 Type const kInteger = CreateRange(-V8_INFINITY, V8_INFINITY); 70 Type const kIntegerOrMinusZero = 71 Type::Union(kInteger, Type::MinusZero(), zone()); 72 Type const kIntegerOrMinusZeroOrNaN = 73 Type::Union(kIntegerOrMinusZero, Type::NaN(), zone()); 74 Type const kPositiveInteger = CreateRange(0.0, V8_INFINITY); 75 Type const kPositiveIntegerOrMinusZero = 76 Type::Union(kPositiveInteger, Type::MinusZero(), zone()); 77 Type const kPositiveIntegerOrNaN = 78 Type::Union(kPositiveInteger, Type::NaN(), zone()); 79 Type const kPositiveIntegerOrMinusZeroOrNaN = 80 Type::Union(kPositiveIntegerOrMinusZero, Type::NaN(), zone()); 81 82 Type const kAdditiveSafeInteger = 83 CreateRange(-4503599627370496.0, 4503599627370496.0); 84 Type const kSafeInteger = CreateRange(-kMaxSafeInteger, kMaxSafeInteger); 85 Type const kAdditiveSafeIntegerOrMinusZero = 86 Type::Union(kAdditiveSafeInteger, Type::MinusZero(), zone()); 87 Type const kSafeIntegerOrMinusZero = 88 Type::Union(kSafeInteger, Type::MinusZero(), zone()); 89 Type const kPositiveSafeInteger = CreateRange(0.0, kMaxSafeInteger); 90 91 // The FixedArray::length property always containts a smi in the range 92 // [0, FixedArray::kMaxLength]. 93 Type const kFixedArrayLengthType = CreateRange(0.0, FixedArray::kMaxLength); 94 95 // The WeakFixedArray::length property always containts a smi in the range 96 // [0, WeakFixedArray::kMaxLength]. 97 Type const kWeakFixedArrayLengthType = 98 CreateRange(0.0, WeakFixedArray::kMaxLength); 99 100 // The FixedDoubleArray::length property always containts a smi in the range 101 // [0, FixedDoubleArray::kMaxLength]. 102 Type const kFixedDoubleArrayLengthType = 103 CreateRange(0.0, FixedDoubleArray::kMaxLength); 104 105 // The JSArray::length property always contains a tagged number in the range 106 // [0, kMaxUInt32]. 107 Type const kJSArrayLengthType = Type::Unsigned32(); 108 109 // The JSArrayBuffer::byte_length property is limited to safe integer range 110 // per specification, but on 32-bit architectures is implemented as uint32_t 111 // field, so it's in the [0, kMaxUInt32] range in that case. 112 Type const kJSArrayBufferByteLengthType = 113 CreateRange(0.0, JSArrayBuffer::kMaxByteLength); 114 115 // The type for the JSArrayBufferView::byte_length property is the same as 116 // JSArrayBuffer::byte_length above. 117 Type const kJSArrayBufferViewByteLengthType = kJSArrayBufferByteLengthType; 118 119 // The type for the JSArrayBufferView::byte_offset property is the same as 120 // JSArrayBuffer::byte_length above. 121 Type const kJSArrayBufferViewByteOffsetType = kJSArrayBufferByteLengthType; 122 123 // The JSTypedArray::length property always contains an untagged number in 124 // the range [0, JSTypedArray::kMaxLength]. 125 Type const kJSTypedArrayLengthType = 126 CreateRange(0.0, JSTypedArray::kMaxLength); 127 128 // The String::length property always contains a smi in the range 129 // [0, String::kMaxLength]. 130 Type const kStringLengthType = CreateRange(0.0, String::kMaxLength); 131 132 // A time value always contains a tagged number in the range 133 // [-kMaxTimeInMs, kMaxTimeInMs]. 134 Type const kTimeValueType = 135 CreateRange(-DateCache::kMaxTimeInMs, DateCache::kMaxTimeInMs); 136 137 // The JSDate::day property always contains a tagged number in the range 138 // [1, 31] or NaN. 139 Type const kJSDateDayType = 140 Type::Union(CreateRange(1, 31.0), Type::NaN(), zone()); 141 142 // The JSDate::hour property always contains a tagged number in the range 143 // [0, 23] or NaN. 144 Type const kJSDateHourType = 145 Type::Union(CreateRange(0, 23.0), Type::NaN(), zone()); 146 147 // The JSDate::minute property always contains a tagged number in the range 148 // [0, 59] or NaN. 149 Type const kJSDateMinuteType = 150 Type::Union(CreateRange(0, 59.0), Type::NaN(), zone()); 151 152 // The JSDate::month property always contains a tagged number in the range 153 // [0, 11] or NaN. 154 Type const kJSDateMonthType = 155 Type::Union(CreateRange(0, 11.0), Type::NaN(), zone()); 156 157 // The JSDate::second property always contains a tagged number in the range 158 // [0, 59] or NaN. 159 Type const kJSDateSecondType = kJSDateMinuteType; 160 161 // The JSDate::value property always contains a tagged number in the range 162 // [-kMaxTimeInMs, kMaxTimeInMs] or NaN. 163 Type const kJSDateValueType = 164 Type::Union(kTimeValueType, Type::NaN(), zone()); 165 166 // The JSDate::weekday property always contains a tagged number in the range 167 // [0, 6] or NaN. 168 Type const kJSDateWeekdayType = 169 Type::Union(CreateRange(0, 6.0), Type::NaN(), zone()); 170 171 // The JSDate::year property always contains a tagged number in the signed 172 // small range or NaN. 173 Type const kJSDateYearType = 174 Type::Union(Type::SignedSmall(), Type::NaN(), zone()); 175 176 // The valid number of arguments for JavaScript functions. We can never 177 // materialize more than the max size of a fixed array, because we require a 178 // fixed array in spread/apply calls. 179 Type const kArgumentsLengthType = CreateRange(0.0, FixedArray::kMaxLength); 180 181 // The valid number of arguments for rest parameters. We can never 182 // materialize more than the max size of a fixed array, because we require a 183 // fixed array in spread/apply calls. 184 Type const kRestLengthType = CreateRange(0.0, FixedArray::kMaxLength); 185 186 // The JSArrayIterator::kind property always contains an integer in the 187 // range [0, 2], representing the possible IterationKinds. 188 Type const kJSArrayIteratorKindType = CreateRange(0.0, 2.0); 189 190 private: 191 template <typename T> CreateRange()192 Type CreateRange() { 193 return CreateRange(std::numeric_limits<T>::min(), 194 std::numeric_limits<T>::max()); 195 } 196 CreateRange(double min,double max)197 Type CreateRange(double min, double max) { 198 return Type::Range(min, max, zone()); 199 } 200 zone()201 Zone* zone() { return &zone_; } 202 }; 203 204 } // namespace compiler 205 } // namespace internal 206 } // namespace v8 207 208 #endif // V8_COMPILER_TYPE_CACHE_H_ 209