Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/FoldHandle.java
12520 views
/*[INCLUDE-IF Sidecar17 & !OPENJDK_METHODHANDLES]*/1/*******************************************************************************2* Copyright (c) 2011, 2020 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/22package java.lang.invoke;2324/*[IF JAVA_SPEC_VERSION >= 15]*/25import java.util.List;26/*[ENDIF] JAVA_SPEC_VERSION >= 15 */2728abstract class FoldHandle extends MethodHandle {29protected final MethodHandle next;30protected final MethodHandle combiner;31@VMCONSTANTPOOL_FIELD32private final int foldPosition; /* The starting position of fold arguments */33@VMCONSTANTPOOL_FIELD34private final int[] argumentIndices; /* An array of argument indexes of fold handle */3536protected FoldHandle(MethodHandle next, MethodHandle combiner, MethodType type, int foldPosition, int... argumentIndices) {37super(type, KIND_FOLDHANDLE, infoAffectingThunks(combiner.type(), foldPosition, argumentIndices));38this.next = next;39this.combiner = combiner;40this.foldPosition = foldPosition;41this.argumentIndices = argumentIndices;42}4344FoldHandle(FoldHandle originalHandle, MethodType newType) {45super(originalHandle, newType);46this.next = originalHandle.next;47this.combiner = originalHandle.combiner;48this.foldPosition = originalHandle.foldPosition;49this.argumentIndices = originalHandle.argumentIndices;50}5152public static FoldHandle get(MethodHandle next, MethodHandle combiner, MethodType type, int foldPosition, int... argumentIndices) {53if (combiner.type().returnType() == void.class) {54/* The foldPosition is used to determine the starting position of combiner,55* so it must be kept unchanged even though the combiner returns void.56*/57return new FoldVoidHandle(next, combiner, type, foldPosition, argumentIndices);58} else {59return new FoldNonvoidHandle(next, combiner, type, foldPosition, argumentIndices);60}61}6263private static Object[] infoAffectingThunks(MethodType combinerType, int foldPosition, int... argumentIndices) {64// The number and types of values to fold affect jitted thunks65Object[] result = { combinerType, foldPosition, argumentIndices};66return result;67}6869private static final ThunkTable _thunkTable = new ThunkTable();70protected ThunkTable thunkTable(){ return _thunkTable; }717273protected final ThunkTuple computeThunks(Object info) {74return thunkTable().get(new ThunkKeyWithObjectArray(ThunkKey.computeThunkableType(type()), (Object[])info));75}7677final void compareWith(MethodHandle right, Comparator c) {78if (right instanceof FoldHandle) {79((FoldHandle)right).compareWithFold(this, c);80} else {81c.fail();82}83}8485final void compareWithFold(FoldHandle left, Comparator c) {86c.compareChildHandle(left.next, this.next);87c.compareChildHandle(left.combiner, this.combiner);88c.compareStructuralParameter(left.foldPosition, this.foldPosition);89/* The comparator does not address the case where two FoldHandles,90* one with empty indices array and the other with non-empty indices array91* but the argument indices for them are the same, should share the same thunkArchetype.92* This is because that case will not happen due to the way we create the FoldHandle:93* if the argument indices in the array are exactly the same to the argument indices94* starting from the fold position, the FoldHandle will be created as if no array is passed in.95*/96c.compareStructuralParameter(left.argumentIndices, this.argumentIndices);97}9899/*[IF JAVA_SPEC_VERSION >= 15]*/100@Override101boolean addRelatedMHs(List<MethodHandle> relatedMHs) {102relatedMHs.add(next);103relatedMHs.add(combiner);104return true;105}106/*[ENDIF] JAVA_SPEC_VERSION >= 15 */107108// {{{ JIT support109protected static native int foldPosition();110protected static native int argIndices();111protected static native int argumentsForCombiner(int indice, int argPlaceholder);112// }}} JIT support113}114115final class FoldNonvoidHandle extends FoldHandle {116117FoldNonvoidHandle(MethodHandle next, MethodHandle combiner, MethodType type, int foldPosition, int... argumentIndices) {118super(next, combiner, type, foldPosition, argumentIndices); //$NON-NLS-1$119}120121// {{{ JIT support122123FoldNonvoidHandle(FoldNonvoidHandle originalHandle, MethodType newType) {124super(originalHandle, newType);125}126127@FrameIteratorSkip128private final int invokeExact_thunkArchetype_X(int argPlaceholder) {129if (ILGenMacros.isShareableThunk()) {130undoCustomizationLogic(combiner, next);131}132if (!ILGenMacros.isCustomThunk()) {133doCustomizationLogic();134}135return ILGenMacros.invokeExact_X(next, ILGenMacros.placeholder(136ILGenMacros.firstN(foldPosition(), argPlaceholder),137ILGenMacros.invokeExact(combiner, argumentsForCombiner(argIndices(), argPlaceholder)),138ILGenMacros.dropFirstN(foldPosition(), argPlaceholder)));139}140141// }}} JIT support142143@Override144MethodHandle cloneWithNewType(MethodType newType) {145return new FoldNonvoidHandle(this, newType);146}147}148149final class FoldVoidHandle extends FoldHandle {150151FoldVoidHandle(MethodHandle next, MethodHandle combiner, MethodType type, int foldPosition, int... argumentIndices) {152super(next, combiner, type, foldPosition, argumentIndices); //$NON-NLS-1$153}154155FoldVoidHandle(FoldVoidHandle foldVoidHandle, MethodType newType) {156super(foldVoidHandle, newType);157}158159// {{{ JIT support160161162@FrameIteratorSkip163private final int invokeExact_thunkArchetype_X(int argPlaceholder) {164if (ILGenMacros.isShareableThunk()) {165undoCustomizationLogic(combiner, next);166}167if (!ILGenMacros.isCustomThunk()) {168doCustomizationLogic();169}170ILGenMacros.invokeExact(combiner, argumentsForCombiner(argIndices(), argPlaceholder));171return ILGenMacros.invokeExact_X(next, argPlaceholder);172}173174// }}} JIT support175176@Override177MethodHandle cloneWithNewType(MethodType newType) {178return new FoldVoidHandle(this, newType);179}180}181182183