• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_ELEMENTS_KIND_H_
6 #define V8_ELEMENTS_KIND_H_
7 
8 #include "src/base/macros.h"
9 #include "src/checks.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // V has parameters (Type, type, TYPE, C type)
15 #define TYPED_ARRAYS(V)                                  \
16   V(Uint8, uint8, UINT8, uint8_t)                        \
17   V(Int8, int8, INT8, int8_t)                            \
18   V(Uint16, uint16, UINT16, uint16_t)                    \
19   V(Int16, int16, INT16, int16_t)                        \
20   V(Uint32, uint32, UINT32, uint32_t)                    \
21   V(Int32, int32, INT32, int32_t)                        \
22   V(Float32, float32, FLOAT32, float)                    \
23   V(Float64, float64, FLOAT64, double)                   \
24   V(Uint8Clamped, uint8_clamped, UINT8_CLAMPED, uint8_t) \
25   V(BigUint64, biguint64, BIGUINT64, uint64_t)           \
26   V(BigInt64, bigint64, BIGINT64, int64_t)
27 
28 enum ElementsKind {
29   // The "fast" kind for elements that only contain SMI values. Must be first
30   // to make it possible to efficiently check maps for this kind.
31   PACKED_SMI_ELEMENTS,
32   HOLEY_SMI_ELEMENTS,
33 
34   // The "fast" kind for tagged values. Must be second to make it possible to
35   // efficiently check maps for this and the PACKED_SMI_ELEMENTS kind
36   // together at once.
37   PACKED_ELEMENTS,
38   HOLEY_ELEMENTS,
39 
40   // The "fast" kind for unwrapped, non-tagged double values.
41   PACKED_DOUBLE_ELEMENTS,
42   HOLEY_DOUBLE_ELEMENTS,
43 
44   // The "slow" kind.
45   DICTIONARY_ELEMENTS,
46 
47   // Elements kind of the "arguments" object (only in sloppy mode).
48   FAST_SLOPPY_ARGUMENTS_ELEMENTS,
49   SLOW_SLOPPY_ARGUMENTS_ELEMENTS,
50 
51   // For string wrapper objects ("new String('...')"), the string's characters
52   // are overlaid onto a regular elements backing store.
53   FAST_STRING_WRAPPER_ELEMENTS,
54   SLOW_STRING_WRAPPER_ELEMENTS,
55 
56 // Fixed typed arrays.
57 #define TYPED_ARRAY_ELEMENTS_KIND(Type, type, TYPE, ctype) TYPE##_ELEMENTS,
58   TYPED_ARRAYS(TYPED_ARRAY_ELEMENTS_KIND)
59 #undef TYPED_ARRAY_ELEMENTS_KIND
60 
61   // Sentinel ElementsKind for objects with no elements.
62   NO_ELEMENTS,
63 
64   // Derived constants from ElementsKind.
65   FIRST_ELEMENTS_KIND = PACKED_SMI_ELEMENTS,
66   LAST_ELEMENTS_KIND = BIGINT64_ELEMENTS,
67   FIRST_FAST_ELEMENTS_KIND = PACKED_SMI_ELEMENTS,
68   LAST_FAST_ELEMENTS_KIND = HOLEY_DOUBLE_ELEMENTS,
69   FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND = UINT8_ELEMENTS,
70   LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND = BIGINT64_ELEMENTS,
71   TERMINAL_FAST_ELEMENTS_KIND = HOLEY_ELEMENTS
72 };
73 
74 const int kElementsKindCount = LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
75 const int kFastElementsKindCount =
76     LAST_FAST_ELEMENTS_KIND - FIRST_FAST_ELEMENTS_KIND + 1;
77 
78 // The number to add to a packed elements kind to reach a holey elements kind
79 const int kFastElementsKindPackedToHoley =
80     HOLEY_SMI_ELEMENTS - PACKED_SMI_ELEMENTS;
81 
82 int ElementsKindToShiftSize(ElementsKind elements_kind);
83 int ElementsKindToByteSize(ElementsKind elements_kind);
84 int GetDefaultHeaderSizeForElementsKind(ElementsKind elements_kind);
85 const char* ElementsKindToString(ElementsKind kind);
86 
GetInitialFastElementsKind()87 inline ElementsKind GetInitialFastElementsKind() { return PACKED_SMI_ELEMENTS; }
88 
89 ElementsKind GetFastElementsKindFromSequenceIndex(int sequence_number);
90 int GetSequenceIndexFromFastElementsKind(ElementsKind elements_kind);
91 
92 ElementsKind GetNextTransitionElementsKind(ElementsKind elements_kind);
93 
IsDictionaryElementsKind(ElementsKind kind)94 inline bool IsDictionaryElementsKind(ElementsKind kind) {
95   return kind == DICTIONARY_ELEMENTS;
96 }
97 
IsSloppyArgumentsElementsKind(ElementsKind kind)98 inline bool IsSloppyArgumentsElementsKind(ElementsKind kind) {
99   return kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS ||
100          kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS;
101 }
102 
IsStringWrapperElementsKind(ElementsKind kind)103 inline bool IsStringWrapperElementsKind(ElementsKind kind) {
104   return kind == FAST_STRING_WRAPPER_ELEMENTS ||
105          kind == SLOW_STRING_WRAPPER_ELEMENTS;
106 }
107 
IsFixedTypedArrayElementsKind(ElementsKind kind)108 inline bool IsFixedTypedArrayElementsKind(ElementsKind kind) {
109   return kind >= FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND &&
110          kind <= LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND;
111 }
112 
IsTerminalElementsKind(ElementsKind kind)113 inline bool IsTerminalElementsKind(ElementsKind kind) {
114   return kind == TERMINAL_FAST_ELEMENTS_KIND ||
115          IsFixedTypedArrayElementsKind(kind);
116 }
117 
IsFastElementsKind(ElementsKind kind)118 inline bool IsFastElementsKind(ElementsKind kind) {
119   STATIC_ASSERT(FIRST_FAST_ELEMENTS_KIND == 0);
120   return kind <= HOLEY_DOUBLE_ELEMENTS;
121 }
122 
IsTransitionElementsKind(ElementsKind kind)123 inline bool IsTransitionElementsKind(ElementsKind kind) {
124   return IsFastElementsKind(kind) || IsFixedTypedArrayElementsKind(kind) ||
125          kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS ||
126          kind == FAST_STRING_WRAPPER_ELEMENTS;
127 }
128 
IsDoubleElementsKind(ElementsKind kind)129 inline bool IsDoubleElementsKind(ElementsKind kind) {
130   return kind == PACKED_DOUBLE_ELEMENTS || kind == HOLEY_DOUBLE_ELEMENTS;
131 }
132 
133 
IsFixedFloatElementsKind(ElementsKind kind)134 inline bool IsFixedFloatElementsKind(ElementsKind kind) {
135   return kind == FLOAT32_ELEMENTS || kind == FLOAT64_ELEMENTS;
136 }
137 
138 
IsDoubleOrFloatElementsKind(ElementsKind kind)139 inline bool IsDoubleOrFloatElementsKind(ElementsKind kind) {
140   return IsDoubleElementsKind(kind) || IsFixedFloatElementsKind(kind);
141 }
142 
IsSmiOrObjectElementsKind(ElementsKind kind)143 inline bool IsSmiOrObjectElementsKind(ElementsKind kind) {
144   return kind == PACKED_SMI_ELEMENTS || kind == HOLEY_SMI_ELEMENTS ||
145          kind == PACKED_ELEMENTS || kind == HOLEY_ELEMENTS;
146 }
147 
IsSmiElementsKind(ElementsKind kind)148 inline bool IsSmiElementsKind(ElementsKind kind) {
149   return kind == PACKED_SMI_ELEMENTS || kind == HOLEY_SMI_ELEMENTS;
150 }
151 
IsFastNumberElementsKind(ElementsKind kind)152 inline bool IsFastNumberElementsKind(ElementsKind kind) {
153   return IsSmiElementsKind(kind) || IsDoubleElementsKind(kind);
154 }
155 
IsObjectElementsKind(ElementsKind kind)156 inline bool IsObjectElementsKind(ElementsKind kind) {
157   return kind == PACKED_ELEMENTS || kind == HOLEY_ELEMENTS;
158 }
159 
IsHoleyElementsKind(ElementsKind kind)160 inline bool IsHoleyElementsKind(ElementsKind kind) {
161   return kind == HOLEY_SMI_ELEMENTS || kind == HOLEY_DOUBLE_ELEMENTS ||
162          kind == HOLEY_ELEMENTS;
163 }
164 
IsHoleyOrDictionaryElementsKind(ElementsKind kind)165 inline bool IsHoleyOrDictionaryElementsKind(ElementsKind kind) {
166   return IsHoleyElementsKind(kind) || kind == DICTIONARY_ELEMENTS;
167 }
168 
169 
IsFastPackedElementsKind(ElementsKind kind)170 inline bool IsFastPackedElementsKind(ElementsKind kind) {
171   return kind == PACKED_SMI_ELEMENTS || kind == PACKED_DOUBLE_ELEMENTS ||
172          kind == PACKED_ELEMENTS;
173 }
174 
175 
GetPackedElementsKind(ElementsKind holey_kind)176 inline ElementsKind GetPackedElementsKind(ElementsKind holey_kind) {
177   if (holey_kind == HOLEY_SMI_ELEMENTS) {
178     return PACKED_SMI_ELEMENTS;
179   }
180   if (holey_kind == HOLEY_DOUBLE_ELEMENTS) {
181     return PACKED_DOUBLE_ELEMENTS;
182   }
183   if (holey_kind == HOLEY_ELEMENTS) {
184     return PACKED_ELEMENTS;
185   }
186   return holey_kind;
187 }
188 
189 
GetHoleyElementsKind(ElementsKind packed_kind)190 inline ElementsKind GetHoleyElementsKind(ElementsKind packed_kind) {
191   if (packed_kind == PACKED_SMI_ELEMENTS) {
192     return HOLEY_SMI_ELEMENTS;
193   }
194   if (packed_kind == PACKED_DOUBLE_ELEMENTS) {
195     return HOLEY_DOUBLE_ELEMENTS;
196   }
197   if (packed_kind == PACKED_ELEMENTS) {
198     return HOLEY_ELEMENTS;
199   }
200   return packed_kind;
201 }
202 
UnionElementsKindUptoPackedness(ElementsKind * a_out,ElementsKind b)203 inline bool UnionElementsKindUptoPackedness(ElementsKind* a_out,
204                                             ElementsKind b) {
205   // Assert that the union of two ElementKinds can be computed via std::max.
206   static_assert(PACKED_SMI_ELEMENTS < HOLEY_SMI_ELEMENTS,
207                 "ElementsKind union not computable via std::max.");
208   static_assert(PACKED_ELEMENTS < HOLEY_ELEMENTS,
209                 "ElementsKind union not computable via std::max.");
210   static_assert(PACKED_DOUBLE_ELEMENTS < HOLEY_DOUBLE_ELEMENTS,
211                 "ElementsKind union not computable via std::max.");
212   ElementsKind a = *a_out;
213   switch (a) {
214     case HOLEY_SMI_ELEMENTS:
215     case PACKED_SMI_ELEMENTS:
216       if (b == PACKED_SMI_ELEMENTS || b == HOLEY_SMI_ELEMENTS) {
217         *a_out = std::max(a, b);
218         return true;
219       }
220       break;
221     case PACKED_ELEMENTS:
222     case HOLEY_ELEMENTS:
223       if (b == PACKED_ELEMENTS || b == HOLEY_ELEMENTS) {
224         *a_out = std::max(a, b);
225         return true;
226       }
227       break;
228     case PACKED_DOUBLE_ELEMENTS:
229     case HOLEY_DOUBLE_ELEMENTS:
230       if (b == PACKED_DOUBLE_ELEMENTS || b == HOLEY_DOUBLE_ELEMENTS) {
231         *a_out = std::max(a, b);
232         return true;
233       }
234       break;
235     default:
236       break;
237   }
238   return false;
239 }
240 
241 bool UnionElementsKindUptoSize(ElementsKind* a_out, ElementsKind b);
242 
FastSmiToObjectElementsKind(ElementsKind from_kind)243 inline ElementsKind FastSmiToObjectElementsKind(ElementsKind from_kind) {
244   DCHECK(IsSmiElementsKind(from_kind));
245   return (from_kind == PACKED_SMI_ELEMENTS) ? PACKED_ELEMENTS : HOLEY_ELEMENTS;
246 }
247 
248 
IsSimpleMapChangeTransition(ElementsKind from_kind,ElementsKind to_kind)249 inline bool IsSimpleMapChangeTransition(ElementsKind from_kind,
250                                         ElementsKind to_kind) {
251   return (GetHoleyElementsKind(from_kind) == to_kind) ||
252          (IsSmiElementsKind(from_kind) && IsObjectElementsKind(to_kind));
253 }
254 
255 
256 bool IsMoreGeneralElementsKindTransition(ElementsKind from_kind,
257                                          ElementsKind to_kind);
258 
259 
GetMoreGeneralElementsKind(ElementsKind from_kind,ElementsKind to_kind)260 inline ElementsKind GetMoreGeneralElementsKind(ElementsKind from_kind,
261                                                ElementsKind to_kind) {
262   if (IsMoreGeneralElementsKindTransition(from_kind, to_kind)) {
263     return to_kind;
264   }
265   return from_kind;
266 }
267 
268 
IsTransitionableFastElementsKind(ElementsKind from_kind)269 inline bool IsTransitionableFastElementsKind(ElementsKind from_kind) {
270   return IsFastElementsKind(from_kind) &&
271          from_kind != TERMINAL_FAST_ELEMENTS_KIND;
272 }
273 
ElementsKindEqual(ElementsKind a,ElementsKind b)274 inline bool ElementsKindEqual(ElementsKind a, ElementsKind b) { return a == b; }
275 
276 }  // namespace internal
277 }  // namespace v8
278 
279 #endif  // V8_ELEMENTS_KIND_H_
280