jdk/src/java.base/share/classes/java/lang/invoke/VarForm.java
author shade
Tue, 17 May 2016 22:28:00 +0300
changeset 38372 017d7578731c
parent 37792 dd626e6f5967
child 40272 6af4511ee5a4
permissions -rw-r--r--
8157171: Hook up Unsafe.weakCompareAndSetVolatile to VarHandles Reviewed-by: psandoz, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     1
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     4
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    10
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    15
 * accompanied this code).
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    16
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    20
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    23
 * questions.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    24
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    25
package java.lang.invoke;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    26
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    27
import jdk.internal.vm.annotation.ForceInline;
36973
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
    28
import jdk.internal.vm.annotation.Stable;
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
    29
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    30
import java.lang.invoke.VarHandle.AccessMode;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
import java.lang.reflect.Method;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
import java.lang.reflect.Modifier;
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    33
import java.util.ArrayList;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    34
import java.util.Arrays;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    35
import java.util.List;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
/**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
 * A var handle form containing a set of member name, one for each operation.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
 * Each member characterizes a static method.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
 */
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    41
final class VarForm {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    42
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    43
    final @Stable MethodType[] methodType_table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    44
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    45
    final @Stable MemberName[] memberName_table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    46
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    47
    VarForm(Class<?> implClass, Class<?> receiver, Class<?> value, Class<?>... intermediate) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    48
        this.methodType_table = new MethodType[VarHandle.AccessType.values().length];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    49
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    50
        // TODO lazily calculate
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    51
        this.memberName_table = linkFromStatic(implClass);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    52
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    53
        // (Receiver, <Intermediates>)
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    54
        List<Class<?>> l = new ArrayList<>();
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    55
        if (receiver != null)
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    56
            l.add(receiver);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    57
        l.addAll(Arrays.asList(intermediate));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    59
        // (Receiver, <Intermediates>)Value
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    60
        methodType_table[VarHandle.AccessType.GET.ordinal()] =
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    61
                MethodType.methodType(value, l).erase();
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    62
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    63
        // (Receiver, <Intermediates>, Value)void
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    64
        l.add(value);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    65
        methodType_table[VarHandle.AccessType.SET.ordinal()] =
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    66
                MethodType.methodType(void.class, l).erase();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    68
        // (Receiver, <Intermediates>, Value)Value
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    69
        methodType_table[VarHandle.AccessType.GET_AND_UPDATE.ordinal()] =
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    70
                MethodType.methodType(value, l).erase();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    71
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    72
        // (Receiver, <Intermediates>, Value, Value)boolean
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    73
        l.add(value);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    74
        methodType_table[VarHandle.AccessType.COMPARE_AND_SWAP.ordinal()] =
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    75
                MethodType.methodType(boolean.class, l).erase();
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    76
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    77
        // (Receiver, <Intermediates>, Value, Value)Value
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    78
        methodType_table[VarHandle.AccessType.COMPARE_AND_EXCHANGE.ordinal()] =
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    79
                MethodType.methodType(value, l).erase();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    81
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    82
    @ForceInline
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    83
    final MethodType getMethodType(int type) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    84
        return methodType_table[type];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    85
    }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    86
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    87
    @ForceInline
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    88
    final MemberName getMemberName(int mode) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    89
        // TODO calculate lazily
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    90
        MemberName mn = memberName_table[mode];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    91
        if (mn == null) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    92
            throw new UnsupportedOperationException();
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    93
        }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    94
        return mn;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    95
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    96
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    97
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    98
    @Stable
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
    99
    MethodType[] methodType_V_table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   100
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   101
    @ForceInline
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   102
    final MethodType[] getMethodType_V_init() {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   103
        MethodType[] table = new MethodType[VarHandle.AccessType.values().length];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   104
        for (int i = 0; i < methodType_table.length; i++) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   105
            MethodType mt = methodType_table[i];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   106
            // TODO only adjust for sig-poly methods returning Object
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   107
            table[i] = mt.changeReturnType(void.class);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   108
        }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   109
        methodType_V_table = table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   110
        return table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   111
    }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   112
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   113
    @ForceInline
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   114
    final MethodType getMethodType_V(int type) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   115
        MethodType[] table = methodType_V_table;
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   116
        if (table == null) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   117
            table = getMethodType_V_init();
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   118
        }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   119
        return table[type];
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   120
    }
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   121
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   122
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   124
     * Link all signature polymorphic methods.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   125
     */
36973
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   126
    private static MemberName[] linkFromStatic(Class<?> implClass) {
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   127
        MemberName[] table = new MemberName[AccessMode.values().length];
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
        for (Class<?> c = implClass; c != VarHandle.class; c = c.getSuperclass()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   130
            for (Method m : c.getDeclaredMethods()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   131
                if (Modifier.isStatic(m.getModifiers())) {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36973
diff changeset
   132
                    AccessMode am = AccessMode.methodNameToAccessMode.get(m.getName());
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36973
diff changeset
   133
                    if (am != null) {
36973
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   134
                        assert table[am.ordinal()] == null;
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   135
                        table[am.ordinal()] = new MemberName(m);
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   136
                    }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   139
        }
36973
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   140
        return table;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   141
    }
36973
951bb58383a4 8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents: 36934
diff changeset
   142
}