1 /*
   2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.misc.Unsafe;
  29 import jdk.internal.vm.annotation.ForceInline;
  30 import jdk.internal.vm.annotation.Stable;
  31 import sun.invoke.util.ValueConversions;
  32 import sun.invoke.util.VerifyAccess;
  33 import sun.invoke.util.VerifyType;
  34 import sun.invoke.util.Wrapper;
  35 
  36 import java.lang.ref.WeakReference;
  37 import java.util.Arrays;
  38 import java.util.Objects;
  39 
  40 import static java.lang.invoke.LambdaForm.*;
  41 import static java.lang.invoke.LambdaForm.Kind.*;
  42 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  43 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  44 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  45 import static java.lang.invoke.MethodTypeForm.*;
  46 
  47 /**
  48  * The flavor of method handle which implements a constant reference
  49  * to a class member.
  50  * @author jrose
  51  */
  52 class DirectMethodHandle extends MethodHandle {
  53     final MemberName member;
  54 
  55     // Constructors and factory methods in this class *must* be package scoped or private.
  56     private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member) {
  57         super(mtype, form);
  58         if (!member.isResolved())  throw new InternalError();
  59 
  60         if (member.getDeclaringClass().isInterface() &&
  61             member.getReferenceKind() == REF_invokeInterface &&
  62             member.isMethod() && !member.isAbstract()) {
  63             // Check for corner case: invokeinterface of Object method
  64             MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind());
  65             m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null);
  66             if (m != null && m.isPublic()) {
  67                 assert(member.getReferenceKind() == m.getReferenceKind());  // else this.form is wrong
  68                 member = m;
  69             }
  70         }
  71 
  72         this.member = member;
  73     }
  74 
  75     // Factory methods:
  76     static DirectMethodHandle make(byte refKind, Class<?> refc, MemberName member, Class<?> callerClass) {
  77         MethodType mtype = member.getMethodOrFieldType();
  78         if (!member.isStatic()) {
  79             if (!member.getDeclaringClass().isAssignableFrom(refc) || member.isObjectConstructor())
  80                 throw new InternalError(member.toString());
  81             mtype = mtype.insertParameterTypes(0, refc.asPrimaryType());
  82         }
  83         if (!member.isField()) {
  84             // refKind reflects the original type of lookup via findSpecial or
  85             // findVirtual etc.
  86             switch (refKind) {
  87                 case REF_invokeSpecial: {
  88                     member = member.asSpecial();
  89                     // if caller is an interface we need to adapt to get the
  90                     // receiver check inserted
  91                     if (callerClass == null) {
  92                         throw new InternalError("callerClass must not be null for REF_invokeSpecial");
  93                     }
  94                     LambdaForm lform = preparedLambdaForm(member, callerClass.isInterface());
  95                     return new Special(mtype, lform, member, callerClass);
  96                 }
  97                 case REF_invokeInterface: {
  98                     // for interfaces we always need the receiver typecheck,
  99                     // so we always pass 'true' to ensure we adapt if needed
 100                     // to include the REF_invokeSpecial case
 101                     LambdaForm lform = preparedLambdaForm(member, true);
 102                     return new Interface(mtype, lform, member, refc);
 103                 }
 104                 default: {
 105                     LambdaForm lform = preparedLambdaForm(member);
 106                     return new DirectMethodHandle(mtype, lform, member);
 107                 }
 108             }
 109         } else {
 110             LambdaForm lform = preparedFieldLambdaForm(member);
 111             if (member.isStatic()) {
 112                 long offset = MethodHandleNatives.staticFieldOffset(member);
 113                 Object base = MethodHandleNatives.staticFieldBase(member);
 114                 return member.isIndirect() ? new IndirectStaticAccessor(mtype, lform, member, base, offset)
 115                                            : new InlineStaticAccessor(mtype, lform, member, base, offset);
 116             } else {
 117                 long offset = MethodHandleNatives.objectFieldOffset(member);
 118                 assert(offset == (int)offset);
 119                 return  member.isIndirect() ? new IndirectAccessor(mtype, lform, member, (int)offset)
 120                                             : new InlineAccessor(mtype, lform, member, (int)offset);
 121             }
 122         }
 123     }
 124     static DirectMethodHandle make(Class<?> refc, MemberName member) {
 125         byte refKind = member.getReferenceKind();
 126         if (refKind == REF_invokeSpecial)
 127             refKind =  REF_invokeVirtual;
 128         return make(refKind, refc, member, null /* no callerClass context */);
 129     }
 130     static DirectMethodHandle make(MemberName member) {
 131         if (member.isObjectConstructor() && member.getReturnType() == void.class)
 132             return makeAllocator(member);
 133         return make(member.getDeclaringClass(), member);
 134     }
 135     private static DirectMethodHandle makeAllocator(MemberName ctor) {
 136         assert(ctor.isObjectConstructor() && !ctor.getDeclaringClass().isInlineClass()) : ctor;
 137 
 138         Class<?> instanceClass = ctor.getDeclaringClass();
 139         ctor = ctor.asObjectConstructor();
 140         assert(ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
 141         MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
 142         LambdaForm lform = preparedLambdaForm(ctor);
 143         MemberName init = ctor.asSpecial();
 144         assert(init.getMethodType().returnType() == void.class);
 145         return new Constructor(mtype, lform, ctor, init, instanceClass);
 146     }
 147 
 148     @Override
 149     BoundMethodHandle rebind() {
 150         return BoundMethodHandle.makeReinvoker(this);
 151     }
 152 
 153     @Override
 154     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 155         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
 156         return new DirectMethodHandle(mt, lf, member);
 157     }
 158 
 159     @Override
 160     String internalProperties() {
 161         return "\n& DMH.MN="+internalMemberName();
 162     }
 163 
 164     //// Implementation methods.
 165     @Override
 166     @ForceInline
 167     MemberName internalMemberName() {
 168         return member;
 169     }
 170 
 171     private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
 172 
 173     /**
 174      * Create a LF which can invoke the given method.
 175      * Cache and share this structure among all methods with
 176      * the same basicType and refKind.
 177      */
 178     private static LambdaForm preparedLambdaForm(MemberName m, boolean adaptToSpecialIfc) {
 179         assert(m.isInvocable()) : m;  // call preparedFieldLambdaForm instead
 180         MethodType mtype = m.getInvocationType().basicType();
 181         assert(!m.isMethodHandleInvoke()) : m;
 182         int which;
 183         // MemberName.getReferenceKind represents the JVM optimized form of the call
 184         // as distinct from the "kind" passed to DMH.make which represents the original
 185         // bytecode-equivalent request. Specifically private/final methods that use a direct
 186         // call have getReferenceKind adapted to REF_invokeSpecial, even though the actual
 187         // invocation mode may be invokevirtual or invokeinterface.
 188         switch (m.getReferenceKind()) {
 189         case REF_invokeVirtual:    which = LF_INVVIRTUAL;    break;
 190         case REF_invokeStatic:     which = LF_INVSTATIC;     break;
 191         case REF_invokeSpecial:    which = LF_INVSPECIAL;    break;
 192         case REF_invokeInterface:  which = LF_INVINTERFACE;  break;
 193         case REF_newInvokeSpecial: which = LF_NEWINVSPECIAL; break;
 194         default:  throw new InternalError(m.toString());
 195         }
 196         if (which == LF_INVSTATIC && shouldBeInitialized(m)) {
 197             // precompute the barrier-free version:
 198             preparedLambdaForm(mtype, which);
 199             which = LF_INVSTATIC_INIT;
 200         }
 201         if (which == LF_INVSPECIAL && adaptToSpecialIfc) {
 202             which = LF_INVSPECIAL_IFC;
 203         }
 204         LambdaForm lform = preparedLambdaForm(mtype, which);
 205         maybeCompile(lform, m);
 206         assert(lform.methodType().dropParameterTypes(0, 1)
 207                 .equals(m.getInvocationType().basicType()))
 208                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
 209         return lform;
 210     }
 211 
 212     private static LambdaForm preparedLambdaForm(MemberName m) {
 213         return preparedLambdaForm(m, false);
 214     }
 215 
 216     private static LambdaForm preparedLambdaForm(MethodType mtype, int which) {
 217         LambdaForm lform = mtype.form().cachedLambdaForm(which);
 218         if (lform != null)  return lform;
 219         lform = makePreparedLambdaForm(mtype, which);
 220         return mtype.form().setCachedLambdaForm(which, lform);
 221     }
 222 
 223     static LambdaForm makePreparedLambdaForm(MethodType mtype, int which) {
 224         boolean needsInit = (which == LF_INVSTATIC_INIT);
 225         boolean doesAlloc = (which == LF_NEWINVSPECIAL);
 226         boolean needsReceiverCheck = (which == LF_INVINTERFACE ||
 227                                       which == LF_INVSPECIAL_IFC);
 228 
 229         String linkerName;
 230         LambdaForm.Kind kind;
 231         switch (which) {
 232         case LF_INVVIRTUAL:    linkerName = "linkToVirtual";   kind = DIRECT_INVOKE_VIRTUAL;     break;
 233         case LF_INVSTATIC:     linkerName = "linkToStatic";    kind = DIRECT_INVOKE_STATIC;      break;
 234         case LF_INVSTATIC_INIT:linkerName = "linkToStatic";    kind = DIRECT_INVOKE_STATIC_INIT; break;
 235         case LF_INVSPECIAL_IFC:linkerName = "linkToSpecial";   kind = DIRECT_INVOKE_SPECIAL_IFC; break;
 236         case LF_INVSPECIAL:    linkerName = "linkToSpecial";   kind = DIRECT_INVOKE_SPECIAL;     break;
 237         case LF_INVINTERFACE:  linkerName = "linkToInterface"; kind = DIRECT_INVOKE_INTERFACE;   break;
 238         case LF_NEWINVSPECIAL: linkerName = "linkToSpecial";   kind = DIRECT_NEW_INVOKE_SPECIAL; break;
 239         default:  throw new InternalError("which="+which);
 240         }
 241 
 242         MethodType mtypeWithArg = mtype.appendParameterTypes(MemberName.class);
 243         if (doesAlloc)
 244             mtypeWithArg = mtypeWithArg
 245                     .insertParameterTypes(0, Object.class)  // insert newly allocated obj
 246                     .changeReturnType(void.class);          // <init> returns void
 247         MemberName linker = new MemberName(MethodHandle.class, linkerName, mtypeWithArg, REF_invokeStatic);
 248         try {
 249             linker = IMPL_NAMES.resolveOrFail(REF_invokeStatic, linker, null, NoSuchMethodException.class);
 250         } catch (ReflectiveOperationException ex) {
 251             throw newInternalError(ex);
 252         }
 253         final int DMH_THIS    = 0;
 254         final int ARG_BASE    = 1;
 255         final int ARG_LIMIT   = ARG_BASE + mtype.parameterCount();
 256         int nameCursor = ARG_LIMIT;
 257         final int NEW_OBJ     = (doesAlloc ? nameCursor++ : -1);
 258         final int GET_MEMBER  = nameCursor++;
 259         final int CHECK_RECEIVER = (needsReceiverCheck ? nameCursor++ : -1);
 260         final int LINKER_CALL = nameCursor++;
 261         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 262         assert(names.length == nameCursor);
 263         if (doesAlloc) {
 264             // names = { argx,y,z,... new C, init method }
 265             names[NEW_OBJ] = new Name(getFunction(NF_allocateInstance), names[DMH_THIS]);
 266             names[GET_MEMBER] = new Name(getFunction(NF_constructorMethod), names[DMH_THIS]);
 267         } else if (needsInit) {
 268             names[GET_MEMBER] = new Name(getFunction(NF_internalMemberNameEnsureInit), names[DMH_THIS]);
 269         } else {
 270             names[GET_MEMBER] = new Name(getFunction(NF_internalMemberName), names[DMH_THIS]);
 271         }
 272         assert(findDirectMethodHandle(names[GET_MEMBER]) == names[DMH_THIS]);
 273         Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, GET_MEMBER+1, Object[].class);
 274         if (needsReceiverCheck) {
 275             names[CHECK_RECEIVER] = new Name(getFunction(NF_checkReceiver), names[DMH_THIS], names[ARG_BASE]);
 276             outArgs[0] = names[CHECK_RECEIVER];
 277         }
 278         assert(outArgs[outArgs.length-1] == names[GET_MEMBER]);  // look, shifted args!
 279         int result = LAST_RESULT;
 280         if (doesAlloc) {
 281             assert(outArgs[outArgs.length-2] == names[NEW_OBJ]);  // got to move this one
 282             System.arraycopy(outArgs, 0, outArgs, 1, outArgs.length-2);
 283             outArgs[0] = names[NEW_OBJ];
 284             result = NEW_OBJ;
 285         }
 286         names[LINKER_CALL] = new Name(linker, outArgs);
 287         LambdaForm lform = new LambdaForm(ARG_LIMIT, names, result, kind);
 288 
 289         // This is a tricky bit of code.  Don't send it through the LF interpreter.
 290         lform.compileToBytecode();
 291         return lform;
 292     }
 293 
 294     /* assert */ static Object findDirectMethodHandle(Name name) {
 295         if (name.function.equals(getFunction(NF_internalMemberName)) ||
 296             name.function.equals(getFunction(NF_internalMemberNameEnsureInit)) ||
 297             name.function.equals(getFunction(NF_constructorMethod))) {
 298             assert(name.arguments.length == 1);
 299             return name.arguments[0];
 300         }
 301         return null;
 302     }
 303 
 304     private static void maybeCompile(LambdaForm lform, MemberName m) {
 305         if (lform.vmentry == null && VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class))
 306             // Help along bootstrapping...
 307             lform.compileToBytecode();
 308     }
 309 
 310     /** Static wrapper for DirectMethodHandle.internalMemberName. */
 311     @ForceInline
 312     /*non-public*/ static Object internalMemberName(Object mh) {
 313         return ((DirectMethodHandle)mh).member;
 314     }
 315 
 316     /** Static wrapper for DirectMethodHandle.internalMemberName.
 317      * This one also forces initialization.
 318      */
 319     /*non-public*/ static Object internalMemberNameEnsureInit(Object mh) {
 320         DirectMethodHandle dmh = (DirectMethodHandle)mh;
 321         dmh.ensureInitialized();
 322         return dmh.member;
 323     }
 324 
 325     /*non-public*/ static
 326     boolean shouldBeInitialized(MemberName member) {
 327         switch (member.getReferenceKind()) {
 328         case REF_invokeStatic:
 329         case REF_getStatic:
 330         case REF_putStatic:
 331         case REF_newInvokeSpecial:
 332             break;
 333         default:
 334             // No need to initialize the class on this kind of member.
 335             return false;
 336         }
 337         Class<?> cls = member.getDeclaringClass();
 338         if (cls == ValueConversions.class ||
 339             cls == MethodHandleImpl.class ||
 340             cls == Invokers.class) {
 341             // These guys have lots of <clinit> DMH creation but we know
 342             // the MHs will not be used until the system is booted.
 343             return false;
 344         }
 345         if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
 346             VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
 347             // It is a system class.  It is probably in the process of
 348             // being initialized, but we will help it along just to be safe.
 349             if (UNSAFE.shouldBeInitialized(cls)) {
 350                 UNSAFE.ensureClassInitialized(cls);
 351             }
 352             return false;
 353         }
 354         return UNSAFE.shouldBeInitialized(cls);
 355     }
 356 
 357     private static class EnsureInitialized extends ClassValue<WeakReference<Thread>> {
 358         @Override
 359         protected WeakReference<Thread> computeValue(Class<?> type) {
 360             UNSAFE.ensureClassInitialized(type);
 361             if (UNSAFE.shouldBeInitialized(type))
 362                 // If the previous call didn't block, this can happen.
 363                 // We are executing inside <clinit>.
 364                 return new WeakReference<>(Thread.currentThread());
 365             return null;
 366         }
 367         static final EnsureInitialized INSTANCE = new EnsureInitialized();
 368     }
 369 
 370     private void ensureInitialized() {
 371         if (checkInitialized(member)) {
 372             // The coast is clear.  Delete the <clinit> barrier.
 373             if (member.isField())
 374                 updateForm(preparedFieldLambdaForm(member));
 375             else
 376                 updateForm(preparedLambdaForm(member));
 377         }
 378     }
 379     private static boolean checkInitialized(MemberName member) {
 380         Class<?> defc = member.getDeclaringClass();
 381         WeakReference<Thread> ref = EnsureInitialized.INSTANCE.get(defc);
 382         if (ref == null) {
 383             return true;  // the final state
 384         }
 385         Thread clinitThread = ref.get();
 386         // Somebody may still be running defc.<clinit>.
 387         if (clinitThread == Thread.currentThread()) {
 388             // If anybody is running defc.<clinit>, it is this thread.
 389             if (UNSAFE.shouldBeInitialized(defc))
 390                 // Yes, we are running it; keep the barrier for now.
 391                 return false;
 392         } else {
 393             // We are in a random thread.  Block.
 394             UNSAFE.ensureClassInitialized(defc);
 395         }
 396         assert(!UNSAFE.shouldBeInitialized(defc));
 397         // put it into the final state
 398         EnsureInitialized.INSTANCE.remove(defc);
 399         return true;
 400     }
 401 
 402     /*non-public*/ static void ensureInitialized(Object mh) {
 403         ((DirectMethodHandle)mh).ensureInitialized();
 404     }
 405 
 406     /** This subclass represents invokespecial instructions. */
 407     static class Special extends DirectMethodHandle {
 408         private final Class<?> caller;
 409         private Special(MethodType mtype, LambdaForm form, MemberName member, Class<?> caller) {
 410             super(mtype, form, member);
 411             this.caller = caller;
 412         }
 413         @Override
 414         boolean isInvokeSpecial() {
 415             return true;
 416         }
 417         @Override
 418         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 419             return new Special(mt, lf, member, caller);
 420         }
 421         Object checkReceiver(Object recv) {
 422             if (!caller.isInstance(recv)) {
 423                 String msg = String.format("Receiver class %s is not a subclass of caller class %s",
 424                                            recv.getClass().getName(), caller.getName());
 425                 throw new IncompatibleClassChangeError(msg);
 426             }
 427             return recv;
 428         }
 429     }
 430 
 431     /** This subclass represents invokeinterface instructions. */
 432     static class Interface extends DirectMethodHandle {
 433         private final Class<?> refc;
 434         private Interface(MethodType mtype, LambdaForm form, MemberName member, Class<?> refc) {
 435             super(mtype, form, member);
 436             assert refc.isInterface() : refc;
 437             this.refc = refc;
 438         }
 439         @Override
 440         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 441             return new Interface(mt, lf, member, refc);
 442         }
 443         @Override
 444         Object checkReceiver(Object recv) {
 445             if (!refc.isInstance(recv)) {
 446                 String msg = String.format("Receiver class %s does not implement the requested interface %s",
 447                                            recv.getClass().getName(), refc.getName());
 448                 throw new IncompatibleClassChangeError(msg);
 449             }
 450             return recv;
 451         }
 452     }
 453 
 454     /** Used for interface receiver type checks, by Interface and Special modes. */
 455     Object checkReceiver(Object recv) {
 456         throw new InternalError("Should only be invoked on a subclass");
 457     }
 458 
 459 
 460     /** This subclass handles constructor references. */
 461     static class Constructor extends DirectMethodHandle {
 462         final MemberName initMethod;
 463         final Class<?>   instanceClass;
 464 
 465         private Constructor(MethodType mtype, LambdaForm form, MemberName constructor,
 466                             MemberName initMethod, Class<?> instanceClass) {
 467             super(mtype, form, constructor);
 468             this.initMethod = initMethod;
 469             this.instanceClass = instanceClass;
 470             assert(initMethod.isResolved());
 471         }
 472         @Override
 473         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 474             return new Constructor(mt, lf, member, initMethod, instanceClass);
 475         }
 476     }
 477 
 478     /*non-public*/ static Object constructorMethod(Object mh) {
 479         Constructor dmh = (Constructor)mh;
 480         return dmh.initMethod;
 481     }
 482 
 483     /*non-public*/ static Object allocateInstance(Object mh) throws InstantiationException {
 484         Constructor dmh = (Constructor)mh;
 485         return UNSAFE.allocateInstance(dmh.instanceClass);
 486     }
 487 
 488     /** This subclass handles non-static field references. */
 489     static abstract class Accessor extends DirectMethodHandle {
 490         final Class<?> fieldType;
 491         final int fieldOffset;
 492 
 493         protected Accessor(MethodType mtype, LambdaForm form, MemberName member,
 494                            int fieldOffset) {
 495             super(mtype, form, member);
 496             this.fieldType = member.getFieldType();
 497             this.fieldOffset = fieldOffset;
 498         }
 499         abstract Object checkCast(Object obj);
 500         abstract MethodHandle copyWith(MethodType mt, LambdaForm lf);
 501     }
 502 
 503     /** This subclass handles non-static field references of indirect type */
 504     static class IndirectAccessor extends Accessor {
 505         private IndirectAccessor(MethodType mtype, LambdaForm form, MemberName member,
 506                                  int fieldOffset) {
 507             super(mtype, form, member, fieldOffset);
 508         }
 509 
 510         @Override Object checkCast(Object obj) {
 511             return fieldType.cast(obj);
 512         }
 513         @Override
 514         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 515             return new IndirectAccessor(mt, lf, member, fieldOffset);
 516         }
 517     }
 518 
 519     /** This subclass handles non-static field references of inline type */
 520     static class InlineAccessor extends Accessor {
 521         private InlineAccessor(MethodType mtype, LambdaForm form, MemberName member,
 522                                int fieldOffset) {
 523             super(mtype, form, member, fieldOffset);
 524         }
 525 
 526         @Override Object checkCast(Object obj) {
 527             return fieldType.cast(Objects.requireNonNull(obj));
 528         }
 529         @Override
 530         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 531             return new InlineAccessor(mt, lf, member, fieldOffset);
 532         }
 533     }
 534 
 535     @ForceInline
 536     /*non-public*/ static long fieldOffset(Object accessorObj) {
 537         // Note: We return a long because that is what Unsafe.getObject likes.
 538         // We store a plain int because it is more compact.
 539         return ((Accessor)accessorObj).fieldOffset;
 540     }
 541 
 542     @ForceInline
 543     /*non-public*/ static Object checkBase(Object obj) {
 544         // Note that the object's class has already been verified,
 545         // since the parameter type of the Accessor method handle
 546         // is either member.getDeclaringClass or a subclass.
 547         // This was verified in DirectMethodHandle.make.
 548         // Therefore, the only remaining check is for null.
 549         // Since this check is *not* guaranteed by Unsafe.getInt
 550         // and its siblings, we need to make an explicit one here.
 551         return Objects.requireNonNull(obj);
 552     }
 553 
 554     static abstract class StaticAccessor extends DirectMethodHandle {
 555         final Class<?> fieldType;
 556         final Object staticBase;
 557         final long staticOffset;
 558 
 559         protected StaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 560                                  Object staticBase, long staticOffset) {
 561             super(mtype, form, member);
 562             this.fieldType = member.getFieldType();
 563             this.staticBase = staticBase;
 564             this.staticOffset = staticOffset;
 565         }
 566         abstract Object checkCast(Object obj);
 567         abstract MethodHandle copyWith(MethodType mt, LambdaForm lf);
 568     }
 569 
 570     /** This subclass handles static field references of indirect type. */
 571     static class IndirectStaticAccessor extends StaticAccessor {
 572         private IndirectStaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 573                                      Object staticBase, long staticOffset) {
 574             super(mtype, form, member, staticBase, staticOffset);
 575         }
 576 
 577         // indirect type is always nullable
 578         @Override Object checkCast(Object obj) {
 579             return fieldType.cast(obj);
 580         }
 581         @Override
 582         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 583             return new IndirectStaticAccessor(mt, lf, member, staticBase, staticOffset);
 584         }
 585     }
 586 
 587     /** This subclass handles static field references of inline type . */
 588     static class InlineStaticAccessor extends StaticAccessor {
 589         private InlineStaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 590                                      Object staticBase, long staticOffset) {
 591             super(mtype, form, member, staticBase, staticOffset);
 592         }
 593 
 594         // zero-default inline type is not-nullable
 595         @Override Object checkCast(Object obj) {
 596             assert !fieldType.isNullableType() : "null-default inline type not yet supported";
 597             return fieldType.cast(Objects.requireNonNull(obj));
 598         }
 599         @Override
 600         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 601             return new InlineStaticAccessor(mt, lf, member, staticBase, staticOffset);
 602         }
 603     }
 604 
 605     @ForceInline
 606     /*non-public*/ static Object nullCheck(Object obj) {
 607         return Objects.requireNonNull(obj);
 608     }
 609 
 610     @ForceInline
 611     /*non-public*/ static Object staticBase(Object accessorObj) {
 612         return ((StaticAccessor)accessorObj).staticBase;
 613     }
 614 
 615     @ForceInline
 616     /*non-public*/ static long staticOffset(Object accessorObj) {
 617         return ((StaticAccessor)accessorObj).staticOffset;
 618     }
 619 
 620     @ForceInline
 621     /*non-public*/ static Object checkCast(Object mh, Object obj) {
 622         return ((DirectMethodHandle) mh).checkCast(obj);
 623     }
 624 
 625     @ForceInline
 626     /*non-public*/ static Class<?> fieldType(Object accessorObj) {
 627         return ((Accessor) accessorObj).fieldType;
 628     }
 629 
 630     @ForceInline
 631     /*non-public*/ static Class<?> staticFieldType(Object accessorObj) {
 632         return ((StaticAccessor) accessorObj).fieldType;
 633     }
 634 
 635     Object checkCast(Object obj) {
 636         return member.getReturnType().cast(obj);
 637     }
 638 
 639     // Caching machinery for field accessors:
 640     static final byte
 641             AF_GETFIELD        = 0,
 642             AF_PUTFIELD        = 1,
 643             AF_GETSTATIC       = 2,
 644             AF_PUTSTATIC       = 3,
 645             AF_GETSTATIC_INIT  = 4,
 646             AF_PUTSTATIC_INIT  = 5,
 647             AF_LIMIT           = 6;
 648     // Enumerate the different field kinds using Wrapper,
 649     // with an extra case added for checked references and value field access
 650     static final int
 651             FT_LAST_WRAPPER     = Wrapper.COUNT-1,
 652             FT_UNCHECKED_REF    = Wrapper.OBJECT.ordinal(),
 653             FT_CHECKED_REF      = FT_LAST_WRAPPER+1,
 654             FT_CHECKED_VALUE    = FT_LAST_WRAPPER+2,  // flattened and non-flattened
 655             FT_LIMIT            = FT_LAST_WRAPPER+4;
 656     private static int afIndex(byte formOp, boolean isVolatile, boolean isFlatValue, int ftypeKind) {
 657         return ((formOp * FT_LIMIT * 2)
 658                 + (isVolatile ? FT_LIMIT : 0)
 659                 + (isFlatValue ? 1 : 0)
 660                 + ftypeKind);
 661     }
 662     @Stable
 663     private static final LambdaForm[] ACCESSOR_FORMS
 664             = new LambdaForm[afIndex(AF_LIMIT, false, false, 0)];
 665     static int ftypeKind(Class<?> ftype, boolean isValue) {
 666         if (ftype.isPrimitive())
 667             return Wrapper.forPrimitiveType(ftype).ordinal();
 668         else if (VerifyType.isNullReferenceConversion(Object.class, ftype)) {
 669             return FT_UNCHECKED_REF;
 670         } else
 671             // null check for value type in addition to check cast
 672             return isValue ? FT_CHECKED_VALUE : FT_CHECKED_REF;
 673     }
 674 
 675     /**
 676      * Create a LF which can access the given field.
 677      * Cache and share this structure among all fields with
 678      * the same basicType and refKind.
 679      */
 680     private static LambdaForm preparedFieldLambdaForm(MemberName m) {
 681         Class<?> ftype = m.getFieldType();
 682         byte formOp;
 683         switch (m.getReferenceKind()) {
 684         case REF_getField:      formOp = AF_GETFIELD;    break;
 685         case REF_putField:      formOp = AF_PUTFIELD;    break;
 686         case REF_getStatic:     formOp = AF_GETSTATIC;   break;
 687         case REF_putStatic:     formOp = AF_PUTSTATIC;   break;
 688         default:  throw new InternalError(m.toString());
 689         }
 690         if (shouldBeInitialized(m)) {
 691             // precompute the barrier-free version:
 692             preparedFieldLambdaForm(formOp, m.isVolatile(), m.isInlineable(), m.isFlattened(), ftype);
 693             assert((AF_GETSTATIC_INIT - AF_GETSTATIC) ==
 694                    (AF_PUTSTATIC_INIT - AF_PUTSTATIC));
 695             formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC);
 696         }
 697         LambdaForm lform = preparedFieldLambdaForm(formOp, m.isVolatile(), m.isInlineable(), m.isFlattened(), ftype);
 698         maybeCompile(lform, m);
 699         assert(lform.methodType().dropParameterTypes(0, 1)
 700                 .equals(m.getInvocationType().basicType()))
 701                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
 702         return lform;
 703     }
 704 
 705     private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, boolean isValue, boolean isFlatValue, Class<?> ftype) {
 706         int ftypeKind = ftypeKind(ftype, isValue);
 707         int afIndex = afIndex(formOp, isVolatile, isFlatValue, ftypeKind);
 708         LambdaForm lform = ACCESSOR_FORMS[afIndex];
 709         if (lform != null)  return lform;
 710         lform = makePreparedFieldLambdaForm(formOp, isVolatile, isValue, isFlatValue, ftypeKind);
 711         ACCESSOR_FORMS[afIndex] = lform;  // don't bother with a CAS
 712         return lform;
 713     }
 714 
 715     private static final Wrapper[] ALL_WRAPPERS = Wrapper.values();
 716 
 717     private static Kind getFieldKind(boolean isGetter, boolean isVolatile, boolean isFlatValue, Wrapper wrapper) {
 718         if (isGetter) {
 719             if (isVolatile) {
 720                 switch (wrapper) {
 721                     case BOOLEAN: return GET_BOOLEAN_VOLATILE;
 722                     case BYTE:    return GET_BYTE_VOLATILE;
 723                     case SHORT:   return GET_SHORT_VOLATILE;
 724                     case CHAR:    return GET_CHAR_VOLATILE;
 725                     case INT:     return GET_INT_VOLATILE;
 726                     case LONG:    return GET_LONG_VOLATILE;
 727                     case FLOAT:   return GET_FLOAT_VOLATILE;
 728                     case DOUBLE:  return GET_DOUBLE_VOLATILE;
 729                     case OBJECT:  return isFlatValue ? GET_VALUE_VOLATILE : GET_REFERENCE_VOLATILE;
 730                 }
 731             } else {
 732                 switch (wrapper) {
 733                     case BOOLEAN: return GET_BOOLEAN;
 734                     case BYTE:    return GET_BYTE;
 735                     case SHORT:   return GET_SHORT;
 736                     case CHAR:    return GET_CHAR;
 737                     case INT:     return GET_INT;
 738                     case LONG:    return GET_LONG;
 739                     case FLOAT:   return GET_FLOAT;
 740                     case DOUBLE:  return GET_DOUBLE;
 741                     case OBJECT:  return isFlatValue ? GET_VALUE : GET_REFERENCE;
 742                 }
 743             }
 744         } else {
 745             if (isVolatile) {
 746                 switch (wrapper) {
 747                     case BOOLEAN: return PUT_BOOLEAN_VOLATILE;
 748                     case BYTE:    return PUT_BYTE_VOLATILE;
 749                     case SHORT:   return PUT_SHORT_VOLATILE;
 750                     case CHAR:    return PUT_CHAR_VOLATILE;
 751                     case INT:     return PUT_INT_VOLATILE;
 752                     case LONG:    return PUT_LONG_VOLATILE;
 753                     case FLOAT:   return PUT_FLOAT_VOLATILE;
 754                     case DOUBLE:  return PUT_DOUBLE_VOLATILE;
 755                     case OBJECT:  return isFlatValue ? PUT_VALUE_VOLATILE : PUT_REFERENCE_VOLATILE;
 756                 }
 757             } else {
 758                 switch (wrapper) {
 759                     case BOOLEAN: return PUT_BOOLEAN;
 760                     case BYTE:    return PUT_BYTE;
 761                     case SHORT:   return PUT_SHORT;
 762                     case CHAR:    return PUT_CHAR;
 763                     case INT:     return PUT_INT;
 764                     case LONG:    return PUT_LONG;
 765                     case FLOAT:   return PUT_FLOAT;
 766                     case DOUBLE:  return PUT_DOUBLE;
 767                     case OBJECT:  return isFlatValue ? PUT_VALUE : PUT_REFERENCE;
 768                 }
 769             }
 770         }
 771         throw new AssertionError("Invalid arguments");
 772     }
 773 
 774     /** invoked by GenerateJLIClassesHelper */
 775     static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftype) {
 776         return makePreparedFieldLambdaForm(formOp, isVolatile, false, false, ftype);
 777     }
 778 
 779     private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, boolean isValue, boolean isFlatValue, int ftypeKind) {
 780         boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
 781         boolean isStatic  = (formOp >= AF_GETSTATIC);
 782         boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
 783         boolean needsCast = (ftypeKind == FT_CHECKED_REF || ftypeKind == FT_CHECKED_VALUE);
 784         Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]);
 785         Class<?> ft = fw.primitiveType();
 786         assert(needsCast ? true : ftypeKind(ft, isValue) == ftypeKind);
 787 
 788         // getObject, putIntVolatile, etc.
 789         Kind kind = getFieldKind(isGetter, isVolatile, isFlatValue, fw);
 790 
 791         MethodType linkerType;
 792         boolean hasValueTypeArg = isGetter ? isValue : isFlatValue;
 793         if (isGetter) {
 794             linkerType = isValue ? MethodType.methodType(ft, Object.class, long.class, Class.class)
 795                                  : MethodType.methodType(ft, Object.class, long.class);
 796         } else {
 797             linkerType = isFlatValue ? MethodType.methodType(void.class, Object.class, long.class, Class.class, ft)
 798                                      : MethodType.methodType(void.class, Object.class, long.class, ft);
 799         }
 800         MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual);
 801         try {
 802             linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class);
 803         } catch (ReflectiveOperationException ex) {
 804             throw newInternalError(ex);
 805         }
 806 
 807         // What is the external type of the lambda form?
 808         MethodType mtype;
 809         if (isGetter)
 810             mtype = MethodType.methodType(ft);
 811         else
 812             mtype = MethodType.methodType(void.class, ft);
 813         mtype = mtype.basicType();  // erase short to int, etc.
 814         if (!isStatic)
 815             mtype = mtype.insertParameterTypes(0, Object.class);
 816         final int DMH_THIS  = 0;
 817         final int ARG_BASE  = 1;
 818         final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
 819         // if this is for non-static access, the base pointer is stored at this index:
 820         final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
 821         // if this is for write access, the value to be written is stored at this index:
 822         final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
 823         int nameCursor = ARG_LIMIT;
 824         final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
 825         final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
 826         final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
 827         final int U_HOLDER  = nameCursor++;  // UNSAFE holder
 828         final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
 829         final int VALUE_TYPE = (hasValueTypeArg ? nameCursor++ : -1);
 830         final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
 831         final int LINKER_CALL = nameCursor++;
 832         final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
 833         final int RESULT    = nameCursor-1;  // either the call or the cast
 834         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 835         if (needsInit)
 836             names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]);
 837         if (needsCast && !isGetter)
 838             names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]);
 839         Object[] outArgs = new Object[1 + linkerType.parameterCount()];
 840         assert (outArgs.length == (isGetter ? 3 : 4) + (hasValueTypeArg ? 1 : 0));
 841         outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE));
 842         if (isStatic) {
 843             outArgs[1] = names[F_HOLDER]  = new Name(getFunction(NF_staticBase), names[DMH_THIS]);
 844             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_staticOffset), names[DMH_THIS]);
 845         } else {
 846             outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]);
 847             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]);
 848         }
 849         int x = 3;
 850         if (hasValueTypeArg) {
 851             outArgs[x++] = names[VALUE_TYPE] = isStatic ? new Name(getFunction(NF_staticFieldType), names[DMH_THIS])
 852                                                         : new Name(getFunction(NF_fieldType), names[DMH_THIS]);
 853         }
 854         if (!isGetter) {
 855             outArgs[x] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
 856         }
 857         for (Object a : outArgs)  assert(a != null);
 858         names[LINKER_CALL] = new Name(linker, outArgs);
 859         if (needsCast && isGetter)
 860             names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[LINKER_CALL]);
 861         for (Name n : names)  assert(n != null);
 862 
 863         LambdaForm form;
 864         if (needsCast || needsInit) {
 865             // can't use the pre-generated form when casting and/or initializing
 866             form = new LambdaForm(ARG_LIMIT, names, RESULT);
 867         } else {
 868             form = new LambdaForm(ARG_LIMIT, names, RESULT, kind);
 869         }
 870 
 871         if (LambdaForm.debugNames()) {
 872             // add some detail to the lambdaForm debugname,
 873             // significant only for debugging
 874             StringBuilder nameBuilder = new StringBuilder(kind.methodName);
 875             if (isStatic) {
 876                 nameBuilder.append("Static");
 877             } else {
 878                 nameBuilder.append("Field");
 879             }
 880             if (needsCast) {
 881                 nameBuilder.append("Cast");
 882             }
 883             if (needsInit) {
 884                 nameBuilder.append("Init");
 885             }
 886             LambdaForm.associateWithDebugName(form, nameBuilder.toString());
 887         }
 888         return form;
 889     }
 890 
 891     /**
 892      * Pre-initialized NamedFunctions for bootstrapping purposes.
 893      */
 894     static final byte NF_internalMemberName = 0,
 895             NF_internalMemberNameEnsureInit = 1,
 896             NF_ensureInitialized = 2,
 897             NF_fieldOffset = 3,
 898             NF_checkBase = 4,
 899             NF_staticBase = 5,
 900             NF_staticOffset = 6,
 901             NF_checkCast = 7,
 902             NF_allocateInstance = 8,
 903             NF_constructorMethod = 9,
 904             NF_UNSAFE = 10,
 905             NF_checkReceiver = 11,
 906             NF_fieldType = 12,
 907             NF_staticFieldType = 13,
 908             NF_LIMIT = 14;
 909 
 910     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
 911 
 912     private static NamedFunction getFunction(byte func) {
 913         NamedFunction nf = NFS[func];
 914         if (nf != null) {
 915             return nf;
 916         }
 917         // Each nf must be statically invocable or we get tied up in our bootstraps.
 918         nf = NFS[func] = createFunction(func);
 919         assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
 920         return nf;
 921     }
 922 
 923     private static final MethodType CLS_OBJ_TYPE = MethodType.methodType(Class.class, Object.class);
 924 
 925     private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
 926 
 927     private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
 928 
 929     private static NamedFunction createFunction(byte func) {
 930         try {
 931             switch (func) {
 932                 case NF_internalMemberName:
 933                     return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
 934                 case NF_internalMemberNameEnsureInit:
 935                     return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
 936                 case NF_ensureInitialized:
 937                     return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
 938                 case NF_fieldOffset:
 939                     return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
 940                 case NF_checkBase:
 941                     return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
 942                 case NF_staticBase:
 943                     return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
 944                 case NF_staticOffset:
 945                     return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
 946                 case NF_checkCast:
 947                     return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
 948                 case NF_allocateInstance:
 949                     return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
 950                 case NF_constructorMethod:
 951                     return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
 952                 case NF_UNSAFE:
 953                     MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getField);
 954                     return new NamedFunction(
 955                             MemberName.getFactory()
 956                                     .resolveOrFail(REF_getField, member, DirectMethodHandle.class, NoSuchMethodException.class));
 957                 case NF_checkReceiver:
 958                     member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual);
 959                     return new NamedFunction(
 960                         MemberName.getFactory()
 961                             .resolveOrFail(REF_invokeVirtual, member, DirectMethodHandle.class, NoSuchMethodException.class));
 962                 case NF_fieldType:
 963                     return getNamedFunction("fieldType", CLS_OBJ_TYPE);
 964                 case NF_staticFieldType:
 965                     return getNamedFunction("staticFieldType", CLS_OBJ_TYPE);
 966                 default:
 967                     throw newInternalError("Unknown function: " + func);
 968             }
 969         } catch (ReflectiveOperationException ex) {
 970             throw newInternalError(ex);
 971         }
 972     }
 973 
 974     private static NamedFunction getNamedFunction(String name, MethodType type)
 975         throws ReflectiveOperationException
 976     {
 977         MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
 978         return new NamedFunction(
 979             MemberName.getFactory()
 980                 .resolveOrFail(REF_invokeStatic, member, DirectMethodHandle.class, NoSuchMethodException.class));
 981     }
 982 
 983     static {
 984         // The Holder class will contain pre-generated DirectMethodHandles resolved
 985         // speculatively using MemberName.getFactory().resolveOrNull. However, that
 986         // doesn't initialize the class, which subtly breaks inlining etc. By forcing
 987         // initialization of the Holder class we avoid these issues.
 988         UNSAFE.ensureClassInitialized(Holder.class);
 989     }
 990 
 991     /* Placeholder class for DirectMethodHandles generated ahead of time */
 992     final class Holder {}
 993 }