Test6991596.java 16.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

/**
 * @test
 * @bug 6991596
 * @summary JSR 292 unimplemented adapter_opt_i2i and adapter_opt_l2i on SPARC
 *
 * @run main/othervm -ea -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles -XX:+EnableInvokeDynamic -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test6991596
 */

import java.dyn.*;

public class Test6991596 {
    private static final Class   CLASS = Test6991596.class;
    private static final String  NAME  = "foo";
38
    private static final boolean DEBUG = System.getProperty("DEBUG", "false").equals("true");
39 40 41 42 43 44 45 46 47 48 49

    public static void main(String[] args) throws Throwable {
        testboolean();
        testbyte();
        testchar();
        testshort();
        testint();
        testlong();
    }

    // Helpers to get various methods.
50
    static MethodHandle getmh1(Class ret, Class arg) throws NoAccessException {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        return MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(ret, arg));
    }
    static MethodHandle getmh2(MethodHandle mh1, Class ret, Class arg) {
        return MethodHandles.convertArguments(mh1, MethodType.methodType(ret, arg));
    }
    static MethodHandle getmh3(MethodHandle mh1, Class ret, Class arg) {
        return MethodHandles.convertArguments(mh1, MethodType.methodType(ret, arg));
    }

    // test adapter_opt_i2i
    static void testboolean() throws Throwable {
        boolean[] a = new boolean[] {
            true,
            false
        };
        for (int i = 0; i < a.length; i++) {
            doboolean(a[i]);
        }
    }
    static void doboolean(boolean x) throws Throwable {
        if (DEBUG)  System.out.println("boolean=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, boolean.class);
            // TODO add this for all cases when the bugs are fixed.
            //MethodHandle mh3 = getmh3(mh1, boolean.class, boolean.class);
79 80
            boolean a = (boolean) mh1.invokeExact((boolean) x);
            boolean b = (boolean) mh2.invokeExact(x);
81
            //boolean c = mh3.<boolean>invokeExact((boolean) x);
82 83
            check(x, a, b);
            //check(x, c, x);
84 85 86 87 88 89
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class,    byte.class   );
            MethodHandle mh2 = getmh2(mh1, byte.class,    boolean.class);
90 91 92
            byte a = (byte) mh1.invokeExact((byte) (x ? 1 : 0));
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
93 94 95 96 97 98
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, boolean.class);
99 100 101
            char a = (char) mh1.invokeExact((char) (x ? 1 : 0));
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
102 103 104 105 106 107
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, boolean.class);
108 109 110
            short a = (short) mh1.invokeExact((short) (x ? 1 : 0));
            short b = (short) mh2.invokeExact(x);
            check(x, a, b);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }
    }

    static void testbyte() throws Throwable {
        byte[] a = new byte[] {
            Byte.MIN_VALUE,
            Byte.MIN_VALUE + 1,
            -0x0F,
            -1,
            0,
            1,
            0x0F,
            Byte.MAX_VALUE - 1,
            Byte.MAX_VALUE
        };
        for (int i = 0; i < a.length; i++) {
            dobyte(a[i]);
        }
    }
    static void dobyte(byte x) throws Throwable {
        if (DEBUG)  System.out.println("byte=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, byte.class);
137 138 139
            boolean a = (boolean) mh1.invokeExact((x & 1) == 1);
            boolean b = (boolean) mh2.invokeExact(x);
            check(x, a, b);
140 141 142 143 144 145
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class, byte.class);
            MethodHandle mh2 = getmh2(mh1, byte.class, byte.class);
146 147 148
            byte a = (byte) mh1.invokeExact((byte) x);
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
149 150 151 152 153 154
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, byte.class);
155 156 157
            char a = (char) mh1.invokeExact((char) x);
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
158 159 160 161 162 163
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, byte.class);
164 165 166
            short a = (short) mh1.invokeExact((short) x);
            short b = (short) mh2.invokeExact(x);
            check(x, a, b);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        }
    }

    static void testchar() throws Throwable {
        char[] a = new char[] {
            Character.MIN_VALUE,
            Character.MIN_VALUE + 1,
            0x000F,
            0x00FF,
            0x0FFF,
            Character.MAX_VALUE - 1,
            Character.MAX_VALUE
        };
        for (int i = 0; i < a.length; i++) {
            dochar(a[i]);
        }
    }
    static void dochar(char x) throws Throwable {
        if (DEBUG)  System.out.println("char=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, char.class);
191 192 193
            boolean a = (boolean) mh1.invokeExact((x & 1) == 1);
            boolean b = (boolean) mh2.invokeExact(x);
            check(x, a, b);
194 195 196 197 198 199
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class, byte.class);
            MethodHandle mh2 = getmh2(mh1, byte.class, char.class);
200 201 202
            byte a = (byte) mh1.invokeExact((byte) x);
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
203 204 205 206 207 208
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, char.class);
209 210 211
            char a = (char) mh1.invokeExact((char) x);
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
212 213 214 215 216 217
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, char.class);
218 219 220
            short a = (short) mh1.invokeExact((short) x);
            short b = (short) mh2.invokeExact(x);
            check(x, a, b);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        }
    }

    static void testshort() throws Throwable {
        short[] a = new short[] {
            Short.MIN_VALUE,
            Short.MIN_VALUE + 1,
            -0x0FFF,
            -0x00FF,
            -0x000F,
            -1,
            0,
            1,
            0x000F,
            0x00FF,
            0x0FFF,
            Short.MAX_VALUE - 1,
            Short.MAX_VALUE
        };
        for (int i = 0; i < a.length; i++) {
            doshort(a[i]);
        }
    }
    static void doshort(short x) throws Throwable {
        if (DEBUG)  System.out.println("short=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, short.class);
251 252 253
            boolean a = (boolean) mh1.invokeExact((x & 1) == 1);
            boolean b = (boolean) mh2.invokeExact(x);
            check(x, a, b);
254 255 256 257 258 259
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class, byte.class);
            MethodHandle mh2 = getmh2(mh1, byte.class, short.class);
260 261 262
            byte a = (byte) mh1.invokeExact((byte) x);
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
263 264 265 266 267 268
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, short.class);
269 270 271
            char a = (char) mh1.invokeExact((char) x);
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
272 273 274 275 276 277
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, short.class);
278 279 280
            short a = (short) mh1.invokeExact((short) x);
            short b = (short) mh2.invokeExact(x);
            check(x, a, b);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        }
    }

    static void testint() throws Throwable {
        int[] a = new int[] {
            Integer.MIN_VALUE,
            Integer.MIN_VALUE + 1,
            -0x0FFFFFFF,
            -0x00FFFFFF,
            -0x000FFFFF,
            -0x0000FFFF,
            -0x00000FFF,
            -0x000000FF,
            -0x0000000F,
            -1,
            0,
            1,
            0x0000000F,
            0x000000FF,
            0x00000FFF,
            0x0000FFFF,
            0x000FFFFF,
            0x00FFFFFF,
            0x0FFFFFFF,
            Integer.MAX_VALUE - 1,
            Integer.MAX_VALUE
        };
        for (int i = 0; i < a.length; i++) {
            doint(a[i]);
        }
    }
    static void doint(int x) throws Throwable {
        if (DEBUG)  System.out.println("int=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, int.class);
319 320 321
            boolean a = (boolean) mh1.invokeExact((x & 1) == 1);
            boolean b = (boolean) mh2.invokeExact(x);
            check(x, a, b);
322 323 324 325 326 327
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class, byte.class);
            MethodHandle mh2 = getmh2(mh1, byte.class, int.class);
328 329 330
            byte a = (byte) mh1.invokeExact((byte) x);
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
331 332 333 334 335 336
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, int.class);
337 338 339
            char a = (char) mh1.invokeExact((char) x);
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
340 341 342 343 344 345
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, int.class);
346 347
            short a = (short) mh1.invokeExact((short) x);
            short b = (short) mh2.invokeExact(x);
