# HG changeset patch # User Christian Humer # Date 1363084704 -3600 # Node ID aad7e9f4f71c040d232e2411fa95b143152c3913 # Parent c4c3f50fa9c259ef0e5de34447db6fb466929191 A few additions to codegen tests. diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BinaryOperationTest.java --- a/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BinaryOperationTest.java Tue Mar 12 11:37:32 2013 +0100 +++ b/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BinaryOperationTest.java Tue Mar 12 11:38:24 2013 +0100 @@ -23,6 +23,7 @@ package com.oracle.truffle.api.codegen.test; import com.oracle.truffle.api.codegen.*; +import com.oracle.truffle.api.codegen.test.TypeSystemTest.ValueNode; public class BinaryOperationTest { diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeString.java --- a/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeString.java Tue Mar 12 11:37:32 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.truffle.api.codegen.test; - -import com.oracle.truffle.api.codegen.*; - -@NodeClass(RuntimeString.BuiltinNode.class) -public class RuntimeString { - - abstract static class BuiltinNode extends ValueNode { - - @Children ValueNode[] parameters; - - BuiltinNode(ValueNode[] parameters) { - this.parameters = adoptChildren(parameters); - } - - BuiltinNode(BuiltinNode prev) { - this(prev.parameters); - } - } - - private final String internal; - - public RuntimeString(String internal) { - this.internal = internal; - } - - @Specialization - RuntimeString substr(int beginIndex, int endIndex) { - return new RuntimeString(internal.substring(beginIndex, endIndex)); - } - - @Generic - static RuntimeString substr(Object s, Object beginIndex, Object endIndex) { - return ((RuntimeString) s).substr(convertInt(beginIndex), convertInt(endIndex)); - } - - @Specialization - static RuntimeString concat(RuntimeString s1, RuntimeString s2) { - return new RuntimeString(s1.internal + s2.internal); - } - - @Generic - static RuntimeString concat(Object s1, Object s2) { - return concat(((RuntimeString) s1), (RuntimeString) s2); - } - - static int convertInt(Object value) { - if (value instanceof Number) { - return ((Number) value).intValue(); - } else if (value instanceof String) { - return Integer.parseInt((String) value); - } - throw new RuntimeException("Invalid datatype"); - } - -} diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeStringTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeStringTest.java Tue Mar 12 11:38:24 2013 +0100 @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api.codegen.test; + +import org.junit.*; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.codegen.*; +import com.oracle.truffle.api.codegen.test.TypeSystemTest.TestRootNode; +import com.oracle.truffle.api.codegen.test.TypeSystemTest.ValueNode; + +public class RuntimeStringTest { + + @Test + public void testSubstr() { + assertExecute(new RuntimeString("es"), "substr", new RuntimeString("test"), 1, 3); + } + + @Test + public void testConcat() { + assertExecute(new RuntimeString("concatconcat"), "concat", new RuntimeString("concat"), new RuntimeString("concat")); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testConcatFail() { + assertExecute(new RuntimeString("concatconcat"), "concat", new RuntimeString("concat")); + } + + @Test + public void testFindMethodByMethodName() { + // TODO + } + + private static void assertExecute(Object expectedResult, String name, Object... argumentsArray) { + ArgNode[] args = new ArgNode[argumentsArray.length]; + for (int i = 0; i < args.length; i++) { + args[i] = new ArgNode(argumentsArray, i); + } + + BuiltinNode node = null; + for (NodeFactory nodeFactory : RuntimeStringTestFactory.getFactories()) { + GeneratedBy generated = nodeFactory.getClass().getAnnotation(GeneratedBy.class); + Assert.assertNotNull(generated); + Assert.assertNotSame("", generated.methodName()); + if (generated.methodName().equals(name)) { + node = nodeFactory.createNode((Object) args); + break; + } + } + Assert.assertNotNull("Node not found", node); + CallTarget target = Truffle.getRuntime().createCallTarget(new TestRootNode(node)); + Assert.assertEquals(expectedResult, target.call()); + } + + static class ArgNode extends ValueNode { + + final Object[] arguments; + final int index; + + ArgNode(Object[] args, int index) { + this.arguments = args; + this.index = index; + } + + @Override + Object execute() { + return arguments[index]; + } + } + + abstract static class BuiltinNode extends ValueNode { + + @Children ArgNode[] parameters; + + BuiltinNode(ArgNode[] parameters) { + this.parameters = adoptChildren(parameters); + } + + BuiltinNode(BuiltinNode prev) { + this(prev.parameters); + } + + } + + @NodeClass(BuiltinNode.class) + static class RuntimeString { + + private final String internal; + + public RuntimeString(String internal) { + this.internal = internal; + } + + @Specialization + static RuntimeString concat(RuntimeString s1, RuntimeString s2) { + return new RuntimeString(s1.internal + s2.internal); + } + + @Specialization + RuntimeString substr(int beginIndex, int endIndex) { + return new RuntimeString(internal.substring(beginIndex, endIndex)); + } + + @Generic + RuntimeString substr(Object beginIndex, Object endIndex) { + return substr(convertInt(beginIndex), convertInt(endIndex)); + } + + static int convertInt(Object value) { + if (value instanceof Number) { + return ((Number) value).intValue(); + } else if (value instanceof String) { + return Integer.parseInt((String) value); + } + throw new RuntimeException("Invalid datatype"); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof RuntimeString) { + return internal.equals(((RuntimeString) obj).internal); + } + return super.equals(obj); + } + + @Override + public int hashCode() { + return internal.hashCode(); + } + + @Override + public String toString() { + return internal; + } + + } + +} diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/SimpleTypes.java --- a/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/SimpleTypes.java Tue Mar 12 11:37:32 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.truffle.api.codegen.test; - -import com.oracle.truffle.api.codegen.*; - -@TypeSystem({int.class, RuntimeString.class}) -class SimpleTypes { -} diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/TypeSystemTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/TypeSystemTest.java Tue Mar 12 11:38:24 2013 +0100 @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api.codegen.test; + +import com.oracle.truffle.api.codegen.*; +import com.oracle.truffle.api.codegen.test.RuntimeStringTest.RuntimeString; +import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.nodes.*; + +public class TypeSystemTest { + + @TypeSystem({int.class, RuntimeString.class}) + static class SimpleTypes { + } + + @TypeSystemReference(SimpleTypes.class) + abstract static class ValueNode extends Node { + + int executeInt() throws UnexpectedResultException { + return SimpleTypesGen.SIMPLETYPES.expectInteger(execute()); + } + + RuntimeString executeString() { + return new RuntimeString(execute().toString()); + } + + @SuppressWarnings("static-method") + final long executeSpecial() { + return 42L; + } + + abstract Object execute(); + + } + + @TypeSystemReference(SimpleTypes.class) + static class TestRootNode extends RootNode { + + @Child private ValueNode node; + + public TestRootNode(ValueNode node) { + this.node = adoptChild(node); + } + + @Override + public Object execute(VirtualFrame frame) { + return node.execute(); + } + } + +} diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/ValueNode.java --- a/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/ValueNode.java Tue Mar 12 11:37:32 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.truffle.api.codegen.test; - -import com.oracle.truffle.api.codegen.*; -import com.oracle.truffle.api.nodes.*; - -@TypeSystemReference(SimpleTypes.class) -abstract class ValueNode extends Node { - - abstract int executeInt() throws UnexpectedResultException; - - abstract RuntimeString executeString() throws UnexpectedResultException; - - abstract Object execute(); - -} diff -r c4c3f50fa9c2 -r aad7e9f4f71c graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/package-info.java --- a/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/package-info.java Tue Mar 12 11:37:32 2013 +0100 +++ b/graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/package-info.java Tue Mar 12 11:38:24 2013 +0100 @@ -40,7 +40,8 @@ * * * *

*