changeset 415:b6a3c70ad80a

meth-lazy: simplify field accessor code
author jrose
date Thu, 19 Jul 2012 00:27:37 -0700
parents c73fecb5e075
children 971e1b4846fc
files meth-lazy-7023639.bmh.patch
diffstat 1 files changed, 25 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/meth-lazy-7023639.bmh.patch	Tue Jul 17 17:03:38 2012 -0700
+++ b/meth-lazy-7023639.bmh.patch	Thu Jul 19 00:27:37 2012 -0700
@@ -1048,7 +1048,7 @@
 diff --git a/src/share/classes/java/lang/invoke/DirectMethodHandle.java b/src/share/classes/java/lang/invoke/DirectMethodHandle.java
 --- a/src/share/classes/java/lang/invoke/DirectMethodHandle.java
 +++ b/src/share/classes/java/lang/invoke/DirectMethodHandle.java
-@@ -31,20 +31,26 @@
+@@ -31,20 +31,25 @@
  import sun.invoke.util.VerifyAccess;
  import static java.lang.invoke.MethodHandleNatives.Constants.*;
  import static java.lang.invoke.LambdaForm.*;
@@ -1056,7 +1056,6 @@
 +import static java.lang.invoke.MethodHandleStatics.*;
  import java.lang.ref.WeakReference;
 +import java.lang.reflect.Field;
-+import java.util.Objects;
  import sun.invoke.util.ValueConversions;
 +import sun.invoke.util.VerifyType;
 +import sun.invoke.util.Wrapper;
@@ -1078,7 +1077,7 @@
          if (!member.isResolved())  throw new InternalError();
          this.member = member;
      }
-@@ -52,24 +58,54 @@
+@@ -52,24 +57,54 @@
      // Factory methods:
  
      static DirectMethodHandle make(Class<?> receiver, MemberName member) {
@@ -1137,7 +1136,7 @@
      }
  
      @Override
-@@ -116,7 +152,7 @@
+@@ -116,7 +151,7 @@
                  MemberName concrete = new MemberName(concreteClass, member.getName(), member.getMethodType(), REF_invokeSpecial);
                  concrete = IMPL_NAMES.resolveOrNull(REF_invokeSpecial, concrete, concreteClass);
                  if (concrete != null)
@@ -1146,7 +1145,7 @@
                  break;
              }
          }
-@@ -128,65 +164,117 @@
+@@ -128,65 +163,117 @@
       * Cache and share this structure among all methods with
       * the same basicType and refKind.
       */
@@ -1291,7 +1290,7 @@
      }
  
      /*non-public*/ static
-@@ -213,26 +301,19 @@
+@@ -213,26 +300,19 @@
              VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
              // It is a system class.  It is probably in the process of
              // being initialized, but we will help it along just to be safe.
@@ -1323,7 +1322,7 @@
                  // If the previous call didn't block, this can happen.
                  // We are executing inside <clinit>.
                  return new WeakReference<>(Thread.currentThread());
-@@ -241,29 +322,16 @@
+@@ -241,29 +321,16 @@
          static final EnsureInitialized INSTANCE = new EnsureInitialized();
      }
  
