Debian Codegraph @

rhino/src/org/mozilla/javascript/optimizer/Optimizer.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6
7 package org.mozilla.javascript.optimizer;
8
9 import org.mozilla.javascript.*;
10 import org.mozilla.javascript.ast.ScriptNode;
11
12 class Optimizer
13 {
14
15 static final int NoType = 0;
16 static final int NumberType = 1;
17 static final int AnyType = 3;
18
19 // It is assumed that (NumberType | AnyType) == AnyType
20
21 void optimize(ScriptNode scriptOrFn)
22 {
23 // run on one function at a time for now
24 int functionCount = scriptOrFn.getFunctionCount();
25 for (int i = 0; i != functionCount; ++i) {
26 OptFunctionNode f = OptFunctionNode.get(scriptOrFn, i);
27 optimizeFunction(f);
28 }
29 }
30
31 private void optimizeFunction(OptFunctionNode theFunction)
32 {
33 if (theFunction.fnode.requiresActivation()) return;
34
35 inDirectCallFunction = theFunction.isTargetOfDirectCall();
36 this.theFunction = theFunction;
37
38 ObjArray statementsArray = new ObjArray();
39 buildStatementList_r(theFunction.fnode, statementsArray);
40 Node[] theStatementNodes = new Node[statementsArray.size()];
41 statementsArray.toArray(theStatementNodes);
42
43 Block.runFlowAnalyzes(theFunction, theStatementNodes);
44
45 if (!theFunction.fnode.requiresActivation()) {
46 /*
47 * Now that we know which local vars are in fact always
48 * Numbers, we re-write the tree to take advantage of
49 * that. Any arithmetic or assignment op involving just
50 * Number typed vars is marked so that the codegen will
51 * generate non-object code.
52 */
53 parameterUsedInNumberContext = false;
54 for (Node theStatementNode : theStatementNodes) {
55 rewriteForNumberVariables(theStatementNode, NumberType);
56 }
57 theFunction.setParameterNumberContext(parameterUsedInNumberContext);
58 }
59
60 }
61
62
63 /*
64 Each directCall parameter is passed as a pair of values - an object
65 and a double. The value passed depends on the type of value available at
66 the call site. If a double is available, the object in java/lang/Void.TYPE
67 is passed as the object value, and if an object value is available, then
68 0.0 is passed as the double value.
69
70 The receiving routine always tests the object value before proceeding.
71 If the parameter is being accessed in a 'Number Context' then the code
72 sequence is :
73 if ("parameter_objectValue" == java/lang/Void.TYPE)
74 ...fine..., use the parameter_doubleValue
75 else
76 toNumber(parameter_objectValue)
77
78 and if the parameter is being referenced in an Object context, the code is
79 if ("parameter_objectValue" == java/lang/Void.TYPE)
80 new Double(parameter_doubleValue)
81 else
82 ...fine..., use the parameter_objectValue
83
84 If the receiving code never uses the doubleValue, it is converted on
85 entry to a Double instead.
86 */
87
88
89 /*
90 We're referencing a node in a Number context (i.e. we'd prefer it
91 was a double value). If the node is a parameter in a directCall
92 function, mark it as being referenced in this context.
93 */
94 private void markDCPNumberContext(Node n)
95 {
96 if (inDirectCallFunction && n.getType() == Token.GETVAR) {
97 int varIndex = theFunction.getVarIndex(n);
98 if (theFunction.isParameter(varIndex)) {
99 parameterUsedInNumberContext = true;
100 }
101 }
102 }
103
104 private boolean convertParameter(Node n)
105 {
106 if (inDirectCallFunction && n.getType() == Token.GETVAR) {
107 int varIndex = theFunction.getVarIndex(n);
108 if (theFunction.isParameter(varIndex)) {
109 n.removeProp(Node.ISNUMBER_PROP);
110 return true;
111 }
112 }
113 return false;
114 }
115
116 private int rewriteForNumberVariables(Node n, int desired)
117 {
118 switch (n.getType()) {
119 case Token.EXPR_VOID : {
120 Node child = n.getFirstChild();
121 int type = rewriteForNumberVariables(child, NumberType);
122 if (type == NumberType)
123 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
124 return NoType;
125 }
126 case Token.NUMBER :
127 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
128 return NumberType;
129
130 case Token.GETVAR :
131 {
132 int varIndex = theFunction.getVarIndex(n);
133 if (inDirectCallFunction
134 && theFunction.isParameter(varIndex)
135 && desired == NumberType)
136 {
137 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
138 return NumberType;
139 }
140 else if (theFunction.isNumberVar(varIndex)) {
141 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
142 return NumberType;
143 }
144 return NoType;
145 }
146
147 case Token.INC :
148 case Token.DEC : {
149 Node child = n.getFirstChild();
150 int type = rewriteForNumberVariables(child, NumberType);
151 if (child.getType() == Token.GETVAR) {
152 if (type == NumberType && !convertParameter(child))
153 {
154 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
155 markDCPNumberContext(child);
156 return NumberType;
157 }
158 return NoType;
159 }
160 else if (child.getType() == Token.GETELEM
161 || child.getType() == Token.GETPROP) {
162 return type;
163 }
164 return NoType;
165 }
166 case Token.SETVAR :
167 case Token.SETCONSTVAR : {
168 Node lChild = n.getFirstChild();
169 Node rChild = lChild.getNext();
170 int rType = rewriteForNumberVariables(rChild, NumberType);
171 int varIndex = theFunction.getVarIndex(n);
172 if (inDirectCallFunction
173 && theFunction.isParameter(varIndex))
174 {
175 if (rType == NumberType) {
176 if (!convertParameter(rChild)) {
177 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
178 return NumberType;
179 }
180 markDCPNumberContext(rChild);
181 return NoType;
182 }
183 else
184 return rType;
185 }
186 else if (theFunction.isNumberVar(varIndex)) {
187 if (rType != NumberType) {
188 n.removeChild(rChild);
189 n.addChildToBack(
190 new Node(Token.TO_DOUBLE, rChild));
191 }
192 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
193 markDCPNumberContext(rChild);
194 return NumberType;
195 }
196 else {
197 if (rType == NumberType) {
198 if (!convertParameter(rChild)) {
199 n.removeChild(rChild);
200 n.addChildToBack(
201 new Node(Token.TO_OBJECT, rChild));
202 }
203 }
204 return NoType;
205 }
206 }
207 case Token.LE :
208 case Token.LT :
209 case Token.GE :
210 case Token.GT : {
211 Node lChild = n.getFirstChild();
212 Node rChild = lChild.getNext();
213 int lType = rewriteForNumberVariables(lChild, NumberType);
214 int rType = rewriteForNumberVariables(rChild, NumberType);
215 markDCPNumberContext(lChild);
216 markDCPNumberContext(rChild);
217
218 if (convertParameter(lChild)) {
219 if (convertParameter(rChild)) {
220 return NoType;
221 } else if (rType == NumberType) {
222 n.putIntProp(Node.ISNUMBER_PROP, Node.RIGHT);
223 }
224 }
225 else if (convertParameter(rChild)) {
226 if (lType == NumberType) {
227 n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
228 }
229 }
230 else {
231 if (lType == NumberType) {
232 if (rType == NumberType) {
233 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
234 }
235 else {
236 n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
237 }
238 }
239 else {
240 if (rType == NumberType) {
241 n.putIntProp(Node.ISNUMBER_PROP, Node.RIGHT);
242 }
243 }
244 }
245 // we actually build a boolean value
246 return NoType;
247 }
248
249 case Token.ADD : {
250 Node lChild = n.getFirstChild();
251 Node rChild = lChild.getNext();
252 int lType = rewriteForNumberVariables(lChild, NumberType);
253 int rType = rewriteForNumberVariables(rChild, NumberType);
254
255
256 if (convertParameter(lChild)) {
257 if (convertParameter(rChild)) {
258 return NoType;
259 }
260 else {
261 if (rType == NumberType) {
262 n.putIntProp(Node.ISNUMBER_PROP, Node.RIGHT);
263 }
264 }
265 }
266 else {
267 if (convertParameter(rChild)) {
268 if (lType == NumberType) {
269 n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
270 }
271 }
272 else {
273 if (lType == NumberType) {
274 if (rType == NumberType) {
275 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
276 return NumberType;
277 }
278 else {
279 n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
280 }
281 }
282 else {
283 if (rType == NumberType) {
284 n.putIntProp(Node.ISNUMBER_PROP,
285 Node.RIGHT);
286 }
287 }
288 }
289 }
290 return NoType;
291 }
292
293 case Token.BITXOR :
294 case Token.BITOR :
295 case Token.BITAND :
296 case Token.RSH :
297 case Token.LSH :
298 case Token.SUB :
299 case Token.MUL :
300 case Token.DIV :
301 case Token.MOD : {
302 Node lChild = n.getFirstChild();
303 Node rChild = lChild.getNext();
304 int lType = rewriteForNumberVariables(lChild, NumberType);
305 int rType = rewriteForNumberVariables(rChild, NumberType);
306 markDCPNumberContext(lChild);
307 markDCPNumberContext(rChild);
308 if (lType == NumberType) {
309 if (rType == NumberType) {
310 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
311 return NumberType;
312 }
313 else {
314 if (!convertParameter(rChild)) {
315 n.removeChild(rChild);
316 n.addChildToBack(
317 new Node(Token.TO_DOUBLE, rChild));
318 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
319 }
320 return NumberType;
321 }
322 }
323 else {
324 if (rType == NumberType) {
325 if (!convertParameter(lChild)) {
326 n.removeChild(lChild);
327 n.addChildToFront(
328 new Node(Token.TO_DOUBLE, lChild));
329 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
330 }
331 return NumberType;
332 }
333 else {
334 if (!convertParameter(lChild)) {
335 n.removeChild(lChild);
336 n.addChildToFront(
337 new Node(Token.TO_DOUBLE, lChild));
338 }
339 if (!convertParameter(rChild)) {
340 n.removeChild(rChild);
341 n.addChildToBack(
342 new Node(Token.TO_DOUBLE, rChild));
343 }
344 n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
345 return NumberType;
346 }
347 }
348 }
349 case Token.SETELEM :
350 case Token.SETELEM_OP : {
351 Node arrayBase = n.getFirstChild();
352 Node arrayIndex = arrayBase.getNext();
353 Node rValue = arrayIndex.getNext();
354 int baseType = rewriteForNumberVariables(arrayBase, NumberType);
355 if (baseType == NumberType) {
356 if (!convertParameter(arrayBase)) {
357 n.removeChild(arrayBase);
358 n.addChildToFront(
359 new Node(Token.TO_OBJECT, arrayBase));
360 }
361 }
362 int indexType = rewriteForNumberVariables(arrayIndex, NumberType);
363 if (indexType == NumberType) {
364 if (!convertParameter(arrayIndex)) {
365 // setting the ISNUMBER_PROP signals the codegen
366 // to use the OptRuntime.setObjectIndex that takes
367 // a double index
368 n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
369 }
370 }
371 int rValueType = rewriteForNumberVariables(rValue, NumberType);
372 if (rValueType == NumberType) {
373 if (!convertParameter(rValue)) {
374 n.removeChild(rValue);
375 n.addChildToBack(
376 new Node(Token.TO_OBJECT, rValue));
377 }
378 }
379 return NoType;
380 }
381 case Token.GETELEM : {
382 Node arrayBase = n.getFirstChild();
383 Node arrayIndex = arrayBase.getNext();
384 int baseType = rewriteForNumberVariables(arrayBase, NumberType);
385 if (baseType == NumberType) {
386 if (!convertParameter(arrayBase)) {
387 n.removeChild(arrayBase);
388 n.addChildToFront(
389 new Node(Token.TO_OBJECT, arrayBase));
390 }
391 }
392 int indexType = rewriteForNumberVariables(arrayIndex, NumberType);
393 if (indexType == NumberType) {
394 if (!convertParameter(arrayIndex)) {
395 // setting the ISNUMBER_PROP signals the codegen
396 // to use the OptRuntime.getObjectIndex that takes
397 // a double index
398 n.putIntProp(Node.ISNUMBER_PROP, Node.RIGHT);
399 }
400 }
401 return NoType;
402 }
403 case Token.CALL :
404 {
405 Node child = n.getFirstChild(); // the function node
406 // must be an object
407 rewriteAsObjectChildren(child, child.getFirstChild());
408 child = child.getNext(); // the first arg
409
410 OptFunctionNode target
411 = (OptFunctionNode)n.getProp(Node.DIRECTCALL_PROP);
412 if (target != null) {
413 /*
414 we leave each child as a Number if it can be. The codegen will
415 handle moving the pairs of parameters.
416 */
417 while (child != null) {
418 int type = rewriteForNumberVariables(child, NumberType);
419 if (type == NumberType) {
420 markDCPNumberContext(child);
421 }
422 child = child.getNext();
423 }
424 } else {
425 rewriteAsObjectChildren(n, child);
426 }
427 return NoType;
428 }
429 default : {
430 rewriteAsObjectChildren(n, n.getFirstChild());
431 return NoType;
432 }
433 }
434 }
435
436 private void rewriteAsObjectChildren(Node n, Node child)
437 {
438 // Force optimized children to be objects
439 while (child != null) {
440 Node nextChild = child.getNext();
441 int type = rewriteForNumberVariables(child, NoType);
442 if (type == NumberType) {
443 if (!convertParameter(child)) {
444 n.removeChild(child);
445 Node nuChild = new Node(Token.TO_OBJECT, child);
446 if (nextChild == null)
447 n.addChildToBack(nuChild);
448 else
449 n.addChildBefore(nuChild, nextChild);
450 }
451 }
452 child = nextChild;
453 }
454 }
455
456 private static void buildStatementList_r(Node node, ObjArray statements)
457 {
458 int type = node.getType();
459 if (type == Token.BLOCK
460 || type == Token.LOCAL_BLOCK
461 || type == Token.LOOP
462 || type == Token.FUNCTION)
463 {
464 Node child = node.getFirstChild();
465 while (child != null) {
466 buildStatementList_r(child, statements);
467 child = child.getNext();
468 }
469 } else {
470 statements.add(node);
471 }
472 }
473
474 private boolean inDirectCallFunction;
475 OptFunctionNode theFunction;
476 private boolean parameterUsedInNumberContext;
477 }