2 * Copyright (C) 2011-2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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 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.
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.
29 #ifndef SpeculatedType_h
30 #define SpeculatedType_h
32 #include "JSCJSValue.h"
33 #include "TypedArrayType.h"
34 #include <wtf/PrintStream.h>
40 typedef uint32_t SpeculatedType;
41 static const SpeculatedType SpecNone = 0; // We don't know anything yet.
42 static const SpeculatedType SpecFinalObject = 1u << 0; // It's definitely a JSFinalObject.
43 static const SpeculatedType SpecArray = 1u << 1; // It's definitely a JSArray.
44 static const SpeculatedType SpecFunction = 1u << 2; // It's definitely a JSFunction.
45 static const SpeculatedType SpecInt8Array = 1u << 3; // It's definitely an Int8Array or one of its subclasses.
46 static const SpeculatedType SpecInt16Array = 1u << 4; // It's definitely an Int16Array or one of its subclasses.
47 static const SpeculatedType SpecInt32Array = 1u << 5; // It's definitely an Int32Array or one of its subclasses.
48 static const SpeculatedType SpecUint8Array = 1u << 6; // It's definitely an Uint8Array or one of its subclasses.
49 static const SpeculatedType SpecUint8ClampedArray = 1u << 7; // It's definitely an Uint8ClampedArray or one of its subclasses.
50 static const SpeculatedType SpecUint16Array = 1u << 8; // It's definitely an Uint16Array or one of its subclasses.
51 static const SpeculatedType SpecUint32Array = 1u << 9; // It's definitely an Uint32Array or one of its subclasses.
52 static const SpeculatedType SpecFloat32Array = 1u << 10; // It's definitely an Uint16Array or one of its subclasses.
53 static const SpeculatedType SpecFloat64Array = 1u << 11; // It's definitely an Uint16Array or one of its subclasses.
54 static const SpeculatedType SpecTypedArrayView = SpecInt8Array | SpecInt16Array | SpecInt32Array | SpecUint8Array | SpecUint8ClampedArray | SpecUint16Array | SpecUint32Array | SpecFloat32Array | SpecFloat64Array;
55 static const SpeculatedType SpecDirectArguments = 1u << 12; // It's definitely a DirectArguments object.
56 static const SpeculatedType SpecScopedArguments = 1u << 13; // It's definitely a ScopedArguments object.
57 static const SpeculatedType SpecStringObject = 1u << 14; // It's definitely a StringObject.
58 static const SpeculatedType SpecRegExpObject = 1u << 15; // It's definitely a RegExpObject (and not any subclass of RegExpObject).
59 static const SpeculatedType SpecObjectOther = 1u << 16; // It's definitely an object but not JSFinalObject, JSArray, or JSFunction.
60 static const SpeculatedType SpecObject = SpecFinalObject | SpecArray | SpecFunction | SpecTypedArrayView | SpecDirectArguments | SpecScopedArguments | SpecStringObject | SpecRegExpObject | SpecObjectOther; // Bitmask used for testing for any kind of object prediction.
61 static const SpeculatedType SpecStringIdent = 1u << 17; // It's definitely a JSString, and it's an identifier.
62 static const SpeculatedType SpecStringVar = 1u << 18; // It's definitely a JSString, and it's not an identifier.
63 static const SpeculatedType SpecString = SpecStringIdent | SpecStringVar; // It's definitely a JSString.
64 static const SpeculatedType SpecSymbol = 1u << 19; // It's definitely a Symbol.
65 static const SpeculatedType SpecCellOther = 1u << 20; // It's definitely a JSCell but not a subclass of JSObject and definitely not a JSString or a Symbol. FIXME: This shouldn't be part of heap-top or bytecode-top. https://bugs.webkit.org/show_bug.cgi?id=133078
66 static const SpeculatedType SpecCell = SpecObject | SpecString | SpecSymbol | SpecCellOther; // It's definitely a JSCell.
67 static const SpeculatedType SpecBoolInt32 = 1u << 21; // It's definitely an Int32 with value 0 or 1.
68 static const SpeculatedType SpecNonBoolInt32 = 1u << 22; // It's definitely an Int32 with value other than 0 or 1.
69 static const SpeculatedType SpecInt32 = SpecBoolInt32 | SpecNonBoolInt32; // It's definitely an Int32.
70 static const SpeculatedType SpecInt52 = 1u << 23; // It's definitely an Int52 and we intend it to unbox it. It's also definitely not an Int32.
71 static const SpeculatedType SpecMachineInt = SpecInt32 | SpecInt52; // It's something that we can do machine int arithmetic on.
72 static const SpeculatedType SpecInt52AsDouble = 1u << 24; // It's definitely an Int52 and it's inside a double.
73 static const SpeculatedType SpecInteger = SpecMachineInt | SpecInt52AsDouble; // It's definitely some kind of integer.
74 static const SpeculatedType SpecNonIntAsDouble = 1u << 25; // It's definitely not an Int52 but it's a real number and it's a double.
75 static const SpeculatedType SpecDoubleReal = SpecNonIntAsDouble | SpecInt52AsDouble; // It's definitely a non-NaN double.
76 static const SpeculatedType SpecDoublePureNaN = 1u << 26; // It's definitely a NaN that is sae to tag (i.e. pure).
77 static const SpeculatedType SpecDoubleImpureNaN = 1u << 27; // It's definitely a NaN that is unsafe to tag (i.e. impure).
78 static const SpeculatedType SpecDoubleNaN = SpecDoublePureNaN | SpecDoubleImpureNaN; // It's definitely some kind of NaN.
79 static const SpeculatedType SpecBytecodeDouble = SpecDoubleReal | SpecDoublePureNaN; // It's either a non-NaN or a NaN double, but it's definitely not impure NaN.
80 static const SpeculatedType SpecFullDouble = SpecDoubleReal | SpecDoubleNaN; // It's either a non-NaN or a NaN double.
81 static const SpeculatedType SpecBytecodeRealNumber = SpecInt32 | SpecDoubleReal; // It's either an Int32 or a DoubleReal.
82 static const SpeculatedType SpecFullRealNumber = SpecMachineInt | SpecDoubleReal; // It's either an Int32 or a DoubleReal, or a Int52.
83 static const SpeculatedType SpecBytecodeNumber = SpecInt32 | SpecBytecodeDouble; // It's either an Int32 or a Double, and the Double cannot be an impure NaN.
84 static const SpeculatedType SpecFullNumber = SpecMachineInt | SpecFullDouble; // It's either an Int32, Int52, or a Double, and the Double can be impure NaN.
85 static const SpeculatedType SpecBoolean = 1u << 28; // It's definitely a Boolean.
86 static const SpeculatedType SpecOther = 1u << 29; // It's definitely either Null or Undefined.
87 static const SpeculatedType SpecMisc = SpecBoolean | SpecOther; // It's definitely either a boolean, Null, or Undefined.
88 static const SpeculatedType SpecHeapTop = SpecCell | SpecBytecodeNumber | SpecMisc; // It can be any of the above, except for SpecInt52 and SpecDoubleImpureNaN.
89 static const SpeculatedType SpecPrimitive = SpecString | SpecSymbol | SpecBytecodeNumber | SpecMisc; // It's any non-Object JSValue.
90 static const SpeculatedType SpecEmpty = 1u << 30; // It's definitely an empty value marker.
91 static const SpeculatedType SpecBytecodeTop = SpecHeapTop | SpecEmpty; // It can be any of the above, except for SpecInt52 and SpecDoubleImpureNaN. Corresponds to what could be found in a bytecode local.
92 static const SpeculatedType SpecFullTop = SpecBytecodeTop | SpecFullNumber; // It can be anything that bytecode could see plus exotic encodings of numbers.
94 typedef bool (*SpeculatedTypeChecker)(SpeculatedType);
96 // Dummy prediction checker, only useful if someone insists on requiring a prediction checker.
97 inline bool isAnySpeculation(SpeculatedType)
102 inline bool isCellSpeculation(SpeculatedType value)
104 return !!(value & SpecCell) && !(value & ~SpecCell);
107 inline bool isCellOrOtherSpeculation(SpeculatedType value)
109 return !!value && !(value & ~(SpecCell | SpecOther));
112 inline bool isNotCellSpeculation(SpeculatedType value)
114 return !(value & SpecCell) && value;
117 inline bool isObjectSpeculation(SpeculatedType value)
119 return !!(value & SpecObject) && !(value & ~SpecObject);
122 inline bool isObjectOrOtherSpeculation(SpeculatedType value)
124 return !!(value & (SpecObject | SpecOther)) && !(value & ~(SpecObject | SpecOther));
127 inline bool isFinalObjectSpeculation(SpeculatedType value)
129 return value == SpecFinalObject;
132 inline bool isFinalObjectOrOtherSpeculation(SpeculatedType value)
134 return !!(value & (SpecFinalObject | SpecOther)) && !(value & ~(SpecFinalObject | SpecOther));
137 inline bool isStringIdentSpeculation(SpeculatedType value)
139 return value == SpecStringIdent;
142 inline bool isNotStringVarSpeculation(SpeculatedType value)
144 return !(value & SpecStringVar);
147 inline bool isStringSpeculation(SpeculatedType value)
149 return !!value && (value & SpecString) == value;
152 inline bool isStringOrOtherSpeculation(SpeculatedType value)
154 return !!value && (value & (SpecString | SpecOther)) == value;
157 inline bool isSymbolSpeculation(SpeculatedType value)
159 return value == SpecSymbol;
162 inline bool isArraySpeculation(SpeculatedType value)
164 return value == SpecArray;
167 inline bool isFunctionSpeculation(SpeculatedType value)
169 return value == SpecFunction;
172 inline bool isInt8ArraySpeculation(SpeculatedType value)
174 return value == SpecInt8Array;
177 inline bool isInt16ArraySpeculation(SpeculatedType value)
179 return value == SpecInt16Array;
182 inline bool isInt32ArraySpeculation(SpeculatedType value)
184 return value == SpecInt32Array;
187 inline bool isUint8ArraySpeculation(SpeculatedType value)
189 return value == SpecUint8Array;
192 inline bool isUint8ClampedArraySpeculation(SpeculatedType value)
194 return value == SpecUint8ClampedArray;
197 inline bool isUint16ArraySpeculation(SpeculatedType value)
199 return value == SpecUint16Array;
202 inline bool isUint32ArraySpeculation(SpeculatedType value)
204 return value == SpecUint32Array;
207 inline bool isFloat32ArraySpeculation(SpeculatedType value)
209 return value == SpecFloat32Array;
212 inline bool isFloat64ArraySpeculation(SpeculatedType value)
214 return value == SpecFloat64Array;
217 inline bool isDirectArgumentsSpeculation(SpeculatedType value)
219 return value == SpecDirectArguments;
222 inline bool isScopedArgumentsSpeculation(SpeculatedType value)
224 return value == SpecScopedArguments;
227 inline bool isActionableIntMutableArraySpeculation(SpeculatedType value)
229 return isInt8ArraySpeculation(value)
230 || isInt16ArraySpeculation(value)
231 || isInt32ArraySpeculation(value)
232 || isUint8ArraySpeculation(value)
233 || isUint8ClampedArraySpeculation(value)
234 || isUint16ArraySpeculation(value)
235 || isUint32ArraySpeculation(value);
238 inline bool isActionableFloatMutableArraySpeculation(SpeculatedType value)
240 return isFloat32ArraySpeculation(value)
241 || isFloat64ArraySpeculation(value);
244 inline bool isActionableTypedMutableArraySpeculation(SpeculatedType value)
246 return isActionableIntMutableArraySpeculation(value)
247 || isActionableFloatMutableArraySpeculation(value);
250 inline bool isActionableMutableArraySpeculation(SpeculatedType value)
252 return isArraySpeculation(value)
253 || isActionableTypedMutableArraySpeculation(value);
256 inline bool isActionableArraySpeculation(SpeculatedType value)
258 return isStringSpeculation(value)
259 || isDirectArgumentsSpeculation(value)
260 || isScopedArgumentsSpeculation(value)
261 || isActionableMutableArraySpeculation(value);
264 inline bool isArrayOrOtherSpeculation(SpeculatedType value)
266 return !!(value & (SpecArray | SpecOther)) && !(value & ~(SpecArray | SpecOther));
269 inline bool isStringObjectSpeculation(SpeculatedType value)
271 return value == SpecStringObject;
274 inline bool isStringOrStringObjectSpeculation(SpeculatedType value)
276 return !!value && !(value & ~(SpecString | SpecStringObject));
279 inline bool isRegExpObjectSpeculation(SpeculatedType value)
281 return value == SpecRegExpObject;
284 inline bool isBoolInt32Speculation(SpeculatedType value)
286 return value == SpecBoolInt32;
289 inline bool isInt32Speculation(SpeculatedType value)
291 return value && !(value & ~SpecInt32);
294 inline bool isInt32OrBooleanSpeculation(SpeculatedType value)
296 return value && !(value & ~(SpecBoolean | SpecInt32));
299 inline bool isInt32SpeculationForArithmetic(SpeculatedType value)
301 return !(value & (SpecFullDouble | SpecInt52));
304 inline bool isInt32OrBooleanSpeculationForArithmetic(SpeculatedType value)
306 return !(value & (SpecFullDouble | SpecInt52));
309 inline bool isInt32OrBooleanSpeculationExpectingDefined(SpeculatedType value)
311 return isInt32OrBooleanSpeculation(value & ~SpecOther);
314 inline bool isInt52Speculation(SpeculatedType value)
316 return value == SpecInt52;
319 inline bool isMachineIntSpeculation(SpeculatedType value)
321 return !!value && (value & SpecMachineInt) == value;
324 inline bool isInt52AsDoubleSpeculation(SpeculatedType value)
326 return value == SpecInt52AsDouble;
329 inline bool isIntegerSpeculation(SpeculatedType value)
331 return !!value && (value & SpecInteger) == value;
334 inline bool isDoubleRealSpeculation(SpeculatedType value)
336 return !!value && (value & SpecDoubleReal) == value;
339 inline bool isDoubleSpeculation(SpeculatedType value)
341 return !!value && (value & SpecFullDouble) == value;
344 inline bool isDoubleSpeculationForArithmetic(SpeculatedType value)
346 return !!(value & SpecFullDouble);
349 inline bool isBytecodeRealNumberSpeculation(SpeculatedType value)
351 return !!(value & SpecBytecodeRealNumber) && !(value & ~SpecBytecodeRealNumber);
354 inline bool isFullRealNumberSpeculation(SpeculatedType value)
356 return !!(value & SpecFullRealNumber) && !(value & ~SpecFullRealNumber);
359 inline bool isBytecodeNumberSpeculation(SpeculatedType value)
361 return !!(value & SpecBytecodeNumber) && !(value & ~SpecBytecodeNumber);
364 inline bool isFullNumberSpeculation(SpeculatedType value)
366 return !!(value & SpecFullNumber) && !(value & ~SpecFullNumber);
369 inline bool isFullNumberOrBooleanSpeculation(SpeculatedType value)
371 return value && !(value & ~(SpecFullNumber | SpecBoolean));
374 inline bool isFullNumberOrBooleanSpeculationExpectingDefined(SpeculatedType value)
376 return isFullNumberOrBooleanSpeculation(value & ~SpecOther);
379 inline bool isBooleanSpeculation(SpeculatedType value)
381 return value == SpecBoolean;
384 inline bool isOtherSpeculation(SpeculatedType value)
386 return value == SpecOther;
389 inline bool isMiscSpeculation(SpeculatedType value)
391 return !!value && !(value & ~SpecMisc);
394 inline bool isOtherOrEmptySpeculation(SpeculatedType value)
396 return !value || value == SpecOther;
399 inline bool isEmptySpeculation(SpeculatedType value)
401 return value == SpecEmpty;
404 inline bool isUntypedSpeculationForArithmetic(SpeculatedType value)
406 return !!(value & ~(SpecFullNumber | SpecBoolean));
409 inline bool isUntypedSpeculationForBitOps(SpeculatedType value)
411 return !!(value & ~(SpecFullNumber | SpecBoolean | SpecOther));
414 void dumpSpeculation(PrintStream&, SpeculatedType);
415 void dumpSpeculationAbbreviated(PrintStream&, SpeculatedType);
417 MAKE_PRINT_ADAPTOR(SpeculationDump, SpeculatedType, dumpSpeculation);
418 MAKE_PRINT_ADAPTOR(AbbreviatedSpeculationDump, SpeculatedType, dumpSpeculationAbbreviated);
420 // Merge two predictions. Note that currently this just does left | right. It may
421 // seem tempting to do so directly, but you would be doing so at your own peril,
422 // since the merging protocol SpeculatedType may change at any time (and has already
423 // changed several times in its history).
424 inline SpeculatedType mergeSpeculations(SpeculatedType left, SpeculatedType right)
430 inline bool mergeSpeculation(T& left, SpeculatedType right)
432 SpeculatedType newSpeculation = static_cast<T>(mergeSpeculations(static_cast<SpeculatedType>(left), right));
433 bool result = newSpeculation != static_cast<SpeculatedType>(left);
434 left = newSpeculation;
438 inline bool speculationChecked(SpeculatedType actual, SpeculatedType desired)
440 return (actual | desired) == desired;
443 SpeculatedType speculationFromClassInfo(const ClassInfo*);
444 SpeculatedType speculationFromStructure(Structure*);
445 SpeculatedType speculationFromCell(JSCell*);
446 SpeculatedType speculationFromValue(JSValue);
448 SpeculatedType speculationFromTypedArrayType(TypedArrayType); // only valid for typed views.
449 TypedArrayType typedArrayTypeFromSpeculation(SpeculatedType);
451 SpeculatedType leastUpperBoundOfStrictlyEquivalentSpeculations(SpeculatedType);
453 bool valuesCouldBeEqual(SpeculatedType, SpeculatedType);
455 // Precise computation of the type of the result of a double computation after we
456 // already know that the inputs are doubles and that the result must be a double. Use
457 // the closest one of these that applies.
458 SpeculatedType typeOfDoubleSum(SpeculatedType, SpeculatedType);
459 SpeculatedType typeOfDoubleDifference(SpeculatedType, SpeculatedType);
460 SpeculatedType typeOfDoubleProduct(SpeculatedType, SpeculatedType);
461 SpeculatedType typeOfDoubleQuotient(SpeculatedType, SpeculatedType);
462 SpeculatedType typeOfDoubleMinMax(SpeculatedType, SpeculatedType);
463 SpeculatedType typeOfDoubleNegation(SpeculatedType);
464 SpeculatedType typeOfDoubleAbs(SpeculatedType);
465 SpeculatedType typeOfDoubleRounding(SpeculatedType);
466 SpeculatedType typeOfDoublePow(SpeculatedType, SpeculatedType);
468 // This conservatively models the behavior of arbitrary double operations.
469 SpeculatedType typeOfDoubleBinaryOp(SpeculatedType, SpeculatedType);
470 SpeculatedType typeOfDoubleUnaryOp(SpeculatedType);
474 #endif // SpeculatedType_h