# HG changeset patch
# User Christian Humer
+ * Provides a way to define a custom type check for a defined type. The name of the annotated method must fit to the
+ * pattern is${typeName} (eg. isInteger), where ${typeName} must be a valid type defined in the parent
+ * {@link TypeSystem}. The annotated method must have exactly one argument where the type of the argument is the generic
+ * type {@link Object} or a more specific one from the {@link TypeSystem}. You can define multiple overloaded
+ * {@link TypeCheck} methods for the same type. This can be used to reduce the boxing overhead in type conversions.
+ *
+ * By default the system generates type checks for all types in the parent {@link TypeSystem} which look like the
+ * follows:
+ *
+ *
+ * @TypeCheck
+ * boolean is${typeName}(Object value) {
+ * return value instanceof ${typeName};
+ * }
+ *
+ *
+ *
+ * A type check for BigInteger with one overloaded optimized variant to reduce boxing. + *
+ * + *+ * + * + * @TypeSystem(types = {int.class, BigInteger.class, String.class}, nodeBaseClass = TypedNode.class) + * public abstract class Types { + * + * @TypeCheck + * public boolean isBigInteger(Object value) { + * return value instanceof Integer || value instanceof BigInteger; + * } + * + * @TypeCheck + * public boolean isBigInteger(int value) { + * return true; + * } + * + * } + *+ * + * + */ +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.METHOD}) +public @interface TypeCheck { + +} diff -r a81db08fe930 -r a748e4d44694 graal/com.oracle.truffle.api.codegen/src/com/oracle/truffle/api/codegen/TypeSystem.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.codegen/src/com/oracle/truffle/api/codegen/TypeSystem.java Fri Dec 21 10:44:31 2012 -0800 @@ -0,0 +1,90 @@ +/* + * 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; + +import java.lang.annotation.*; + +import com.oracle.truffle.api.nodes.*; + +/** + *
+ * Annotates a type system class that represents type information for a node. Generates code for converting and managing + * types. Methods contained in the type system may be annotated with {@link TypeCast}, {@link TypeCheck} or + * {@link GuardCheck}. These methods alter the default behavior of the type system. + *
+ * + * + * Example: + *
+ * Shows a @TypeSystem
definition with three types. In this example BigIntegers can be also treated as
+ * integers if their bit width is less than 32.
+ *
+ * + * @TypeSystem(types = {int.class, BigInteger.class, String.class}, nodeBaseClass = TypedNode.class) + * public abstract class Types { + * + * @TypeCheck + * public boolean isInteger(Object value) { + * return value instanceof Integer || (value instanceof BigInteger && ((BigInteger) value).bitLength() < Integer.SIZE); + * } + * + * @TypeCast + * public int asInteger(Object value) { + * if (value instanceof Integer) { + * return (int) value; + * } else { + * return ((BigInteger) value).intValue(); + * } + * } + * } + *+ * + * @see TypeCast + * @see TypeCheck + * @see GuardCheck + */ +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.TYPE}) +public @interface TypeSystem { + + /** + * Sets the types contained by this type system. The order of types also determines the order of specialization. + */ + Class[] types(); + + /** + * Specifies whether the node base class has an executeVoid method or not. Operations derived from this type system + * will automatically implement executeVoid for all generated nodes. Defaults to false. + */ + boolean hasVoid() default false; + + /** + * Specifies the node base class used for generated code that uses this type system. The node base class must extend + * {@link Node} and provide a public non-final method
${type} execute${typeName}(VirtualFrame)
for all
+ * types specified in the types attribute.
+ */
+ Class< ? extends Node> nodeBaseClass();
+
+}
diff -r a81db08fe930 -r a748e4d44694 graal/com.oracle.truffle.codegen.processor/src/META-INF/services/javax.annotation.processing.Processor
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.codegen.processor/src/META-INF/services/javax.annotation.processing.Processor Fri Dec 21 10:44:31 2012 -0800
@@ -0,0 +1,1 @@
+com.oracle.truffle.codegen.processor.TruffleProcessor
\ No newline at end of file
diff -r a81db08fe930 -r a748e4d44694 graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/AbstractParser.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/AbstractParser.java Fri Dec 21 10:44:31 2012 -0800
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 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.codegen.processor;
+
+import java.lang.annotation.*;
+
+import javax.annotation.processing.*;
+import javax.lang.model.element.*;
+
+import com.oracle.truffle.codegen.processor.template.*;
+
+/**
+ * THIS IS NOT PUBLIC API.
+ */
+public abstract class AbstractParser void accept(CodeElementScanner, P> s, P p) {
+ s.visitImport(this, p);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((importString == null) ? 0 : importString.hashCode());
+ result = prime * result + (staticImport ? 1231 : 1237);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ CodeImport other = (CodeImport) obj;
+ if (importString == null) {
+ if (other.importString != null) {
+ return false;
+ }
+ } else if (!importString.equals(other.importString)) {
+ return false;
+ }
+ if (staticImport != other.staticImport) {
+ return false;
+ }
+ return true;
+ }
+
+}
diff -r a81db08fe930 -r a748e4d44694 graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ast/CodeNames.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ast/CodeNames.java Fri Dec 21 10:44:31 2012 -0800
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 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.codegen.processor.ast;
+
+import java.util.*;
+
+import javax.lang.model.element.*;
+
+public abstract class CodeNames {
+
+ private static Map void acceptCodeElementScanner(CodeElementScanner, P> s, P p) {
+ s.visitTree(this, p);
+ }
+
+ @Override
+ public TypeMirror asType() {
+ return type;
+ }
+
+ @Override
+ public ElementKind getKind() {
+ return ElementKind.OTHER;
+ }
+
+ @Override
+ public Name getSimpleName() {
+ return CodeNames.of(getString());
+ }
+
+ @Override
+ public