jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import sun.misc.Unsafe; jaroslav@1646: import java.lang.reflect.Method; jaroslav@1646: import java.util.Arrays; jaroslav@1646: import sun.invoke.util.VerifyAccess; jaroslav@1646: import static java.lang.invoke.MethodHandleNatives.Constants.*; jaroslav@1646: import static java.lang.invoke.LambdaForm.*; jaroslav@1646: import static java.lang.invoke.MethodTypeForm.*; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: import java.lang.ref.WeakReference; jaroslav@1646: import java.lang.reflect.Field; jaroslav@1646: import sun.invoke.util.ValueConversions; jaroslav@1646: import sun.invoke.util.VerifyType; jaroslav@1646: import sun.invoke.util.Wrapper; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * The flavor of method handle which implements a constant reference jaroslav@1646: * to a class member. jaroslav@1646: * @author jrose jaroslav@1646: */ jaroslav@1646: class DirectMethodHandle extends MethodHandle { jaroslav@1646: final MemberName member; jaroslav@1646: jaroslav@1646: // Constructors and factory methods in this class *must* be package scoped or private. jaroslav@1646: private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member) { jaroslav@1646: super(mtype, form); jaroslav@1646: if (!member.isResolved()) throw new InternalError(); jaroslav@1646: jaroslav@1646: if (member.getDeclaringClass().isInterface() && jaroslav@1646: member.isMethod() && !member.isAbstract()) { jaroslav@1646: // Check for corner case: invokeinterface of Object method jaroslav@1646: MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind()); jaroslav@1646: m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null); jaroslav@1646: if (m != null && m.isPublic()) { jaroslav@1646: member = m; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: this.member = member; jaroslav@1646: } jaroslav@1646: jaroslav@1646: // Factory methods: jaroslav@1646: static DirectMethodHandle make(byte refKind, Class receiver, MemberName member) { jaroslav@1646: MethodType mtype = member.getMethodOrFieldType(); jaroslav@1646: if (!member.isStatic()) { jaroslav@1646: if (!member.getDeclaringClass().isAssignableFrom(receiver) || member.isConstructor()) jaroslav@1646: throw new InternalError(member.toString()); jaroslav@1646: mtype = mtype.insertParameterTypes(0, receiver); jaroslav@1646: } jaroslav@1646: if (!member.isField()) { jaroslav@1646: if (refKind == REF_invokeSpecial) { jaroslav@1646: member = member.asSpecial(); jaroslav@1646: LambdaForm lform = preparedLambdaForm(member); jaroslav@1646: return new Special(mtype, lform, member); jaroslav@1646: } else { jaroslav@1646: LambdaForm lform = preparedLambdaForm(member); jaroslav@1646: return new DirectMethodHandle(mtype, lform, member); jaroslav@1646: } jaroslav@1646: } else { jaroslav@1646: LambdaForm lform = preparedFieldLambdaForm(member); jaroslav@1646: if (member.isStatic()) { jaroslav@1646: long offset = MethodHandleNatives.staticFieldOffset(member); jaroslav@1646: Object base = MethodHandleNatives.staticFieldBase(member); jaroslav@1646: return new StaticAccessor(mtype, lform, member, base, offset); jaroslav@1646: } else { jaroslav@1646: long offset = MethodHandleNatives.objectFieldOffset(member); jaroslav@1646: assert(offset == (int)offset); jaroslav@1646: return new Accessor(mtype, lform, member, (int)offset); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: } jaroslav@1646: static DirectMethodHandle make(Class receiver, MemberName member) { jaroslav@1646: byte refKind = member.getReferenceKind(); jaroslav@1646: if (refKind == REF_invokeSpecial) jaroslav@1646: refKind = REF_invokeVirtual; jaroslav@1646: return make(refKind, receiver, member); jaroslav@1646: } jaroslav@1646: static DirectMethodHandle make(MemberName member) { jaroslav@1646: if (member.isConstructor()) jaroslav@1646: return makeAllocator(member); jaroslav@1646: return make(member.getDeclaringClass(), member); jaroslav@1646: } jaroslav@1646: static DirectMethodHandle make(Method method) { jaroslav@1646: return make(method.getDeclaringClass(), new MemberName(method)); jaroslav@1646: } jaroslav@1646: static DirectMethodHandle make(Field field) { jaroslav@1646: return make(field.getDeclaringClass(), new MemberName(field)); jaroslav@1646: } jaroslav@1646: private static DirectMethodHandle makeAllocator(MemberName ctor) { jaroslav@1646: assert(ctor.isConstructor() && ctor.getName().equals("")); jaroslav@1646: Class instanceClass = ctor.getDeclaringClass(); jaroslav@1646: ctor = ctor.asConstructor(); jaroslav@1646: assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor; jaroslav@1646: MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass); jaroslav@1646: LambdaForm lform = preparedLambdaForm(ctor); jaroslav@1646: MemberName init = ctor.asSpecial(); jaroslav@1646: assert(init.getMethodType().returnType() == void.class); jaroslav@1646: return new Constructor(mtype, lform, ctor, init, instanceClass); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle copyWith(MethodType mt, LambdaForm lf) { jaroslav@1646: return new DirectMethodHandle(mt, lf, member); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: String internalProperties() { jaroslav@1646: return "/DMH="+member.toString(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: //// Implementation methods. jaroslav@1646: @Override jaroslav@1646: MethodHandle viewAsType(MethodType newType) { jaroslav@1646: return new DirectMethodHandle(newType, form, member); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: @ForceInline jaroslav@1646: MemberName internalMemberName() { jaroslav@1646: return member; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle bindArgument(int pos, char basicType, Object value) { jaroslav@1646: // If the member needs dispatching, do so. jaroslav@1646: if (pos == 0 && basicType == 'L') { jaroslav@1646: DirectMethodHandle concrete = maybeRebind(value); jaroslav@1646: if (concrete != null) jaroslav@1646: return concrete.bindReceiver(value); jaroslav@1646: } jaroslav@1646: return super.bindArgument(pos, basicType, value); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle bindReceiver(Object receiver) { jaroslav@1646: // If the member needs dispatching, do so. jaroslav@1646: DirectMethodHandle concrete = maybeRebind(receiver); jaroslav@1646: if (concrete != null) jaroslav@1646: return concrete.bindReceiver(receiver); jaroslav@1646: return super.bindReceiver(receiver); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(); jaroslav@1646: jaroslav@1646: private DirectMethodHandle maybeRebind(Object receiver) { jaroslav@1646: if (receiver != null) { jaroslav@1646: switch (member.getReferenceKind()) { jaroslav@1646: case REF_invokeInterface: jaroslav@1646: case REF_invokeVirtual: jaroslav@1646: // Pre-dispatch the member. jaroslav@1646: Class concreteClass = receiver.getClass(); jaroslav@1646: MemberName concrete = new MemberName(concreteClass, member.getName(), member.getMethodType(), REF_invokeSpecial); jaroslav@1646: concrete = IMPL_NAMES.resolveOrNull(REF_invokeSpecial, concrete, concreteClass); jaroslav@1646: if (concrete != null) jaroslav@1646: return new DirectMethodHandle(type(), preparedLambdaForm(concrete), concrete); jaroslav@1646: break; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: return null; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Create a LF which can invoke the given method. jaroslav@1646: * Cache and share this structure among all methods with jaroslav@1646: * the same basicType and refKind. jaroslav@1646: */ jaroslav@1646: private static LambdaForm preparedLambdaForm(MemberName m) { jaroslav@1646: assert(m.isInvocable()) : m; // call preparedFieldLambdaForm instead jaroslav@1646: MethodType mtype = m.getInvocationType().basicType(); jaroslav@1646: assert(!m.isMethodHandleInvoke() || "invokeBasic".equals(m.getName())) : m; jaroslav@1646: int which; jaroslav@1646: switch (m.getReferenceKind()) { jaroslav@1646: case REF_invokeVirtual: which = LF_INVVIRTUAL; break; jaroslav@1646: case REF_invokeStatic: which = LF_INVSTATIC; break; jaroslav@1646: case REF_invokeSpecial: which = LF_INVSPECIAL; break; jaroslav@1646: case REF_invokeInterface: which = LF_INVINTERFACE; break; jaroslav@1646: case REF_newInvokeSpecial: which = LF_NEWINVSPECIAL; break; jaroslav@1646: default: throw new InternalError(m.toString()); jaroslav@1646: } jaroslav@1646: if (which == LF_INVSTATIC && shouldBeInitialized(m)) { jaroslav@1646: // precompute the barrier-free version: jaroslav@1646: preparedLambdaForm(mtype, which); jaroslav@1646: which = LF_INVSTATIC_INIT; jaroslav@1646: } jaroslav@1646: LambdaForm lform = preparedLambdaForm(mtype, which); jaroslav@1646: maybeCompile(lform, m); jaroslav@1646: assert(lform.methodType().dropParameterTypes(0, 1) jaroslav@1646: .equals(m.getInvocationType().basicType())) jaroslav@1646: : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); jaroslav@1646: return lform; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static LambdaForm preparedLambdaForm(MethodType mtype, int which) { jaroslav@1646: LambdaForm lform = mtype.form().cachedLambdaForm(which); jaroslav@1646: if (lform != null) return lform; jaroslav@1646: lform = makePreparedLambdaForm(mtype, which); jaroslav@1646: return mtype.form().setCachedLambdaForm(which, lform); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static LambdaForm makePreparedLambdaForm(MethodType mtype, int which) { jaroslav@1646: boolean needsInit = (which == LF_INVSTATIC_INIT); jaroslav@1646: boolean doesAlloc = (which == LF_NEWINVSPECIAL); jaroslav@1646: String linkerName, lambdaName; jaroslav@1646: switch (which) { jaroslav@1646: case LF_INVVIRTUAL: linkerName = "linkToVirtual"; lambdaName = "DMH.invokeVirtual"; break; jaroslav@1646: case LF_INVSTATIC: linkerName = "linkToStatic"; lambdaName = "DMH.invokeStatic"; break; jaroslav@1646: case LF_INVSTATIC_INIT:linkerName = "linkToStatic"; lambdaName = "DMH.invokeStaticInit"; break; jaroslav@1646: case LF_INVSPECIAL: linkerName = "linkToSpecial"; lambdaName = "DMH.invokeSpecial"; break; jaroslav@1646: case LF_INVINTERFACE: linkerName = "linkToInterface"; lambdaName = "DMH.invokeInterface"; break; jaroslav@1646: case LF_NEWINVSPECIAL: linkerName = "linkToSpecial"; lambdaName = "DMH.newInvokeSpecial"; break; jaroslav@1646: default: throw new InternalError("which="+which); jaroslav@1646: } jaroslav@1646: MethodType mtypeWithArg = mtype.appendParameterTypes(MemberName.class); jaroslav@1646: if (doesAlloc) jaroslav@1646: mtypeWithArg = mtypeWithArg jaroslav@1646: .insertParameterTypes(0, Object.class) // insert newly allocated obj jaroslav@1646: .changeReturnType(void.class); // returns void jaroslav@1646: MemberName linker = new MemberName(MethodHandle.class, linkerName, mtypeWithArg, REF_invokeStatic); jaroslav@1646: try { jaroslav@1646: linker = IMPL_NAMES.resolveOrFail(REF_invokeStatic, linker, null, NoSuchMethodException.class); jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: final int DMH_THIS = 0; jaroslav@1646: final int ARG_BASE = 1; jaroslav@1646: final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); jaroslav@1646: int nameCursor = ARG_LIMIT; jaroslav@1646: final int NEW_OBJ = (doesAlloc ? nameCursor++ : -1); jaroslav@1646: final int GET_MEMBER = nameCursor++; jaroslav@1646: final int LINKER_CALL = nameCursor++; jaroslav@1646: Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); jaroslav@1646: assert(names.length == nameCursor); jaroslav@1646: if (doesAlloc) { jaroslav@1646: // names = { argx,y,z,... new C, init method } jaroslav@1646: names[NEW_OBJ] = new Name(Lazy.NF_allocateInstance, names[DMH_THIS]); jaroslav@1646: names[GET_MEMBER] = new Name(Lazy.NF_constructorMethod, names[DMH_THIS]); jaroslav@1646: } else if (needsInit) { jaroslav@1646: names[GET_MEMBER] = new Name(Lazy.NF_internalMemberNameEnsureInit, names[DMH_THIS]); jaroslav@1646: } else { jaroslav@1646: names[GET_MEMBER] = new Name(Lazy.NF_internalMemberName, names[DMH_THIS]); jaroslav@1646: } jaroslav@1646: Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, GET_MEMBER+1, Object[].class); jaroslav@1646: assert(outArgs[outArgs.length-1] == names[GET_MEMBER]); // look, shifted args! jaroslav@1646: int result = LambdaForm.LAST_RESULT; jaroslav@1646: if (doesAlloc) { jaroslav@1646: assert(outArgs[outArgs.length-2] == names[NEW_OBJ]); // got to move this one jaroslav@1646: System.arraycopy(outArgs, 0, outArgs, 1, outArgs.length-2); jaroslav@1646: outArgs[0] = names[NEW_OBJ]; jaroslav@1646: result = NEW_OBJ; jaroslav@1646: } jaroslav@1646: names[LINKER_CALL] = new Name(linker, outArgs); jaroslav@1646: lambdaName += "_" + LambdaForm.basicTypeSignature(mtype); jaroslav@1646: LambdaForm lform = new LambdaForm(lambdaName, ARG_LIMIT, names, result); jaroslav@1646: // This is a tricky bit of code. Don't send it through the LF interpreter. jaroslav@1646: lform.compileToBytecode(); jaroslav@1646: return lform; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static void maybeCompile(LambdaForm lform, MemberName m) { jaroslav@1646: if (VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class)) jaroslav@1646: // Help along bootstrapping... jaroslav@1646: lform.compileToBytecode(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** Static wrapper for DirectMethodHandle.internalMemberName. */ jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static Object internalMemberName(Object mh) { jaroslav@1646: return ((DirectMethodHandle)mh).member; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** Static wrapper for DirectMethodHandle.internalMemberName. jaroslav@1646: * This one also forces initialization. jaroslav@1646: */ jaroslav@1646: /*non-public*/ static Object internalMemberNameEnsureInit(Object mh) { jaroslav@1646: DirectMethodHandle dmh = (DirectMethodHandle)mh; jaroslav@1646: dmh.ensureInitialized(); jaroslav@1646: return dmh.member; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /*non-public*/ static jaroslav@1646: boolean shouldBeInitialized(MemberName member) { jaroslav@1646: switch (member.getReferenceKind()) { jaroslav@1646: case REF_invokeStatic: jaroslav@1646: case REF_getStatic: jaroslav@1646: case REF_putStatic: jaroslav@1646: case REF_newInvokeSpecial: jaroslav@1646: break; jaroslav@1646: default: jaroslav@1646: // No need to initialize the class on this kind of member. jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: Class cls = member.getDeclaringClass(); jaroslav@1646: if (cls == ValueConversions.class || jaroslav@1646: cls == MethodHandleImpl.class || jaroslav@1646: cls == Invokers.class) { jaroslav@1646: // These guys have lots of DMH creation but we know jaroslav@1646: // the MHs will not be used until the system is booted. jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: if (VerifyAccess.isSamePackage(MethodHandle.class, cls) || jaroslav@1646: VerifyAccess.isSamePackage(ValueConversions.class, cls)) { jaroslav@1646: // It is a system class. It is probably in the process of jaroslav@1646: // being initialized, but we will help it along just to be safe. jaroslav@1646: if (UNSAFE.shouldBeInitialized(cls)) { jaroslav@1646: UNSAFE.ensureClassInitialized(cls); jaroslav@1646: } jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: return UNSAFE.shouldBeInitialized(cls); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static class EnsureInitialized extends ClassValue> { jaroslav@1646: @Override jaroslav@1646: protected WeakReference computeValue(Class type) { jaroslav@1646: UNSAFE.ensureClassInitialized(type); jaroslav@1646: if (UNSAFE.shouldBeInitialized(type)) jaroslav@1646: // If the previous call didn't block, this can happen. jaroslav@1646: // We are executing inside . jaroslav@1646: return new WeakReference<>(Thread.currentThread()); jaroslav@1646: return null; jaroslav@1646: } jaroslav@1646: static final EnsureInitialized INSTANCE = new EnsureInitialized(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private void ensureInitialized() { jaroslav@1646: if (checkInitialized(member)) { jaroslav@1646: // The coast is clear. Delete the barrier. jaroslav@1646: if (member.isField()) jaroslav@1646: updateForm(preparedFieldLambdaForm(member)); jaroslav@1646: else jaroslav@1646: updateForm(preparedLambdaForm(member)); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: private static boolean checkInitialized(MemberName member) { jaroslav@1646: Class defc = member.getDeclaringClass(); jaroslav@1646: WeakReference ref = EnsureInitialized.INSTANCE.get(defc); jaroslav@1646: if (ref == null) { jaroslav@1646: return true; // the final state jaroslav@1646: } jaroslav@1646: Thread clinitThread = ref.get(); jaroslav@1646: // Somebody may still be running defc.. jaroslav@1646: if (clinitThread == Thread.currentThread()) { jaroslav@1646: // If anybody is running defc., it is this thread. jaroslav@1646: if (UNSAFE.shouldBeInitialized(defc)) jaroslav@1646: // Yes, we are running it; keep the barrier for now. jaroslav@1646: return false; jaroslav@1646: } else { jaroslav@1646: // We are in a random thread. Block. jaroslav@1646: UNSAFE.ensureClassInitialized(defc); jaroslav@1646: } jaroslav@1646: assert(!UNSAFE.shouldBeInitialized(defc)); jaroslav@1646: // put it into the final state jaroslav@1646: EnsureInitialized.INSTANCE.remove(defc); jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /*non-public*/ static void ensureInitialized(Object mh) { jaroslav@1646: ((DirectMethodHandle)mh).ensureInitialized(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** This subclass represents invokespecial instructions. */ jaroslav@1646: static class Special extends DirectMethodHandle { jaroslav@1646: private Special(MethodType mtype, LambdaForm form, MemberName member) { jaroslav@1646: super(mtype, form, member); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: boolean isInvokeSpecial() { jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: MethodHandle viewAsType(MethodType newType) { jaroslav@1646: return new Special(newType, form, member); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** This subclass handles constructor references. */ jaroslav@1646: static class Constructor extends DirectMethodHandle { jaroslav@1646: final MemberName initMethod; jaroslav@1646: final Class instanceClass; jaroslav@1646: jaroslav@1646: private Constructor(MethodType mtype, LambdaForm form, MemberName constructor, jaroslav@1646: MemberName initMethod, Class instanceClass) { jaroslav@1646: super(mtype, form, constructor); jaroslav@1646: this.initMethod = initMethod; jaroslav@1646: this.instanceClass = instanceClass; jaroslav@1646: assert(initMethod.isResolved()); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: MethodHandle viewAsType(MethodType newType) { jaroslav@1646: return new Constructor(newType, form, member, initMethod, instanceClass); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /*non-public*/ static Object constructorMethod(Object mh) { jaroslav@1646: Constructor dmh = (Constructor)mh; jaroslav@1646: return dmh.initMethod; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /*non-public*/ static Object allocateInstance(Object mh) throws InstantiationException { jaroslav@1646: Constructor dmh = (Constructor)mh; jaroslav@1646: return UNSAFE.allocateInstance(dmh.instanceClass); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** This subclass handles non-static field references. */ jaroslav@1646: static class Accessor extends DirectMethodHandle { jaroslav@1646: final Class fieldType; jaroslav@1646: final int fieldOffset; jaroslav@1646: private Accessor(MethodType mtype, LambdaForm form, MemberName member, jaroslav@1646: int fieldOffset) { jaroslav@1646: super(mtype, form, member); jaroslav@1646: this.fieldType = member.getFieldType(); jaroslav@1646: this.fieldOffset = fieldOffset; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override Object checkCast(Object obj) { jaroslav@1646: return fieldType.cast(obj); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: MethodHandle viewAsType(MethodType newType) { jaroslav@1646: return new Accessor(newType, form, member, fieldOffset); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static long fieldOffset(Object accessorObj) { jaroslav@1646: // Note: We return a long because that is what Unsafe.getObject likes. jaroslav@1646: // We store a plain int because it is more compact. jaroslav@1646: return ((Accessor)accessorObj).fieldOffset; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static Object checkBase(Object obj) { jaroslav@1646: // Note that the object's class has already been verified, jaroslav@1646: // since the parameter type of the Accessor method handle jaroslav@1646: // is either member.getDeclaringClass or a subclass. jaroslav@1646: // This was verified in DirectMethodHandle.make. jaroslav@1646: // Therefore, the only remaining check is for null. jaroslav@1646: // Since this check is *not* guaranteed by Unsafe.getInt jaroslav@1646: // and its siblings, we need to make an explicit one here. jaroslav@1646: obj.getClass(); // maybe throw NPE jaroslav@1646: return obj; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** This subclass handles static field references. */ jaroslav@1646: static class StaticAccessor extends DirectMethodHandle { jaroslav@1646: final private Class fieldType; jaroslav@1646: final private Object staticBase; jaroslav@1646: final private long staticOffset; jaroslav@1646: jaroslav@1646: private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member, jaroslav@1646: Object staticBase, long staticOffset) { jaroslav@1646: super(mtype, form, member); jaroslav@1646: this.fieldType = member.getFieldType(); jaroslav@1646: this.staticBase = staticBase; jaroslav@1646: this.staticOffset = staticOffset; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override Object checkCast(Object obj) { jaroslav@1646: return fieldType.cast(obj); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: MethodHandle viewAsType(MethodType newType) { jaroslav@1646: return new StaticAccessor(newType, form, member, staticBase, staticOffset); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static Object nullCheck(Object obj) { jaroslav@1646: obj.getClass(); jaroslav@1646: return obj; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static Object staticBase(Object accessorObj) { jaroslav@1646: return ((StaticAccessor)accessorObj).staticBase; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static long staticOffset(Object accessorObj) { jaroslav@1646: return ((StaticAccessor)accessorObj).staticOffset; jaroslav@1646: } jaroslav@1646: jaroslav@1646: @ForceInline jaroslav@1646: /*non-public*/ static Object checkCast(Object mh, Object obj) { jaroslav@1646: return ((DirectMethodHandle) mh).checkCast(obj); jaroslav@1646: } jaroslav@1646: jaroslav@1646: Object checkCast(Object obj) { jaroslav@1646: return member.getReturnType().cast(obj); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // Caching machinery for field accessors: jaroslav@1646: private static byte jaroslav@1646: AF_GETFIELD = 0, jaroslav@1646: AF_PUTFIELD = 1, jaroslav@1646: AF_GETSTATIC = 2, jaroslav@1646: AF_PUTSTATIC = 3, jaroslav@1646: AF_GETSTATIC_INIT = 4, jaroslav@1646: AF_PUTSTATIC_INIT = 5, jaroslav@1646: AF_LIMIT = 6; jaroslav@1646: // Enumerate the different field kinds using Wrapper, jaroslav@1646: // with an extra case added for checked references. jaroslav@1646: private static int jaroslav@1646: FT_LAST_WRAPPER = Wrapper.values().length-1, jaroslav@1646: FT_UNCHECKED_REF = Wrapper.OBJECT.ordinal(), jaroslav@1646: FT_CHECKED_REF = FT_LAST_WRAPPER+1, jaroslav@1646: FT_LIMIT = FT_LAST_WRAPPER+2; jaroslav@1646: private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) { jaroslav@1646: return ((formOp * FT_LIMIT * 2) jaroslav@1646: + (isVolatile ? FT_LIMIT : 0) jaroslav@1646: + ftypeKind); jaroslav@1646: } jaroslav@1646: private static final LambdaForm[] ACCESSOR_FORMS jaroslav@1646: = new LambdaForm[afIndex(AF_LIMIT, false, 0)]; jaroslav@1646: private static int ftypeKind(Class ftype) { jaroslav@1646: if (ftype.isPrimitive()) jaroslav@1646: return Wrapper.forPrimitiveType(ftype).ordinal(); jaroslav@1646: else if (VerifyType.isNullReferenceConversion(Object.class, ftype)) jaroslav@1646: return FT_UNCHECKED_REF; jaroslav@1646: else jaroslav@1646: return FT_CHECKED_REF; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Create a LF which can access the given field. jaroslav@1646: * Cache and share this structure among all fields with jaroslav@1646: * the same basicType and refKind. jaroslav@1646: */ jaroslav@1646: private static LambdaForm preparedFieldLambdaForm(MemberName m) { jaroslav@1646: Class ftype = m.getFieldType(); jaroslav@1646: boolean isVolatile = m.isVolatile(); jaroslav@1646: byte formOp; jaroslav@1646: switch (m.getReferenceKind()) { jaroslav@1646: case REF_getField: formOp = AF_GETFIELD; break; jaroslav@1646: case REF_putField: formOp = AF_PUTFIELD; break; jaroslav@1646: case REF_getStatic: formOp = AF_GETSTATIC; break; jaroslav@1646: case REF_putStatic: formOp = AF_PUTSTATIC; break; jaroslav@1646: default: throw new InternalError(m.toString()); jaroslav@1646: } jaroslav@1646: if (shouldBeInitialized(m)) { jaroslav@1646: // precompute the barrier-free version: jaroslav@1646: preparedFieldLambdaForm(formOp, isVolatile, ftype); jaroslav@1646: assert((AF_GETSTATIC_INIT - AF_GETSTATIC) == jaroslav@1646: (AF_PUTSTATIC_INIT - AF_PUTSTATIC)); jaroslav@1646: formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC); jaroslav@1646: } jaroslav@1646: LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype); jaroslav@1646: maybeCompile(lform, m); jaroslav@1646: assert(lform.methodType().dropParameterTypes(0, 1) jaroslav@1646: .equals(m.getInvocationType().basicType())) jaroslav@1646: : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); jaroslav@1646: return lform; jaroslav@1646: } jaroslav@1646: private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class ftype) { jaroslav@1646: int afIndex = afIndex(formOp, isVolatile, ftypeKind(ftype)); jaroslav@1646: LambdaForm lform = ACCESSOR_FORMS[afIndex]; jaroslav@1646: if (lform != null) return lform; jaroslav@1646: lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind(ftype)); jaroslav@1646: ACCESSOR_FORMS[afIndex] = lform; // don't bother with a CAS jaroslav@1646: return lform; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) { jaroslav@1646: boolean isGetter = (formOp & 1) == (AF_GETFIELD & 1); jaroslav@1646: boolean isStatic = (formOp >= AF_GETSTATIC); jaroslav@1646: boolean needsInit = (formOp >= AF_GETSTATIC_INIT); jaroslav@1646: boolean needsCast = (ftypeKind == FT_CHECKED_REF); jaroslav@1646: Wrapper fw = (needsCast ? Wrapper.OBJECT : Wrapper.values()[ftypeKind]); jaroslav@1646: Class ft = fw.primitiveType(); jaroslav@1646: assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind); jaroslav@1646: String tname = fw.primitiveSimpleName(); jaroslav@1646: String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1); jaroslav@1646: if (isVolatile) ctname += "Volatile"; jaroslav@1646: String getOrPut = (isGetter ? "get" : "put"); jaroslav@1646: String linkerName = (getOrPut + ctname); // getObject, putIntVolatile, etc. jaroslav@1646: MethodType linkerType; jaroslav@1646: if (isGetter) jaroslav@1646: linkerType = MethodType.methodType(ft, Object.class, long.class); jaroslav@1646: else jaroslav@1646: linkerType = MethodType.methodType(void.class, Object.class, long.class, ft); jaroslav@1646: MemberName linker = new MemberName(Unsafe.class, linkerName, linkerType, REF_invokeVirtual); jaroslav@1646: try { jaroslav@1646: linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class); jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // What is the external type of the lambda form? jaroslav@1646: MethodType mtype; jaroslav@1646: if (isGetter) jaroslav@1646: mtype = MethodType.methodType(ft); jaroslav@1646: else jaroslav@1646: mtype = MethodType.methodType(void.class, ft); jaroslav@1646: mtype = mtype.basicType(); // erase short to int, etc. jaroslav@1646: if (!isStatic) jaroslav@1646: mtype = mtype.insertParameterTypes(0, Object.class); jaroslav@1646: final int DMH_THIS = 0; jaroslav@1646: final int ARG_BASE = 1; jaroslav@1646: final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); jaroslav@1646: // if this is for non-static access, the base pointer is stored at this index: jaroslav@1646: final int OBJ_BASE = isStatic ? -1 : ARG_BASE; jaroslav@1646: // if this is for write access, the value to be written is stored at this index: jaroslav@1646: final int SET_VALUE = isGetter ? -1 : ARG_LIMIT - 1; jaroslav@1646: int nameCursor = ARG_LIMIT; jaroslav@1646: final int F_HOLDER = (isStatic ? nameCursor++ : -1); // static base if any jaroslav@1646: final int F_OFFSET = nameCursor++; // Either static offset or field offset. jaroslav@1646: final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1); jaroslav@1646: final int INIT_BAR = (needsInit ? nameCursor++ : -1); jaroslav@1646: final int PRE_CAST = (needsCast && !isGetter ? nameCursor++ : -1); jaroslav@1646: final int LINKER_CALL = nameCursor++; jaroslav@1646: final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1); jaroslav@1646: final int RESULT = nameCursor-1; // either the call or the cast jaroslav@1646: Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); jaroslav@1646: if (needsInit) jaroslav@1646: names[INIT_BAR] = new Name(Lazy.NF_ensureInitialized, names[DMH_THIS]); jaroslav@1646: if (needsCast && !isGetter) jaroslav@1646: names[PRE_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[SET_VALUE]); jaroslav@1646: Object[] outArgs = new Object[1 + linkerType.parameterCount()]; jaroslav@1646: assert(outArgs.length == (isGetter ? 3 : 4)); jaroslav@1646: outArgs[0] = UNSAFE; jaroslav@1646: if (isStatic) { jaroslav@1646: outArgs[1] = names[F_HOLDER] = new Name(Lazy.NF_staticBase, names[DMH_THIS]); jaroslav@1646: outArgs[2] = names[F_OFFSET] = new Name(Lazy.NF_staticOffset, names[DMH_THIS]); jaroslav@1646: } else { jaroslav@1646: outArgs[1] = names[OBJ_CHECK] = new Name(Lazy.NF_checkBase, names[OBJ_BASE]); jaroslav@1646: outArgs[2] = names[F_OFFSET] = new Name(Lazy.NF_fieldOffset, names[DMH_THIS]); jaroslav@1646: } jaroslav@1646: if (!isGetter) { jaroslav@1646: outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]); jaroslav@1646: } jaroslav@1646: for (Object a : outArgs) assert(a != null); jaroslav@1646: names[LINKER_CALL] = new Name(linker, outArgs); jaroslav@1646: if (needsCast && isGetter) jaroslav@1646: names[POST_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[LINKER_CALL]); jaroslav@1646: for (Name n : names) assert(n != null); jaroslav@1646: String fieldOrStatic = (isStatic ? "Static" : "Field"); jaroslav@1646: String lambdaName = (linkerName + fieldOrStatic); // significant only for debugging jaroslav@1646: if (needsCast) lambdaName += "Cast"; jaroslav@1646: if (needsInit) lambdaName += "Init"; jaroslav@1646: return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Pre-initialized NamedFunctions for bootstrapping purposes. jaroslav@1646: * Factored in an inner class to delay initialization until first usage. jaroslav@1646: */ jaroslav@1646: private static class Lazy { jaroslav@1646: static final NamedFunction jaroslav@1646: NF_internalMemberName, jaroslav@1646: NF_internalMemberNameEnsureInit, jaroslav@1646: NF_ensureInitialized, jaroslav@1646: NF_fieldOffset, jaroslav@1646: NF_checkBase, jaroslav@1646: NF_staticBase, jaroslav@1646: NF_staticOffset, jaroslav@1646: NF_checkCast, jaroslav@1646: NF_allocateInstance, jaroslav@1646: NF_constructorMethod; jaroslav@1646: static { jaroslav@1646: try { jaroslav@1646: NamedFunction nfs[] = { jaroslav@1646: NF_internalMemberName = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("internalMemberName", Object.class)), jaroslav@1646: NF_internalMemberNameEnsureInit = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("internalMemberNameEnsureInit", Object.class)), jaroslav@1646: NF_ensureInitialized = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("ensureInitialized", Object.class)), jaroslav@1646: NF_fieldOffset = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("fieldOffset", Object.class)), jaroslav@1646: NF_checkBase = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("checkBase", Object.class)), jaroslav@1646: NF_staticBase = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("staticBase", Object.class)), jaroslav@1646: NF_staticOffset = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("staticOffset", Object.class)), jaroslav@1646: NF_checkCast = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("checkCast", Object.class, Object.class)), jaroslav@1646: NF_allocateInstance = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("allocateInstance", Object.class)), jaroslav@1646: NF_constructorMethod = new NamedFunction(DirectMethodHandle.class jaroslav@1646: .getDeclaredMethod("constructorMethod", Object.class)) jaroslav@1646: }; jaroslav@1646: for (NamedFunction nf : nfs) { jaroslav@1646: // Each nf must be statically invocable or we get tied up in our bootstraps. jaroslav@1646: assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf.member)) : nf; jaroslav@1646: nf.resolve(); jaroslav@1646: } jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: } jaroslav@1646: }