changeset 13883:ff3136ecb5a7

SL: small changes
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 05 Feb 2014 03:16:21 -0800
parents d04be74665fb
children edc9eb74bb7a
files graal/com.oracle.truffle.sl.test/tests/ControlFlow.output graal/com.oracle.truffle.sl.test/tests/ControlFlow.sl graal/com.oracle.truffle.sl.test/tests/Fibonacci.output graal/com.oracle.truffle.sl.test/tests/Fibonacci.sl graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.output graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.sl graal/com.oracle.truffle.sl.test/tests/HelloWorld.output graal/com.oracle.truffle.sl.test/tests/HelloWorld.sl graal/com.oracle.truffle.sl.test/tests/String.output graal/com.oracle.truffle.sl.test/tests/String.sl graal/com.oracle.truffle.sl.test/tests/Sum.output graal/com.oracle.truffle.sl.test/tests/Sum.sl graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLExpressionNode.java graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLAddNode.java graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/demo/SLAddWithoutSpecializationNode.java
diffstat 15 files changed, 126 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/ControlFlow.output	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,1 @@
+1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/ControlFlow.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,10 @@
+function foo() {}
+function bar() {}
+
+function main() {  
+  foo();
+  if (1 < 2) {
+    bar();
+    return 1;
+  }
+}  
--- a/graal/com.oracle.truffle.sl.test/tests/Fibonacci.output	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/Fibonacci.output	Wed Feb 05 03:16:21 2014 -0800
@@ -1,1 +1,10 @@
-267914296
+1: 1
+2: 1
+3: 2
+4: 3
+5: 5
+6: 8
+7: 13
+8: 21
+9: 34
+10: 55
--- a/graal/com.oracle.truffle.sl.test/tests/Fibonacci.sl	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/Fibonacci.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -13,5 +13,9 @@
 }
 
 function main() {  
-  println(fib(42));
+  i = 1;
+  while (i <= 10) {
+    println(i + ": " + fib(i));
+    i = i + 1;
+  }
 }  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.output	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,2 @@
+42
+38
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,16 @@
+function add(a, b) {
+  return a + b;
+}
+
+function sub(a, b) {
+  return a - b;
+}
+
+function foo(f) {
+  println(f(40, 2));
+}
+
+function main() {
+  foo(add);
+  foo(sub);
+}  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/HelloWorld.output	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,1 @@
+Hello World!
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl.test/tests/HelloWorld.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,3 @@
+function main() {  
+  println("Hello World!");  
+}  
--- a/graal/com.oracle.truffle.sl.test/tests/String.output	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/String.output	Wed Feb 05 03:16:21 2014 -0800
@@ -6,3 +6,5 @@
 nulls
 bars
 foos
+2 < 4: true
+Type error at String.sl line 9 col 36: operation "<" not defined for Number 2, String "4"
--- a/graal/com.oracle.truffle.sl.test/tests/String.sl	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/String.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -5,6 +5,10 @@
   return "bar";
 }
 
+function f(a, b) {
+  return a + " < " + b + ": " + (a < b);
+}
+
 function main() {  
   println("s" + null());  
   println("s" + null);  
@@ -15,4 +19,7 @@
   println(null() + "s");  
   println(foo() + "s");  
   println(foo + "s");
+
+  println(f(2, 4));
+  println(f(2, "4"));
 }  
--- a/graal/com.oracle.truffle.sl.test/tests/Sum.output	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/Sum.output	Wed Feb 05 03:16:21 2014 -0800
@@ -1,1 +1,1 @@
-100000000000
+50005000
--- a/graal/com.oracle.truffle.sl.test/tests/Sum.sl	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl.test/tests/Sum.sl	Wed Feb 05 03:16:21 2014 -0800
@@ -1,8 +1,8 @@
 function main() {  
   i = 0;  
   sum = 0;  
-  while (i < 100000) {  
-    sum = sum + 1000000;  
+  while (i <= 10000) {  
+    sum = sum + i;  
     i = i + 1;  
   }  
   return sum;  
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLExpressionNode.java	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLExpressionNode.java	Wed Feb 05 03:16:21 2014 -0800
@@ -58,10 +58,6 @@
      * subclasses overwrite the appropriate methods.
      */
 
-    public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
-        return SLTypesGen.SLTYPES.expectBoolean(executeGeneric(frame));
-    }
-
     public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
         return SLTypesGen.SLTYPES.expectLong(executeGeneric(frame));
     }
