package java.lang.invoke;
import jdk.internal.vm.annotation.DontInline;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Stable;
import java.lang.reflect.Array;
import java.util.Arrays;
import static java.lang.invoke.MethodHandleStatics.*;
import static java.lang.invoke.MethodHandleNatives.Constants.*;
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
import static java.lang.invoke.LambdaForm.*;
import static java.lang.invoke.LambdaForm.Kind.*;
class Invokers {
private final MethodType targetType;
private final @Stable MethodHandle[] invokers = new MethodHandle[INV_LIMIT];
static final int
INV_EXACT = 0,
INV_GENERIC = 1,
INV_BASIC = 2,
INV_LIMIT = 3;
Invokers(MethodType targetType) {
this.targetType = targetType;
}
MethodHandle exactInvoker() {
MethodHandle invoker = cachedInvoker(INV_EXACT);
if (invoker != null) return invoker;
invoker = makeExactOrGeneralInvoker(true);
return setCachedInvoker(INV_EXACT, invoker);
}
MethodHandle genericInvoker() {
MethodHandle invoker = cachedInvoker(INV_GENERIC);
if (invoker != null) return invoker;
invoker = makeExactOrGeneralInvoker(false);
return setCachedInvoker(INV_GENERIC, invoker);
}
MethodHandle basicInvoker() {
MethodHandle invoker = cachedInvoker(INV_BASIC);
if (invoker != null) return invoker;
MethodType basicType = targetType.basicType();
if (basicType != targetType) {
return setCachedInvoker(INV_BASIC, basicType.invokers().basicInvoker());
}
invoker = basicType.form().cachedMethodHandle(MethodTypeForm.MH_BASIC_INV);
if (invoker == null) {
MemberName method = invokeBasicMethod(basicType);
invoker = DirectMethodHandle.make(method);
assert(checkInvoker(invoker));
invoker = basicType.form().setCachedMethodHandle(MethodTypeForm.MH_BASIC_INV, invoker);
}
return setCachedInvoker(INV_BASIC, invoker);
}
MethodHandle varHandleMethodInvoker(VarHandle.AccessMode ak) {
return makeVarHandleMethodInvoker(ak, false);
}
MethodHandle varHandleMethodExactInvoker(VarHandle.AccessMode ak) {
return makeVarHandleMethodInvoker(ak, true);
}
private MethodHandle cachedInvoker(int idx) {
return invokers[idx];
}
private synchronized MethodHandle setCachedInvoker(int idx, final MethodHandle invoker) {
MethodHandle prev = invokers[idx];
if (prev != null) return prev;
return invokers[idx] = invoker;
}
private MethodHandle makeExactOrGeneralInvoker(boolean isExact) {
MethodType mtype = targetType;
MethodType invokerType = mtype.invokerType();
int which = (isExact ? MethodTypeForm.LF_EX_INVOKER : MethodTypeForm.LF_GEN_INVOKER);
LambdaForm lform = invokeHandleForm(mtype, false, which);
MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, mtype);
String whichName = (isExact ? "invokeExact" : "invoke");
invoker = invoker.withInternalMemberName(MemberName.makeMethodHandleInvoke(whichName, mtype), false);
assert(checkInvoker(invoker));
maybeCompileToBytecode(invoker);
return invoker;
}
private MethodHandle makeVarHandleMethodInvoker(VarHandle.AccessMode ak, boolean isExact) {
MethodType mtype = targetType;
MethodType invokerType = mtype.insertParameterTypes(0, VarHandle.class);
LambdaForm lform = varHandleMethodInvokerHandleForm(ak.methodName(), mtype, isExact);
VarHandle.AccessDescriptor ad = new VarHandle.AccessDescriptor(mtype, ak.at.ordinal(), ak.ordinal());
MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, ad);
invoker = invoker.withInternalMemberName(MemberName.makeVarHandleMethodInvoke(ak.methodName(), mtype), false);
assert(checkVarHandleInvoker(invoker));
maybeCompileToBytecode(invoker);
return invoker;
}
private void maybeCompileToBytecode(MethodHandle invoker) {
final int EAGER_COMPILE_ARITY_LIMIT = 10;
if (targetType == targetType.erase() &&
targetType.parameterCount() < EAGER_COMPILE_ARITY_LIMIT) {
invoker.form.compileToBytecode();
}
}
static MemberName invokeBasicMethod(MethodType basicType) {
assert(basicType == basicType.basicType());
try {
return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType);
} catch (ReflectiveOperationException ex) {
throw newInternalError("JVM cannot find invoker for "+basicType, ex);
}
}
private boolean checkInvoker(MethodHandle invoker) {
assert(targetType.invokerType().equals(invoker.type()))
: java.util.Arrays.asList(targetType, targetType.invokerType(), invoker);
assert(invoker.internalMemberName() == null ||
invoker.internalMemberName().getMethodType().equals(targetType));
assert(!invoker.isVarargsCollector());
return true;
}
private boolean checkVarHandleInvoker(MethodHandle invoker) {
MethodType invokerType = targetType.insertParameterTypes(0, VarHandle.class);
assert(invokerType.equals(invoker.type()))
: java.util.Arrays.asList(targetType, invokerType, invoker);
assert(invoker.internalMemberName() == null ||
invoker.internalMemberName().getMethodType().equals(targetType));
assert(!invoker.isVarargsCollector());
return true;
}
MethodHandle spreadInvoker(int leadingArgCount) {
int spreadArgCount = targetType.parameterCount() - leadingArgCount;
MethodType postSpreadType = targetType;
Class<?> argArrayType = impliedRestargType(postSpreadType, leadingArgCount);
if (postSpreadType.parameterSlotCount() <= MethodType.MAX_MH_INVOKER_ARITY) {
return genericInvoker().asSpreader(argArrayType, spreadArgCount);
}
MethodType preSpreadType = postSpreadType
.replaceParameterTypes(leadingArgCount, postSpreadType.parameterCount(), argArrayType);
MethodHandle arrayInvoker = MethodHandles.invoker(preSpreadType);
MethodHandle makeSpreader = MethodHandles.insertArguments(Lazy.MH_asSpreader, 1, argArrayType, spreadArgCount);
return MethodHandles.filterArgument(arrayInvoker, 0, makeSpreader);
}
private static Class<?> impliedRestargType(MethodType restargType, int fromPos) {
if (restargType.isGeneric()) return Object[].class;
int maxPos = restargType.parameterCount();
if (fromPos >= maxPos) return Object[].class;
Class<?> argType = restargType.parameterType(fromPos);
for (int i = fromPos+1; i < maxPos; i++) {
if (argType != restargType.parameterType(i))
throw newIllegalArgumentException("need homogeneous rest arguments", restargType);
}
if (argType == Object.class) return Object[].class;
return Array.newInstance(argType, 0).getClass();
}
public String toString() {
return "Invokers"+targetType;
}
static MemberName methodHandleInvokeLinkerMethod(String name,
MethodType mtype,
Object[] appendixResult) {
int which;
switch (name) {
case "invokeExact": which = MethodTypeForm.LF_EX_LINKER; break;
case "invoke": which = MethodTypeForm.LF_GEN_LINKER; break;
default: throw new InternalError("not invoker: "+name);
}
LambdaForm lform;
if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
lform = invokeHandleForm(mtype, false, which);
appendixResult[0] = mtype;
} else {
lform = invokeHandleForm(mtype, true, which);
}
return lform.vmentry;
}
private static final int MH_LINKER_ARG_APPENDED = 1;
static LambdaForm invokeHandleForm(MethodType mtype, boolean customized, int which) {
boolean isCached;
if (!customized) {
mtype = mtype.basicType();
isCached = true;
} else {
isCached = false;
}
boolean isLinker, isGeneric;
Kind kind;
switch (which) {
case MethodTypeForm.LF_EX_LINKER: isLinker = true; isGeneric = false; kind = EXACT_LINKER; break;
case MethodTypeForm.LF_EX_INVOKER: isLinker = false; isGeneric = false; kind = EXACT_INVOKER; break;
case MethodTypeForm.LF_GEN_LINKER: isLinker = true; isGeneric = true; kind = GENERIC_LINKER; break;
case MethodTypeForm.LF_GEN_INVOKER: isLinker = false; isGeneric = true; kind = GENERIC_INVOKER; break;
default: throw new InternalError();
}
LambdaForm lform;
if (isCached) {
lform = mtype.form().cachedLambdaForm(which);
if (lform != null) return lform;
}
final int THIS_MH = 0;
final int CALL_MH = THIS_MH + (isLinker ? 0 : 1);
final int ARG_BASE = CALL_MH + 1;
final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount();
final int INARG_LIMIT = OUTARG_LIMIT + (isLinker && !customized ? 1 : 0);
int nameCursor = OUTARG_LIMIT;
final int MTYPE_ARG = customized ? -1 : nameCursor++;
final int CHECK_TYPE = nameCursor++;
final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1;
final int LINKER_CALL = nameCursor++;
MethodType invokerFormType = mtype.invokerType();
if (isLinker) {
if (!customized)
invokerFormType = invokerFormType.appendParameterTypes(MemberName.class);
} else {
invokerFormType = invokerFormType.invokerType();
}
Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
assert(names.length == nameCursor)
: Arrays.asList(mtype, customized, which, nameCursor, names.length);
if (MTYPE_ARG >= INARG_LIMIT) {
assert(names[MTYPE_ARG] == null);
BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
NamedFunction getter = speciesData.getterFunction(0);
names[MTYPE_ARG] = new Name(getter, names[THIS_MH]);
}
MethodType outCallType = mtype.basicType();
Object[] outArgs = Arrays.copyOfRange(names, CALL_MH, OUTARG_LIMIT, Object[].class);
Object mtypeArg = (customized ? mtype : names[MTYPE_ARG]);
if (!isGeneric) {
names[CHECK_TYPE] = new Name(NF_checkExactType, names[CALL_MH], mtypeArg);
} else {
names[CHECK_TYPE] = new Name(NF_checkGenericType, names[CALL_MH], mtypeArg);
outArgs[0] = names[CHECK_TYPE];
}
if (CHECK_CUSTOM != -1) {
names[CHECK_CUSTOM] = new Name(NF_checkCustomized, outArgs[0]);
}
names[LINKER_CALL] = new Name(outCallType, outArgs);
if (customized) {
lform = new LambdaForm(kind.defaultLambdaName, INARG_LIMIT, names);
} else {
lform = new LambdaForm(kind.defaultLambdaName, INARG_LIMIT, names, kind);
}
if (isLinker)
lform.compileToBytecode();
if (isCached)
lform = mtype.form().setCachedLambdaForm(which, lform);
return lform;
}
static MemberName varHandleInvokeLinkerMethod(String name,
MethodType mtype) {
LambdaForm lform;
if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
lform = varHandleMethodGenericLinkerHandleForm(name, mtype);
} else {
throw newInternalError("Unsupported parameter slot count " + mtype.parameterSlotCount());
}
return lform.vmentry;
}
private static LambdaForm varHandleMethodGenericLinkerHandleForm(String name, MethodType mtype) {
final int THIS_VH = 0;
final int ARG_BASE = THIS_VH + 1;
final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
int nameCursor = ARG_LIMIT;
final int VAD_ARG = nameCursor++;
final int CHECK_TYPE = nameCursor++;
final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1;
final int LINKER_CALL = nameCursor++;
Name[] names = new Name[LINKER_CALL + 1];
names[THIS_VH] = argument(THIS_VH, BasicType.basicType(Object.class));
for (int i = 0; i < mtype.parameterCount(); i++) {
names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i)));
}
names[VAD_ARG] = new Name(ARG_LIMIT, BasicType.basicType(Object.class));
names[CHECK_TYPE] = new Name(NF_checkVarHandleGenericType, names[THIS_VH], names[VAD_ARG]);
Object[] outArgs = new Object[ARG_LIMIT + 1];
outArgs[0] = names[CHECK_TYPE];
for (int i = 0; i < ARG_LIMIT; i++) {
outArgs[i + 1] = names[i];
}
if (CHECK_CUSTOM != -1) {
names[CHECK_CUSTOM] = new Name(NF_checkCustomized, outArgs[0]);
}
MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
.basicType();
names[LINKER_CALL] = new Name(outCallType, outArgs);
LambdaForm lform = new LambdaForm(name + ":VarHandle_invoke_MT_" + shortenSignature(basicTypeSignature(mtype)),
ARG_LIMIT + 1, names);
lform.compileToBytecode();
return lform;
}
private static LambdaForm varHandleMethodInvokerHandleForm(String name, MethodType mtype, boolean isExact) {
final int THIS_MH = 0;
final int CALL_VH = THIS_MH + 1;
final int ARG_BASE = CALL_VH + 1;
final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
int nameCursor = ARG_LIMIT;
final int VAD_ARG = nameCursor++;
final int CHECK_TYPE = nameCursor++;
final int LINKER_CALL = nameCursor++;
Name[] names = new Name[LINKER_CALL + 1];
names[THIS_MH] = argument(THIS_MH, BasicType.basicType(Object.class));
names[CALL_VH] = argument(CALL_VH, BasicType.basicType(Object.class));
for (int i = 0; i < mtype.parameterCount(); i++) {
names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i)));
}
BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
NamedFunction getter = speciesData.getterFunction(0);
names[VAD_ARG] = new Name(getter, names[THIS_MH]);
if (isExact) {
names[CHECK_TYPE] = new Name(NF_checkVarHandleExactType, names[CALL_VH], names[VAD_ARG]);
} else {
names[CHECK_TYPE] = new Name(NF_checkVarHandleGenericType, names[CALL_VH], names[VAD_ARG]);
}
Object[] outArgs = new Object[ARG_LIMIT];
outArgs[0] = names[CHECK_TYPE];
for (int i = 1; i < ARG_LIMIT; i++) {
outArgs[i] = names[i];
}
MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
.basicType();
names[LINKER_CALL] = new Name(outCallType, outArgs);
String debugName = isExact ? ":VarHandle_exactInvoker" : ":VarHandle_invoker";
LambdaForm lform = new LambdaForm(name + debugName + shortenSignature(basicTypeSignature(mtype)),
ARG_LIMIT, names);
lform.prepare();
return lform;
}
static
@ForceInline
MethodHandle checkVarHandleGenericType(VarHandle handle, VarHandle.AccessDescriptor ad) {
MethodHandle mh = handle.getMethodHandle(ad.mode);
if (mh.type() == ad.symbolicMethodTypeInvoker) {
return mh;
}
else {
return mh.asType(ad.symbolicMethodTypeInvoker);
}
}
static
@ForceInline
MethodHandle checkVarHandleExactType(VarHandle handle, VarHandle.AccessDescriptor ad) {
MethodHandle mh = handle.getMethodHandle(ad.mode);
MethodType mt = mh.type();
if (mt != ad.symbolicMethodTypeInvoker) {
throw newWrongMethodTypeException(mt, ad.symbolicMethodTypeInvoker);
}
return mh;
}
static
WrongMethodTypeException newWrongMethodTypeException(MethodType actual, MethodType expected) {
return new WrongMethodTypeException("expected "+expected+" but found "+actual);
}
static
@ForceInline
void checkExactType(MethodHandle mh, MethodType expected) {
MethodType actual = mh.type();
if (actual != expected)
throw newWrongMethodTypeException(expected, actual);
}
static
@ForceInline
MethodHandle checkGenericType(MethodHandle mh, MethodType expected) {
return mh.asType(expected);
}
static MemberName linkToCallSiteMethod(MethodType mtype) {
LambdaForm lform = callSiteForm(mtype, false);
return lform.vmentry;
}
static MemberName linkToTargetMethod(MethodType mtype) {
LambdaForm lform = callSiteForm(mtype, true);
return lform.vmentry;
}
private static LambdaForm callSiteForm(MethodType mtype, boolean skipCallSite) {
mtype = mtype.basicType();
final int which = (skipCallSite ? MethodTypeForm.LF_MH_LINKER : MethodTypeForm.LF_CS_LINKER);
LambdaForm lform = mtype.form().cachedLambdaForm(which);
if (lform != null) return lform;
final int ARG_BASE = 0;
final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount();
final int INARG_LIMIT = OUTARG_LIMIT + 1;
int nameCursor = OUTARG_LIMIT;
final int APPENDIX_ARG = nameCursor++;
final int CSITE_ARG = skipCallSite ? -1 : APPENDIX_ARG;
final int CALL_MH = skipCallSite ? APPENDIX_ARG : nameCursor++;
final int LINKER_CALL = nameCursor++;
MethodType invokerFormType = mtype.appendParameterTypes(skipCallSite ? MethodHandle.class : CallSite.class);
Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
assert(names.length == nameCursor);
assert(names[APPENDIX_ARG] != null);
if (!skipCallSite)
names[CALL_MH] = new Name(NF_getCallSiteTarget, names[CSITE_ARG]);
final int PREPEND_MH = 0, PREPEND_COUNT = 1;
Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, OUTARG_LIMIT + PREPEND_COUNT, Object[].class);
System.arraycopy(outArgs, 0, outArgs, PREPEND_COUNT, outArgs.length - PREPEND_COUNT);
outArgs[PREPEND_MH] = names[CALL_MH];
names[LINKER_CALL] = new Name(mtype, outArgs);
lform = new LambdaForm((skipCallSite ? "linkToTargetMethod" : "linkToCallSite"), INARG_LIMIT, names);
lform.compileToBytecode();
lform = mtype.form().setCachedLambdaForm(which, lform);
return lform;
}
static
@ForceInline
MethodHandle getCallSiteTarget(CallSite site) {
return site.getTarget();
}
static
@ForceInline
void checkCustomized(MethodHandle mh) {
if (MethodHandleImpl.isCompileConstant(mh)) return;
if (mh.form.customized == null) {
maybeCustomize(mh);
}
}
static
@DontInline
void maybeCustomize(MethodHandle mh) {
byte count = mh.customizationCount;
if (count >= CUSTOMIZE_THRESHOLD) {
mh.customize();
} else {
mh.customizationCount = (byte)(count+1);
}
}
private static final NamedFunction
NF_checkExactType,
NF_checkGenericType,
NF_getCallSiteTarget,
NF_checkCustomized,
NF_checkVarHandleGenericType,
NF_checkVarHandleExactType;
static {
try {
NamedFunction nfs[] = {
NF_checkExactType = new NamedFunction(Invokers.class
.getDeclaredMethod("checkExactType", MethodHandle.class, MethodType.class)),
NF_checkGenericType = new NamedFunction(Invokers.class
.getDeclaredMethod("checkGenericType", MethodHandle.class, MethodType.class)),
NF_getCallSiteTarget = new NamedFunction(Invokers.class
.getDeclaredMethod("getCallSiteTarget", CallSite.class)),
NF_checkCustomized = new NamedFunction(Invokers.class
.getDeclaredMethod("checkCustomized", MethodHandle.class)),
NF_checkVarHandleGenericType = new NamedFunction(Invokers.class
.getDeclaredMethod("checkVarHandleGenericType", VarHandle.class, VarHandle.AccessDescriptor.class)),
NF_checkVarHandleExactType = new NamedFunction(Invokers.class
.getDeclaredMethod("checkVarHandleExactType", VarHandle.class, VarHandle.AccessDescriptor.class)),
};
assert(InvokerBytecodeGenerator.isStaticallyInvocable(nfs));
} catch (ReflectiveOperationException ex) {
throw newInternalError(ex);
}
}
private static class Lazy {
private static final MethodHandle MH_asSpreader;
static {
try {
MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader",
MethodType.methodType(MethodHandle.class, Class.class, int.class));
} catch (ReflectiveOperationException ex) {
throw newInternalError(ex);
}
}
}
static {
UNSAFE.ensureClassInitialized(Holder.class);
}
final class Holder {}
}