348
            assert a == b : a + " != " + b;
349
            check(x, a, b);
350 351 352 353 354 355
        }

        // int
        {
            MethodHandle mh1 = getmh1(     int.class, int.class);
            MethodHandle mh2 = getmh2(mh1, int.class, int.class);
356 357 358
            int a = (int) mh1.invokeExact((int) x);
            int b = (int) mh2.invokeExact(x);
            check(x, a, b);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        }
    }

    // test adapter_opt_l2i
    static void testlong() throws Throwable {
        long[] a = new long[] {
            Long.MIN_VALUE,
            Long.MIN_VALUE + 1,
            -0x000000000FFFFFFFL,
            -0x0000000000FFFFFFL,
            -0x00000000000FFFFFL,
            -0x000000000000FFFFL,
            -0x0000000000000FFFL,
            -0x00000000000000FFL,
            -0x000000000000000FL,
            -1L,
            0L,
            1L,
            0x000000000000000FL,
            0x00000000000000FFL,
            0x0000000000000FFFL,
            0x0000000000000FFFL,
            0x000000000000FFFFL,
            0x00000000000FFFFFL,
            0x0000000000FFFFFFL,
            0x000000000FFFFFFFL,
            Long.MAX_VALUE - 1,
            Long.MAX_VALUE
        };
        for (int i = 0; i < a.length; i++) {
            dolong(a[i]);
        }
    }
    static void dolong(long x) throws Throwable {
        if (DEBUG)  System.out.println("long=" + x);

        // boolean
        {
            MethodHandle mh1 = getmh1(     boolean.class, boolean.class);
            MethodHandle mh2 = getmh2(mh1, boolean.class, long.class);
399 400 401
            boolean a = (boolean) mh1.invokeExact((x & 1L) == 1L);
            boolean b = (boolean) mh2.invokeExact(x);
            check(x, a, b);
402 403 404 405 406 407
        }

        // byte
        {
            MethodHandle mh1 = getmh1(     byte.class, byte.class);
            MethodHandle mh2 = getmh2(mh1, byte.class, long.class);
408 409 410
            byte a = (byte) mh1.invokeExact((byte) x);
            byte b = (byte) mh2.invokeExact(x);
            check(x, a, b);
411 412 413 414 415 416
        }

        // char
        {
            MethodHandle mh1 = getmh1(     char.class, char.class);
            MethodHandle mh2 = getmh2(mh1, char.class, long.class);
417 418 419
            char a = (char) mh1.invokeExact((char) x);
            char b = (char) mh2.invokeExact(x);
            check(x, a, b);
420 421 422 423 424 425
        }

        // short
        {
            MethodHandle mh1 = getmh1(     short.class, short.class);
            MethodHandle mh2 = getmh2(mh1, short.class, long.class);
426 427 428
            short a = (short) mh1.invokeExact((short) x);
            short b = (short) mh2.invokeExact(x);
            check(x, a, b);
429 430 431 432 433 434
        }

        // int
        {
            MethodHandle mh1 = getmh1(     int.class, int.class);
            MethodHandle mh2 = getmh2(mh1, int.class, long.class);
435 436 437
            int a = (int) mh1.invokeExact((int) x);
            int b = (int) mh2.invokeExact(x);
            check(x, a, b);
438 439 440
        }
    }

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    static void check(boolean x, boolean e, boolean a) { p(z2h(x), z2h(e), z2h(a)); assert e == a : z2h(x) + ": " + z2h(e) + " != " + z2h(a); }
    static void check(boolean x, byte    e, byte    a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); }
    static void check(boolean x, int     e, int     a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); }

    static void check(int     x, boolean e, boolean a) { p(i2h(x), z2h(e), z2h(a)); assert e == a : i2h(x) + ": " + z2h(e) + " != " + z2h(a); }
    static void check(int     x, byte    e, byte    a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); }
    static void check(int     x, int     e, int     a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); }

    static void check(long    x, boolean e, boolean a) { p(l2h(x), z2h(e), z2h(a)); assert e == a : l2h(x) + ": " + z2h(e) + " != " + z2h(a); }
    static void check(long    x, byte    e, byte    a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); }
    static void check(long    x, int     e, int     a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); }

    static void p(String x, String e, String a) { if (DEBUG)  System.out.println(x + ": expected: " + e + ", actual: " + a); }

    static String z2h(boolean x) { return x ? "1" : "0"; }
    static String i2h(int     x) { return Integer.toHexString(x); }
    static String l2h(long    x) { return Long.toHexString(x); }

459 460 461 462 463 464 465
    // to int
    public static boolean foo(boolean i) { return i; }
    public static byte    foo(byte    i) { return i; }
    public static char    foo(char    i) { return i; }
    public static short   foo(short   i) { return i; }
    public static int     foo(int     i) { return i; }
}