2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
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.
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.
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.
22 #ifndef KJS_JS_IMMEDIATE_H
23 #define KJS_JS_IMMEDIATE_H
25 #include <wtf/Assertions.h>
26 #include <wtf/AlwaysInline.h>
27 #include <wtf/MathExtras.h>
42 * A JSValue* is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged
43 * value masquerading as a pointer). The low two bits in a JSValue* are available for type tagging
44 * because allocator alignment guarantees they will be 00 in cell pointers.
46 * For example, on a 32 bit system:
48 * JSCell*: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00
49 * [ high 30 bits: pointer address ] [ low 2 bits -- always 0 ]
50 * JSImmediate: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TT
51 * [ high 30 bits: 'payload' ] [ low 2 bits -- tag ]
53 * Where the bottom two bits are non-zero they either indicate that the immediate is a 31 bit signed
54 * integer, or they mark the value as being an immediate of a type other than integer, with a secondary
55 * tag used to indicate the exact type.
57 * Where the lowest bit is set (TT is equal to 01 or 11) the high 31 bits form a 31 bit signed int value.
58 * Where TT is equal to 10 this indicates this is a type of immediate other than an integer, and the next
59 * two bits will form an extended tag.
61 * 31 bit signed int: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X1
62 * [ high 30 bits of the value ] [ high bit part of value ]
63 * Other: YYYYYYYYYYYYYYYYYYYYYYYYYYYY ZZ 10
64 * [ extended 'payload' ] [ extended tag ] [ tag 'other' ]
66 * Where the first bit of the extended tag is set this flags the value as being a boolean, and the following
67 * bit would flag the value as undefined. If neither bits are set, the value is null.
69 * Other: YYYYYYYYYYYYYYYYYYYYYYYYYYYY UB 10
70 * [ extended 'payload' ] [ undefined | bool ] [ tag 'other' ]
72 * For boolean value the lowest bit in the payload holds the value of the bool, all remaining bits are zero.
73 * For undefined or null immediates the payload is zero.
75 * Boolean: 000000000000000000000000000V 01 10
76 * [ boolean value ] [ bool ] [ tag 'other' ]
77 * Undefined: 0000000000000000000000000000 10 10
78 * [ zero ] [ undefined ] [ tag 'other' ]
79 * Null: 0000000000000000000000000000 00 10
80 * [ zero ] [ zero ] [ tag 'other' ]
85 friend class CTI; // Whooo!
87 static const uintptr_t TagMask = 0x3u; // primary tag is 2 bits long
88 static const uintptr_t TagBitTypeInteger = 0x1u; // bottom bit set indicates integer, this dominates the following bit
89 static const uintptr_t TagBitTypeOther = 0x2u; // second bit set indicates immediate other than an integer
91 static const uintptr_t ExtendedTagMask = 0xCu; // extended tag holds a further two bits
92 static const uintptr_t ExtendedTagBitBool = 0x4u;
93 static const uintptr_t ExtendedTagBitUndefined = 0x8u;
95 static const uintptr_t FullTagTypeMask = TagMask | ExtendedTagMask;
96 static const uintptr_t FullTagTypeBool = TagBitTypeOther | ExtendedTagBitBool;
97 static const uintptr_t FullTagTypeUndefined = TagBitTypeOther | ExtendedTagBitUndefined;
98 static const uintptr_t FullTagTypeNull = TagBitTypeOther;
100 static const uint32_t IntegerPayloadShift = 1u;
101 static const uint32_t ExtendedPayloadShift = 4u;
103 static const uintptr_t ExtendedPayloadBitBoolValue = 1 << ExtendedPayloadShift;
106 static ALWAYS_INLINE bool isImmediate(const JSValue* v)
108 return reinterpret_cast<uintptr_t>(v) & TagMask;
111 static ALWAYS_INLINE bool isNumber(const JSValue* v)
113 return reinterpret_cast<uintptr_t>(v) & TagBitTypeInteger;
116 static ALWAYS_INLINE bool isPositiveNumber(const JSValue* v)
118 // A single mask to check for the sign bit and the number tag all at once.
119 return (reinterpret_cast<uintptr_t>(v) & (0x80000000 | TagBitTypeInteger)) == TagBitTypeInteger;
122 static ALWAYS_INLINE bool isBoolean(const JSValue* v)
124 return (reinterpret_cast<uintptr_t>(v) & FullTagTypeMask) == FullTagTypeBool;
127 static ALWAYS_INLINE bool isUndefinedOrNull(const JSValue* v)
129 // Undefined and null share the same value, bar the 'undefined' bit in the extended tag.
130 return (reinterpret_cast<uintptr_t>(v) & ~ExtendedTagBitUndefined) == FullTagTypeNull;
133 static bool isNegative(const JSValue* v)
136 return reinterpret_cast<uintptr_t>(v) & 0x80000000;
139 static JSValue* from(char);
140 static JSValue* from(signed char);
141 static JSValue* from(unsigned char);
142 static JSValue* from(short);
143 static JSValue* from(unsigned short);
144 static JSValue* from(int);
145 static JSValue* from(unsigned);
146 static JSValue* from(long);
147 static JSValue* from(unsigned long);
148 static JSValue* from(long long);
149 static JSValue* from(unsigned long long);
150 static JSValue* from(double);
152 static ALWAYS_INLINE bool isEitherImmediate(const JSValue* v1, const JSValue* v2)
154 return (reinterpret_cast<uintptr_t>(v1) | reinterpret_cast<uintptr_t>(v2)) & TagMask;
157 static ALWAYS_INLINE bool areBothImmediate(const JSValue* v1, const JSValue* v2)
159 return isImmediate(v1) & isImmediate(v2);
162 static ALWAYS_INLINE bool areBothImmediateNumbers(const JSValue* v1, const JSValue* v2)
164 return reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2) & TagBitTypeInteger;
167 static ALWAYS_INLINE JSValue* andImmediateNumbers(const JSValue* v1, const JSValue* v2)
169 ASSERT(areBothImmediateNumbers(v1, v2));
170 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) & reinterpret_cast<uintptr_t>(v2));
173 static ALWAYS_INLINE JSValue* xorImmediateNumbers(const JSValue* v1, const JSValue* v2)
175 ASSERT(areBothImmediateNumbers(v1, v2));
176 return reinterpret_cast<JSValue*>((reinterpret_cast<uintptr_t>(v1) ^ reinterpret_cast<uintptr_t>(v2)) | TagBitTypeInteger);
179 static ALWAYS_INLINE JSValue* orImmediateNumbers(const JSValue* v1, const JSValue* v2)
181 ASSERT(areBothImmediateNumbers(v1, v2));
182 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) | reinterpret_cast<uintptr_t>(v2));
185 static ALWAYS_INLINE JSValue* rightShiftImmediateNumbers(const JSValue* val, const JSValue* shift)
187 ASSERT(areBothImmediateNumbers(val, shift));
188 return reinterpret_cast<JSValue*>((reinterpret_cast<intptr_t>(val) >> ((reinterpret_cast<uintptr_t>(shift) >> IntegerPayloadShift) & 0x1f)) | TagBitTypeInteger);
191 static ALWAYS_INLINE bool canDoFastAdditiveOperations(const JSValue* v)
193 // Number is non-negative and an operation involving two of these can't overflow.
194 // Checking for allowed negative numbers takes more time than it's worth on SunSpider.
195 return (reinterpret_cast<uintptr_t>(v) & (TagBitTypeInteger + (3u << 30))) == TagBitTypeInteger;
198 static ALWAYS_INLINE JSValue* addImmediateNumbers(const JSValue* v1, const JSValue* v2)
200 ASSERT(canDoFastAdditiveOperations(v1));
201 ASSERT(canDoFastAdditiveOperations(v2));
202 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) + reinterpret_cast<uintptr_t>(v2) - TagBitTypeInteger);
205 static ALWAYS_INLINE JSValue* subImmediateNumbers(const JSValue* v1, const JSValue* v2)
207 ASSERT(canDoFastAdditiveOperations(v1));
208 ASSERT(canDoFastAdditiveOperations(v2));
209 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v1) - reinterpret_cast<uintptr_t>(v2) + TagBitTypeInteger);
212 static ALWAYS_INLINE JSValue* incImmediateNumber(const JSValue* v)
214 ASSERT(canDoFastAdditiveOperations(v));
215 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v) + (1 << IntegerPayloadShift));
218 static ALWAYS_INLINE JSValue* decImmediateNumber(const JSValue* v)
220 ASSERT(canDoFastAdditiveOperations(v));
221 return reinterpret_cast<JSValue*>(reinterpret_cast<uintptr_t>(v) - (1 << IntegerPayloadShift));
224 static double toDouble(const JSValue*);
225 static bool toBoolean(const JSValue*);
226 static JSObject* toObject(const JSValue*, ExecState*);
227 static UString toString(const JSValue*);
229 static bool getUInt32(const JSValue*, uint32_t&);
230 static bool getTruncatedInt32(const JSValue*, int32_t&);
231 static bool getTruncatedUInt32(const JSValue*, uint32_t&);
233 static int32_t getTruncatedInt32(const JSValue*);
234 static uint32_t getTruncatedUInt32(const JSValue*);
236 static JSValue* trueImmediate();
237 static JSValue* falseImmediate();
238 static JSValue* undefinedImmediate();
239 static JSValue* nullImmediate();
240 static JSValue* zeroImmediate();
241 static JSValue* oneImmediate();
243 static JSValue* impossibleValue();
245 static JSObject* prototype(const JSValue*, ExecState*);
248 static const int minImmediateInt = ((-INT_MAX) - 1) >> IntegerPayloadShift;
249 static const int maxImmediateInt = INT_MAX >> IntegerPayloadShift;
250 static const unsigned maxImmediateUInt = maxImmediateInt;
252 static ALWAYS_INLINE JSValue* makeInt(int32_t value)
254 return reinterpret_cast<JSValue*>((value << IntegerPayloadShift) | TagBitTypeInteger);
257 static ALWAYS_INLINE JSValue* makeBool(bool b)
259 return reinterpret_cast<JSValue*>((static_cast<uintptr_t>(b) << ExtendedPayloadShift) | FullTagTypeBool);
262 static ALWAYS_INLINE JSValue* makeUndefined()
264 return reinterpret_cast<JSValue*>(FullTagTypeUndefined);
267 static ALWAYS_INLINE JSValue* makeNull()
269 return reinterpret_cast<JSValue*>(FullTagTypeNull);
272 static ALWAYS_INLINE int32_t intValue(const JSValue* v)
274 return static_cast<int32_t>(reinterpret_cast<intptr_t>(v) >> IntegerPayloadShift);
277 static ALWAYS_INLINE uint32_t uintValue(const JSValue* v)
279 return static_cast<uint32_t>(rawValue(v) >> IntegerPayloadShift);
282 static ALWAYS_INLINE bool boolValue(const JSValue* v)
284 return rawValue(v) & ExtendedPayloadBitBoolValue;
287 static ALWAYS_INLINE uintptr_t rawValue(const JSValue* v)
289 return reinterpret_cast<uintptr_t>(v);
292 static double nonInlineNaN();
295 ALWAYS_INLINE JSValue* JSImmediate::trueImmediate() { return makeBool(true); }
296 ALWAYS_INLINE JSValue* JSImmediate::falseImmediate() { return makeBool(false); }
297 ALWAYS_INLINE JSValue* JSImmediate::undefinedImmediate() { return makeUndefined(); }
298 ALWAYS_INLINE JSValue* JSImmediate::nullImmediate() { return makeNull(); }
299 ALWAYS_INLINE JSValue* JSImmediate::zeroImmediate() { return makeInt(0); }
300 ALWAYS_INLINE JSValue* JSImmediate::oneImmediate() { return makeInt(1); }
302 // This value is impossible because 0x4 is not a valid pointer but a tag of 0 would indicate non-immediate
303 ALWAYS_INLINE JSValue* JSImmediate::impossibleValue() { return reinterpret_cast<JSValue*>(0x4); }
305 ALWAYS_INLINE bool JSImmediate::toBoolean(const JSValue* v)
307 ASSERT(isImmediate(v));
308 uintptr_t bits = rawValue(v);
309 return (bits & TagBitTypeInteger)
310 ? bits != TagBitTypeInteger // !0 ints
311 : bits == (FullTagTypeBool | ExtendedPayloadBitBoolValue); // bool true
314 ALWAYS_INLINE uint32_t JSImmediate::getTruncatedUInt32(const JSValue* v)
320 ALWAYS_INLINE JSValue* JSImmediate::from(char i)
325 ALWAYS_INLINE JSValue* JSImmediate::from(signed char i)
330 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned char i)
335 ALWAYS_INLINE JSValue* JSImmediate::from(short i)
340 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned short i)
345 ALWAYS_INLINE JSValue* JSImmediate::from(int i)
347 if ((i < minImmediateInt) | (i > maxImmediateInt))
352 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned i)
354 if (i > maxImmediateUInt)
359 ALWAYS_INLINE JSValue* JSImmediate::from(long i)
361 if ((i < minImmediateInt) | (i > maxImmediateInt))
366 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long i)
368 if (i > maxImmediateUInt)
373 ALWAYS_INLINE JSValue* JSImmediate::from(long long i)
375 if ((i < minImmediateInt) | (i > maxImmediateInt))
377 return makeInt(static_cast<uintptr_t>(i));
380 ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long long i)
382 if (i > maxImmediateUInt)
384 return makeInt(static_cast<uintptr_t>(i));
387 ALWAYS_INLINE JSValue* JSImmediate::from(double d)
389 const int intVal = static_cast<int>(d);
391 if ((intVal < minImmediateInt) | (intVal > maxImmediateInt))
394 // Check for data loss from conversion to int.
395 if (intVal != d || (!intVal && signbit(d)))
398 return makeInt(intVal);
401 ALWAYS_INLINE int32_t JSImmediate::getTruncatedInt32(const JSValue* v)
407 ALWAYS_INLINE double JSImmediate::toDouble(const JSValue* v)
409 ASSERT(isImmediate(v));
413 else if (rawValue(v) == FullTagTypeUndefined)
414 return nonInlineNaN();
416 i = rawValue(v) >> ExtendedPayloadShift;
420 ALWAYS_INLINE bool JSImmediate::getUInt32(const JSValue* v, uint32_t& i)
423 return isPositiveNumber(v);
426 ALWAYS_INLINE bool JSImmediate::getTruncatedInt32(const JSValue* v, int32_t& i)
432 ALWAYS_INLINE bool JSImmediate::getTruncatedUInt32(const JSValue* v, uint32_t& i)
434 return getUInt32(v, i);
437 ALWAYS_INLINE JSValue* jsUndefined()
439 return JSImmediate::undefinedImmediate();
442 inline JSValue* jsNull()
444 return JSImmediate::nullImmediate();
447 inline JSValue* jsBoolean(bool b)
449 return b ? JSImmediate::trueImmediate() : JSImmediate::falseImmediate();