# HG changeset patch # User Christian Wimmer # Date 1391598981 28800 # Node ID ff3136ecb5a710efe31ce44b4e2a96e67437dab9 # Parent d04be74665fb24531b853c8e3067029019c2b4f4 SL: small changes diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/ControlFlow.output --- /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 diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/ControlFlow.sl --- /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; + } +} diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/Fibonacci.output --- 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 diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/Fibonacci.sl --- 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; + } } diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.output --- /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 diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/FunctionLiteral.sl --- /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); +} diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/HelloWorld.output --- /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! diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/HelloWorld.sl --- /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!"); +} diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/String.output --- 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" diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/String.sl --- 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")); } diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/Sum.output --- 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 diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl.test/tests/Sum.sl --- 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; diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLExpressionNode.java --- 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)); } diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLAddNode.java --- 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. diff -r d04be74665fb -r ff3136ecb5a7 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/demo/SLAddWithoutSpecializationNode.java --- /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}); + } +}