1/*
2 * Copyright (C) 2011, 2012, 2013 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef SpeculatedType_h
30#define SpeculatedType_h
31
32#include "JSCJSValue.h"
33#include <wtf/PrintStream.h>
34
35namespace JSC {
36
37class Structure;
38
39typedef uint32_t SpeculatedType;
40static const SpeculatedType SpecNone              = 0x00000000; // We don't know anything yet.
41static const SpeculatedType SpecFinalObject       = 0x00000001; // It's definitely a JSFinalObject.
42static const SpeculatedType SpecArray             = 0x00000002; // It's definitely a JSArray.
43static const SpeculatedType SpecFunction          = 0x00000008; // It's definitely a JSFunction or one of its subclasses.
44static const SpeculatedType SpecInt8Array         = 0x00000010; // It's definitely an Int8Array or one of its subclasses.
45static const SpeculatedType SpecInt16Array        = 0x00000020; // It's definitely an Int16Array or one of its subclasses.
46static const SpeculatedType SpecInt32Array        = 0x00000040; // It's definitely an Int32Array or one of its subclasses.
47static const SpeculatedType SpecUint8Array        = 0x00000080; // It's definitely an Uint8Array or one of its subclasses.
48static const SpeculatedType SpecUint8ClampedArray = 0x00000100; // It's definitely an Uint8ClampedArray or one of its subclasses.
49static const SpeculatedType SpecUint16Array       = 0x00000200; // It's definitely an Uint16Array or one of its subclasses.
50static const SpeculatedType SpecUint32Array       = 0x00000400; // It's definitely an Uint32Array or one of its subclasses.
51static const SpeculatedType SpecFloat32Array      = 0x00000800; // It's definitely an Uint16Array or one of its subclasses.
52static const SpeculatedType SpecFloat64Array      = 0x00001000; // It's definitely an Uint16Array or one of its subclasses.
53static const SpeculatedType SpecArguments         = 0x00002000; // It's definitely an Arguments object.
54static const SpeculatedType SpecStringObject      = 0x00004000; // It's definitely a StringObject.
55static const SpeculatedType SpecObjectOther       = 0x00008000; // It's definitely an object but not JSFinalObject, JSArray, or JSFunction.
56static const SpeculatedType SpecObject            = 0x0000ffff; // Bitmask used for testing for any kind of object prediction.
57static const SpeculatedType SpecString            = 0x00010000; // It's definitely a JSString.
58static const SpeculatedType SpecCellOther         = 0x00020000; // It's definitely a JSCell but not a subclass of JSObject and definitely not a JSString.
59static const SpeculatedType SpecCell              = 0x0003ffff; // It's definitely a JSCell.
60static const SpeculatedType SpecInt32             = 0x00800000; // It's definitely an Int32.
61static const SpeculatedType SpecDoubleReal        = 0x01000000; // It's definitely a non-NaN double.
62static const SpeculatedType SpecDoubleNaN         = 0x02000000; // It's definitely a NaN.
63static const SpeculatedType SpecDouble            = 0x03000000; // It's either a non-NaN or a NaN double.
64static const SpeculatedType SpecRealNumber        = 0x01800000; // It's either an Int32 or a DoubleReal.
65static const SpeculatedType SpecNumber            = 0x03800000; // It's either an Int32 or a Double.
66static const SpeculatedType SpecBoolean           = 0x04000000; // It's definitely a Boolean.
67static const SpeculatedType SpecOther             = 0x08000000; // It's definitely none of the above.
68static const SpeculatedType SpecTop               = 0x0fffffff; // It can be any of the above.
69static const SpeculatedType SpecEmpty             = 0x10000000; // It's definitely an empty value marker.
70static const SpeculatedType SpecEmptyOrTop        = 0x1fffffff; // It can be any of the above.
71static const SpeculatedType FixedIndexedStorageMask    = SpecInt8Array | SpecInt16Array | SpecInt32Array | SpecUint8Array | SpecUint8ClampedArray | SpecUint16Array | SpecUint32Array | SpecFloat32Array | SpecFloat64Array;
72
73typedef bool (*SpeculatedTypeChecker)(SpeculatedType);
74
75// Dummy prediction checker, only useful if someone insists on requiring a prediction checker.
76inline bool isAnySpeculation(SpeculatedType)
77{
78    return true;
79}
80
81inline bool isCellSpeculation(SpeculatedType value)
82{
83    return !!(value & SpecCell) && !(value & ~SpecCell);
84}
85
86inline bool isObjectSpeculation(SpeculatedType value)
87{
88    return !!(value & SpecObject) && !(value & ~SpecObject);
89}
90
91inline bool isObjectOrOtherSpeculation(SpeculatedType value)
92{
93    return !!(value & (SpecObject | SpecOther)) && !(value & ~(SpecObject | SpecOther));
94}
95
96inline bool isFinalObjectSpeculation(SpeculatedType value)
97{
98    return value == SpecFinalObject;
99}
100
101inline bool isFinalObjectOrOtherSpeculation(SpeculatedType value)
102{
103    return !!(value & (SpecFinalObject | SpecOther)) && !(value & ~(SpecFinalObject | SpecOther));
104}
105
106inline bool isFixedIndexedStorageObjectSpeculation(SpeculatedType value)
107{
108    return !!value && (value & FixedIndexedStorageMask) == value;
109}
110
111inline bool isStringSpeculation(SpeculatedType value)
112{
113    return value == SpecString;
114}
115
116inline bool isArraySpeculation(SpeculatedType value)
117{
118    return value == SpecArray;
119}
120
121inline bool isFunctionSpeculation(SpeculatedType value)
122{
123    return value == SpecFunction;
124}
125
126inline bool isInt8ArraySpeculation(SpeculatedType value)
127{
128    return value == SpecInt8Array;
129}
130
131inline bool isInt16ArraySpeculation(SpeculatedType value)
132{
133    return value == SpecInt16Array;
134}
135
136inline bool isInt32ArraySpeculation(SpeculatedType value)
137{
138    return value == SpecInt32Array;
139}
140
141inline bool isUint8ArraySpeculation(SpeculatedType value)
142{
143    return value == SpecUint8Array;
144}
145
146inline bool isUint8ClampedArraySpeculation(SpeculatedType value)
147{
148    return value == SpecUint8ClampedArray;
149}
150
151inline bool isUint16ArraySpeculation(SpeculatedType value)
152{
153    return value == SpecUint16Array;
154}
155
156inline bool isUint32ArraySpeculation(SpeculatedType value)
157{
158    return value == SpecUint32Array;
159}
160
161inline bool isFloat32ArraySpeculation(SpeculatedType value)
162{
163    return value == SpecFloat32Array;
164}
165
166inline bool isFloat64ArraySpeculation(SpeculatedType value)
167{
168    return value == SpecFloat64Array;
169}
170
171inline bool isArgumentsSpeculation(SpeculatedType value)
172{
173    return !!value && (value & SpecArguments) == value;
174}
175
176inline bool isActionableIntMutableArraySpeculation(SpeculatedType value)
177{
178    return isInt8ArraySpeculation(value)
179        || isInt16ArraySpeculation(value)
180        || isInt32ArraySpeculation(value)
181        || isUint8ArraySpeculation(value)
182        || isUint8ClampedArraySpeculation(value)
183        || isUint16ArraySpeculation(value)
184        || isUint32ArraySpeculation(value);
185}
186
187inline bool isActionableFloatMutableArraySpeculation(SpeculatedType value)
188{
189    return isFloat32ArraySpeculation(value)
190        || isFloat64ArraySpeculation(value);
191}
192
193inline bool isActionableTypedMutableArraySpeculation(SpeculatedType value)
194{
195    return isActionableIntMutableArraySpeculation(value)
196        || isActionableFloatMutableArraySpeculation(value);
197}
198
199inline bool isActionableMutableArraySpeculation(SpeculatedType value)
200{
201    return isArraySpeculation(value)
202        || isArgumentsSpeculation(value)
203        || isActionableTypedMutableArraySpeculation(value);
204}
205
206inline bool isActionableArraySpeculation(SpeculatedType value)
207{
208    return isStringSpeculation(value)
209        || isActionableMutableArraySpeculation(value);
210}
211
212inline bool isArrayOrOtherSpeculation(SpeculatedType value)
213{
214    return !!(value & (SpecArray | SpecOther)) && !(value & ~(SpecArray | SpecOther));
215}
216
217inline bool isStringObjectSpeculation(SpeculatedType value)
218{
219    return value == SpecStringObject;
220}
221
222inline bool isStringOrStringObjectSpeculation(SpeculatedType value)
223{
224    return !!value && !(value & ~(SpecString | SpecStringObject));
225}
226
227inline bool isInt32Speculation(SpeculatedType value)
228{
229    return value == SpecInt32;
230}
231
232inline bool isInt32SpeculationForArithmetic(SpeculatedType value)
233{
234    return !(value & SpecDouble);
235}
236
237inline bool isInt32SpeculationExpectingDefined(SpeculatedType value)
238{
239    return isInt32Speculation(value & ~SpecOther);
240}
241
242inline bool isDoubleRealSpeculation(SpeculatedType value)
243{
244    return value == SpecDoubleReal;
245}
246
247inline bool isDoubleSpeculation(SpeculatedType value)
248{
249    return !!value && (value & SpecDouble) == value;
250}
251
252inline bool isDoubleSpeculationForArithmetic(SpeculatedType value)
253{
254    return !!(value & SpecDouble);
255}
256
257inline bool isRealNumberSpeculation(SpeculatedType value)
258{
259    return !!(value & SpecRealNumber) && !(value & ~SpecRealNumber);
260}
261
262inline bool isNumberSpeculation(SpeculatedType value)
263{
264    return !!(value & SpecNumber) && !(value & ~SpecNumber);
265}
266
267inline bool isNumberSpeculationExpectingDefined(SpeculatedType value)
268{
269    return isNumberSpeculation(value & ~SpecOther);
270}
271
272inline bool isBooleanSpeculation(SpeculatedType value)
273{
274    return value == SpecBoolean;
275}
276
277inline bool isOtherSpeculation(SpeculatedType value)
278{
279    return value == SpecOther;
280}
281
282inline bool isOtherOrEmptySpeculation(SpeculatedType value)
283{
284    return !value || value == SpecOther;
285}
286
287inline bool isEmptySpeculation(SpeculatedType value)
288{
289    return value == SpecEmpty;
290}
291
292void dumpSpeculation(PrintStream&, SpeculatedType);
293void dumpSpeculationAbbreviated(PrintStream&, SpeculatedType);
294
295MAKE_PRINT_ADAPTOR(SpeculationDump, SpeculatedType, dumpSpeculation);
296MAKE_PRINT_ADAPTOR(AbbreviatedSpeculationDump, SpeculatedType, dumpSpeculationAbbreviated);
297
298// Merge two predictions. Note that currently this just does left | right. It may
299// seem tempting to do so directly, but you would be doing so at your own peril,
300// since the merging protocol SpeculatedType may change at any time (and has already
301// changed several times in its history).
302inline SpeculatedType mergeSpeculations(SpeculatedType left, SpeculatedType right)
303{
304    return left | right;
305}
306
307template<typename T>
308inline bool mergeSpeculation(T& left, SpeculatedType right)
309{
310    SpeculatedType newSpeculation = static_cast<T>(mergeSpeculations(static_cast<SpeculatedType>(left), right));
311    bool result = newSpeculation != static_cast<SpeculatedType>(left);
312    left = newSpeculation;
313    return result;
314}
315
316inline bool speculationChecked(SpeculatedType actual, SpeculatedType desired)
317{
318    return (actual | desired) == desired;
319}
320
321SpeculatedType speculationFromClassInfo(const ClassInfo*);
322SpeculatedType speculationFromStructure(Structure*);
323SpeculatedType speculationFromCell(JSCell*);
324SpeculatedType speculationFromValue(JSValue);
325
326} // namespace JSC
327
328#endif // SpeculatedType_h
329