]> git.saurik.com Git - apple/javascriptcore.git/blob - bytecode/DataFormat.h
JavaScriptCore-1097.13.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
53 inline const char* dataFormatToString(DataFormat dataFormat)
54 {
55 switch (dataFormat) {
56 case DataFormatNone:
57 return "None";
58 case DataFormatInteger:
59 return "Integer";
60 case DataFormatDouble:
61 return "Double";
62 case DataFormatCell:
63 return "Cell";
64 case DataFormatBoolean:
65 return "Boolean";
66 case DataFormatStorage:
67 return "Storage";
68 case DataFormatJS:
69 return "JS";
70 case DataFormatJSInteger:
71 return "JSInteger";
72 case DataFormatJSDouble:
73 return "JSDouble";
74 case DataFormatJSCell:
75 return "JSCell";
76 case DataFormatJSBoolean:
77 return "JSBoolean";
78 default:
79 return "Unknown";
80 }
81 }
82
83 #if USE(JSVALUE64)
84 inline bool needDataFormatConversion(DataFormat from, DataFormat to)
85 {
86 ASSERT(from != DataFormatNone);
87 ASSERT(to != DataFormatNone);
88 switch (from) {
89 case DataFormatInteger:
90 case DataFormatDouble:
91 return to != from;
92 case DataFormatCell:
93 case DataFormatJS:
94 case DataFormatJSInteger:
95 case DataFormatJSDouble:
96 case DataFormatJSCell:
97 case DataFormatJSBoolean:
98 switch (to) {
99 case DataFormatInteger:
100 case DataFormatDouble:
101 return true;
102 case DataFormatCell:
103 case DataFormatJS:
104 case DataFormatJSInteger:
105 case DataFormatJSDouble:
106 case DataFormatJSCell:
107 case DataFormatJSBoolean:
108 return false;
109 default:
110 // This captures DataFormatBoolean, which is currently unused.
111 ASSERT_NOT_REACHED();
112 }
113 case DataFormatStorage:
114 ASSERT(to == DataFormatStorage);
115 return false;
116 default:
117 // This captures DataFormatBoolean, which is currently unused.
118 ASSERT_NOT_REACHED();
119 }
120 return true;
121 }
122
123 #elif USE(JSVALUE32_64)
124 inline bool needDataFormatConversion(DataFormat from, DataFormat to)
125 {
126 ASSERT(from != DataFormatNone);
127 ASSERT(to != DataFormatNone);
128 switch (from) {
129 case DataFormatInteger:
130 case DataFormatCell:
131 case DataFormatBoolean:
132 return ((to & DataFormatJS) || to == DataFormatDouble);
133 case DataFormatDouble:
134 case DataFormatJSDouble:
135 return (to != DataFormatDouble && to != DataFormatJSDouble);
136 case DataFormatJS:
137 case DataFormatJSInteger:
138 case DataFormatJSCell:
139 case DataFormatJSBoolean:
140 return (!(to & DataFormatJS) || to == DataFormatJSDouble);
141 case DataFormatStorage:
142 ASSERT(to == DataFormatStorage);
143 return false;
144 default:
145 ASSERT_NOT_REACHED();
146 }
147 return true;
148 }
149 #endif
150
151 inline bool isJSFormat(DataFormat format, DataFormat expectedFormat)
152 {
153 ASSERT(expectedFormat & DataFormatJS);
154 return (format | DataFormatJS) == expectedFormat;
155 }
156
157 inline bool isJSInteger(DataFormat format)
158 {
159 return isJSFormat(format, DataFormatJSInteger);
160 }
161
162 inline bool isJSDouble(DataFormat format)
163 {
164 return isJSFormat(format, DataFormatJSDouble);
165 }
166
167 inline bool isJSCell(DataFormat format)
168 {
169 return isJSFormat(format, DataFormatJSCell);
170 }
171
172 inline bool isJSBoolean(DataFormat format)
173 {
174 return isJSFormat(format, DataFormatJSBoolean);
175 }
176
177 }
178
179 #endif // DataFormat_h