Debian Codegraph @

rhino/src/org/mozilla/javascript/optimizer/OptTransformer.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 package org.mozilla.javascript.optimizer;
7
8 import org.mozilla.javascript.*;
9 import org.mozilla.javascript.ast.ScriptNode;
10 import java.util.Map;
11
12 /**
13 * This class performs node transforms to prepare for optimization.
14 *
15 * @see NodeTransformer
16 * @author Norris Boyd
17 */
18
19 class OptTransformer extends NodeTransformer {
20
21 OptTransformer(Map<String,OptFunctionNode> possibleDirectCalls, ObjArray directCallTargets)
22 {
23 this.possibleDirectCalls = possibleDirectCalls;
24 this.directCallTargets = directCallTargets;
25 }
26
27 @Override
28 protected void visitNew(Node node, ScriptNode tree) {
29 detectDirectCall(node, tree);
30 super.visitNew(node, tree);
31 }
32
33 @Override
34 protected void visitCall(Node node, ScriptNode tree) {
35 detectDirectCall(node, tree);
36 super.visitCall(node, tree);
37 }
38
39 private void detectDirectCall(Node node, ScriptNode tree)
40 {
41 if (tree.getType() == Token.FUNCTION) {
42 Node left = node.getFirstChild();
43
44 // count the arguments
45 int argCount = 0;
46 Node arg = left.getNext();
47 while (arg != null) {
48 arg = arg.getNext();
49 argCount++;
50 }
51
52 if (argCount == 0) {
53 OptFunctionNode.get(tree).itsContainsCalls0 = true;
54 }
55
56 /*
57 * Optimize a call site by converting call("a", b, c) into :
58 *
59 * FunctionObjectFor"a" <-- instance variable init'd by constructor
60 *
61 * // this is a DIRECTCALL node
62 * fn = GetProp(tmp = GetBase("a"), "a");
63 * if (fn == FunctionObjectFor"a")
64 * fn.call(tmp, b, c)
65 * else
66 * ScriptRuntime.Call(fn, tmp, b, c)
67 */
68 if (possibleDirectCalls != null) {
69 String targetName = null;
70 if (left.getType() == Token.NAME) {
71 targetName = left.getString();
72 } else if (left.getType() == Token.GETPROP) {
73 targetName = left.getFirstChild().getNext().getString();
74 } else if (left.getType() == Token.GETPROPNOWARN) {
75 throw Kit.codeBug();
76 }
77 if (targetName != null) {
78 OptFunctionNode ofn;
79 ofn = possibleDirectCalls.get(targetName);
80 if (ofn != null
81 && argCount == ofn.fnode.getParamCount()
82 && !ofn.fnode.requiresActivation())
83 {
84 // Refuse to directCall any function with more
85 // than 32 parameters - prevent code explosion
86 // for wacky test cases
87 if (argCount <= 32) {
88 node.putProp(Node.DIRECTCALL_PROP, ofn);
89 if (!ofn.isTargetOfDirectCall()) {
90 int index = directCallTargets.size();
91 directCallTargets.add(ofn);
92 ofn.setDirectTargetIndex(index);
93 }
94 }
95 }
96 }
97 }
98 }
99 }
100
101 private Map<String,OptFunctionNode> possibleDirectCalls;
102 private ObjArray directCallTargets;
103 }