Reviewers: acleung,

Description:
Adds <, <=, >, >=, ==, and != to JsStaticEval for numeric values.


Please review this at http://gwt-code-reviews.appspot.com/1593803/

Affected files:
  M dev/core/src/com/google/gwt/dev/js/JsStaticEval.java
  M dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java


Index: dev/core/src/com/google/gwt/dev/js/JsStaticEval.java
===================================================================
--- dev/core/src/com/google/gwt/dev/js/JsStaticEval.java        (revision 10731)
+++ dev/core/src/com/google/gwt/dev/js/JsStaticEval.java        (working copy)
@@ -15,6 +15,7 @@
  */
 package com.google.gwt.dev.js;

+import com.google.gwt.dev.jjs.InternalCompilerException;
 import com.google.gwt.dev.jjs.SourceInfo;
 import com.google.gwt.dev.jjs.impl.OptimizerStats;
 import com.google.gwt.dev.js.ast.CanBooleanEval;
@@ -182,6 +183,17 @@
         trySimplifyNe(x, arg1, arg2, ctx);
       } else if (op == JsBinaryOperator.ADD) {
         trySimplifyAdd(x, arg1, arg2, ctx);
+      } else  {
+       switch (op) {
+         case GT:
+         case GTE:
+         case LT:
+         case LTE:
+           trySimplifyCompare(x, arg1, arg2, op, ctx);
+           break;
+         default:
+           break;
+       }
       }
     }

@@ -563,6 +575,37 @@
       return num;
     }

+ private JsExpression simplifyCompare(JsExpression original, JsExpression arg1,
+        JsExpression arg2, JsBinaryOperator op) {
+      assert (original != null);
+
+      // TODO(cromwellian) handle all types
+ if (arg1 instanceof JsNumberLiteral && arg2 instanceof JsNumberLiteral) {
+          double num1 = ((JsNumberLiteral) arg1).getValue();
+          double num2 = ((JsNumberLiteral) arg2).getValue();
+          boolean result = false;
+          switch(op) {
+            case LT:
+              result = num1 < num2;
+              break;
+            case LTE:
+              result = num1 <= num2;
+              break;
+            case GT:
+              result = num1 > num2;
+              break;
+            case GTE:
+              result = num1 >= num2;
+              break;
+            default:
+ throw new InternalCompilerException("Can't handle simplify of op " + op);
+          }
+        return JsBooleanLiteral.get(result);
+      }
+      // no simplification made
+      return original;
+    }
+
private JsExpression simplifyEq(JsExpression original, JsExpression arg1,
         JsExpression arg2) {
       assert (original != null);
@@ -575,6 +618,10 @@
         return simplifyNullEq(original, arg1);
       }

+ if (arg1 instanceof JsNumberLiteral && arg2 instanceof JsNumberLiteral) {
+          return JsBooleanLiteral.get(((JsNumberLiteral) arg1).getValue()
+           == ((JsNumberLiteral) arg2).getValue());
+      }
       // no simplification made
       return original;
     }
@@ -591,6 +638,10 @@
         return simplifyNullNe(original, arg1);
       }

+ if (arg1 instanceof JsNumberLiteral && arg2 instanceof JsNumberLiteral) {
+        return JsBooleanLiteral.get(((JsNumberLiteral) arg1).getValue()
+            != ((JsNumberLiteral) arg2).getValue());
+      }
       // no simplification made
       return original;
     }
@@ -749,6 +800,14 @@
         }
       }
       return toReturn;
+    }
+
+ private void trySimplifyCompare(JsExpression original, JsExpression arg1,
+        JsExpression arg2, JsBinaryOperator op, JsContext ctx) {
+      JsExpression updated = simplifyCompare(original, arg1, arg2, op);
+      if (updated != original) {
+        ctx.replaceMe(updated);
+      }
     }

     private void trySimplifyEq(JsExpression original, JsExpression arg1,
Index: dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java
===================================================================
--- dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java (revision 10731)
+++ dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java   (working copy)
@@ -124,6 +124,21 @@
         optimize("if (a()) { b() } else { throw 1; }"));
   }

+  public void testLiteralCompares() throws Exception {
+    assertEquals("alert(false);", optimize("alert(2 != 2)"));
+    assertEquals("alert(false);", optimize("alert(2 == 3)"));
+    assertEquals("alert(true);", optimize("alert(2 == 2)"));
+    assertEquals("alert(true);", optimize("alert(2 != 3)"));
+    assertEquals("alert(true);", optimize("alert(2 < 3)"));
+    assertEquals("alert(true);", optimize("alert(3 <= 3)"));
+    assertEquals("alert(true);", optimize("alert(3 > 2)"));
+    assertEquals("alert(true);", optimize("alert(3 >= 3)"));
+    assertEquals("alert(false);", optimize("alert(2 > 3)"));
+    assertEquals("alert(false);", optimize("alert(2 >= 3)"));
+    assertEquals("alert(false);", optimize("alert(3 < 2)"));
+    assertEquals("alert(false);", optimize("alert(3 <= 2)"));
+  }
+
   public void testLiteralEqNull() throws Exception {
     assertEquals("alert(false);", optimize("alert('test' == null)"));
   }


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to