Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/GuardWithTestHandle.java
12521 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 */2728final class GuardWithTestHandle extends MethodHandle {2930final MethodHandle guard;31final MethodHandle trueTarget;32final MethodHandle falseTarget;3334protected GuardWithTestHandle(MethodHandle guard, MethodHandle trueTarget, MethodHandle falseTarget) {35super(trueTarget.type(), KIND_GUARDWITHTEST, guard.type()); //$NON-NLS-1$36this.guard = guard;37this.trueTarget = trueTarget;38this.falseTarget = falseTarget;39}4041GuardWithTestHandle(GuardWithTestHandle originalHandle, MethodType newType) {42super(originalHandle, newType);43this.guard = originalHandle.guard;44this.trueTarget = originalHandle.trueTarget;45this.falseTarget = originalHandle.falseTarget;46}4748public static MethodHandle get(MethodHandle guard, MethodHandle trueTarget, MethodHandle falseTarget) {49/* Constant boolean is implemented with ConstantIntHandle, if `guard` handle is a ConstantIntHandle,50we can evaluate the if statement now and return the target handle*/51if (guard instanceof ConstantIntHandle) {52ConstantIntHandle constantHandle = (ConstantIntHandle)guard;53if (constantHandle.value != 0) {54return trueTarget;55} else {56return falseTarget;57}58}5960return new GuardWithTestHandle(guard, trueTarget, falseTarget);61}6263// {{{ JIT support6465private static final ThunkTable _thunkTable = new ThunkTable();66protected final ThunkTable thunkTable(){ return _thunkTable; }6768protected final ThunkTuple computeThunks(Object guardType) {69// Different thunks accommodate guards with different numbers of parameters70return thunkTable().get(new ThunkKeyWithObject(ThunkKey.computeThunkableType(type()), ThunkKey.computeThunkableType((MethodType)guardType)));71}7273private static native int numGuardArgs();7475@FrameIteratorSkip76private final int invokeExact_thunkArchetype_X(int argPlaceholder) {77if (ILGenMacros.isShareableThunk()) {78undoCustomizationLogic(guard, trueTarget, falseTarget);79}80if (!ILGenMacros.isCustomThunk()) {81doCustomizationLogic();82}83if (ILGenMacros.invokeExact_Z(guard, ILGenMacros.firstN(numGuardArgs(), argPlaceholder))) {84return ILGenMacros.invokeExact_X(trueTarget, argPlaceholder);85} else {86return ILGenMacros.invokeExact_X(falseTarget, argPlaceholder);87}88}8990/*[IF JAVA_SPEC_VERSION >= 15]*/91@Override92boolean addRelatedMHs(List<MethodHandle> relatedMHs) {93relatedMHs.add(guard);94relatedMHs.add(falseTarget);95relatedMHs.add(trueTarget);96return true;97}98/*[ENDIF] JAVA_SPEC_VERSION >= 15 */99100// }}} JIT support101102@Override103MethodHandle cloneWithNewType(MethodType newType) {104return new GuardWithTestHandle(this, newType);105}106107final void compareWith(MethodHandle right, Comparator c) {108if (right instanceof GuardWithTestHandle) {109((GuardWithTestHandle)right).compareWithGuardWithTest(this, c);110} else {111c.fail();112}113}114115final void compareWithGuardWithTest(GuardWithTestHandle left, Comparator c) {116c.compareChildHandle(left.guard, this.guard);117c.compareChildHandle(left.trueTarget, this.trueTarget);118c.compareChildHandle(left.falseTarget, this.falseTarget);119}120}121122123