test/jdk/java/lang/invoke/CallSiteTest.java
author vlivanov
Tue, 26 Nov 2019 16:09:17 +0300
changeset 59275 a6e25566cb56
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234401: ConstantCallSite may stuck in non-frozen state Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11534
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     4
 *
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     7
 * published by the Free Software Foundation.
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     8
 *
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    13
 * accompanied this code).
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    14
 *
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    18
 *
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    21
 * questions.
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    22
 */
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    23
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    24
/**
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    25
 * @test
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    26
 * @summary smoke tests for CallSite
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    27
 * @library /test/lib
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    28
 *
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    29
 * @build indify.Indify
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    30
 * @compile CallSiteTest.java
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    31
 * @run main/othervm/timeout=3600 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -Xbatch
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    32
 *      indify.Indify
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    33
 *      --expand-properties --classpath ${test.classes}
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    34
 *      --java test.java.lang.invoke.CallSiteTest
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    35
 */
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    36
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    37
package test.java.lang.invoke;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    38
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    39
import java.io.*;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    40
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    41
import java.lang.invoke.*;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    42
import static java.lang.invoke.MethodHandles.*;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    43
import static java.lang.invoke.MethodType.*;
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    44
import static jdk.test.lib.Asserts.*;
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    45
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    46
public class CallSiteTest {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 14408
diff changeset
    47
    private static final Class<?> CLASS = CallSiteTest.class;
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    48
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    49
    private static CallSite mcs;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    50
    private static CallSite vcs;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    51
    private static MethodHandle mh_foo;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    52
    private static MethodHandle mh_bar;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    53
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    54
    static {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    55
        try {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    56
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    57
            mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class));
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    58
            mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class));
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    59
            mcs = new MutableCallSite(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    60
            vcs = new VolatileCallSite(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    61
        } catch (Exception e) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    62
            e.printStackTrace();
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    63
            throw new Error(e);
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    64
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    65
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    66
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    67
    public static void main(String... av) throws Throwable {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    68
        testConstantCallSite();
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    69
        testMutableCallSite();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    70
        testVolatileCallSite();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    71
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    72
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 14408
diff changeset
    73
    private static final int N = Integer.MAX_VALUE / 100;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 14408
diff changeset
    74
    private static final int RESULT1 = 762786192;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 14408
diff changeset
    75
    private static final int RESULT2 = -21474836;
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
    76
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    77
    static final CallSite     MCS         = new MutableCallSite(methodType(void.class));
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    78
    static final MethodHandle MCS_INVOKER = MCS.dynamicInvoker();
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    79
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    80
    static void test(boolean shouldThrow) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    81
        try {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    82
            MCS_INVOKER.invokeExact();
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    83
            if (shouldThrow) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    84
                throw new AssertionError("should throw");
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    85
            }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    86
        } catch (IllegalStateException ise) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    87
            if (!shouldThrow) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    88
                throw new AssertionError("should not throw", ise);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    89
            }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    90
        } catch (Throwable e) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    91
            throw new Error(e);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    92
        }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    93
    }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    94
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    95
    static class MyCCS extends ConstantCallSite {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    96
        public MyCCS(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    97
            super(targetType, createTargetHook);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    98
        }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
    99
    }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   100
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   101
    private static MethodHandle testConstantCallSiteHandler(CallSite cs, CallSite[] holder) throws Throwable {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   102
        holder[0] = cs; // capture call site instance for subsequent checks
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   103
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   104
        MethodType csType = cs.type(); // should not throw on partially constructed instance
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   105
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   106
        // Truly dynamic invoker for constant call site
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   107
        MethodHandle getTarget = lookup().findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class))
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   108
                                         .bindTo(cs);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   109
        MethodHandle invoker = MethodHandles.exactInvoker(csType);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   110
        MethodHandle target = MethodHandles.foldArguments(invoker, getTarget);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   111
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   112
        MCS.setTarget(target);
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   113
        // warmup
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   114
        for (int i = 0; i < 20_000; i++) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   115
            test(true); // should throw IllegalStateException
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   116
        }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   117
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   118
        return MethodHandles.empty(csType); // initialize cs with an empty method handle
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   119
    }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   120
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   121
    private static void testConstantCallSite() throws Throwable {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   122
        CallSite[] holder = new CallSite[1];
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   123
        MethodHandle handler = lookup().findStatic(CLASS, "testConstantCallSiteHandler", MethodType.methodType(MethodHandle.class, CallSite.class, CallSite[].class));
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   124
        handler = MethodHandles.insertArguments(handler, 1, new Object[] { holder } );
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   125
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   126
        CallSite ccs = new MyCCS(MCS.type(), handler); // trigger call to handler
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   127
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   128
        if (ccs != holder[0]) {
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   129
            throw new AssertionError("different call site instances");
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   130
        }
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   131
        test(false); // should not throw
11007
09d3dddac781 7109063: JSR 292: fix for 7085860 is incomplete
twisti
parents: 11006
diff changeset
   132
    }