@@ -70,6 +66,10 @@
         return SLTypesGen.SLTYPES.expectBigInteger(executeGeneric(frame));
     }
 
+    public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
+        return SLTypesGen.SLTYPES.expectBoolean(executeGeneric(frame));
+    }
+
     public String executeString(VirtualFrame frame) throws UnexpectedResultException {
         return SLTypesGen.SLTYPES.expectString(executeGeneric(frame));
     }
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLAddNode.java	Wed Feb 05 09:32:30 2014 +0100
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLAddNode.java	Wed Feb 05 03:16:21 2014 -0800
@@ -78,16 +78,6 @@
     }
 
     /**
-     * Specialization for String concatenation. This specialization is not strictly necessary, since
-     * {@link #add(Object, Object)} covers this case too. But it leads to slightly better code,
-     * since we do not require the {@link Object#toString()} calls in this specialization.
-     */
-    @Specialization
-    protected String add(String left, String right) {
-        return left + right;
-    }
-
-    /**
      * Specialization for String concatenation. The SL specification says that String concatenation
      * works if either the left or the right operand is a String. The non-string operand is
      * converted then automatically converted to a String.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/demo/SLAddWithoutSpecializationNode.java	Wed Feb 05 03:16:21 2014 -0800
@@ -0,0 +1,62 @@
+package com.oracle.truffle.sl.nodes.expression.demo;
+
+import java.math.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.dsl.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.sl.nodes.*;
+import com.oracle.truffle.sl.nodes.expression.*;
+
+/**
+ * This is an example how the add operation would be implemented without specializations and without
+ * the Truffle DSL. Do not write such code in your language! See {@link SLAddNode} how the add
+ * operation is implemented correctly.
+ */
+public class SLAddWithoutSpecializationNode extends SLExpressionNode {
+
+    @Child private SLExpressionNode leftNode;
+    @Child private SLExpressionNode rightNode;
+
+    public SLAddWithoutSpecializationNode(SLExpressionNode leftNode, SLExpressionNode rightNode) {
+        this.leftNode = adoptChild(leftNode);
+        this.rightNode = adoptChild(rightNode);
+    }
+
+    @Override
+    public Object executeGeneric(VirtualFrame frame) {
+        /* Evaluate the child nodes. */
+        Object left = leftNode.executeGeneric(frame);
+        Object right = rightNode.executeGeneric(frame);
+
+        if (left instanceof Long && right instanceof Long) {
+            /* Fast path of the arbitrary-precision arithmetic. We need to check for overflows */
+            try {
+                return ExactMath.addExact((Long) left, (Long) right);
+            } catch (ArithmeticException ex) {
+                /* Fall through to BigInteger case. */
+            }
+        }
+
+        /* Implicit type conversions. */
+        if (left instanceof Long) {
+            left = BigInteger.valueOf((Long) left);
+        }
+        if (right instanceof Long) {
+            right = BigInteger.valueOf((Long) right);
+        }
+        if (left instanceof BigInteger && right instanceof BigInteger) {
+            /* Slow path of the arbitrary-precision arithmetic. */
+            return ((BigInteger) left).add((BigInteger) right);
+        }
+
+        /* String concatenation if either the left or the right operand is a String. */
+        if (left instanceof String || right instanceof String) {
+            return left.toString() + right.toString();
+        }
+
+        /* Type error. */
+        throw new UnsupportedSpecializationException(this, new Node[]{leftNode, rightNode}, new Object[]{left, right});
+    }
+}