]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/JSImmediate.h
JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / kjs / JSImmediate.h
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #ifndef KJS_JS_IMMEDIATE_H
23 #define KJS_JS_IMMEDIATE_H
24
25 #include "JSType.h"
26 #include <wtf/Assertions.h>
27 #include <wtf/AlwaysInline.h>
28 #include <wtf/MathExtras.h>
29 #include <limits>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33
34 namespace KJS {
35
36 class ExecState;
37 class JSObject;
38 class JSValue;
39 class UString;
40
41 /*
42 * A JSValue* is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged
43 * signed int masquerading as a pointer). The low two bits in a JSValue* are available
44 * for type tagging because allocator alignment guarantees they will be 00 in cell pointers.
45 *
46 * For example, on a 32 bit system:
47 *
48 * JSCell*: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00
49 * [ high 30 bits: pointer address ] [ low 2 bits -- always 0 ]
50 *
51 * JSImmediate: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TT
52 * [ high 30 bits: signed int ] [ low 2 bits -- type tag ]
53 *
54 * The bit "payload" (the high 30 bits) is a 30 bit signed int for immediate numbers, a flag to distinguish true/false
55 * and undefined/null.
56 *
57 * Notice that the JSType value of NullType is 4, which requires 3 bits to encode. Since we only have 2 bits
58 * available for type tagging, we tag the null immediate with UndefinedType, and JSImmediate::type() has
59 * to sort them out.
60 */
61
62 class JSImmediate {
63 public:
64 static ALWAYS_INLINE bool isImmediate(const JSValue* v)
65 {
66 return getTag(v) != 0;
67 }
68
69 static ALWAYS_INLINE bool isNumber(const JSValue* v)
70 {
71 return (getTag(v) == NumberType);
72 }
73
74 static ALWAYS_INLINE bool isBoolean(const JSValue* v)
75 {
76 return (getTag(v) == BooleanType);
77 }
78
79 // Since we have room for only 3 unique tags, null and undefined have to share.
80 static ALWAYS_INLINE bool isUndefinedOrNull(const JSValue* v)
81 {
82 return (getTag(v) == UndefinedType);
83 }
84
85 static JSValue* from(char);
86 static JSValue* from(signed char);
87 static JSValue* from(unsigned char);
88 static JSValue* from(short);
89 static JSValue* from(unsigned short);
90 static JSValue* from(int);
91 static JSValue* from(unsigned);
92 static JSValue* from(long);
93 static JSValue* from(unsigned long);
94 static JSValue* from(long long);
95 static JSValue* from(unsigned long long);
96 static JSValue* from(double);
97
98 static ALWAYS_INLINE bool areBothImmediateNumbers(const JSValue* v1, const JSValue* v2)
99 {
100 return (reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2) & TagMask) == NumberType;
101 }
102
103 static ALWAYS_INLINE JSValue* andImmediateNumbers(const JSValue* v1, const JSValue* v2)
104 {
105 ASSERT(areBothImmediateNumbers(v1, v2));
106 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2));
107 }
108
109 static double toDouble(const JSValue*);
110 static bool toBoolean(const JSValue*);
111 static JSObject* toObject(const JSValue*, ExecState*);
112 static UString toString(const JSValue*);
113 static JSType type(const JSValue*);
114
115 static bool getUInt32(const JSValue*, uint32_t&);
116 static bool getTruncatedInt32(const JSValue*, int32_t&);
117 static bool getTruncatedUInt32(const JSValue*, uint32_t&);
118
119 static int32_t getTruncatedInt32(const JSValue*);
120
121 static JSValue* trueImmediate();
122 static JSValue* falseImmediate();
123 static JSValue* undefinedImmediate();
124 static JSValue* nullImmediate();
125
126 private:
127 static const uintptr_t TagMask = 3; // type tags are 2 bits long
128
129 // Immediate values are restricted to a 30 bit signed value.
130 static const int minImmediateInt = -(1 << 29);
131 static const int maxImmediateInt = (1 << 29) - 1;
132 static const unsigned maxImmediateUInt = maxImmediateInt;
133
134 static ALWAYS_INLINE JSValue* tag(uintptr_t bits, uintptr_t tag)
135 {
136 return reinterpret_cast<JSValue*>(bits | tag);
137 }
138
139 static ALWAYS_INLINE uintptr_t unTag(const JSValue* v)
140 {
141 return reinterpret_cast<uintptr_t>(v) & ~TagMask;
142 }
143
144 static ALWAYS_INLINE uintptr_t getTag(const JSValue* v)
145 {
146 return reinterpret_cast<uintptr_t>(v) & TagMask;
147 }
148 };
149
150 ALWAYS_INLINE JSValue* JSImmediate::trueImmediate() { return tag(1 << 2, BooleanType); }
151 ALWAYS_INLINE JSValue* JSImmediate::falseImmediate() { return tag(0, BooleanType); }
152 ALWAYS_INLINE JSValue* JSImmediate::undefinedImmediate() { return tag(1 << 2, UndefinedType); }
153 ALWAYS_INLINE JSValue* JSImmediate::nullImmediate() { return tag(0, UndefinedType); }
154
155 ALWAYS_INLINE bool JSImmediate::toBoolean(const JSValue* v)
156 {
157 ASSERT(isImmediate(v));
158 uintptr_t bits = unTag(v);
159 return (bits != 0) & (JSImmediate::getTag(v) != UndefinedType);
160 }
161
162 ALWAYS_INLINE JSValue* JSImmediate::from(char i)
163 {
164 return tag(i << 2, NumberType);
165 }
166
167 ALWAYS_INLINE JSValue* JSImmediate::from(signed char i)
168 {
169 return tag(i << 2, NumberType);
170 }
171
172 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned char i)
173 {
174 return tag(i << 2, NumberType);
175 }
176
177 ALWAYS_INLINE JSValue* JSImmediate::from(short i)
178 {
179 return tag(i << 2, NumberType);
180 }
181
182 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned short i)
183 {
184 return tag(i << 2, NumberType);
185 }
186
187 ALWAYS_INLINE JSValue* JSImmediate::from(int i)
188 {
189 if ((i < minImmediateInt) | (i > maxImmediateInt))
190 return 0;
191 return tag(i << 2, NumberType);
192 }
193
194 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned i)
195 {
196 if (i > maxImmediateUInt)
197 return 0;
198 return tag(i << 2, NumberType);
199 }
200
201 ALWAYS_INLINE JSValue* JSImmediate::from(long i)
202 {
203 if ((i < minImmediateInt) | (i > maxImmediateInt))
204 return 0;
205 return tag(i << 2, NumberType);
206 }
207
208 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long i)
209 {
210 if (i > maxImmediateUInt)
211 return 0;
212 return tag(i << 2, NumberType);
213 }
214
215 ALWAYS_INLINE JSValue* JSImmediate::from(long long i)
216 {
217 if ((i < minImmediateInt) | (i > maxImmediateInt))
218 return 0;
219 return tag(static_cast<uintptr_t>(i) << 2, NumberType);
220 }
221
222 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long long i)
223 {
224 if (i > maxImmediateUInt)
225 return 0;
226 return tag(static_cast<uintptr_t>(i) << 2, NumberType);
227 }
228
229 ALWAYS_INLINE JSValue* JSImmediate::from(double d)
230 {
231 const int intVal = static_cast<int>(d);
232
233 if ((intVal < minImmediateInt) | (intVal > maxImmediateInt))
234 return 0;
235
236 // Check for data loss from conversion to int.
237 if ((intVal != d) || (!intVal && signbit(d)))
238 return 0;
239
240 return tag(intVal << 2, NumberType);
241 }
242
243 ALWAYS_INLINE int32_t JSImmediate::getTruncatedInt32(const JSValue* v)
244 {
245 ASSERT(isNumber(v));
246 return static_cast<int32_t>(unTag(v)) >> 2;
247 }
248
249 ALWAYS_INLINE double JSImmediate::toDouble(const JSValue* v)
250 {
251 ASSERT(isImmediate(v));
252 const int32_t i = static_cast<int32_t>(unTag(v)) >> 2;
253 if (JSImmediate::getTag(v) == UndefinedType && i)
254 return std::numeric_limits<double>::quiet_NaN();
255 return i;
256 }
257
258 ALWAYS_INLINE bool JSImmediate::getUInt32(const JSValue* v, uint32_t& i)
259 {
260 const int32_t si = static_cast<int32_t>(unTag(v)) >> 2;
261 i = si;
262 return isNumber(v) & (si >= 0);
263 }
264
265 ALWAYS_INLINE bool JSImmediate::getTruncatedInt32(const JSValue* v, int32_t& i)
266 {
267 i = static_cast<int32_t>(unTag(v)) >> 2;
268 return isNumber(v);
269 }
270
271 ALWAYS_INLINE bool JSImmediate::getTruncatedUInt32(const JSValue* v, uint32_t& i)
272 {
273 return getUInt32(v, i);
274 }
275
276 } // namespace KJS
277
278 #endif