1 | /*
|
---|
2 | * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
|
---|
3 | * Copyright (C) 2006 Alexey Proskuryakov ([email protected])
|
---|
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 ALWAYS_INLINE JSValue* xorImmediateNumbers(const JSValue* v1, const JSValue* v2)
|
---|
110 | {
|
---|
111 | ASSERT(areBothImmediateNumbers(v1, v2));
|
---|
112 | return tag(reinterpret_cast<uintptr_t>(v1) ^ reinterpret_cast<uintptr_t>(v2), NumberType);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static ALWAYS_INLINE JSValue* orImmediateNumbers(const JSValue* v1, const JSValue* v2)
|
---|
116 | {
|
---|
117 | ASSERT(areBothImmediateNumbers(v1, v2));
|
---|
118 | return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) | reinterpret_cast<uintptr_t>(v2));
|
---|
119 | }
|
---|
120 |
|
---|
121 | static ALWAYS_INLINE bool canDoFastAdditiveOperations(const JSValue* v)
|
---|
122 | {
|
---|
123 | // Number is positive and an operation involving two of these can't overflow.
|
---|
124 | // Checking for allowed negative numbers takes more time than it's worth on SunSpider.
|
---|
125 | return (reinterpret_cast<uintptr_t>(v) & (NumberType + (3 << 30))) == NumberType;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static ALWAYS_INLINE JSValue* addImmediateNumbers(const JSValue* v1, const JSValue* v2)
|
---|
129 | {
|
---|
130 | ASSERT(canDoFastAdditiveOperations(v1));
|
---|
131 | ASSERT(canDoFastAdditiveOperations(v2));
|
---|
132 | return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) + (reinterpret_cast<uintptr_t>(v2) & ~NumberType));
|
---|
133 | }
|
---|
134 |
|
---|
135 | static ALWAYS_INLINE JSValue* subImmediateNumbers(const JSValue* v1, const JSValue* v2)
|
---|
136 | {
|
---|
137 | ASSERT(canDoFastAdditiveOperations(v1));
|
---|
138 | ASSERT(canDoFastAdditiveOperations(v2));
|
---|
139 | return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) - (reinterpret_cast<uintptr_t>(v2) & ~NumberType));
|
---|
140 | }
|
---|
141 |
|
---|
142 | static double toDouble(const JSValue*);
|
---|
143 | static bool toBoolean(const JSValue*);
|
---|
144 | static JSObject* toObject(const JSValue*, ExecState*);
|
---|
145 | static UString toString(const JSValue*);
|
---|
146 | static JSType type(const JSValue*);
|
---|
147 |
|
---|
148 | static bool getUInt32(const JSValue*, uint32_t&);
|
---|
149 | static bool getTruncatedInt32(const JSValue*, int32_t&);
|
---|
150 | static bool getTruncatedUInt32(const JSValue*, uint32_t&);
|
---|
151 |
|
---|
152 | static int32_t getTruncatedInt32(const JSValue*);
|
---|
153 |
|
---|
154 | static JSValue* trueImmediate();
|
---|
155 | static JSValue* falseImmediate();
|
---|
156 | static JSValue* undefinedImmediate();
|
---|
157 | static JSValue* nullImmediate();
|
---|
158 |
|
---|
159 | static JSValue* impossibleValue();
|
---|
160 |
|
---|
161 | private:
|
---|
162 | static const uintptr_t TagMask = 3; // type tags are 2 bits long
|
---|
163 |
|
---|
164 | // Immediate values are restricted to a 30 bit signed value.
|
---|
165 | static const int minImmediateInt = -(1 << 29);
|
---|
166 | static const int maxImmediateInt = (1 << 29) - 1;
|
---|
167 | static const unsigned maxImmediateUInt = maxImmediateInt;
|
---|
168 |
|
---|
169 | static ALWAYS_INLINE JSValue* tag(uintptr_t bits, uintptr_t tag)
|
---|
170 | {
|
---|
171 | return reinterpret_cast<JSValue*>(bits | tag);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static ALWAYS_INLINE uintptr_t unTag(const JSValue* v)
|
---|
175 | {
|
---|
176 | return reinterpret_cast<uintptr_t>(v) & ~TagMask;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static ALWAYS_INLINE uintptr_t getTag(const JSValue* v)
|
---|
180 | {
|
---|
181 | return reinterpret_cast<uintptr_t>(v) & TagMask;
|
---|
182 | }
|
---|
183 | };
|
---|
184 |
|
---|
185 | ALWAYS_INLINE JSValue* JSImmediate::trueImmediate() { return tag(1 << 2, BooleanType); }
|
---|
186 | ALWAYS_INLINE JSValue* JSImmediate::falseImmediate() { return tag(0, BooleanType); }
|
---|
187 | ALWAYS_INLINE JSValue* JSImmediate::undefinedImmediate() { return tag(1 << 2, UndefinedType); }
|
---|
188 | ALWAYS_INLINE JSValue* JSImmediate::nullImmediate() { return tag(0, UndefinedType); }
|
---|
189 |
|
---|
190 | // This value is impossible because 0x4 is not a valid pointer but a tag of 0 would indicate non-immediate
|
---|
191 | ALWAYS_INLINE JSValue* JSImmediate::impossibleValue() { return tag(1 << 2, 0); }
|
---|
192 |
|
---|
193 | ALWAYS_INLINE bool JSImmediate::toBoolean(const JSValue* v)
|
---|
194 | {
|
---|
195 | ASSERT(isImmediate(v));
|
---|
196 | uintptr_t bits = unTag(v);
|
---|
197 | return (bits != 0) & (JSImmediate::getTag(v) != UndefinedType);
|
---|
198 | }
|
---|
199 |
|
---|
200 | ALWAYS_INLINE JSValue* JSImmediate::from(char i)
|
---|
201 | {
|
---|
202 | return tag(i << 2, NumberType);
|
---|
203 | }
|
---|
204 |
|
---|
205 | ALWAYS_INLINE JSValue* JSImmediate::from(signed char i)
|
---|
206 | {
|
---|
207 | return tag(i << 2, NumberType);
|
---|
208 | }
|
---|
209 |
|
---|
210 | ALWAYS_INLINE JSValue* JSImmediate::from(unsigned char i)
|
---|
211 | {
|
---|
212 | return tag(i << 2, NumberType);
|
---|
213 | }
|
---|
214 |
|
---|
215 | ALWAYS_INLINE JSValue* JSImmediate::from(short i)
|
---|
216 | {
|
---|
217 | return tag(i << 2, NumberType);
|
---|
218 | }
|
---|
219 |
|
---|
220 | ALWAYS_INLINE JSValue* JSImmediate::from(unsigned short i)
|
---|
221 | {
|
---|
222 | return tag(i << 2, NumberType);
|
---|
223 | }
|
---|
224 |
|
---|
225 | ALWAYS_INLINE JSValue* JSImmediate::from(int i)
|
---|
226 | {
|
---|
227 | if ((i < minImmediateInt) | (i > maxImmediateInt))
|
---|
228 | return 0;
|
---|
229 | return tag(i << 2, NumberType);
|
---|
230 | }
|
---|
231 |
|
---|
232 | ALWAYS_INLINE JSValue* JSImmediate::from(unsigned i)
|
---|
233 | {
|
---|
234 | if (i > maxImmediateUInt)
|
---|
235 | return 0;
|
---|
236 | return tag(i << 2, NumberType);
|
---|
237 | }
|
---|
238 |
|
---|
239 | ALWAYS_INLINE JSValue* JSImmediate::from(long i)
|
---|
240 | {
|
---|
241 | if ((i < minImmediateInt) | (i > maxImmediateInt))
|
---|
242 | return 0;
|
---|
243 | return tag(i << 2, NumberType);
|
---|
244 | }
|
---|
245 |
|
---|
246 | ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long i)
|
---|
247 | {
|
---|
248 | if (i > maxImmediateUInt)
|
---|
249 | return 0;
|
---|
250 | return tag(i << 2, NumberType);
|
---|
251 | }
|
---|
252 |
|
---|
253 | ALWAYS_INLINE JSValue* JSImmediate::from(long long i)
|
---|
254 | {
|
---|
255 | if ((i < minImmediateInt) | (i > maxImmediateInt))
|
---|
256 | return 0;
|
---|
257 | return tag(static_cast<uintptr_t>(i) << 2, NumberType);
|
---|
258 | }
|
---|
259 |
|
---|
260 | ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long long i)
|
---|
261 | {
|
---|
262 | if (i > maxImmediateUInt)
|
---|
263 | return 0;
|
---|
264 | return tag(static_cast<uintptr_t>(i) << 2, NumberType);
|
---|
265 | }
|
---|
266 |
|
---|
267 | ALWAYS_INLINE JSValue* JSImmediate::from(double d)
|
---|
268 | {
|
---|
269 | const int intVal = static_cast<int>(d);
|
---|
270 |
|
---|
271 | if ((intVal < minImmediateInt) | (intVal > maxImmediateInt))
|
---|
272 | return 0;
|
---|
273 |
|
---|
274 | // Check for data loss from conversion to int.
|
---|
275 | if ((intVal != d) || (!intVal && signbit(d)))
|
---|
276 | return 0;
|
---|
277 |
|
---|
278 | return tag(intVal << 2, NumberType);
|
---|
279 | }
|
---|
280 |
|
---|
281 | ALWAYS_INLINE int32_t JSImmediate::getTruncatedInt32(const JSValue* v)
|
---|
282 | {
|
---|
283 | ASSERT(isNumber(v));
|
---|
284 | return static_cast<int32_t>(unTag(v)) >> 2;
|
---|
285 | }
|
---|
286 |
|
---|
287 | ALWAYS_INLINE double JSImmediate::toDouble(const JSValue* v)
|
---|
288 | {
|
---|
289 | ASSERT(isImmediate(v));
|
---|
290 | const int32_t i = static_cast<int32_t>(unTag(v)) >> 2;
|
---|
291 | if (JSImmediate::getTag(v) == UndefinedType && i)
|
---|
292 | return std::numeric_limits<double>::quiet_NaN();
|
---|
293 | return i;
|
---|
294 | }
|
---|
295 |
|
---|
296 | ALWAYS_INLINE bool JSImmediate::getUInt32(const JSValue* v, uint32_t& i)
|
---|
297 | {
|
---|
298 | const int32_t si = static_cast<int32_t>(unTag(v)) >> 2;
|
---|
299 | i = si;
|
---|
300 | return isNumber(v) & (si >= 0);
|
---|
301 | }
|
---|
302 |
|
---|
303 | ALWAYS_INLINE bool JSImmediate::getTruncatedInt32(const JSValue* v, int32_t& i)
|
---|
304 | {
|
---|
305 | i = static_cast<int32_t>(unTag(v)) >> 2;
|
---|
306 | return isNumber(v);
|
---|
307 | }
|
---|
308 |
|
---|
309 | ALWAYS_INLINE bool JSImmediate::getTruncatedUInt32(const JSValue* v, uint32_t& i)
|
---|
310 | {
|
---|
311 | return getUInt32(v, i);
|
---|
312 | }
|
---|
313 |
|
---|
314 | ALWAYS_INLINE JSType JSImmediate::type(const JSValue* v)
|
---|
315 | {
|
---|
316 | ASSERT(isImmediate(v));
|
---|
317 |
|
---|
318 | uintptr_t tag = getTag(v);
|
---|
319 | if (tag == UndefinedType)
|
---|
320 | return v == undefinedImmediate() ? UndefinedType : NullType;
|
---|
321 | return static_cast<JSType>(tag);
|
---|
322 | }
|
---|
323 |
|
---|
324 | } // namespace KJS
|
---|
325 |
|
---|
326 | #endif
|
---|