@@ -1362,7 +1361,7 @@
          Class<?> defc = member.getDeclaringClass();
          WeakReference<Thread> ref = EnsureInitialized.INSTANCE.get(defc);
          if (ref == null) {
-@@ -273,65 +341,321 @@
+@@ -273,65 +340,320 @@
          // Somebody may still be running defc.<clinit>.
          if (clinitThread == Thread.currentThread()) {
              // If anybody is running defc.<clinit>, it is this thread.
@@ -1425,13 +1424,11 @@
 +    /** This subclass handles non-static field references. */
 +    static class Accessor extends DirectMethodHandle {
 +        final Class<?> fieldType;
-+        final Class<?> fieldHolder;
 +        final int      fieldOffset;
 +        private Accessor(MethodType mtype, LambdaForm form, MemberName member,
 +                         int fieldOffset) {
 +            super(mtype, form, member);
 +            this.fieldType   = member.getFieldType();
-+            this.fieldHolder = member.getDeclaringClass();
 +            this.fieldOffset = fieldOffset;
 +        }
 +
@@ -1448,14 +1445,16 @@
 +    }
 +
 +    @ForceInline
-+    /*non-public*/ static Object fieldHolder(Object accessorObj) {
-+        return ((Accessor)accessorObj).fieldHolder;
-+    }
-+
-+    @ForceInline
-+    /*non-public*/ static Object checkBase(Object obj, Object fieldHolderObj) {
-+        Objects.requireNonNull(obj);
-+        return ((Class<?>)fieldHolderObj).cast(obj);
++    /*non-public*/ static Object checkBase(Object obj) {
++        // Note that the object's class has already been verified,
++        // since the parameter type of the Accessor method handle
++        // is either member.getDeclaringClass or a subclass.
++        // This was verified in DirectMethodHandle.make.
++        // Therefore, the only remaining check is for null.
++        // Since this check is *not* guaranteed by Unsafe.getInt
++        // and its siblings, we need to make an explicit one here.
++        obj.getClass();  // maybe throw NPE
++        return obj;
 +    }
 +
 +    /** This subclass handles static field references. */
@@ -1583,7 +1582,9 @@
 +        assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
 +        String tname  = fw.primitiveSimpleName();
 +        String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1);
-+        String linkerName = ((isGetter ? "get" : "put") + ctname + (isVolatile? "Volatile" : ""));
++        if (isVolatile)  ctname += "Volatile";
++        String getOrPut = (isGetter ? "get" : "put");
++        String linkerName = (getOrPut + ctname);  // getObject, putIntVolatile, etc.
 +        MethodType linkerType;
 +        if (isGetter)
 +            linkerType = MethodType.methodType(ft, Object.class, long.class);
@@ -1613,7 +1614,7 @@
 +        // if this is for write access, the value to be written is stored at this index:
 +        final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
 +        int nameCursor = ARG_LIMIT;
-+        final int F_HOLDER  = nameCursor++;  // Either static base or field holder.
++        final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
 +        final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
 +        final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
 +        final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
@@ -1633,8 +1634,7 @@
 +            outArgs[1] = names[F_HOLDER]  = new Name(NF_staticBase, names[DMH_THIS]);
 +            outArgs[2] = names[F_OFFSET]  = new Name(NF_staticOffset, names[DMH_THIS]);
 +        } else {
-+            names[F_HOLDER]               = new Name(NF_fieldHolder, names[DMH_THIS]);
-+            outArgs[1] = names[OBJ_CHECK] = new Name(NF_checkBase, names[OBJ_BASE], names[F_HOLDER]);
++            outArgs[1] = names[OBJ_CHECK] = new Name(NF_checkBase, names[OBJ_BASE]);
 +            outArgs[2] = names[F_OFFSET]  = new Name(NF_fieldOffset, names[DMH_THIS]);
 +        }
 +        if (!isGetter) {
@@ -1645,7 +1645,8 @@
 +        if (needsCast && isGetter)
 +            names[POST_CAST] = new Name(NF_checkCast, names[DMH_THIS], names[LINKER_CALL]);
 +        for (Name n : names)  assert(n != null);
-+        String lambdaName = linkerName;  // significant only for debugging
++        String fieldOrStatic = (isStatic ? "Static" : "Field");
++        String lambdaName = (linkerName + fieldOrStatic);  // significant only for debugging
 +        if (needsCast)  lambdaName += "Cast";
 +        if (needsInit)  lambdaName += "Init";
 +        return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT);
@@ -1655,7 +1656,6 @@
 +            NF_internalMemberName,
 +            NF_internalMemberNameEnsureInit,
 +            NF_ensureInitialized,
-+            NF_fieldHolder,
 +            NF_fieldOffset,
 +            NF_checkBase,
 +            NF_staticBase,
@@ -1679,12 +1679,10 @@
 +                    .getDeclaredMethod("internalMemberNameEnsureInit", Object.class)),
 +                NF_ensureInitialized = new NamedFunction(DirectMethodHandle.class
 +                    .getDeclaredMethod("ensureInitialized", Object.class)),
-+                NF_fieldHolder = new NamedFunction(DirectMethodHandle.class
-+                    .getDeclaredMethod("fieldHolder", Object.class)),
 +                NF_fieldOffset = new NamedFunction(DirectMethodHandle.class
 +                    .getDeclaredMethod("fieldOffset", Object.class)),
 +                NF_checkBase = new NamedFunction(DirectMethodHandle.class
-+                    .getDeclaredMethod("checkBase", Object.class, Object.class)),
++                    .getDeclaredMethod("checkBase", Object.class)),
 +                NF_staticBase = new NamedFunction(DirectMethodHandle.class
 +                    .getDeclaredMethod("staticBase", Object.class)),
 +                NF_staticOffset = new NamedFunction(DirectMethodHandle.class