changeset 22464:453c458c711e

Allow NodePlugin to intercept allocation bytecodes
author Christian Wimmer <christian.wimmer@oracle.com>
date Fri, 14 Aug 2015 17:29:40 -0700
parents 457474423f92
children bc0f6b7a2b0e
files graal/com.oracle.graal.graphbuilderconf/src/com/oracle/graal/graphbuilderconf/NodePlugin.java graal/com.oracle.graal.java/src/com/oracle/graal/java/BytecodeParser.java
diffstat 2 files changed, 71 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graphbuilderconf/src/com/oracle/graal/graphbuilderconf/NodePlugin.java	Fri Aug 14 17:28:00 2015 -0700
+++ b/graal/com.oracle.graal.graphbuilderconf/src/com/oracle/graal/graphbuilderconf/NodePlugin.java	Fri Aug 14 17:29:40 2015 -0700
@@ -155,6 +155,47 @@
     }
 
     /**
+     * Handle the parsing of a NEW bytecode. If the method returns true, it must
+     * {@link GraphBuilderContext#push push} a value with the result of the allocation using
+     * {@link Kind#Object}.
+     *
+     * @param b the context
+     * @param type the type to be instantiated
+     * @return true if the plugin handles the bytecode, false otherwise
+     */
+    default boolean handleNewInstance(GraphBuilderContext b, ResolvedJavaType type) {
+        return false;
+    }
+
+    /**
+     * Handle the parsing of a NEWARRAY and ANEWARRAY bytecode. If the method returns true, it must
+     * {@link GraphBuilderContext#push push} a value with the result of the allocation using
+     * {@link Kind#Object}.
+     *
+     * @param b the context
+     * @param elementType the element type of the array to be instantiated
+     * @param length the length of the new array
+     * @return true if the plugin handles the bytecode, false otherwise
+     */
+    default boolean handleNewArray(GraphBuilderContext b, ResolvedJavaType elementType, ValueNode length) {
+        return false;
+    }
+
+    /**
+     * Handle the parsing of a MULTIANEWARRAY bytecode. If the method returns true, it must
+     * {@link GraphBuilderContext#push push} a value with the result of the allocation using
+     * {@link Kind#Object}.
+     *
+     * @param b the context
+     * @param type the type of the outermost array to be instantiated
+     * @param dimensions the array of lengths for all the dimensions to be instantiated
+     * @return true if the plugin handles the bytecode, false otherwise
+     */
+    default boolean handleNewMultiArray(GraphBuilderContext b, ResolvedJavaType type, ValueNode[] dimensions) {
+        return false;
+    }
+
+    /**
      * If the plugin {@link GraphBuilderContext#push pushes} a value with a different {@link Kind}
      * than specified by the bytecode, it must override this method and return {@code true}. This
      * disables assertion checking for value kinds.
--- a/graal/com.oracle.graal.java/src/com/oracle/graal/java/BytecodeParser.java	Fri Aug 14 17:28:00 2015 -0700
+++ b/graal/com.oracle.graal.java/src/com/oracle/graal/java/BytecodeParser.java	Fri Aug 14 17:29:40 2015 -0700
@@ -863,7 +863,7 @@
      * @param type the type being instantiated
      * @param dims the dimensions for the multi-array
      */
-    protected void handleUnresolvedNewMultiArray(JavaType type, List<ValueNode> dims) {
+    protected void handleUnresolvedNewMultiArray(JavaType type, ValueNode[] dims) {
         assert !graphBuilderConfig.eagerResolving();
         append(new DeoptimizeNode(InvalidateRecompile, Unresolved));
     }
@@ -1091,8 +1091,8 @@
         return new NewArrayNode(elementType, length, fillContents);
     }
 
-    protected NewMultiArrayNode createNewMultiArray(ResolvedJavaType type, List<ValueNode> dimensions) {
-        return new NewMultiArrayNode(type, dimensions.toArray(new ValueNode[0]));
+    protected NewMultiArrayNode createNewMultiArray(ResolvedJavaType type, ValueNode[] dimensions) {
+        return new NewMultiArrayNode(type, dimensions);
     }
 
     protected ValueNode genLoadField(ValueNode receiver, ResolvedJavaField field) {
@@ -3062,6 +3062,12 @@
             }
         }
 
+        for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
+            if (plugin.handleNewInstance(this, resolvedType)) {
+                return;
+            }
+        }
+
         frameState.push(Kind.Object, append(createNewInstance(resolvedType, true)));
     }
 
@@ -3098,6 +3104,13 @@
     private void genNewPrimitiveArray(int typeCode) {
         ResolvedJavaType elementType = metaAccess.lookupJavaType(arrayTypeCodeToClass(typeCode));
         ValueNode length = frameState.pop(Kind.Int);
+
+        for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
+            if (plugin.handleNewArray(this, elementType, length)) {
+                return;
+            }
+        }
+
         frameState.push(Kind.Object, append(createNewArray(elementType, length, true)));
     }
 
@@ -3111,15 +3124,21 @@
         }
         ResolvedJavaType resolvedType = (ResolvedJavaType) type;
 
+        for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
+            if (plugin.handleNewArray(this, resolvedType, length)) {
+                return;
+            }
+        }
+
         frameState.push(Kind.Object, append(createNewArray(resolvedType, length, true)));
     }
 
     private void genNewMultiArray(int cpi) {
         JavaType type = lookupType(cpi, MULTIANEWARRAY);
         int rank = getStream().readUByte(bci() + 3);
-        List<ValueNode> dims = new ArrayList<>(Collections.nCopies(rank, null));
+        ValueNode[] dims = new ValueNode[rank];
         for (int i = rank - 1; i >= 0; i--) {
-            dims.set(i, frameState.pop(Kind.Int));
+            dims[i] = frameState.pop(Kind.Int);
         }
 
         if (!(type instanceof ResolvedJavaType)) {
@@ -3128,6 +3147,12 @@
         }
         ResolvedJavaType resolvedType = (ResolvedJavaType) type;
 
+        for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
+            if (plugin.handleNewMultiArray(this, resolvedType, dims)) {
+                return;
+            }
+        }
+
         frameState.push(Kind.Object, append(createNewMultiArray(resolvedType, dims)));
     }