Skip to content

Commit

Permalink
The pass that tries to put locals into java registers as doubles was …
Browse files Browse the repository at this point in the history
…not functioning properly for values called as functions. Test case:

function f(tree, callback) {
 callback();
  f(tree, callback);
  }
  • Loading branch information
norrisboyd committed Aug 31, 2008
1 parent 534f0ff commit ceba1c0
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/org/mozilla/javascript/optimizer/Optimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void optimizeFunction(OptFunctionNode theFunction)
*/
parameterUsedInNumberContext = false;
for (int i = 0; i < theStatementNodes.length; i++) {
rewriteForNumberVariables(theStatementNodes[i]);
rewriteForNumberVariables(theStatementNodes[i], NumberType);
}
theFunction.setParameterNumberContext(parameterUsedInNumberContext);
}
Expand Down Expand Up @@ -145,12 +145,12 @@ private boolean convertParameter(Node n)
return false;
}

private int rewriteForNumberVariables(Node n)
private int rewriteForNumberVariables(Node n, int desired)
{
switch (n.getType()) {
case Token.EXPR_VOID : {
Node child = n.getFirstChild();
int type = rewriteForNumberVariables(child);
int type = rewriteForNumberVariables(child, NumberType);
if (type == NumberType)
n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
return NoType;
Expand All @@ -163,7 +163,8 @@ private int rewriteForNumberVariables(Node n)
{
int varIndex = theFunction.getVarIndex(n);
if (inDirectCallFunction
&& theFunction.isParameter(varIndex))
&& theFunction.isParameter(varIndex)
&& desired == NumberType)
{
n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
return NumberType;
Expand All @@ -180,22 +181,22 @@ else if (theFunction.isNumberVar(varIndex)) {
Node child = n.getFirstChild();
// "child" will be GETVAR or GETPROP or GETELEM
if (child.getType() == Token.GETVAR) {
if (rewriteForNumberVariables(child) == NumberType) {
if (rewriteForNumberVariables(child, NumberType) == NumberType) {
n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH);
markDCPNumberContext(child);
return NumberType;
}
return NoType;
}
else if (child.getType() == Token.GETELEM) {
return rewriteForNumberVariables(child);
return rewriteForNumberVariables(child, NumberType);
}
return NoType;
}
case Token.SETVAR : {
Node lChild = n.getFirstChild();
Node rChild = lChild.getNext();
int rType = rewriteForNumberVariables(rChild);
int rType = rewriteForNumberVariables(rChild, NumberType);
int varIndex = theFunction.getVarIndex(n);
if (inDirectCallFunction
&& theFunction.isParameter(varIndex))
Expand Down Expand Up @@ -238,8 +239,8 @@ else if (theFunction.isNumberVar(varIndex)) {
case Token.GT : {
Node lChild = n.getFirstChild();
Node rChild = lChild.getNext();
int lType = rewriteForNumberVariables(lChild);
int rType = rewriteForNumberVariables(rChild);
int lType = rewriteForNumberVariables(lChild, NumberType);
int rType = rewriteForNumberVariables(rChild, NumberType);
markDCPNumberContext(lChild);
markDCPNumberContext(rChild);

Expand Down Expand Up @@ -277,8 +278,8 @@ else if (convertParameter(rChild)) {
case Token.ADD : {
Node lChild = n.getFirstChild();
Node rChild = lChild.getNext();
int lType = rewriteForNumberVariables(lChild);
int rType = rewriteForNumberVariables(rChild);
int lType = rewriteForNumberVariables(lChild, NumberType);
int rType = rewriteForNumberVariables(rChild, NumberType);


if (convertParameter(lChild)) {
Expand Down Expand Up @@ -329,8 +330,8 @@ else if (convertParameter(rChild)) {
case Token.MOD : {
Node lChild = n.getFirstChild();
Node rChild = lChild.getNext();
int lType = rewriteForNumberVariables(lChild);
int rType = rewriteForNumberVariables(rChild);
int lType = rewriteForNumberVariables(lChild, NumberType);
int rType = rewriteForNumberVariables(rChild, NumberType);
markDCPNumberContext(lChild);
markDCPNumberContext(rChild);
if (lType == NumberType) {
Expand Down Expand Up @@ -379,23 +380,23 @@ else if (convertParameter(rChild)) {
Node arrayBase = n.getFirstChild();
Node arrayIndex = arrayBase.getNext();
Node rValue = arrayIndex.getNext();
int baseType = rewriteForNumberVariables(arrayBase);
int baseType = rewriteForNumberVariables(arrayBase, NumberType);
if (baseType == NumberType) {// can never happen ???
if (!convertParameter(arrayBase)) {
n.removeChild(arrayBase);
n.addChildToFront(
new Node(Token.TO_OBJECT, arrayBase));
}
}
int indexType = rewriteForNumberVariables(arrayIndex);
int indexType = rewriteForNumberVariables(arrayIndex, NumberType);
if (indexType == NumberType) {
// setting the ISNUMBER_PROP signals the codegen
// to use the OptRuntime.setObjectIndex that takes
// a double index
n.putIntProp(Node.ISNUMBER_PROP, Node.LEFT);
markDCPNumberContext(arrayIndex);
}
int rValueType = rewriteForNumberVariables(rValue);
int rValueType = rewriteForNumberVariables(rValue, NumberType);
if (rValueType == NumberType) {
if (!convertParameter(rValue)) {
n.removeChild(rValue);
Expand All @@ -408,15 +409,15 @@ else if (convertParameter(rChild)) {
case Token.GETELEM : {
Node arrayBase = n.getFirstChild();
Node arrayIndex = arrayBase.getNext();
int baseType = rewriteForNumberVariables(arrayBase);
int baseType = rewriteForNumberVariables(arrayBase, NumberType);
if (baseType == NumberType) {// can never happen ???
if (!convertParameter(arrayBase)) {
n.removeChild(arrayBase);
n.addChildToFront(
new Node(Token.TO_OBJECT, arrayBase));
}
}
int indexType = rewriteForNumberVariables(arrayIndex);
int indexType = rewriteForNumberVariables(arrayIndex, NumberType);
if (indexType == NumberType) {
if (!convertParameter(arrayIndex)) {
// setting the ISNUMBER_PROP signals the codegen
Expand All @@ -430,14 +431,8 @@ else if (convertParameter(rChild)) {
case Token.CALL :
{
Node child = n.getFirstChild(); // the function node
if (child.getType() == Token.GETELEM) {
// Optimization of x[0]() is not supported
// so bypass GETELEM optimization that
// rewriteForNumberVariables would trigger
rewriteAsObjectChildren(child, child.getFirstChild());
} else {
rewriteForNumberVariables(child);
}
// must be an object
rewriteAsObjectChildren(child, child.getFirstChild());
child = child.getNext(); // the first arg

OptFunctionNode target
Expand All @@ -448,7 +443,7 @@ else if (convertParameter(rChild)) {
handle moving the pairs of parameters.
*/
while (child != null) {
int type = rewriteForNumberVariables(child);
int type = rewriteForNumberVariables(child, NumberType);
if (type == NumberType) {
markDCPNumberContext(child);
}
Expand All @@ -471,7 +466,7 @@ private void rewriteAsObjectChildren(Node n, Node child)
// Force optimized children to be objects
while (child != null) {
Node nextChild = child.getNext();
int type = rewriteForNumberVariables(child);
int type = rewriteForNumberVariables(child, NoType);
if (type == NumberType) {
if (!convertParameter(child)) {
n.removeChild(child);
Expand Down

0 comments on commit ceba1c0

Please sign in to comment.