# HG changeset patch # User Christian Humer # Date 1362153914 -3600 # Node ID 0b48dc5f37c3fd2b9c1d728dc61deb6e1e9fe890 # Parent 6b74ffe38183423a940a0ed6b53c880754d2a60a Added truffle.api.codegen.test project with a BultinFunctionTest. diff -r 6b74ffe38183 -r 0b48dc5f37c3 graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BuiltinFunctionTest.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/BuiltinFunctionTest.java Fri Mar 01 17:05:14 2013 +0100 @@ -0,0 +1,132 @@ +/* + * 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.*; + +/** + * Generated code at {@link BuiltinFunctionTestFactory}. + */ +public class BuiltinFunctionTest { + + @TypeSystemReference(SimpleTypes.class) + abstract static class ValueNode extends Node { + + abstract int executeInt() throws UnexpectedResultException; + + abstract String executeString() throws UnexpectedResultException; + + abstract Object execute(); + + } + + abstract static class FunctionNode extends ValueNode { + + @Children ValueNode[] parameters; + + FunctionNode(ValueNode[] parameters) { + this.parameters = adoptChildren(parameters); + } + + FunctionNode(FunctionNode prev) { + this(prev.parameters); + } + } + + 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"); + } + + abstract static class MathAbsNode extends FunctionNode { + + MathAbsNode(ValueNode[] children) { + super(children); + } + + MathAbsNode(MathAbsNode prev) { + super(prev); + } + + @Specialization + int doInt(int value1) { + return Math.abs(value1); + } + + @Generic + int doGeneric(Object value0) { + return doInt(convertInt(value0)); + } + } + + abstract static class StringThisNode extends ValueNode { + + @Override + final String executeString() { + return (String) execute(); + } + + } + + @ExecuteChildren({"thisNode", "parameters"}) + abstract static class InstanceFunctionNode extends FunctionNode { + + @Child StringThisNode thisNode; + + InstanceFunctionNode(StringThisNode thisNode, ValueNode[] parameters) { + super(parameters); + this.thisNode = thisNode; + } + + InstanceFunctionNode(InstanceFunctionNode prev) { + this(prev.thisNode, prev.parameters); + } + } + + abstract static class StringSubstrNode extends InstanceFunctionNode { + + StringSubstrNode(StringThisNode thisNode, ValueNode[] parameters) { + super(thisNode, parameters); + } + + StringSubstrNode(StringSubstrNode prev) { + super(prev); + } + + @Specialization + String doInt(String thisValue, int beginIndex, int endIndex) { + return thisValue.substring(beginIndex, endIndex); + } + + @Generic + String doGeneric(String thisValue, Object beginIndex, Object endIndex) { + return thisValue.substring(convertInt(beginIndex), convertInt(endIndex)); + } + } + +} diff -r 6b74ffe38183 -r 0b48dc5f37c3 graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/SimpleTypes.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/SimpleTypes.java Fri Mar 01 17:05:14 2013 +0100 @@ -0,0 +1,29 @@ +/* + * 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, String.class}) +class SimpleTypes { +} diff -r 6b74ffe38183 -r 0b48dc5f37c3 graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/package-info.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/package-info.java Fri Mar 01 17:05:14 2013 +0100 @@ -0,0 +1,46 @@ +/* + * 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. + */ +/** + *

This package contains basic tests of the Truffle-Source-Code-Generation (short Codegen) API and serves at the same + * time as an introduction to the Codegen API for language implementors. Every test gives an example on how to use the construct explained in the class description.

+ * + *

+ * This API relies heavily on the concepts described in {@link com.oracle.truffle.api.test}. We assume that the + * reader is already familiarized with those concepts. + *

+ * + *

+ * TODO general description + *

+ * + *

+ * This introduction to Codegen contains items in the following recommended order: + * + *

+ *

+ * + */ +package com.oracle.truffle.api.codegen.test; + diff -r 6b74ffe38183 -r 0b48dc5f37c3 mx/projects --- a/mx/projects Fri Mar 01 17:03:57 2013 +0100 +++ b/mx/projects Fri Mar 01 17:05:14 2013 +0100 @@ -360,6 +360,14 @@ project@com.oracle.truffle.api.codegen@checkstyle=com.oracle.graal.graph project@com.oracle.truffle.api.codegen@javaCompliance=1.7 +# truffle.api.codegen.test +project@com.oracle.truffle.api.codegen.test@subDir=graal +project@com.oracle.truffle.api.codegen.test@sourceDirs=src +project@com.oracle.truffle.api.codegen.test@dependencies=com.oracle.truffle.api.codegen,JUNIT,com.oracle.truffle.api.test +project@com.oracle.truffle.api.codegen.test@checkstyle=com.oracle.graal.graph +project@com.oracle.truffle.api.codegen.test@javaCompliance=1.7 +project@com.oracle.truffle.api.codegen.test@annotationProcessors=com.oracle.truffle.codegen.processor + # truffle.codegen.processor project@com.oracle.truffle.codegen.processor@subDir=graal project@com.oracle.truffle.codegen.processor@sourceDirs=src