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