1 /* 2 * Copyright (c) 2013, 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.api.replacements; 26 27 import java.lang.annotation.ElementType; 28 import java.lang.annotation.Retention; 29 import java.lang.annotation.RetentionPolicy; 30 import java.lang.annotation.Target; 31 import java.lang.reflect.Array; 32 33 import jdk.vm.ci.meta.Signature; 34 35 /** 36 * Denotes a method whose body is used by a compiler as the substitute (or intrinsification) of 37 * another method. The exact mechanism used to do the substitution is compiler dependent but every 38 * compiler should require substitute methods to be annotated with {@link MethodSubstitution}. In 39 * addition, a compiler is recommended to implement {@link MethodSubstitutionRegistry} to advertise 40 * the mechanism by which it supports registration of method substitutes. 41 * 42 * A compiler may support partial intrinsification where only a part of a method is implemented by 43 * the compiler. The unsupported path is expressed by a call to either the original or substitute 44 * method from within the substitute method. Such as call is a <i>partial intrinsic exit</i>. 45 * 46 * For example, here's a HotSpot specific intrinsic for {@link Array#newInstance(Class, int)} that 47 * only handles the case where the VM representation of the array class to be instantiated already 48 * exists: 49 * 50 * <pre> 51 * @MethodSubstitution 52 * public static Object newInstance(Class<?> componentType, int length) { 53 * if (componentType == null || loadKlassFromObject(componentType, arrayKlassOffset(INJECTED_VMCONFIG), CLASS_ARRAY_KLASS_LOCATION).isNull()) { 54 * // Array class not yet created - exit the intrinsic and call the original method 55 * return newInstance(componentType, length); 56 * } 57 * return DynamicNewArrayNode.newArray(GraalDirectives.guardingNonNull(componentType), length, JavaKind.Object); 58 * } 59 * </pre> 60 * 61 * Here's the same intrinsification where the exit is expressed as a call to the original method: 62 * 63 * <pre> 64 * @MethodSubstitution 65 * public static Object newInstance(Class<?> componentType, int length) { 66 * if (componentType == null || loadKlassFromObject(componentType, arrayKlassOffset(INJECTED_VMCONFIG), CLASS_ARRAY_KLASS_LOCATION).isNull()) { 67 * // Array class not yet created - exit the intrinsic and call the original method 68 * return java.lang.reflect.newInstance(componentType, length); 69 * } 70 * return DynamicNewArrayNode.newArray(GraalDirectives.guardingNonNull(componentType), length, JavaKind.Object); 71 * } 72 * </pre> 73 * 74 * A condition for a partial intrinsic exit is that it is uses the unmodified parameters of the 75 * substitute as arguments to the partial intrinsic exit call. There must also be no side effecting 76 * instruction between the start of the substitute method and the partial intrinsic exit. 77 */ 78 @Retention(RetentionPolicy.RUNTIME) 79 @Target(ElementType.METHOD) 80 public @interface MethodSubstitution { 81 82 /** 83 * Gets the name of the original method. 84 * <p> 85 * If the default value is specified for this element, then the name of the original method is 86 * same as the substitute method. 87 */ 88 String value() default ""; 89 90 /** 91 * Determines if the original method is static. 92 */ 93 boolean isStatic() default true; 94 95 /** 96 * Gets the {@linkplain Signature#toMethodDescriptor signature} of the original method. 97 * <p> 98 * If the default value is specified for this element, then the signature of the original method 99 * is the same as the substitute method. 100 */ 101 String signature() default ""; 102 103 /** 104 * Determines if the substitution is for a method that may not be part of the runtime. For 105 * example, a method introduced in a later JDK version. Substitutions for such methods are 106 * omitted if the original method cannot be found. 107 */ 108 boolean optional() default false; 109 }