1 /*
   2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.nodes.graphbuilderconf;
  26 
  27 import org.graalvm.compiler.nodes.ValueNode;
  28 import org.graalvm.compiler.nodes.extended.GuardingNode;
  29 
  30 import jdk.vm.ci.meta.JavaKind;
  31 import jdk.vm.ci.meta.JavaTypeProfile;
  32 import jdk.vm.ci.meta.ResolvedJavaField;
  33 import jdk.vm.ci.meta.ResolvedJavaMethod;
  34 import jdk.vm.ci.meta.ResolvedJavaType;
  35 import jdk.vm.ci.meta.Signature;
  36 
  37 public interface NodePlugin extends GraphBuilderPlugin {
  38     /**
  39      * Handle the parsing of a method invocation bytecode to a method that can be bound statically.
  40      * If the method returns true, it must {@link GraphBuilderContext#push push} a value as the
  41      * result of the method invocation using the {@link Signature#getReturnKind return kind} of the
  42      * method.
  43      *
  44      * @param b the context
  45      * @param method the statically bound, invoked method
  46      * @param args the arguments of the method invocation
  47      * @return true if the plugin handles the invocation, false otherwise
  48      */
  49     default boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  50         return false;
  51     }
  52 
  53     /**
  54      * Handle the parsing of a GETFIELD bytecode. If the method returns true, it must
  55      * {@link GraphBuilderContext#push push} a value using the
  56      * {@link ResolvedJavaField#getJavaKind() kind} of the field.
  57      *
  58      * @param b the context
  59      * @param object the receiver object for the field access
  60      * @param field the accessed field
  61      * @return true if the plugin handles the field access, false otherwise
  62      */
  63     default boolean handleLoadField(GraphBuilderContext b, ValueNode object, ResolvedJavaField field) {
  64         return false;
  65     }
  66 
  67     /**
  68      * Handle the parsing of a GETSTATIC bytecode. If the method returns true, it must
  69      * {@link GraphBuilderContext#push push} a value using the
  70      * {@link ResolvedJavaField#getJavaKind() kind} of the field.
  71      *
  72      * @param b the context
  73      * @param field the accessed field
  74      * @return true if the plugin handles the field access, false otherwise
  75      */
  76     default boolean handleLoadStaticField(GraphBuilderContext b, ResolvedJavaField field) {
  77         return false;
  78     }
  79 
  80     /**
  81      * Handle the parsing of a PUTFIELD bytecode.
  82      *
  83      * @param b the context
  84      * @param object the receiver object for the field access
  85      * @param field the accessed field
  86      * @param value the value to be stored into the field
  87      * @return true if the plugin handles the field access, false otherwise
  88      */
  89     default boolean handleStoreField(GraphBuilderContext b, ValueNode object, ResolvedJavaField field, ValueNode value) {
  90         return false;
  91     }
  92 
  93     /**
  94      * Handle the parsing of a PUTSTATIC bytecode.
  95      *
  96      * @param b the context
  97      * @param field the accessed field
  98      * @param value the value to be stored into the field
  99      * @return true if the plugin handles the field access, false otherwise.
 100      */
 101     default boolean handleStoreStaticField(GraphBuilderContext b, ResolvedJavaField field, ValueNode value) {
 102         return false;
 103     }
 104 
 105     /**
 106      * Handle the parsing of an array load bytecode. If the method returns true, it must
 107      * {@link GraphBuilderContext#push push} a value using the provided elementKind.
 108      *
 109      * @param b the context
 110      * @param array the accessed array
 111      * @param index the index for the array access
 112      * @param boundsCheck the explicit bounds check already emitted, or null if no bounds check was
 113      *            emitted yet
 114      * @param elementKind the element kind of the accessed array
 115      * @return true if the plugin handles the array access, false otherwise.
 116      */
 117     default boolean handleLoadIndexed(GraphBuilderContext b, ValueNode array, ValueNode index, GuardingNode boundsCheck, JavaKind elementKind) {
 118         return false;
 119     }
 120 
 121     /**
 122      * Handle the parsing of an array store bytecode.
 123      *
 124      * @param b the context
 125      * @param array the accessed array
 126      * @param index the index for the array access
 127      * @param boundsCheck the explicit array bounds check already emitted, or null if no array
 128      *            bounds check was emitted yet
 129      * @param storeCheck the explicit array store check already emitted, or null if no array store
 130      *            check was emitted yet
 131      * @param elementKind the element kind of the accessed array
 132      * @param value the value to be stored into the array
 133      * @return true if the plugin handles the array access, false otherwise.
 134      */
 135     default boolean handleStoreIndexed(GraphBuilderContext b, ValueNode array, ValueNode index, GuardingNode boundsCheck, GuardingNode storeCheck, JavaKind elementKind, ValueNode value) {
 136         return false;
 137     }
 138 
 139     /**
 140      * Handle the parsing of a CHECKCAST bytecode. If the method returns true, it must
 141      * {@link GraphBuilderContext#push push} a value with the result of the cast using
 142      * {@link JavaKind#Object}.
 143      *
 144      * @param b the context
 145      * @param object the object to be type checked
 146      * @param type the type that the object is checked against
 147      * @param profile the profiling information for the type check, or null if no profiling
 148      *            information is available
 149      * @return true if the plugin handles the cast, false otherwise
 150      */
 151     default boolean handleCheckCast(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) {
 152         return false;
 153     }
 154 
 155     /**
 156      * Handle the parsing of a INSTANCEOF bytecode. If the method returns true, it must
 157      * {@link GraphBuilderContext#push push} a value with the result of the instanceof using
 158      * {@link JavaKind#Int}.
 159      *
 160      * @param b the context
 161      * @param object the object to be type checked
 162      * @param type the type that the object is checked against
 163      * @param profile the profiling information for the type check, or null if no profiling
 164      *            information is available
 165      * @return true if the plugin handles the instanceof, false otherwise
 166      */
 167     default boolean handleInstanceOf(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) {
 168         return false;
 169     }
 170 
 171     /**
 172      * Handle the parsing of a NEW bytecode. If the method returns true, it must
 173      * {@link GraphBuilderContext#push push} a value with the result of the allocation using
 174      * {@link JavaKind#Object}.
 175      *
 176      * @param b the context
 177      * @param type the type to be instantiated
 178      * @return true if the plugin handles the bytecode, false otherwise
 179      */
 180     default boolean handleNewInstance(GraphBuilderContext b, ResolvedJavaType type) {
 181         return false;
 182     }
 183 
 184     /**
 185      * Handle the parsing of a NEWARRAY and ANEWARRAY bytecode. If the method returns true, it must
 186      * {@link GraphBuilderContext#push push} a value with the result of the allocation using
 187      * {@link JavaKind#Object}.
 188      *
 189      * @param b the context
 190      * @param elementType the element type of the array to be instantiated
 191      * @param length the length of the new array
 192      * @return true if the plugin handles the bytecode, false otherwise
 193      */
 194     default boolean handleNewArray(GraphBuilderContext b, ResolvedJavaType elementType, ValueNode length) {
 195         return false;
 196     }
 197 
 198     /**
 199      * Handle the parsing of a MULTIANEWARRAY bytecode. If the method returns true, it must
 200      * {@link GraphBuilderContext#push push} a value with the result of the allocation using
 201      * {@link JavaKind#Object}.
 202      *
 203      * @param b the context
 204      * @param type the type of the outermost array to be instantiated
 205      * @param dimensions the array of lengths for all the dimensions to be instantiated
 206      * @return true if the plugin handles the bytecode, false otherwise
 207      */
 208     default boolean handleNewMultiArray(GraphBuilderContext b, ResolvedJavaType type, ValueNode[] dimensions) {
 209         return false;
 210     }
 211 
 212     /**
 213      * If the plugin {@link GraphBuilderContext#push pushes} a value with a different
 214      * {@link JavaKind} than specified by the bytecode, it must override this method and return
 215      * {@code true}. This disables assertion checking for value kinds.
 216      *
 217      * @param b the context
 218      */
 219     default boolean canChangeStackKind(GraphBuilderContext b) {
 220         return false;
 221     }
 222 }