source: webkit/trunk/JavaScriptCore/kjs/JSImmediate.h@ 27747

Last change on this file since 27747 was 27747, checked in by [email protected], 18 years ago

2007-11-12 Eric Seidel <[email protected]>

Reviewed by Oliver.

Add evaluateToInt32 and evaluateUInt32 methods and deploy them.
Fix a few missing evaluateToBoolean methods
Deploy all evaluateTo* functions to more nodes to avoid slowdowns
http://bugs.webkit.org/show_bug.cgi?id=15950

SunSpider claims this is at least a 1.4% speedup.

  • kjs/JSImmediate.h: (KJS::JSImmediate::getTruncatedInt32): (KJS::JSImmediate::toDouble): (KJS::JSImmediate::getUInt32):
  • kjs/nodes.cpp: (KJS::ExpressionNode::evaluateToNumber): (KJS::ExpressionNode::evaluateToInt32): (KJS::ExpressionNode::evaluateToUInt32): (KJS::NumberNode::evaluateToInt32): (KJS::NumberNode::evaluateToUInt32): (KJS::ImmediateNumberNode::evaluateToInt32): (KJS::ImmediateNumberNode::evaluateToUInt32): (KJS::ResolveNode::evaluate): (KJS::ResolveNode::evaluateToNumber): (KJS::ResolveNode::evaluateToBoolean): (KJS::ResolveNode::evaluateToInt32): (KJS::ResolveNode::evaluateToUInt32): (KJS::LocalVarAccessNode::evaluateToInt32): (KJS::LocalVarAccessNode::evaluateToUInt32): (KJS::BracketAccessorNode::evaluateToNumber): (KJS::BracketAccessorNode::evaluateToBoolean): (KJS::BracketAccessorNode::evaluateToInt32): (KJS::BracketAccessorNode::evaluateToUInt32): (KJS::DotAccessorNode::inlineEvaluate): (KJS::DotAccessorNode::evaluate): (KJS::DotAccessorNode::evaluateToNumber): (KJS::DotAccessorNode::evaluateToBoolean): (KJS::DotAccessorNode::evaluateToInt32): (KJS::DotAccessorNode::evaluateToUInt32): (KJS::NewExprNode::inlineEvaluate): (KJS::NewExprNode::evaluate): (KJS::NewExprNode::evaluateToNumber): (KJS::NewExprNode::evaluateToBoolean): (KJS::NewExprNode::evaluateToInt32): (KJS::NewExprNode::evaluateToUInt32): (KJS::FunctionCallResolveNode::inlineEvaluate): (KJS::FunctionCallResolveNode::evaluate): (KJS::FunctionCallResolveNode::evaluateToNumber): (KJS::FunctionCallResolveNode::evaluateToBoolean): (KJS::FunctionCallResolveNode::evaluateToInt32): (KJS::FunctionCallResolveNode::evaluateToUInt32): (KJS::LocalVarFunctionCallNode::evaluate): (KJS::LocalVarFunctionCallNode::evaluateToNumber): (KJS::LocalVarFunctionCallNode::evaluateToBoolean): (KJS::LocalVarFunctionCallNode::evaluateToInt32): (KJS::LocalVarFunctionCallNode::evaluateToUInt32): (KJS::FunctionCallDotNode::evaluate): (KJS::FunctionCallDotNode::evaluateToNumber): (KJS::FunctionCallDotNode::evaluateToBoolean): (KJS::FunctionCallDotNode::evaluateToInt32): (KJS::FunctionCallDotNode::evaluateToUInt32): (KJS::PostDecLocalVarNode::inlineEvaluateToNumber): (KJS::PostDecLocalVarNode::evaluateToNumber): (KJS::PostDecLocalVarNode::evaluateToBoolean): (KJS::PostDecLocalVarNode::evaluateToInt32): (KJS::PostDecLocalVarNode::evaluateToUInt32): (KJS::typeStringForValue): (KJS::UnaryPlusNode::evaluate): (KJS::UnaryPlusNode::evaluateToBoolean): (KJS::UnaryPlusNode::evaluateToNumber): (KJS::UnaryPlusNode::evaluateToInt32): (KJS::BitwiseNotNode::inlineEvaluateToInt32): (KJS::BitwiseNotNode::evaluate): (KJS::BitwiseNotNode::evaluateToNumber): (KJS::BitwiseNotNode::evaluateToBoolean): (KJS::BitwiseNotNode::evaluateToInt32): (KJS::MultNode::evaluateToBoolean): (KJS::MultNode::evaluateToInt32): (KJS::MultNode::evaluateToUInt32): (KJS::DivNode::evaluateToInt32): (KJS::DivNode::evaluateToUInt32): (KJS::ModNode::evaluateToBoolean): (KJS::ModNode::evaluateToInt32): (KJS::ModNode::evaluateToUInt32): (KJS::AddNode::evaluateToNumber): (KJS::AddNode::evaluateToInt32): (KJS::AddNode::evaluateToUInt32): (KJS::AddNumbersNode::evaluateToInt32): (KJS::AddNumbersNode::evaluateToUInt32): (KJS::SubNode::evaluateToInt32): (KJS::SubNode::evaluateToUInt32): (KJS::LeftShiftNode::inlineEvaluateToInt32): (KJS::LeftShiftNode::evaluate): (KJS::LeftShiftNode::evaluateToNumber): (KJS::LeftShiftNode::evaluateToInt32): (KJS::RightShiftNode::inlineEvaluateToInt32): (KJS::RightShiftNode::evaluate): (KJS::RightShiftNode::evaluateToNumber): (KJS::RightShiftNode::evaluateToInt32): (KJS::UnsignedRightShiftNode::inlineEvaluateToUInt32): (KJS::UnsignedRightShiftNode::evaluate): (KJS::UnsignedRightShiftNode::evaluateToNumber): (KJS::UnsignedRightShiftNode::evaluateToInt32): (KJS::LessNode::inlineEvaluateToBoolean): (KJS::LessNode::evaluate): (KJS::LessNode::evaluateToBoolean): (KJS::LessNumbersNode::inlineEvaluateToBoolean): (KJS::LessNumbersNode::evaluate): (KJS::LessNumbersNode::evaluateToBoolean): (KJS::LessStringsNode::inlineEvaluateToBoolean): (KJS::LessStringsNode::evaluate): (KJS::BitAndNode::evaluate): (KJS::BitAndNode::inlineEvaluateToInt32): (KJS::BitAndNode::evaluateToNumber): (KJS::BitAndNode::evaluateToBoolean): (KJS::BitAndNode::evaluateToInt32): (KJS::BitXOrNode::inlineEvaluateToInt32): (KJS::BitXOrNode::evaluate): (KJS::BitXOrNode::evaluateToNumber): (KJS::BitXOrNode::evaluateToBoolean): (KJS::BitXOrNode::evaluateToInt32): (KJS::BitOrNode::inlineEvaluateToInt32): (KJS::BitOrNode::evaluate): (KJS::BitOrNode::evaluateToNumber): (KJS::BitOrNode::evaluateToBoolean): (KJS::BitOrNode::evaluateToInt32): (KJS::ConditionalNode::evaluateToNumber): (KJS::ConditionalNode::evaluateToInt32): (KJS::ConditionalNode::evaluateToUInt32): (KJS::valueForReadModifyAssignment): (KJS::AssignExprNode::evaluate): (KJS::AssignExprNode::evaluateToBoolean): (KJS::AssignExprNode::evaluateToNumber): (KJS::AssignExprNode::evaluateToInt32): (KJS::VarDeclNode::handleSlowCase):
  • kjs/nodes.h: (KJS::FunctionCallResolveNode::precedence): (KJS::AddNode::precedence): (KJS::AddNode::): (KJS::LessNumbersNode::): (KJS::LessStringsNode::):
  • kjs/value.cpp: (KJS::JSValue::toInt32SlowCase): (KJS::JSValue::toUInt32SlowCase):
  • kjs/value.h: (KJS::JSValue::asCell): (KJS::JSValue::toInt32): (KJS::JSValue::toUInt32):
  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
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
34namespace KJS {
35
36class ExecState;
37class JSObject;
38class JSValue;
39class 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
62class JSImmediate {
63public:
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
126private:
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
150ALWAYS_INLINE JSValue* JSImmediate::trueImmediate() { return tag(1 << 2, BooleanType); }
151ALWAYS_INLINE JSValue* JSImmediate::falseImmediate() { return tag(0, BooleanType); }
152ALWAYS_INLINE JSValue* JSImmediate::undefinedImmediate() { return tag(1 << 2, UndefinedType); }
153ALWAYS_INLINE JSValue* JSImmediate::nullImmediate() { return tag(0, UndefinedType); }
154
155ALWAYS_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
162ALWAYS_INLINE JSValue* JSImmediate::from(char i)
163{
164 return tag(i << 2, NumberType);
165}
166
167ALWAYS_INLINE JSValue* JSImmediate::from(signed char i)
168{
169 return tag(i << 2, NumberType);
170}
171
172ALWAYS_INLINE JSValue* JSImmediate::from(unsigned char i)
173{
174 return tag(i << 2, NumberType);
175}
176
177ALWAYS_INLINE JSValue* JSImmediate::from(short i)
178{
179 return tag(i << 2, NumberType);
180}
181
182ALWAYS_INLINE JSValue* JSImmediate::from(unsigned short i)
183{
184 return tag(i << 2, NumberType);
185}
186
187ALWAYS_INLINE JSValue* JSImmediate::from(int i)
188{
189 if ((i < minImmediateInt) | (i > maxImmediateInt))
190 return 0;
191 return tag(i << 2, NumberType);
192}
193
194ALWAYS_INLINE JSValue* JSImmediate::from(unsigned i)
195{
196 if (i > maxImmediateUInt)
197 return 0;
198 return tag(i << 2, NumberType);
199}
200
201ALWAYS_INLINE JSValue* JSImmediate::from(long i)
202{
203 if ((i < minImmediateInt) | (i > maxImmediateInt))
204 return 0;
205 return tag(i << 2, NumberType);
206}
207
208ALWAYS_INLINE JSValue* JSImmediate::from(unsigned long i)
209{
210 if (i > maxImmediateUInt)
211 return 0;
212 return tag(i << 2, NumberType);
213}
214
215ALWAYS_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
222ALWAYS_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
229ALWAYS_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) || (signbit(d) && !intVal))
238 return 0;
239
240 return tag(intVal << 2, NumberType);
241}
242
243ALWAYS_INLINE int32_t JSImmediate::getTruncatedInt32(const JSValue* v)
244{
245 return static_cast<int32_t>(unTag(v)) >> 2;
246}
247
248ALWAYS_INLINE double JSImmediate::toDouble(const JSValue* v)
249{
250 ASSERT(isImmediate(v));
251 const int32_t i = getTruncatedInt32(v);
252 if (JSImmediate::getTag(v) == UndefinedType && i)
253 return std::numeric_limits<double>::quiet_NaN();
254 return i;
255}
256
257ALWAYS_INLINE bool JSImmediate::getUInt32(const JSValue* v, uint32_t& i)
258{
259 const int32_t si = getTruncatedInt32(v);
260 i = si;
261 return isNumber(v) & (si >= 0);
262}
263
264ALWAYS_INLINE bool JSImmediate::getTruncatedInt32(const JSValue* v, int32_t& i)
265{
266 i = getTruncatedInt32(v);
267 return isNumber(v);
268}
269
270ALWAYS_INLINE bool JSImmediate::getTruncatedUInt32(const JSValue* v, uint32_t& i)
271{
272 return getUInt32(v, i);
273}
274
275} // namespace KJS
276
277#endif
Note: See TracBrowser for help on using the repository browser.