]> git.saurik.com Git - apple/javascriptcore.git/blob - bytecode/DataFormat.h
JavaScriptCore-1218.0.1.tar.gz
[apple/javascriptcore.git] / bytecode / DataFormat.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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef DataFormat_h
27 #define DataFormat_h
28
29 #include <wtf/Assertions.h>
30
31 namespace JSC {
32
33 // === DataFormat ===
34 //
35 // This enum tracks the current representation in which a value is being held.
36 // Values may be unboxed primitives (int32, double, or cell), or boxed as a JSValue.
37 // For boxed values, we may know the type of boxing that has taken place.
38 // (May also need bool, array, object, string types!)
39 enum DataFormat {
40 DataFormatNone = 0,
41 DataFormatInteger = 1,
42 DataFormatDouble = 2,
43 DataFormatBoolean = 3,
44 DataFormatCell = 4,
45 DataFormatStorage = 5,
46 DataFormatJS = 8,
47 DataFormatJSInteger = DataFormatJS | DataFormatInteger,
48 DataFormatJSDouble = DataFormatJS | DataFormatDouble,
49 DataFormatJSCell = DataFormatJS | DataFormatCell,
50 DataFormatJSBoolean = DataFormatJS | DataFormatBoolean,
51
52 // Marker deliminating ordinary data formats and OSR-only data formats.
53 DataFormatOSRMarker = 32,
54
55 // Special data formats used only for OSR.
56 DataFormatDead = 33, // Implies jsUndefined().
57 DataFormatArguments = 34 // Implies that the arguments object must be reified.
58 };
59
60 inline const char* dataFormatToString(DataFormat dataFormat)
61 {
62 switch (dataFormat) {
63 case DataFormatNone:
64 return "None";
65 case DataFormatInteger:
66 return "Integer";
67 case DataFormatDouble:
68 return "Double";
69 case DataFormatCell:
70 return "Cell";
71 case DataFormatBoolean:
72 return "Boolean";
73 case DataFormatStorage:
74 return "Storage";
75 case DataFormatJS:
76 return "JS";
77 case DataFormatJSInteger:
78 return "JSInteger";
79 case DataFormatJSDouble:
80 return "JSDouble";
81 case DataFormatJSCell:
82 return "JSCell";
83 case DataFormatJSBoolean:
84 return "JSBoolean";
85 case DataFormatDead:
86 return "Dead";
87 case DataFormatArguments:
88 return "Arguments";
89 default:
90 RELEASE_ASSERT_NOT_REACHED();
91 return "Unknown";
92 }
93 }
94
95 #if USE(JSVALUE64)
96 inline bool needDataFormatConversion(DataFormat from, DataFormat to)
97 {
98 ASSERT(from != DataFormatNone);
99 ASSERT(to != DataFormatNone);
100 switch (from) {
101 case DataFormatInteger:
102 case DataFormatDouble:
103 return to != from;
104 case DataFormatCell:
105 case DataFormatJS:
106 case DataFormatJSInteger:
107 case DataFormatJSDouble:
108 case DataFormatJSCell:
109 case DataFormatJSBoolean:
110 switch (to) {
111 case DataFormatInteger:
112 case DataFormatDouble:
113 return true;
114 case DataFormatCell:
115 case DataFormatJS:
116 case DataFormatJSInteger:
117 case DataFormatJSDouble:
118 case DataFormatJSCell:
119 case DataFormatJSBoolean:
120 return false;
121 default:
122 // This captures DataFormatBoolean, which is currently unused.
123 RELEASE_ASSERT_NOT_REACHED();
124 }
125 case DataFormatStorage:
126 ASSERT(to == DataFormatStorage);
127 return false;
128 default:
129 // This captures DataFormatBoolean, which is currently unused.
130 RELEASE_ASSERT_NOT_REACHED();
131 }
132 return true;
133 }
134
135 #elif USE(JSVALUE32_64)
136 inline bool needDataFormatConversion(DataFormat from, DataFormat to)
137 {
138 ASSERT(from != DataFormatNone);
139 ASSERT(to != DataFormatNone);
140 switch (from) {
141 case DataFormatInteger:
142 case DataFormatCell:
143 case DataFormatBoolean:
144 return ((to & DataFormatJS) || to == DataFormatDouble);
145 case DataFormatDouble:
146 case DataFormatJSDouble:
147 return (to != DataFormatDouble && to != DataFormatJSDouble);
148 case DataFormatJS:
149 case DataFormatJSInteger:
150 case DataFormatJSCell:
151 case DataFormatJSBoolean:
152 return (!(to & DataFormatJS) || to == DataFormatJSDouble);
153 case DataFormatStorage:
154 ASSERT(to == DataFormatStorage);
155 return false;
156 default:
157 RELEASE_ASSERT_NOT_REACHED();
158 }
159 return true;
160 }
161 #endif
162
163 inline bool isJSFormat(DataFormat format, DataFormat expectedFormat)
164 {
165 ASSERT(expectedFormat & DataFormatJS);
166 return (format | DataFormatJS) == expectedFormat;
167 }
168
169 inline bool isJSInteger(DataFormat format)
170 {
171 return isJSFormat(format, DataFormatJSInteger);
172 }
173
174 inline bool isJSDouble(DataFormat format)
175 {
176 return isJSFormat(format, DataFormatJSDouble);
177 }
178
179 inline bool isJSCell(DataFormat format)
180 {
181 return isJSFormat(format, DataFormatJSCell);
182 }
183
184 inline bool isJSBoolean(DataFormat format)
185 {
186 return isJSFormat(format, DataFormatJSBoolean);
187 }
188
189 }
190
191 #endif // DataFormat_h