2 * Copyright (C) 2011 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 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.
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 PredictedType_h
30 #define PredictedType_h
36 typedef uint16_t PredictedType;
37 static const PredictedType PredictNone = 0x0000; // We don't know anything yet.
38 static const PredictedType PredictFinalObject = 0x0001; // It's definitely a JSFinalObject.
39 static const PredictedType PredictArray = 0x0002; // It's definitely a JSArray.
40 static const PredictedType PredictObjectOther = 0x0010; // It's definitely an object but not JSFinalObject or JSArray.
41 static const PredictedType PredictObjectUnknown = 0x0020; // It's definitely an object, but we didn't record enough informatin to know more.
42 static const PredictedType PredictObjectMask = 0x003f; // Bitmask used for testing for any kind of object prediction.
43 static const PredictedType PredictString = 0x0040; // It's definitely a JSString.
44 static const PredictedType PredictCellOther = 0x0080; // It's definitely a JSCell but not a subclass of JSObject and definitely not a JSString.
45 static const PredictedType PredictCell = 0x00ff; // It's definitely a JSCell.
46 static const PredictedType PredictInt32 = 0x0100; // It's definitely an Int32.
47 static const PredictedType PredictDouble = 0x0200; // It's definitely a Double.
48 static const PredictedType PredictNumber = 0x0300; // It's either an Int32 or a Double.
49 static const PredictedType PredictBoolean = 0x0400; // It's definitely a Boolean.
50 static const PredictedType PredictOther = 0x4000; // It's definitely none of the above.
51 static const PredictedType PredictTop = 0x7fff; // It can be any of the above.
52 static const PredictedType StrongPredictionTag = 0x8000; // It's a strong prediction (all strong predictions trump all weak ones).
53 static const PredictedType PredictionTagMask = 0x8000;
55 enum PredictionSource { WeakPrediction, StrongPrediction };
57 inline bool isCellPrediction(PredictedType value)
59 return !!(value & PredictCell) && !(value & ~(PredictCell | PredictionTagMask));
62 inline bool isObjectPrediction(PredictedType value)
64 return !!(value & PredictObjectMask) && !(value & ~(PredictObjectMask | PredictionTagMask));
67 inline bool isFinalObjectPrediction(PredictedType value)
69 return (value & ~PredictionTagMask) == PredictFinalObject;
72 inline bool isStringPrediction(PredictedType value)
74 return (value & ~PredictionTagMask) == PredictString;
77 inline bool isArrayPrediction(PredictedType value)
79 return (value & ~PredictionTagMask) == PredictArray;
82 inline bool isInt32Prediction(PredictedType value)
84 return (value & ~PredictionTagMask) == PredictInt32;
87 inline bool isDoublePrediction(PredictedType value)
89 return (value & ~PredictionTagMask) == PredictDouble;
92 inline bool isNumberPrediction(PredictedType value)
94 return !!(value & PredictNumber) && !(value & ~(PredictNumber | PredictionTagMask));
97 inline bool isBooleanPrediction(PredictedType value)
99 return (value & ~PredictionTagMask) == PredictBoolean;
102 inline bool isStrongPrediction(PredictedType value)
104 ASSERT(value != (PredictNone | StrongPredictionTag));
105 return !!(value & StrongPredictionTag);
109 const char* predictionToString(PredictedType value);
112 inline PredictedType mergePredictions(PredictedType left, PredictedType right)
114 if (isStrongPrediction(left) == isStrongPrediction(right)) {
115 if (left & PredictObjectUnknown) {
116 ASSERT(!(left & (PredictObjectMask & ~PredictObjectUnknown)));
117 if (right & PredictObjectMask)
118 return (left & ~PredictObjectUnknown) | right;
119 } else if (right & PredictObjectUnknown) {
120 ASSERT(!(right & (PredictObjectMask & ~PredictObjectUnknown)));
121 if (left & PredictObjectMask)
122 return (right & ~PredictObjectUnknown) | left;
126 if (isStrongPrediction(left)) {
127 ASSERT(!isStrongPrediction(right));
130 ASSERT(!isStrongPrediction(left));
131 ASSERT(isStrongPrediction(right));
136 inline bool mergePrediction(T& left, PredictedType right)
138 PredictedType newPrediction = static_cast<T>(mergePredictions(static_cast<PredictedType>(left), right));
139 bool result = newPrediction != static_cast<PredictedType>(left);
140 left = newPrediction;
144 inline PredictedType makePrediction(PredictedType type, PredictionSource source)
146 ASSERT(!(type & StrongPredictionTag));
147 ASSERT(source == StrongPrediction || source == WeakPrediction);
148 if (type == PredictNone)
150 return type | (source == StrongPrediction ? StrongPredictionTag : 0);
153 PredictedType predictionFromValue(JSValue);
157 #endif // PredictedType_h