blob: 2a95df9f8ca672d1338d3ff39ad57ff0fc4e8c7a [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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_TYPE_CACHE_H_
6#define V8_TYPE_CACHE_H_
7
8#include "src/types.h"
9
10namespace v8 {
11namespace internal {
12
13class TypeCache final {
14 private:
15 // This has to be first for the initialization magic to work.
Ben Murdochda12d292016-06-02 14:46:10 +010016 base::AccountingAllocator allocator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017 Zone zone_;
18
19 public:
20 static TypeCache const& Get();
21
Ben Murdochda12d292016-06-02 14:46:10 +010022 TypeCache() : zone_(&allocator) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023
24 Type* const kInt8 =
25 CreateNative(CreateRange<int8_t>(), Type::UntaggedIntegral8());
26 Type* const kUint8 =
27 CreateNative(CreateRange<uint8_t>(), Type::UntaggedIntegral8());
28 Type* const kUint8Clamped = kUint8;
29 Type* const kInt16 =
30 CreateNative(CreateRange<int16_t>(), Type::UntaggedIntegral16());
31 Type* const kUint16 =
32 CreateNative(CreateRange<uint16_t>(), Type::UntaggedIntegral16());
33 Type* const kInt32 =
34 CreateNative(Type::Signed32(), Type::UntaggedIntegral32());
35 Type* const kUint32 =
36 CreateNative(Type::Unsigned32(), Type::UntaggedIntegral32());
37 Type* const kFloat32 = CreateNative(Type::Number(), Type::UntaggedFloat32());
38 Type* const kFloat64 = CreateNative(Type::Number(), Type::UntaggedFloat64());
39
40 Type* const kSmi = CreateNative(Type::SignedSmall(), Type::TaggedSigned());
41 Type* const kHeapNumber = CreateNative(Type::Number(), Type::TaggedPointer());
42
43 Type* const kSingletonZero = CreateRange(0.0, 0.0);
44 Type* const kSingletonOne = CreateRange(1.0, 1.0);
45 Type* const kZeroOrOne = CreateRange(0.0, 1.0);
46 Type* const kZeroToThirtyOne = CreateRange(0.0, 31.0);
47 Type* const kZeroToThirtyTwo = CreateRange(0.0, 32.0);
48 Type* const kZeroish =
49 Type::Union(kSingletonZero, Type::MinusZeroOrNaN(), zone());
50 Type* const kInteger = CreateRange(-V8_INFINITY, V8_INFINITY);
51 Type* const kPositiveInteger = CreateRange(0.0, V8_INFINITY);
52 Type* const kIntegerOrMinusZero =
53 Type::Union(kInteger, Type::MinusZero(), zone());
54 Type* const kIntegerOrMinusZeroOrNaN =
55 Type::Union(kIntegerOrMinusZero, Type::NaN(), zone());
56
57 Type* const kAdditiveSafeInteger =
58 CreateRange(-4503599627370496.0, 4503599627370496.0);
59 Type* const kSafeInteger = CreateRange(-kMaxSafeInteger, kMaxSafeInteger);
60 Type* const kPositiveSafeInteger = CreateRange(0.0, kMaxSafeInteger);
61
62 Type* const kUntaggedUndefined =
63 Type::Intersect(Type::Undefined(), Type::Untagged(), zone());
64
65 // Asm.js related types.
66 Type* const kAsmSigned = kInt32;
67 Type* const kAsmUnsigned = kUint32;
68 Type* const kAsmInt = Type::Union(kAsmSigned, kAsmUnsigned, zone());
69 Type* const kAsmFixnum = Type::Intersect(kAsmSigned, kAsmUnsigned, zone());
70 Type* const kAsmFloat = kFloat32;
71 Type* const kAsmDouble = kFloat64;
72 Type* const kAsmFloatQ = Type::Union(kAsmFloat, kUntaggedUndefined, zone());
73 Type* const kAsmDoubleQ = Type::Union(kAsmDouble, kUntaggedUndefined, zone());
74 // Not part of the Asm.js type hierarchy, but represents a part of what
75 // intish encompasses.
76 Type* const kAsmIntQ = Type::Union(kAsmInt, kUntaggedUndefined, zone());
77 Type* const kAsmFloatDoubleQ = Type::Union(kAsmFloatQ, kAsmDoubleQ, zone());
78 // Asm.js size unions.
79 Type* const kAsmSize8 = Type::Union(kInt8, kUint8, zone());
80 Type* const kAsmSize16 = Type::Union(kInt16, kUint16, zone());
81 Type* const kAsmSize32 =
82 Type::Union(Type::Union(kInt32, kUint32, zone()), kAsmFloat, zone());
83 Type* const kAsmSize64 = kFloat64;
84 // Asm.js other types.
85 Type* const kAsmComparable = Type::Union(
86 kAsmSigned,
87 Type::Union(kAsmUnsigned, Type::Union(kAsmDouble, kAsmFloat, zone()),
88 zone()),
89 zone());
90 Type* const kAsmIntArrayElement =
91 Type::Union(Type::Union(kInt8, kUint8, zone()),
92 Type::Union(Type::Union(kInt16, kUint16, zone()),
93 Type::Union(kInt32, kUint32, zone()), zone()),
94 zone());
95
96 // The FixedArray::length property always containts a smi in the range
97 // [0, FixedArray::kMaxLength].
98 Type* const kFixedArrayLengthType = CreateNative(
99 CreateRange(0.0, FixedArray::kMaxLength), Type::TaggedSigned());
100
101 // The FixedDoubleArray::length property always containts a smi in the range
102 // [0, FixedDoubleArray::kMaxLength].
103 Type* const kFixedDoubleArrayLengthType = CreateNative(
104 CreateRange(0.0, FixedDoubleArray::kMaxLength), Type::TaggedSigned());
105
106 // The JSArray::length property always contains a tagged number in the range
107 // [0, kMaxUInt32].
108 Type* const kJSArrayLengthType =
109 CreateNative(Type::Unsigned32(), Type::Tagged());
110
111 // The String::length property always contains a smi in the range
112 // [0, String::kMaxLength].
113 Type* const kStringLengthType =
114 CreateNative(CreateRange(0.0, String::kMaxLength), Type::TaggedSigned());
115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116#define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \
117 Type* const k##TypeName##Array = CreateArray(k##TypeName);
118 TYPED_ARRAYS(TYPED_ARRAY)
119#undef TYPED_ARRAY
120
121 private:
122 Type* CreateArray(Type* element) { return Type::Array(element, zone()); }
123
124 Type* CreateArrayFunction(Type* array) {
125 Type* arg1 = Type::Union(Type::Unsigned32(), Type::Object(), zone());
126 Type* arg2 = Type::Union(Type::Unsigned32(), Type::Undefined(), zone());
127 Type* arg3 = arg2;
128 return Type::Function(array, arg1, arg2, arg3, zone());
129 }
130
131 Type* CreateNative(Type* semantic, Type* representation) {
132 return Type::Intersect(semantic, representation, zone());
133 }
134
135 template <typename T>
136 Type* CreateRange() {
137 return CreateRange(std::numeric_limits<T>::min(),
138 std::numeric_limits<T>::max());
139 }
140
141 Type* CreateRange(double min, double max) {
142 return Type::Range(min, max, zone());
143 }
144
145 Zone* zone() { return &zone_; }
146};
147
148} // namespace internal
149} // namespace v8
150
151#endif // V8_TYPE_CACHE_H_