09d3dddac781 7109063: JSR 292: fix for 7085860 is incomplete
twisti
parents: 11006
diff changeset
   133
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   134
    private static void testMutableCallSite() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   135
        // warm-up
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   136
        for (int i = 0; i < 20000; i++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   137
            mcs.setTarget(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   138
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   139
        // run
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   140
        for (int n = 0; n < 2; n++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   141
            mcs.setTarget(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   142
            for (int i = 0; i < 5; i++) {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   143
                assertEQ(RESULT1, runMutableCallSite());
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   144
            }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   145
            mcs.setTarget(mh_bar);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   146
            for (int i = 0; i < 5; i++) {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   147
                assertEQ(RESULT2, runMutableCallSite());
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   148
            }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   149
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   150
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   151
    private static void testVolatileCallSite() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   152
        // warm-up
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   153
        for (int i = 0; i < 20000; i++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   154
            vcs.setTarget(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   155
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   156
        // run
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   157
        for (int n = 0; n < 2; n++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   158
            vcs.setTarget(mh_foo);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   159
            for (int i = 0; i < 5; i++) {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   160
                assertEQ(RESULT1, runVolatileCallSite());
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   161
            }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   162
            vcs.setTarget(mh_bar);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   163
            for (int i = 0; i < 5; i++) {
59275
a6e25566cb56 8234401: ConstantCallSite may stuck in non-frozen state
vlivanov
parents: 47216
diff changeset
   164
                assertEQ(RESULT2, runVolatileCallSite());
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   165
            }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   166
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   167
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   168
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   169
    private static int runMutableCallSite() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   170
        int sum = 0;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   171
        for (int i = 0; i < N; i++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   172
            sum += (int) INDY_mcs().invokeExact(i, i+1);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   173
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   174
        return sum;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   175
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   176
    private static int runVolatileCallSite() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   177
        int sum = 0;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   178
        for (int i = 0; i < N; i++) {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   179
            sum += (int) INDY_vcs().invokeExact(i, i+1);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   180
        }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   181
        return sum;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   182
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   183
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   184
    static int foo(int a, int b) { return a + b; }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   185
    static int bar(int a, int b) { return a - b; }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   186
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   187
    private static MethodType MT_bsm() {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   188
        shouldNotCallThis();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   189
        return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   190
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   191
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   192
    private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   193
        return mcs;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   194
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   195
    private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   196
        shouldNotCallThis();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   197
        return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm());
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   198
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   199
    private static MethodHandle INDY_mcs() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   200
        shouldNotCallThis();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   201
        return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   202
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   203
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   204
    private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   205
        return vcs;
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   206
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   207
    private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   208
        shouldNotCallThis();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   209
        return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm());
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   210
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   211
    private static MethodHandle INDY_vcs() throws Throwable {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   212
        shouldNotCallThis();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   213
        return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   214
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   215
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   216
    private static void shouldNotCallThis() {
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   217
        // if this gets called, the transformation has not taken place
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   218
        throw new AssertionError("this code should be statically transformed away by Indify");
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   219
    }
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents:
diff changeset
   220
}