initial import
[vuplus_webkit] / Source / JavaScriptCore / bytecode / PredictedType.h
1 /*
2  * Copyright (C) 2011 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 PredictedType_h
30 #define PredictedType_h
31
32 #include "JSValue.h"
33
34 namespace JSC {
35
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;
54
55 enum PredictionSource { WeakPrediction, StrongPrediction };
56
57 inline bool isCellPrediction(PredictedType value)
58 {
59     return !!(value & PredictCell) && !(value & ~(PredictCell | PredictionTagMask));
60 }
61
62 inline bool isObjectPrediction(PredictedType value)
63 {
64     return !!(value & PredictObjectMask) && !(value & ~(PredictObjectMask | PredictionTagMask));
65 }
66
67 inline bool isFinalObjectPrediction(PredictedType value)
68 {
69     return (value & ~PredictionTagMask) == PredictFinalObject;
70 }
71
72 inline bool isStringPrediction(PredictedType value)
73 {
74     return (value & ~PredictionTagMask) == PredictString;
75 }
76
77 inline bool isArrayPrediction(PredictedType value)
78 {
79     return (value & ~PredictionTagMask) == PredictArray;
80 }
81
82 inline bool isInt32Prediction(PredictedType value)
83 {
84     return (value & ~PredictionTagMask) == PredictInt32;
85 }
86
87 inline bool isDoublePrediction(PredictedType value)
88 {
89     return (value & ~PredictionTagMask) == PredictDouble;
90 }
91
92 inline bool isNumberPrediction(PredictedType value)
93 {
94     return !!(value & PredictNumber) && !(value & ~(PredictNumber | PredictionTagMask));
95 }
96
97 inline bool isBooleanPrediction(PredictedType value)
98 {
99     return (value & ~PredictionTagMask) == PredictBoolean;
100 }
101
102 inline bool isStrongPrediction(PredictedType value)
103 {
104     ASSERT(value != (PredictNone | StrongPredictionTag));
105     return !!(value & StrongPredictionTag);
106 }
107
108 #ifndef NDEBUG
109 const char* predictionToString(PredictedType value);
110 #endif
111
112 inline PredictedType mergePredictions(PredictedType left, PredictedType right)
113 {
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;
123         }
124         return left | right;
125     }
126     if (isStrongPrediction(left)) {
127         ASSERT(!isStrongPrediction(right));
128         return left;
129     }
130     ASSERT(!isStrongPrediction(left));
131     ASSERT(isStrongPrediction(right));
132     return right;
133 }
134
135 template<typename T>
136 inline bool mergePrediction(T& left, PredictedType right)
137 {
138     PredictedType newPrediction = static_cast<T>(mergePredictions(static_cast<PredictedType>(left), right));
139     bool result = newPrediction != static_cast<PredictedType>(left);
140     left = newPrediction;
141     return result;
142 }
143
144 inline PredictedType makePrediction(PredictedType type, PredictionSource source)
145 {
146     ASSERT(!(type & StrongPredictionTag));
147     ASSERT(source == StrongPrediction || source == WeakPrediction);
148     if (type == PredictNone)
149         return PredictNone;
150     return type | (source == StrongPrediction ? StrongPredictionTag : 0);
151 }
152
153 PredictedType predictionFromValue(JSValue);
154
155 } // namespace JSC
156
157 #endif // PredictedType_h