1/* 2 * Copyright (C) 2011-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 * 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. 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 "TypedArrayType.h" 34#include <wtf/PrintStream.h> 35 36namespace JSC { 37 38class Structure; 39 40typedef uint32_t SpeculatedType; 41static const SpeculatedType SpecNone = 0x00000000; // We don't know anything yet. 42static const SpeculatedType SpecFinalObject = 0x00000001; // It's definitely a JSFinalObject. 43static const SpeculatedType SpecArray = 0x00000002; // It's definitely a JSArray. 44static const SpeculatedType SpecFunction = 0x00000004; // It's definitely a JSFunction. 45static const SpeculatedType SpecInt8Array = 0x00000008; // It's definitely an Int8Array or one of its subclasses. 46static const SpeculatedType SpecInt16Array = 0x00000010; // It's definitely an Int16Array or one of its subclasses. 47static const SpeculatedType SpecInt32Array = 0x00000020; // It's definitely an Int32Array or one of its subclasses. 48static const SpeculatedType SpecUint8Array = 0x00000040; // It's definitely an Uint8Array or one of its subclasses. 49static const SpeculatedType SpecUint8ClampedArray = 0x00000080; // It's definitely an Uint8ClampedArray or one of its subclasses. 50static const SpeculatedType SpecUint16Array = 0x00000100; // It's definitely an Uint16Array or one of its subclasses. 51static const SpeculatedType SpecUint32Array = 0x00000200; // It's definitely an Uint32Array or one of its subclasses. 52static const SpeculatedType SpecFloat32Array = 0x00000400; // It's definitely an Uint16Array or one of its subclasses. 53static const SpeculatedType SpecFloat64Array = 0x00000800; // It's definitely an Uint16Array or one of its subclasses. 54static const SpeculatedType SpecTypedArrayView = SpecInt8Array | SpecInt16Array | SpecInt32Array | SpecUint8Array | SpecUint8ClampedArray | SpecUint16Array | SpecUint32Array | SpecFloat32Array | SpecFloat64Array; 55static const SpeculatedType SpecArguments = 0x00001000; // It's definitely an Arguments object. 56static const SpeculatedType SpecStringObject = 0x00002000; // It's definitely a StringObject. 57static const SpeculatedType SpecObjectOther = 0x00008000; // It's definitely an object but not JSFinalObject, JSArray, or JSFunction. 58static const SpeculatedType SpecObject = 0x0000ffff; // Bitmask used for testing for any kind of object prediction. 59static const SpeculatedType SpecStringIdent = 0x00010000; // It's definitely a JSString, and it's an identifier. 60static const SpeculatedType SpecStringVar = 0x00020000; // It's definitely a JSString, and it's not an identifier. 61static const SpeculatedType SpecString = 0x00030000; // It's definitely a JSString. 62static const SpeculatedType SpecCellOther = 0x00040000; // It's definitely a JSCell but not a subclass of JSObject and definitely not a JSString. 63static const SpeculatedType SpecCell = 0x0007ffff; // It's definitely a JSCell. 64static const SpeculatedType SpecInt32 = 0x00200000; // It's definitely an Int32. 65static const SpeculatedType SpecInt52 = 0x00400000; // It's definitely an Int52 and we intend it to unbox it. 66static const SpeculatedType SpecMachineInt = 0x00600000; // It's something that we can do machine int arithmetic on. 67static const SpeculatedType SpecInt52AsDouble = 0x00800000; // It's definitely an Int52 and it's inside a double. 68static const SpeculatedType SpecInteger = 0x00e00000; // It's definitely some kind of integer. 69static const SpeculatedType SpecNonIntAsDouble = 0x01000000; // It's definitely not an Int52 but it's a real number and it's a double. 70static const SpeculatedType SpecDoubleReal = 0x01800000; // It's definitely a non-NaN double. 71static const SpeculatedType SpecDoublePureNaN = 0x02000000; // It's definitely a NaN that is sae to tag (i.e. pure). 72static const SpeculatedType SpecDoubleImpureNaN = 0x04000000; // It's definitely a NaN that is unsafe to tag (i.e. impure). 73static const SpeculatedType SpecDoubleNaN = 0x06000000; // It's definitely some kind of NaN. 74static const SpeculatedType SpecBytecodeDouble = 0x03800000; // It's either a non-NaN or a NaN double, but it's definitely not impure NaN. 75static const SpeculatedType SpecFullDouble = 0x07800000; // It's either a non-NaN or a NaN double. 76static const SpeculatedType SpecBytecodeRealNumber = 0x01a00000; // It's either an Int32 or a DoubleReal. 77static const SpeculatedType SpecFullRealNumber = 0x01e00000; // It's either an Int32 or a DoubleReal, or a Int52. 78static const SpeculatedType SpecBytecodeNumber = 0x03a00000; // It's either an Int32 or a Double, and the Double cannot be an impure NaN. 79static const SpeculatedType SpecFullNumber = 0x07e00000; // It's either an Int32, Int52, or a Double, and the Double can be impure NaN. 80static const SpeculatedType SpecBoolean = 0x10000000; // It's definitely a Boolean. 81static const SpeculatedType SpecOther = 0x20000000; // It's definitely either Null or Undefined. 82static const SpeculatedType SpecMisc = 0x30000000; // It's definitely either a boolean, Null, or Undefined. 83static const SpeculatedType SpecHeapTop = 0x3bbfffff; // It can be any of the above, except for SpecInt52. 84static const SpeculatedType SpecEmpty = 0x40000000; // It's definitely an empty value marker. 85static const SpeculatedType SpecBytecodeTop = 0x7bbfffff; // It can be any of the above, except for SpecInt52. 86static const SpeculatedType SpecFullTop = 0x7fffffff; // It can be any of the above plus anything the DFG chooses. 87 88typedef bool (*SpeculatedTypeChecker)(SpeculatedType); 89 90// Dummy prediction checker, only useful if someone insists on requiring a prediction checker. 91inline bool isAnySpeculation(SpeculatedType) 92{ 93 return true; 94} 95 96inline bool isCellSpeculation(SpeculatedType value) 97{ 98 return !!(value & SpecCell) && !(value & ~SpecCell); 99} 100 101inline bool isObjectSpeculation(SpeculatedType value) 102{ 103 return !!(value & SpecObject) && !(value & ~SpecObject); 104} 105 106inline bool isObjectOrOtherSpeculation(SpeculatedType value) 107{ 108 return !!(value & (SpecObject | SpecOther)) && !(value & ~(SpecObject | SpecOther)); 109} 110 111inline bool isFinalObjectSpeculation(SpeculatedType value) 112{ 113 return value == SpecFinalObject; 114} 115 116inline bool isFinalObjectOrOtherSpeculation(SpeculatedType value) 117{ 118 return !!(value & (SpecFinalObject | SpecOther)) && !(value & ~(SpecFinalObject | SpecOther)); 119} 120 121inline bool isStringIdentSpeculation(SpeculatedType value) 122{ 123 return value == SpecStringIdent; 124} 125 126inline bool isNotStringVarSpeculation(SpeculatedType value) 127{ 128 return !(value & SpecStringVar); 129} 130 131inline bool isStringSpeculation(SpeculatedType value) 132{ 133 return !!value && (value & SpecString) == value; 134} 135 136inline bool isArraySpeculation(SpeculatedType value) 137{ 138 return value == SpecArray; 139} 140 141inline bool isFunctionSpeculation(SpeculatedType value) 142{ 143 return value == SpecFunction; 144} 145 146inline bool isInt8ArraySpeculation(SpeculatedType value) 147{ 148 return value == SpecInt8Array; 149} 150 151inline bool isInt16ArraySpeculation(SpeculatedType value) 152{ 153 return value == SpecInt16Array; 154} 155 156inline bool isInt32ArraySpeculation(SpeculatedType value) 157{ 158 return value == SpecInt32Array; 159} 160 161inline bool isUint8ArraySpeculation(SpeculatedType value) 162{ 163 return value == SpecUint8Array; 164} 165 166inline bool isUint8ClampedArraySpeculation(SpeculatedType value) 167{ 168 return value == SpecUint8ClampedArray; 169} 170 171inline bool isUint16ArraySpeculation(SpeculatedType value) 172{ 173 return value == SpecUint16Array; 174} 175 176inline bool isUint32ArraySpeculation(SpeculatedType value) 177{ 178 return value == SpecUint32Array; 179} 180 181inline bool isFloat32ArraySpeculation(SpeculatedType value) 182{ 183 return value == SpecFloat32Array; 184} 185 186inline bool isFloat64ArraySpeculation(SpeculatedType value) 187{ 188 return value == SpecFloat64Array; 189} 190 191inline bool isArgumentsSpeculation(SpeculatedType value) 192{ 193 return !!value && (value & SpecArguments) == value; 194} 195 196inline bool isActionableIntMutableArraySpeculation(SpeculatedType value) 197{ 198 return isInt8ArraySpeculation(value) 199 || isInt16ArraySpeculation(value) 200 || isInt32ArraySpeculation(value) 201 || isUint8ArraySpeculation(value) 202 || isUint8ClampedArraySpeculation(value) 203 || isUint16ArraySpeculation(value) 204 || isUint32ArraySpeculation(value); 205} 206 207inline bool isActionableFloatMutableArraySpeculation(SpeculatedType value) 208{ 209 return isFloat32ArraySpeculation(value) 210 || isFloat64ArraySpeculation(value); 211} 212 213inline bool isActionableTypedMutableArraySpeculation(SpeculatedType value) 214{ 215 return isActionableIntMutableArraySpeculation(value) 216 || isActionableFloatMutableArraySpeculation(value); 217} 218 219inline bool isActionableMutableArraySpeculation(SpeculatedType value) 220{ 221 return isArraySpeculation(value) 222 || isArgumentsSpeculation(value) 223 || isActionableTypedMutableArraySpeculation(value); 224} 225 226inline bool isActionableArraySpeculation(SpeculatedType value) 227{ 228 return isStringSpeculation(value) 229 || isActionableMutableArraySpeculation(value); 230} 231 232inline bool isArrayOrOtherSpeculation(SpeculatedType value) 233{ 234 return !!(value & (SpecArray | SpecOther)) && !(value & ~(SpecArray | SpecOther)); 235} 236 237inline bool isStringObjectSpeculation(SpeculatedType value) 238{ 239 return value == SpecStringObject; 240} 241 242inline bool isStringOrStringObjectSpeculation(SpeculatedType value) 243{ 244 return !!value && !(value & ~(SpecString | SpecStringObject)); 245} 246 247inline bool isInt32Speculation(SpeculatedType value) 248{ 249 return value == SpecInt32; 250} 251 252inline bool isInt32OrBooleanSpeculation(SpeculatedType value) 253{ 254 return value && !(value & ~(SpecBoolean | SpecInt32)); 255} 256 257inline bool isInt32SpeculationForArithmetic(SpeculatedType value) 258{ 259 return !(value & (SpecFullDouble | SpecInt52)); 260} 261 262inline bool isInt32OrBooleanSpeculationForArithmetic(SpeculatedType value) 263{ 264 return !(value & (SpecFullDouble | SpecInt52)); 265} 266 267inline bool isInt32OrBooleanSpeculationExpectingDefined(SpeculatedType value) 268{ 269 return isInt32OrBooleanSpeculation(value & ~SpecOther); 270} 271 272inline bool isInt52Speculation(SpeculatedType value) 273{ 274 return value == SpecInt52; 275} 276 277inline bool isMachineIntSpeculation(SpeculatedType value) 278{ 279 return !!value && (value & SpecMachineInt) == value; 280} 281 282inline bool isInt52AsDoubleSpeculation(SpeculatedType value) 283{ 284 return value == SpecInt52AsDouble; 285} 286 287inline bool isIntegerSpeculation(SpeculatedType value) 288{ 289 return !!value && (value & SpecInteger) == value; 290} 291 292inline bool isDoubleRealSpeculation(SpeculatedType value) 293{ 294 return !!value && (value & SpecDoubleReal) == value; 295} 296 297inline bool isDoubleSpeculation(SpeculatedType value) 298{ 299 return !!value && (value & SpecFullDouble) == value; 300} 301 302inline bool isDoubleSpeculationForArithmetic(SpeculatedType value) 303{ 304 return !!(value & SpecFullDouble); 305} 306 307inline bool isBytecodeRealNumberSpeculation(SpeculatedType value) 308{ 309 return !!(value & SpecBytecodeRealNumber) && !(value & ~SpecBytecodeRealNumber); 310} 311 312inline bool isFullRealNumberSpeculation(SpeculatedType value) 313{ 314 return !!(value & SpecFullRealNumber) && !(value & ~SpecFullRealNumber); 315} 316 317inline bool isBytecodeNumberSpeculation(SpeculatedType value) 318{ 319 return !!(value & SpecBytecodeNumber) && !(value & ~SpecBytecodeNumber); 320} 321 322inline bool isFullNumberSpeculation(SpeculatedType value) 323{ 324 return !!(value & SpecFullNumber) && !(value & ~SpecFullNumber); 325} 326 327inline bool isFullNumberOrBooleanSpeculation(SpeculatedType value) 328{ 329 return value && !(value & ~(SpecFullNumber | SpecBoolean)); 330} 331 332inline bool isFullNumberOrBooleanSpeculationExpectingDefined(SpeculatedType value) 333{ 334 return isFullNumberOrBooleanSpeculation(value & ~SpecOther); 335} 336 337inline bool isBooleanSpeculation(SpeculatedType value) 338{ 339 return value == SpecBoolean; 340} 341 342inline bool isOtherSpeculation(SpeculatedType value) 343{ 344 return value == SpecOther; 345} 346 347inline bool isMiscSpeculation(SpeculatedType value) 348{ 349 return !!value && !(value & ~SpecMisc); 350} 351 352inline bool isOtherOrEmptySpeculation(SpeculatedType value) 353{ 354 return !value || value == SpecOther; 355} 356 357inline bool isEmptySpeculation(SpeculatedType value) 358{ 359 return value == SpecEmpty; 360} 361 362void dumpSpeculation(PrintStream&, SpeculatedType); 363void dumpSpeculationAbbreviated(PrintStream&, SpeculatedType); 364 365MAKE_PRINT_ADAPTOR(SpeculationDump, SpeculatedType, dumpSpeculation); 366MAKE_PRINT_ADAPTOR(AbbreviatedSpeculationDump, SpeculatedType, dumpSpeculationAbbreviated); 367 368// Merge two predictions. Note that currently this just does left | right. It may 369// seem tempting to do so directly, but you would be doing so at your own peril, 370// since the merging protocol SpeculatedType may change at any time (and has already 371// changed several times in its history). 372inline SpeculatedType mergeSpeculations(SpeculatedType left, SpeculatedType right) 373{ 374 return left | right; 375} 376 377template<typename T> 378inline bool mergeSpeculation(T& left, SpeculatedType right) 379{ 380 SpeculatedType newSpeculation = static_cast<T>(mergeSpeculations(static_cast<SpeculatedType>(left), right)); 381 bool result = newSpeculation != static_cast<SpeculatedType>(left); 382 left = newSpeculation; 383 return result; 384} 385 386inline bool speculationChecked(SpeculatedType actual, SpeculatedType desired) 387{ 388 return (actual | desired) == desired; 389} 390 391SpeculatedType speculationFromClassInfo(const ClassInfo*); 392SpeculatedType speculationFromStructure(Structure*); 393SpeculatedType speculationFromCell(JSCell*); 394SpeculatedType speculationFromValue(JSValue); 395 396SpeculatedType speculationFromTypedArrayType(TypedArrayType); // only valid for typed views. 397TypedArrayType typedArrayTypeFromSpeculation(SpeculatedType); 398 399SpeculatedType leastUpperBoundOfStrictlyEquivalentSpeculations(SpeculatedType); 400 401bool valuesCouldBeEqual(SpeculatedType, SpeculatedType); 402 403// Precise computation of the type of the result of a double computation after we 404// already know that the inputs are doubles and that the result must be a double. Use 405// the closest one of these that applies. 406SpeculatedType typeOfDoubleSum(SpeculatedType, SpeculatedType); 407SpeculatedType typeOfDoubleDifference(SpeculatedType, SpeculatedType); 408SpeculatedType typeOfDoubleProduct(SpeculatedType, SpeculatedType); 409SpeculatedType typeOfDoubleQuotient(SpeculatedType, SpeculatedType); 410SpeculatedType typeOfDoubleMinMax(SpeculatedType, SpeculatedType); 411SpeculatedType typeOfDoubleNegation(SpeculatedType); 412SpeculatedType typeOfDoubleAbs(SpeculatedType); 413SpeculatedType typeOfDoubleFRound(SpeculatedType); 414 415// This conservatively models the behavior of arbitrary double operations. 416SpeculatedType typeOfDoubleBinaryOp(SpeculatedType, SpeculatedType); 417SpeculatedType typeOfDoubleUnaryOp(SpeculatedType); 418 419} // namespace JSC 420 421#endif // SpeculatedType_h 422