1/*
2 * Copyright (C) 2012 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
34typedef uint8_t IndexingType;
35
36// Flags for testing the presence of capabilities.
37static const IndexingType IsArray                  = 0x01;
38
39// The shape of the indexed property storage.
40static const IndexingType IndexingShapeMask        = 0x1E;
41static const IndexingType NoIndexingShape          = 0x00;
42static const IndexingType UndecidedShape           = 0x02; // Only useful for arrays.
43static const IndexingType Int32Shape               = 0x14;
44static const IndexingType DoubleShape              = 0x16;
45static const IndexingType ContiguousShape          = 0x1A;
46static const IndexingType ArrayStorageShape        = 0x1C;
47static const IndexingType SlowPutArrayStorageShape = 0x1E;
48
49static const IndexingType IndexingShapeShift       = 1;
50static const IndexingType NumberOfIndexingShapes   = 16;
51
52// Additional flags for tracking the history of the type. These are usually
53// masked off unless you ask for them directly.
54static const IndexingType MayHaveIndexedAccessors  = 0x20;
55
56// List of acceptable array types.
57static const IndexingType NonArray                        = 0x0;
58static const IndexingType NonArrayWithInt32               = Int32Shape;
59static const IndexingType NonArrayWithDouble              = DoubleShape;
60static const IndexingType NonArrayWithContiguous          = ContiguousShape;
61static const IndexingType NonArrayWithArrayStorage        = ArrayStorageShape;
62static const IndexingType NonArrayWithSlowPutArrayStorage = SlowPutArrayStorageShape;
63static const IndexingType ArrayClass                      = IsArray; // I'd want to call this "Array" but this would lead to disastrous namespace pollution.
64static const IndexingType ArrayWithUndecided              = IsArray | UndecidedShape;
65static const IndexingType ArrayWithInt32                  = IsArray | Int32Shape;
66static const IndexingType ArrayWithDouble                 = IsArray | DoubleShape;
67static const IndexingType ArrayWithContiguous             = IsArray | ContiguousShape;
68static const IndexingType ArrayWithArrayStorage           = IsArray | ArrayStorageShape;
69static const IndexingType ArrayWithSlowPutArrayStorage    = IsArray | SlowPutArrayStorageShape;
70
71#define ALL_BLANK_INDEXING_TYPES \
72    NonArray:                    \
73    case ArrayClass
74
75#define ALL_UNDECIDED_INDEXING_TYPES \
76    ArrayWithUndecided
77
78#define ALL_INT32_INDEXING_TYPES      \
79    NonArrayWithInt32:                \
80    case ArrayWithInt32
81
82#define ALL_DOUBLE_INDEXING_TYPES     \
83    NonArrayWithDouble:               \
84    case ArrayWithDouble
85
86#define ALL_CONTIGUOUS_INDEXING_TYPES \
87    NonArrayWithContiguous:           \
88    case ArrayWithContiguous
89
90#define ARRAY_WITH_ARRAY_STORAGE_INDEXING_TYPES \
91    ArrayWithArrayStorage:                      \
92    case ArrayWithSlowPutArrayStorage
93
94#define ALL_ARRAY_STORAGE_INDEXING_TYPES                \
95    NonArrayWithArrayStorage:                           \
96    case NonArrayWithSlowPutArrayStorage:               \
97    case ARRAY_WITH_ARRAY_STORAGE_INDEXING_TYPES
98
99static inline bool hasIndexedProperties(IndexingType indexingType)
100{
101    return (indexingType & IndexingShapeMask) != NoIndexingShape;
102}
103
104static inline bool hasUndecided(IndexingType indexingType)
105{
106    return (indexingType & IndexingShapeMask) == UndecidedShape;
107}
108
109static inline bool hasInt32(IndexingType indexingType)
110{
111    return (indexingType & IndexingShapeMask) == Int32Shape;
112}
113
114static inline bool hasDouble(IndexingType indexingType)
115{
116    return (indexingType & IndexingShapeMask) == DoubleShape;
117}
118
119static inline bool hasContiguous(IndexingType indexingType)
120{
121    return (indexingType & IndexingShapeMask) == ContiguousShape;
122}
123
124static inline bool hasArrayStorage(IndexingType indexingType)
125{
126    return (indexingType & IndexingShapeMask) == ArrayStorageShape;
127}
128
129static inline bool hasAnyArrayStorage(IndexingType indexingType)
130{
131    return static_cast<uint8_t>((indexingType & IndexingShapeMask) - ArrayStorageShape) <= static_cast<uint8_t>(SlowPutArrayStorageShape - ArrayStorageShape);
132}
133
134static inline bool shouldUseSlowPut(IndexingType indexingType)
135{
136    return (indexingType & IndexingShapeMask) == SlowPutArrayStorageShape;
137}
138
139// Return an indexing type that can handle all of the elements of both indexing types.
140IndexingType leastUpperBoundOfIndexingTypes(IndexingType, IndexingType);
141
142IndexingType leastUpperBoundOfIndexingTypeAndType(IndexingType, SpeculatedType);
143IndexingType leastUpperBoundOfIndexingTypeAndValue(IndexingType, JSValue);
144
145void dumpIndexingType(PrintStream&, IndexingType);
146MAKE_PRINT_ADAPTOR(IndexingTypeDump, IndexingType, dumpIndexingType);
147
148// Mask of all possible types.
149static const IndexingType AllArrayTypes            = 31;
150
151// Mask of all possible types including the history.
152static const IndexingType AllArrayTypesAndHistory  = 127;
153
154} // namespace JSC
155
156#endif // IndexingType_h
157
158