001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1/GPL 2.0
003: *
004: * The contents of this file are subject to the Mozilla Public License Version
005: * 1.1 (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the
012: * License.
013: *
014: * The Original Code is Rhino code, released
015: * May 6, 1999.
016: *
017: * The Initial Developer of the Original Code is
018: * Netscape Communications Corporation.
019: * Portions created by the Initial Developer are Copyright (C) 1997-1999
020: * the Initial Developer. All Rights Reserved.
021: *
022: * Contributor(s):
023: * Norris Boyd
024: * Roger Lawrence
025: *
026: * Alternatively, the contents of this file may be used under the terms of
027: * the GNU General Public License Version 2 or later (the "GPL"), in which
028: * case the provisions of the GPL are applicable instead of those above. If
029: * you wish to allow use of your version of this file only under the terms of
030: * the GPL and not to allow others to use your version of this file under the
031: * MPL, indicate your decision by deleting the provisions above and replacing
032: * them with the notice and other provisions required by the GPL. If you do
033: * not delete the provisions above, a recipient may use your version of this
034: * file under either the MPL or the GPL.
035: *
036: * ***** END LICENSE BLOCK ***** */
037:
038: package org.mozilla.javascript.optimizer;
039:
040: import org.mozilla.javascript.*;
041: import java.util.Hashtable;
042:
043: /**
044: * This class performs node transforms to prepare for optimization.
045: *
046: * @see NodeTransformer
047: * @author Norris Boyd
048: */
049:
050: class OptTransformer extends NodeTransformer {
051:
052: OptTransformer(Hashtable possibleDirectCalls,
053: ObjArray directCallTargets) {
054: this .possibleDirectCalls = possibleDirectCalls;
055: this .directCallTargets = directCallTargets;
056: }
057:
058: protected void visitNew(Node node, ScriptOrFnNode tree) {
059: detectDirectCall(node, tree);
060: super .visitNew(node, tree);
061: }
062:
063: protected void visitCall(Node node, ScriptOrFnNode tree) {
064: detectDirectCall(node, tree);
065: super .visitCall(node, tree);
066: }
067:
068: private void detectDirectCall(Node node, ScriptOrFnNode tree) {
069: if (tree.getType() == Token.FUNCTION) {
070: Node left = node.getFirstChild();
071:
072: // count the arguments
073: int argCount = 0;
074: Node arg = left.getNext();
075: while (arg != null) {
076: arg = arg.getNext();
077: argCount++;
078: }
079:
080: if (argCount == 0) {
081: OptFunctionNode.get(tree).itsContainsCalls0 = true;
082: }
083:
084: /*
085: * Optimize a call site by converting call("a", b, c) into :
086: *
087: * FunctionObjectFor"a" <-- instance variable init'd by constructor
088: *
089: * // this is a DIRECTCALL node
090: * fn = GetProp(tmp = GetBase("a"), "a");
091: * if (fn == FunctionObjectFor"a")
092: * fn.call(tmp, b, c)
093: * else
094: * ScriptRuntime.Call(fn, tmp, b, c)
095: */
096: if (possibleDirectCalls != null) {
097: String targetName = null;
098: if (left.getType() == Token.NAME) {
099: targetName = left.getString();
100: } else if (left.getType() == Token.GETPROP) {
101: targetName = left.getFirstChild().getNext()
102: .getString();
103: } else if (left.getType() == Token.GETPROPNOWARN) {
104: throw Kit.codeBug();
105: }
106: if (targetName != null) {
107: OptFunctionNode ofn;
108: ofn = (OptFunctionNode) possibleDirectCalls
109: .get(targetName);
110: if (ofn != null
111: && argCount == ofn.fnode.getParamCount()
112: && !ofn.fnode.requiresActivation()) {
113: // Refuse to directCall any function with more
114: // than 32 parameters - prevent code explosion
115: // for wacky test cases
116: if (argCount <= 32) {
117: node.putProp(Node.DIRECTCALL_PROP, ofn);
118: if (!ofn.isTargetOfDirectCall()) {
119: int index = directCallTargets.size();
120: directCallTargets.add(ofn);
121: ofn.setDirectTargetIndex(index);
122: }
123: }
124: }
125: }
126: }
127: }
128: }
129:
130: private Hashtable possibleDirectCalls;
131: private ObjArray directCallTargets;
132: }
|