Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/GuardWithTestHandle.java
12521 views
1
/*[INCLUDE-IF Sidecar17 & !OPENJDK_METHODHANDLES]*/
2
/*******************************************************************************
3
* Copyright (c) 2011, 2020 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* 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-exception
22
*******************************************************************************/
23
package java.lang.invoke;
24
25
/*[IF JAVA_SPEC_VERSION >= 15]*/
26
import java.util.List;
27
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
28
29
final class GuardWithTestHandle extends MethodHandle {
30
31
final MethodHandle guard;
32
final MethodHandle trueTarget;
33
final MethodHandle falseTarget;
34
35
protected GuardWithTestHandle(MethodHandle guard, MethodHandle trueTarget, MethodHandle falseTarget) {
36
super(trueTarget.type(), KIND_GUARDWITHTEST, guard.type()); //$NON-NLS-1$
37
this.guard = guard;
38
this.trueTarget = trueTarget;
39
this.falseTarget = falseTarget;
40
}
41
42
GuardWithTestHandle(GuardWithTestHandle originalHandle, MethodType newType) {
43
super(originalHandle, newType);
44
this.guard = originalHandle.guard;
45
this.trueTarget = originalHandle.trueTarget;
46
this.falseTarget = originalHandle.falseTarget;
47
}
48
49
public static MethodHandle get(MethodHandle guard, MethodHandle trueTarget, MethodHandle falseTarget) {
50
/* Constant boolean is implemented with ConstantIntHandle, if `guard` handle is a ConstantIntHandle,
51
we can evaluate the if statement now and return the target handle*/
52
if (guard instanceof ConstantIntHandle) {
53
ConstantIntHandle constantHandle = (ConstantIntHandle)guard;
54
if (constantHandle.value != 0) {
55
return trueTarget;
56
} else {
57
return falseTarget;
58
}
59
}
60
61
return new GuardWithTestHandle(guard, trueTarget, falseTarget);
62
}
63
64
// {{{ JIT support
65
66
private static final ThunkTable _thunkTable = new ThunkTable();
67
protected final ThunkTable thunkTable(){ return _thunkTable; }
68
69
protected final ThunkTuple computeThunks(Object guardType) {
70
// Different thunks accommodate guards with different numbers of parameters
71
return thunkTable().get(new ThunkKeyWithObject(ThunkKey.computeThunkableType(type()), ThunkKey.computeThunkableType((MethodType)guardType)));
72
}
73
74
private static native int numGuardArgs();
75
76
@FrameIteratorSkip
77
private final int invokeExact_thunkArchetype_X(int argPlaceholder) {
78
if (ILGenMacros.isShareableThunk()) {
79
undoCustomizationLogic(guard, trueTarget, falseTarget);
80
}
81
if (!ILGenMacros.isCustomThunk()) {
82
doCustomizationLogic();
83
}
84
if (ILGenMacros.invokeExact_Z(guard, ILGenMacros.firstN(numGuardArgs(), argPlaceholder))) {
85
return ILGenMacros.invokeExact_X(trueTarget, argPlaceholder);
86
} else {
87
return ILGenMacros.invokeExact_X(falseTarget, argPlaceholder);
88
}
89
}
90
91
/*[IF JAVA_SPEC_VERSION >= 15]*/
92
@Override
93
boolean addRelatedMHs(List<MethodHandle> relatedMHs) {
94
relatedMHs.add(guard);
95
relatedMHs.add(falseTarget);
96
relatedMHs.add(trueTarget);
97
return true;
98
}
99
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
100
101
// }}} JIT support
102
103
@Override
104
MethodHandle cloneWithNewType(MethodType newType) {
105
return new GuardWithTestHandle(this, newType);
106
}
107
108
final void compareWith(MethodHandle right, Comparator c) {
109
if (right instanceof GuardWithTestHandle) {
110
((GuardWithTestHandle)right).compareWithGuardWithTest(this, c);
111
} else {
112
c.fail();
113
}
114
}
115
116
final void compareWithGuardWithTest(GuardWithTestHandle left, Comparator c) {
117
c.compareChildHandle(left.guard, this.guard);
118
c.compareChildHandle(left.trueTarget, this.trueTarget);
119
c.compareChildHandle(left.falseTarget, this.falseTarget);
120
}
121
}
122
123