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