< prev index next >

src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java

Print this page


   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


  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.isConstructor())
  80                 throw new InternalError(member.toString());
  81             mtype = mtype.insertParameterTypes(0, refc);
  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 new StaticAccessor(mtype, lform, member, base, offset);

 115             } else {
 116                 long offset = MethodHandleNatives.objectFieldOffset(member);
 117                 assert(offset == (int)offset);
 118                 return new Accessor(mtype, lform, member, (int)offset);

 119             }
 120         }
 121     }
 122     static DirectMethodHandle make(Class<?> refc, MemberName member) {
 123         byte refKind = member.getReferenceKind();
 124         if (refKind == REF_invokeSpecial)
 125             refKind =  REF_invokeVirtual;
 126         return make(refKind, refc, member, null /* no callerClass context */);
 127     }
 128     static DirectMethodHandle make(MemberName member) {
 129         if (member.isConstructor())
 130             return makeAllocator(member);
 131         return make(member.getDeclaringClass(), member);
 132     }
 133     private static DirectMethodHandle makeAllocator(MemberName ctor) {
 134         assert(ctor.isConstructor() && ctor.getName().equals("<init>"));

 135         Class<?> instanceClass = ctor.getDeclaringClass();
 136         ctor = ctor.asConstructor();
 137         assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
 138         MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
 139         LambdaForm lform = preparedLambdaForm(ctor);
 140         MemberName init = ctor.asSpecial();
 141         assert(init.getMethodType().returnType() == void.class);
 142         return new Constructor(mtype, lform, ctor, init, instanceClass);
 143     }
 144 
 145     @Override
 146     BoundMethodHandle rebind() {
 147         return BoundMethodHandle.makeReinvoker(this);
 148     }
 149 
 150     @Override
 151     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 152         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
 153         return new DirectMethodHandle(mt, lf, member);
 154     }
 155 
 156     @Override
 157     String internalProperties() {


 466             this.instanceClass = instanceClass;
 467             assert(initMethod.isResolved());
 468         }
 469         @Override
 470         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 471             return new Constructor(mt, lf, member, initMethod, instanceClass);
 472         }
 473     }
 474 
 475     /*non-public*/ static Object constructorMethod(Object mh) {
 476         Constructor dmh = (Constructor)mh;
 477         return dmh.initMethod;
 478     }
 479 
 480     /*non-public*/ static Object allocateInstance(Object mh) throws InstantiationException {
 481         Constructor dmh = (Constructor)mh;
 482         return UNSAFE.allocateInstance(dmh.instanceClass);
 483     }
 484 
 485     /** This subclass handles non-static field references. */
 486     static class Accessor extends DirectMethodHandle {
 487         final Class<?> fieldType;
 488         final int      fieldOffset;
 489         private Accessor(MethodType mtype, LambdaForm form, MemberName member,
 490                          int fieldOffset) {

 491             super(mtype, form, member);
 492             this.fieldType   = member.getFieldType();
 493             this.fieldOffset = fieldOffset;
 494         }










 495 
 496         @Override Object checkCast(Object obj) {
 497             return fieldType.cast(obj);
 498         }
 499         @Override
 500         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 501             return new Accessor(mt, lf, member, fieldOffset);
















 502         }
 503     }
 504 
 505     @ForceInline
 506     /*non-public*/ static long fieldOffset(Object accessorObj) {
 507         // Note: We return a long because that is what Unsafe.getObject likes.
 508         // We store a plain int because it is more compact.
 509         return ((Accessor)accessorObj).fieldOffset;
 510     }
 511 
 512     @ForceInline
 513     /*non-public*/ static Object checkBase(Object obj) {
 514         // Note that the object's class has already been verified,
 515         // since the parameter type of the Accessor method handle
 516         // is either member.getDeclaringClass or a subclass.
 517         // This was verified in DirectMethodHandle.make.
 518         // Therefore, the only remaining check is for null.
 519         // Since this check is *not* guaranteed by Unsafe.getInt
 520         // and its siblings, we need to make an explicit one here.
 521         return Objects.requireNonNull(obj);
 522     }
 523 
 524     /** This subclass handles static field references. */
 525     static class StaticAccessor extends DirectMethodHandle {
 526         private final Class<?> fieldType;
 527         private final Object   staticBase;
 528         private final long     staticOffset;
 529 
 530         private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 531                                Object staticBase, long staticOffset) {
 532             super(mtype, form, member);
 533             this.fieldType    = member.getFieldType();
 534             this.staticBase   = staticBase;
 535             this.staticOffset = staticOffset;
 536         }



 537 








 538         @Override Object checkCast(Object obj) {
 539             return fieldType.cast(obj);
 540         }
 541         @Override
 542         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 543             return new StaticAccessor(mt, lf, member, staticBase, staticOffset);


















 544         }
 545     }
 546 
 547     @ForceInline
 548     /*non-public*/ static Object nullCheck(Object obj) {
 549         return Objects.requireNonNull(obj);
 550     }
 551 
 552     @ForceInline
 553     /*non-public*/ static Object staticBase(Object accessorObj) {
 554         return ((StaticAccessor)accessorObj).staticBase;
 555     }
 556 
 557     @ForceInline
 558     /*non-public*/ static long staticOffset(Object accessorObj) {
 559         return ((StaticAccessor)accessorObj).staticOffset;
 560     }
 561 
 562     @ForceInline
 563     /*non-public*/ static Object checkCast(Object mh, Object obj) {
 564         return ((DirectMethodHandle) mh).checkCast(obj);
 565     }
 566 










 567     Object checkCast(Object obj) {
 568         return member.getReturnType().cast(obj);
 569     }
 570 
 571     // Caching machinery for field accessors:
 572     static final byte
 573             AF_GETFIELD        = 0,
 574             AF_PUTFIELD        = 1,
 575             AF_GETSTATIC       = 2,
 576             AF_PUTSTATIC       = 3,
 577             AF_GETSTATIC_INIT  = 4,
 578             AF_PUTSTATIC_INIT  = 5,
 579             AF_LIMIT           = 6;
 580     // Enumerate the different field kinds using Wrapper,
 581     // with an extra case added for checked references.
 582     static final int
 583             FT_LAST_WRAPPER    = Wrapper.COUNT-1,
 584             FT_UNCHECKED_REF   = Wrapper.OBJECT.ordinal(),
 585             FT_CHECKED_REF     = FT_LAST_WRAPPER+1,
 586             FT_LIMIT           = FT_LAST_WRAPPER+2;
 587     private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) {

 588         return ((formOp * FT_LIMIT * 2)
 589                 + (isVolatile ? FT_LIMIT : 0)

 590                 + ftypeKind);
 591     }
 592     @Stable
 593     private static final LambdaForm[] ACCESSOR_FORMS
 594             = new LambdaForm[afIndex(AF_LIMIT, false, 0)];
 595     static int ftypeKind(Class<?> ftype) {
 596         if (ftype.isPrimitive())
 597             return Wrapper.forPrimitiveType(ftype).ordinal();
 598         else if (VerifyType.isNullReferenceConversion(Object.class, ftype))
 599             return FT_UNCHECKED_REF;
 600         else
 601             return FT_CHECKED_REF;

 602     }
 603 
 604     /**
 605      * Create a LF which can access the given field.
 606      * Cache and share this structure among all fields with
 607      * the same basicType and refKind.
 608      */
 609     private static LambdaForm preparedFieldLambdaForm(MemberName m) {
 610         Class<?> ftype = m.getFieldType();
 611         boolean isVolatile = m.isVolatile();
 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, 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, 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     private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class<?> ftype) {
 635         int ftypeKind = ftypeKind(ftype);
 636         int afIndex = afIndex(formOp, isVolatile, ftypeKind);

 637         LambdaForm lform = ACCESSOR_FORMS[afIndex];
 638         if (lform != null)  return lform;
 639         lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind);
 640         ACCESSOR_FORMS[afIndex] = lform;  // don't bother with a CAS
 641         return lform;
 642     }
 643 
 644     private static final Wrapper[] ALL_WRAPPERS = Wrapper.values();
 645 
 646     private static Kind getFieldKind(boolean isGetter, boolean isVolatile, Wrapper wrapper) {
 647         if (isGetter) {
 648             if (isVolatile) {
 649                 switch (wrapper) {
 650                     case BOOLEAN: return GET_BOOLEAN_VOLATILE;
 651                     case BYTE:    return GET_BYTE_VOLATILE;
 652                     case SHORT:   return GET_SHORT_VOLATILE;
 653                     case CHAR:    return GET_CHAR_VOLATILE;
 654                     case INT:     return GET_INT_VOLATILE;
 655                     case LONG:    return GET_LONG_VOLATILE;
 656                     case FLOAT:   return GET_FLOAT_VOLATILE;
 657                     case DOUBLE:  return GET_DOUBLE_VOLATILE;
 658                     case OBJECT:  return GET_REFERENCE_VOLATILE;
 659                 }
 660             } else {
 661                 switch (wrapper) {
 662                     case BOOLEAN: return GET_BOOLEAN;
 663                     case BYTE:    return GET_BYTE;
 664                     case SHORT:   return GET_SHORT;
 665                     case CHAR:    return GET_CHAR;
 666                     case INT:     return GET_INT;
 667                     case LONG:    return GET_LONG;
 668                     case FLOAT:   return GET_FLOAT;
 669                     case DOUBLE:  return GET_DOUBLE;
 670                     case OBJECT:  return GET_REFERENCE;
 671                 }
 672             }
 673         } else {
 674             if (isVolatile) {
 675                 switch (wrapper) {
 676                     case BOOLEAN: return PUT_BOOLEAN_VOLATILE;
 677                     case BYTE:    return PUT_BYTE_VOLATILE;
 678                     case SHORT:   return PUT_SHORT_VOLATILE;
 679                     case CHAR:    return PUT_CHAR_VOLATILE;
 680                     case INT:     return PUT_INT_VOLATILE;
 681                     case LONG:    return PUT_LONG_VOLATILE;
 682                     case FLOAT:   return PUT_FLOAT_VOLATILE;
 683                     case DOUBLE:  return PUT_DOUBLE_VOLATILE;
 684                     case OBJECT:  return PUT_REFERENCE_VOLATILE;
 685                 }
 686             } else {
 687                 switch (wrapper) {
 688                     case BOOLEAN: return PUT_BOOLEAN;
 689                     case BYTE:    return PUT_BYTE;
 690                     case SHORT:   return PUT_SHORT;
 691                     case CHAR:    return PUT_CHAR;
 692                     case INT:     return PUT_INT;
 693                     case LONG:    return PUT_LONG;
 694                     case FLOAT:   return PUT_FLOAT;
 695                     case DOUBLE:  return PUT_DOUBLE;
 696                     case OBJECT:  return PUT_REFERENCE;
 697                 }
 698             }
 699         }
 700         throw new AssertionError("Invalid arguments");
 701     }
 702 
 703     static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {





 704         boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
 705         boolean isStatic  = (formOp >= AF_GETSTATIC);
 706         boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
 707         boolean needsCast = (ftypeKind == FT_CHECKED_REF);
 708         Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]);
 709         Class<?> ft = fw.primitiveType();
 710         assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
 711 
 712         // getObject, putIntVolatile, etc.
 713         Kind kind = getFieldKind(isGetter, isVolatile, fw);
 714 
 715         MethodType linkerType;
 716         if (isGetter)
 717             linkerType = MethodType.methodType(ft, Object.class, long.class);
 718         else
 719             linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);




 720         MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual);
 721         try {
 722             linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class);
 723         } catch (ReflectiveOperationException ex) {
 724             throw newInternalError(ex);
 725         }
 726 
 727         // What is the external type of the lambda form?
 728         MethodType mtype;
 729         if (isGetter)
 730             mtype = MethodType.methodType(ft);
 731         else
 732             mtype = MethodType.methodType(void.class, ft);
 733         mtype = mtype.basicType();  // erase short to int, etc.
 734         if (!isStatic)
 735             mtype = mtype.insertParameterTypes(0, Object.class);
 736         final int DMH_THIS  = 0;
 737         final int ARG_BASE  = 1;
 738         final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
 739         // if this is for non-static access, the base pointer is stored at this index:
 740         final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
 741         // if this is for write access, the value to be written is stored at this index:
 742         final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
 743         int nameCursor = ARG_LIMIT;
 744         final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
 745         final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
 746         final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
 747         final int U_HOLDER  = nameCursor++;  // UNSAFE holder
 748         final int INIT_BAR  = (needsInit ? nameCursor++ : -1);

 749         final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
 750         final int LINKER_CALL = nameCursor++;
 751         final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
 752         final int RESULT    = nameCursor-1;  // either the call or the cast
 753         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 754         if (needsInit)
 755             names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]);
 756         if (needsCast && !isGetter)
 757             names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]);
 758         Object[] outArgs = new Object[1 + linkerType.parameterCount()];
 759         assert(outArgs.length == (isGetter ? 3 : 4));
 760         outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE));
 761         if (isStatic) {
 762             outArgs[1] = names[F_HOLDER]  = new Name(getFunction(NF_staticBase), names[DMH_THIS]);
 763             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_staticOffset), names[DMH_THIS]);
 764         } else {
 765             outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]);
 766             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]);
 767         }





 768         if (!isGetter) {
 769             outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
 770         }
 771         for (Object a : outArgs)  assert(a != null);
 772         names[LINKER_CALL] = new Name(linker, outArgs);
 773         if (needsCast && isGetter)
 774             names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[LINKER_CALL]);
 775         for (Name n : names)  assert(n != null);
 776 
 777         LambdaForm form;
 778         if (needsCast || needsInit) {
 779             // can't use the pre-generated form when casting and/or initializing
 780             form = new LambdaForm(ARG_LIMIT, names, RESULT);
 781         } else {
 782             form = new LambdaForm(ARG_LIMIT, names, RESULT, kind);
 783         }
 784 
 785         if (LambdaForm.debugNames()) {
 786             // add some detail to the lambdaForm debugname,
 787             // significant only for debugging
 788             StringBuilder nameBuilder = new StringBuilder(kind.methodName);
 789             if (isStatic) {


 800             LambdaForm.associateWithDebugName(form, nameBuilder.toString());
 801         }
 802         return form;
 803     }
 804 
 805     /**
 806      * Pre-initialized NamedFunctions for bootstrapping purposes.
 807      */
 808     static final byte NF_internalMemberName = 0,
 809             NF_internalMemberNameEnsureInit = 1,
 810             NF_ensureInitialized = 2,
 811             NF_fieldOffset = 3,
 812             NF_checkBase = 4,
 813             NF_staticBase = 5,
 814             NF_staticOffset = 6,
 815             NF_checkCast = 7,
 816             NF_allocateInstance = 8,
 817             NF_constructorMethod = 9,
 818             NF_UNSAFE = 10,
 819             NF_checkReceiver = 11,
 820             NF_LIMIT = 12;


 821 
 822     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
 823 
 824     private static NamedFunction getFunction(byte func) {
 825         NamedFunction nf = NFS[func];
 826         if (nf != null) {
 827             return nf;
 828         }
 829         // Each nf must be statically invocable or we get tied up in our bootstraps.
 830         nf = NFS[func] = createFunction(func);
 831         assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
 832         return nf;
 833     }
 834 


 835     private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
 836 
 837     private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
 838 
 839     private static NamedFunction createFunction(byte func) {
 840         try {
 841             switch (func) {
 842                 case NF_internalMemberName:
 843                     return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
 844                 case NF_internalMemberNameEnsureInit:
 845                     return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
 846                 case NF_ensureInitialized:
 847                     return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
 848                 case NF_fieldOffset:
 849                     return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
 850                 case NF_checkBase:
 851                     return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
 852                 case NF_staticBase:
 853                     return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
 854                 case NF_staticOffset:
 855                     return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
 856                 case NF_checkCast:
 857                     return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
 858                 case NF_allocateInstance:
 859                     return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
 860                 case NF_constructorMethod:
 861                     return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
 862                 case NF_UNSAFE:
 863                     MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getField);
 864                     return new NamedFunction(
 865                             MemberName.getFactory()
 866                                     .resolveOrFail(REF_getField, member, DirectMethodHandle.class, NoSuchMethodException.class));
 867                 case NF_checkReceiver:
 868                     member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual);
 869                     return new NamedFunction(
 870                         MemberName.getFactory()
 871                             .resolveOrFail(REF_invokeVirtual, member, DirectMethodHandle.class, NoSuchMethodException.class));




 872                 default:
 873                     throw newInternalError("Unknown function: " + func);
 874             }
 875         } catch (ReflectiveOperationException ex) {
 876             throw newInternalError(ex);
 877         }
 878     }
 879 
 880     private static NamedFunction getNamedFunction(String name, MethodType type)
 881         throws ReflectiveOperationException
 882     {
 883         MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
 884         return new NamedFunction(
 885             MemberName.getFactory()
 886                 .resolveOrFail(REF_invokeStatic, member, DirectMethodHandle.class, NoSuchMethodException.class));
 887     }
 888 
 889     static {
 890         // The Holder class will contain pre-generated DirectMethodHandles resolved
 891         // speculatively using MemberName.getFactory().resolveOrNull. However, that
   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


  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() {


 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) {


 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
< prev index next >