?? optimizer.java
字號:
/* * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1997-1999 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Norris Boyd * Roger Lawrence * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the * provisions of the GPL are applicable instead of those above. * If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use your * version of this file under the NPL, indicate your decision by * deleting the provisions above and replace them with the notice * and other provisions required by the GPL. If you do not delete * the provisions above, a recipient may use your version of this * file under either the NPL or the GPL. */package org.mozilla.javascript.optimizer;import org.mozilla.javascript.*;class Optimizer{ static final int NoType = 0; static final int NumberType = 1; static final int AnyType = 3; // It is assumed that (NumberType | AnyType) == AnyType void optimize(ScriptOrFnNode scriptOrFn, int optLevel) { itsOptLevel = optLevel; // run on one function at a time for now int functionCount = scriptOrFn.getFunctionCount(); for (int i = 0; i != functionCount; ++i) { OptFunctionNode f = OptFunctionNode.get(scriptOrFn, i); optimizeFunction(f); } } private void optimizeFunction(OptFunctionNode theFunction) { if (theFunction.fnode.requiresActivation()) return; inDirectCallFunction = theFunction.isTargetOfDirectCall(); this.theFunction = theFunction; ObjArray statementsArray = new ObjArray(); buildStatementList_r(theFunction.fnode, statementsArray); Node[] theStatementNodes = new Node[statementsArray.size()]; statementsArray.toArray(theStatementNodes); Block.runFlowAnalyzes(theFunction, theStatementNodes); if (!theFunction.fnode.requiresActivation()) { /* * Now that we know which local vars are in fact always * Numbers, we re-write the tree to take advantage of * that. Any arithmetic or assignment op involving just * Number typed vars is marked so that the codegen will * generate non-object code. */ parameterUsedInNumberContext = false; for (int i = 0; i < theStatementNodes.length; i++) { rewriteForNumberVariables(theStatementNodes[i]); } theFunction.setParameterNumberContext(parameterUsedInNumberContext); } }/* Each directCall parameter is passed as a pair of values - an object and a double. The value passed depends on the type of value available at the call site. If a double is available, the object in java/lang/Void.TYPE is passed as the object value, and if an object value is available, then 0.0 is passed as the double value. The receiving routine always tests the object value before proceeding. If the parameter is being accessed in a 'Number Context' then the code sequence is : if ("parameter_objectValue" == java/lang/Void.TYPE) ...fine..., use the parameter_doubleValue else toNumber(parameter_objectValue) and if the parameter is being referenced in an Object context, the code is if ("parameter_objectValue" == java/lang/Void.TYPE) new Double(parameter_doubleValue) else ...fine..., use the parameter_objectValue If the receiving code never uses the doubleValue, it is converted on entry to a Double instead.*//* We're referencing a node in a Number context (i.e. we'd prefer it was a double value). If the node is a parameter in a directCall function, mark it as being referenced in this context.*/ private void markDCPNumberContext(Node n) { if (inDirectCallFunction && n.getType() == Token.GETVAR) { int varIndex = theFunction.getVarIndex(n); if (theFunction.isParameter(varIndex)) { parameterUsedInNumberContext = true; } } } private boolean convertParameter(Node n) { if (inDirectCallFunction && n.getType() == Token.GETVAR) { int varIndex = theFunction.getVarIndex(n); if (theFunction.isParameter(varIndex)) { n.removeProp(Node.ISNUMBER_PROP); return true; } } return false; } private int rewriteForNumberVariables(Node n) { switch (n.getType()) { case Token.EXPR_VOID : { Node child = n.getFirstChild(); int type = rewriteForNumberVariables(child); if (type == NumberType) n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); return NoType; } case Token.NUMBER : n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); return NumberType; case Token.GETVAR : { int varIndex = theFunction.getVarIndex(n); if (inDirectCallFunction && theFunction.isParameter(varIndex)) { n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); return NumberType; } else if (theFunction.isNumberVar(varIndex)) { n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); return NumberType; } return NoType; } case Token.INC : case Token.DEC : { Node child = n.getFirstChild(); // will be a GETVAR or GETPROP if (child.getType() == Token.GETVAR) { int varIndex = theFunction.getVarIndex(child); if (theFunction.isNumberVar(varIndex)) { n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); markDCPNumberContext(child); return NumberType; } else return NoType; } else return NoType; } case Token.SETVAR : { Node lChild = n.getFirstChild(); Node rChild = lChild.getNext(); int rType = rewriteForNumberVariables(rChild); int varIndex = theFunction.getVarIndex(n); if (inDirectCallFunction && theFunction.isParameter(varIndex)) { if (rType == NumberType) { if (!convertParameter(rChild)) { n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); return NumberType; } markDCPNumberContext(rChild); return NoType; } else return rType; } else if (theFunction.isNumberVar(varIndex)) { if (rType != NumberType) { n.removeChild(rChild); n.addChildToBack( new Node(Token.TO_DOUBLE, rChild)); } n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); markDCPNumberContext(rChild); return NumberType; } else { if (rType == NumberType) { if (!convertParameter(rChild)) { n.removeChild(rChild); n.addChildToBack( new Node(Token.TO_OBJECT, rChild)); } } return NoType; } } case Token.LE : case Token.LT : case Token.GE : case Token.GT : { Node lChild = n.getFirstChild(); Node rChild = lChild.getNext(); int lType = rewriteForNumberVariables(lChild); int rType = rewriteForNumberVariables(rChild); markDCPNumberContext(lChild); markDCPNumberContext(rChild); if (convertParameter(lChild)) { if (convertParameter(rChild)) { return NoType; } else if (rType == NumberType) { n.putIntProp(Node.ISNUMBER_PROP, Node.RIGHT); } } else if (convertParameter(rChild)) { if (lType == NumberType) { n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -