1/*
2 * Copyright (C) 2012, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef IndexingType_h
27#define IndexingType_h
28
29#include "SpeculatedType.h"
30#include <wtf/StdLibExtras.h>
31
32namespace JSC {
33
34/*
35 Structure of the IndexingType
36 =============================
37 Conceptually, the IndexingType looks like this:
38
39 struct IndexingType {
40 uint8_t isArray:1; // bit 0
41 uint8_t shape:4; // bit 1 - 3
42 uint8_t mayHaveIndexedAccessors:1; // bit 4
43 };
44
45 The shape values (e.g. Int32Shape, ContiguousShape, etc) are an enumeration of
46 various shapes (though not necessarily sequential in terms of their values).
47 Hence, shape values are not bitwise exclusive with respect to each other.
48*/
49
50typedef uint8_t IndexingType;
51
52// Flags for testing the presence of capabilities.
53static const IndexingType IsArray = 0x01;
54
55// The shape of the indexed property storage.
56static const IndexingType IndexingShapeMask = 0x0E;
57static const IndexingType NoIndexingShape = 0x00;
58static const IndexingType UndecidedShape = 0x02; // Only useful for arrays.
59static const IndexingType Int32Shape = 0x04;
60static const IndexingType DoubleShape = 0x06;
61static const IndexingType ContiguousShape = 0x08;
62static const IndexingType ArrayStorageShape = 0x0A;
63static const IndexingType SlowPutArrayStorageShape = 0x0C;
64
65static const IndexingType IndexingShapeShift = 1;
66static const IndexingType NumberOfIndexingShapes = 7;
67
68// Additional flags for tracking the history of the type. These are usually
69// masked off unless you ask for them directly.
70static const IndexingType MayHaveIndexedAccessors = 0x10;
71
72// List of acceptable array types.
73static const IndexingType NonArray = 0x0;
74static const IndexingType NonArrayWithInt32 = Int32Shape;
75static const IndexingType NonArrayWithDouble = DoubleShape;
76static const IndexingType NonArrayWithContiguous = ContiguousShape;
77static const IndexingType NonArrayWithArrayStorage = ArrayStorageShape;
78static const IndexingType NonArrayWithSlowPutArrayStorage = SlowPutArrayStorageShape;
79static const IndexingType ArrayClass = IsArray; // I'd want to call this "Array" but this would lead to disastrous namespace pollution.
80static const IndexingType ArrayWithUndecided = IsArray | UndecidedShape;
81static const IndexingType ArrayWithInt32 = IsArray | Int32Shape;
82static const IndexingType ArrayWithDouble = IsArray | DoubleShape;
83static const IndexingType ArrayWithContiguous = IsArray | ContiguousShape;
84static const IndexingType ArrayWithArrayStorage = IsArray | ArrayStorageShape;
85static const IndexingType ArrayWithSlowPutArrayStorage = IsArray | SlowPutArrayStorageShape;
86
87#define ALL_BLANK_INDEXING_TYPES \
88 NonArray: \
89 case ArrayClass
90
91#define ALL_UNDECIDED_INDEXING_TYPES \
92 ArrayWithUndecided
93
94#define ALL_INT32_INDEXING_TYPES \
95 NonArrayWithInt32: \
96 case ArrayWithInt32
97
98#define ALL_DOUBLE_INDEXING_TYPES \
99 NonArrayWithDouble: \
100 case ArrayWithDouble
101
102#define ALL_CONTIGUOUS_INDEXING_TYPES \
103 NonArrayWithContiguous: \
104 case ArrayWithContiguous
105
106#define ARRAY_WITH_ARRAY_STORAGE_INDEXING_TYPES \
107 ArrayWithArrayStorage: \
108 case ArrayWithSlowPutArrayStorage
109
110#define ALL_ARRAY_STORAGE_INDEXING_TYPES \
111 NonArrayWithArrayStorage: \
112 case NonArrayWithSlowPutArrayStorage: \
113 case ARRAY_WITH_ARRAY_STORAGE_INDEXING_TYPES
114
115static inline bool hasIndexedProperties(IndexingType indexingType)
116{
117 return (indexingType & IndexingShapeMask) != NoIndexingShape;
118}
119
120static inline bool hasUndecided(IndexingType indexingType)
121{
122 return (indexingType & IndexingShapeMask) == UndecidedShape;
123}
124
125static inline bool hasInt32(IndexingType indexingType)
126{
127 return (indexingType & IndexingShapeMask) == Int32Shape;
128}
129
130static inline bool hasDouble(IndexingType indexingType)
131{
132 return (indexingType & IndexingShapeMask) == DoubleShape;
133}
134
135static inline bool hasContiguous(IndexingType indexingType)
136{
137 return (indexingType & IndexingShapeMask) == ContiguousShape;
138}
139
140static inline bool hasArrayStorage(IndexingType indexingType)
141{
142 return (indexingType & IndexingShapeMask) == ArrayStorageShape;
143}
144
145static inline bool hasAnyArrayStorage(IndexingType indexingType)
146{
147 return static_cast<uint8_t>(indexingType & IndexingShapeMask) >= ArrayStorageShape;
148}
149
150static inline bool shouldUseSlowPut(IndexingType indexingType)
151{
152 return (indexingType & IndexingShapeMask) == SlowPutArrayStorageShape;
153}
154
155// Return an indexing type that can handle all of the elements of both indexing types.
156IndexingType leastUpperBoundOfIndexingTypes(IndexingType, IndexingType);
157
158IndexingType leastUpperBoundOfIndexingTypeAndType(IndexingType, SpeculatedType);
159IndexingType leastUpperBoundOfIndexingTypeAndValue(IndexingType, JSValue);
160
161void dumpIndexingType(PrintStream&, IndexingType);
162MAKE_PRINT_ADAPTOR(IndexingTypeDump, IndexingType, dumpIndexingType);
163
164// Mask of all possible types.
165static const IndexingType AllArrayTypes = IndexingShapeMask | IsArray;
166
167// Mask of all possible types including the history.
168static const IndexingType AllArrayTypesAndHistory = AllArrayTypes | MayHaveIndexedAccessors;
169
170} // namespace JSC
171
172#endif // IndexingType_h
173
174