changeset 11322:760e838b3560

Add parameter to CompilerDirectives.unsafeCast.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Fri, 16 Aug 2013 14:23:16 +0200
parents dc14bcf752ea
children e9a22338d2ce
files graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/UnsafeCastNode.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/TypeCastMacroNode.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/TypeCastNode.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java
diffstat 4 files changed, 79 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/UnsafeCastNode.java	Fri Aug 16 12:09:36 2013 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/UnsafeCastNode.java	Fri Aug 16 14:23:16 2013 +0200
@@ -32,21 +32,12 @@
  */
 public class UnsafeCastNode extends PiNode implements Canonicalizable, LIRLowerable {
 
-    private final Object customType;
-
     public UnsafeCastNode(ValueNode object, Stamp stamp) {
         super(object, stamp);
-        customType = null;
     }
 
     public UnsafeCastNode(ValueNode object, Stamp stamp, GuardingNode anchor) {
         super(object, stamp, anchor);
-        customType = null;
-    }
-
-    public UnsafeCastNode(ValueNode object, Stamp stamp, GuardingNode anchor, Object customType) {
-        super(object, stamp, anchor);
-        this.customType = customType;
     }
 
     public UnsafeCastNode(ValueNode object, Stamp stamp, ValueNode anchor) {
@@ -57,10 +48,6 @@
         this(object, toType.getKind() == Kind.Object ? StampFactory.object(toType, exactType, nonNull || ObjectStamp.isObjectNonNull(object.stamp())) : StampFactory.forKind(toType.getKind()));
     }
 
-    public Object getCustomType() {
-        return customType;
-    }
-
     @Override
     public boolean inferStamp() {
         if (stamp() == StampFactory.forNodeIntrinsic()) {
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/TypeCastMacroNode.java	Fri Aug 16 12:09:36 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/TypeCastMacroNode.java	Fri Aug 16 14:23:16 2013 +0200
@@ -24,22 +24,21 @@
 
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.nodes.*;
-import com.oracle.graal.nodes.extended.*;
 import com.oracle.graal.nodes.spi.*;
-import com.oracle.graal.nodes.type.*;
 import com.oracle.graal.truffle.nodes.asserts.*;
 import com.oracle.truffle.api.*;
 
 /**
  * Macro node for method {@link CompilerDirectives#unsafeCast(Object, Class)} and
- * {@link CompilerDirectives#unsafeCast(Object, Class, Object)}.
+ * {@link CompilerDirectives#unsafeCast(Object, Class, Object, Object)}.
  */
 public class TypeCastMacroNode extends NeverPartOfCompilationNode implements Canonicalizable {
 
-    private static final int ARGUMENT_COUNT = 3;
+    private static final int ARGUMENT_COUNT = 4;
     private static final int OBJECT_ARGUMENT_INDEX = 0;
     private static final int CLASS_ARGUMENT_INDEX = 1;
-    private static final int CUSTOM_TYPE_ARGUMENT_INDEX = 2;
+    private static final int RECEIVER_ARGUMENT_INDEX = 2;
+    private static final int CUSTOM_TYPE_ARGUMENT_INDEX = 3;
 
     public TypeCastMacroNode(Invoke invoke) {
         super(invoke, "The class of the unsafe cast could not be reduced to a compile time constant.");
@@ -53,13 +52,10 @@
         if (classArgument.isConstant() && customTypeArgument.isConstant()) {
             Class c = (Class) classArgument.asConstant().asObject();
             ResolvedJavaType lookupJavaType = tool.runtime().lookupJavaType(c);
-            Stamp s = StampFactory.declaredNonNull(lookupJavaType);
-            ValueAnchorNode valueAnchorNode = graph().add(new ValueAnchorNode());
             ValueNode objectArgument = arguments.get(OBJECT_ARGUMENT_INDEX);
+            ValueNode receiverArgument = arguments.get(RECEIVER_ARGUMENT_INDEX);
             Object customType = customTypeArgument.asConstant().asObject();
-            UnsafeCastNode unsafeCast = graph().unique(new UnsafeCastNode(objectArgument, s, valueAnchorNode, customType));
-            this.replaceAtUsages(unsafeCast);
-            return valueAnchorNode;
+            return graph().add(new TypeCastNode(objectArgument, lookupJavaType, receiverArgument, customType));
         }
         return this;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/TypeCastNode.java	Fri Aug 16 14:23:16 2013 +0200
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2012, 2013, 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.graal.truffle.nodes.typesystem;
+
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.extended.*;
+import com.oracle.graal.nodes.spi.*;
+import com.oracle.graal.nodes.type.*;
+
+public final class TypeCastNode extends FixedWithNextNode implements Lowerable, com.oracle.graal.graph.Node.IterableNodeType {
+
+    @Input private ValueNode receiver;
+    @Input private ValueNode object;
+    private final Object customType;
+    private final ResolvedJavaType castTarget;
+
+    public TypeCastNode(ValueNode object, ResolvedJavaType castTarget, ValueNode receiver, Object customType) {
+        super(StampFactory.declaredNonNull(castTarget));
+        this.receiver = receiver;
+        this.object = object;
+        this.customType = customType;
+        this.castTarget = castTarget;
+    }
+
+    public ValueNode getObject() {
+        return object;
+    }
+
+    public ValueNode getReceiver() {
+        return receiver;
+    }
+
+    public ResolvedJavaType getCastTarget() {
+        return castTarget;
+    }
+
+    public Object getCustomType() {
+        return customType;
+    }
+
+    public void lower(LoweringTool tool, LoweringType loweringType) {
+        if (loweringType == LoweringType.BEFORE_GUARDS) {
+            ValueAnchorNode valueAnchorNode = graph().add(new ValueAnchorNode());
+            UnsafeCastNode unsafeCast = graph().unique(new UnsafeCastNode(object, this.stamp(), (GuardingNode) valueAnchorNode));
+            this.replaceAtUsages(unsafeCast);
+            graph().replaceFixedWithFixed(this, valueAnchorNode);
+        }
+    }
+}
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java	Fri Aug 16 12:09:36 2013 +0200
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java	Fri Aug 16 14:23:16 2013 +0200
@@ -147,7 +147,7 @@
      */
     @Unsafe
     public static <T> T unsafeCast(Object value, Class<T> clazz) {
-        return unsafeCast(value, clazz, null);
+        return unsafeCast(value, clazz, null, null);
     }
 
     /**
@@ -168,12 +168,13 @@
      * 
      * @param value the value that is known to have the specified type
      * @param clazz the specified type of the value
+     * @param receiver the receiver on which the custom type is tested
      * @param customType the custom type that if present on a value makes this unsafe cast safe
      * @return the value
      */
     @SuppressWarnings("unchecked")
     @Unsafe
-    public static <T> T unsafeCast(Object value, Class<T> clazz, Object customType) {
+    public static <T> T unsafeCast(Object value, Class<T> clazz, Object receiver, Object customType) {
         return (T) value;
     }