1 /*
   2  * Copyright (c) 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.valhalla.valuetypes;
  25 
  26 import java.lang.reflect.Array;
  27 import java.lang.reflect.Field;
  28 import java.util.Arrays;
  29 
  30 import jdk.test.lib.Asserts;
  31 import jdk.internal.misc.Unsafe;
  32 
  33 /*
  34  * @test
  35  * @summary Test intrinsic support for value types
  36  * @library /testlibrary /test/lib /compiler/whitebox /
  37  * @modules java.base/jdk.internal.misc
  38  * @requires os.simpleArch == "x64"
  39  * @compile TestIntrinsics.java
  40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox jdk.test.lib.Platform
  41  * @run main/othervm/timeout=120 -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
  42  *                               -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI -XX:+EnableValhalla
  43  *                               compiler.valhalla.valuetypes.ValueTypeTest
  44  *                               compiler.valhalla.valuetypes.TestIntrinsics
  45  */
  46 public class TestIntrinsics extends ValueTypeTest {
  47     // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters()
  48     @Override
  49     public String[] getExtraVMParameters(int scenario) {
  50         switch (scenario) {
  51         case 3: return new String[] {"-XX:-MonomorphicArrayCheck", "-XX:ValueArrayElemMaxFlatSize=-1"};
  52         case 4: return new String[] {"-XX:-MonomorphicArrayCheck"};
  53         }
  54         return null;
  55     }
  56 
  57     public static void main(String[] args) throws Throwable {
  58         TestIntrinsics test = new TestIntrinsics();
  59         test.run(args, MyValue1.class, MyValue2.class, MyValue2Inline.class);
  60     }
  61 
  62     // Test correctness of the Class::isAssignableFrom intrinsic
  63     @Test()
  64     public boolean test1(Class<?> supercls, Class<?> subcls) {
  65         return supercls.isAssignableFrom(subcls);
  66     }
  67 
  68     public void test1_verifier(boolean warmup) {
  69         Asserts.assertTrue(test1(java.util.AbstractList.class, java.util.ArrayList.class), "test1_1 failed");
  70         Asserts.assertTrue(test1(MyValue1.class.asBoxType(), MyValue1.class.asBoxType()), "test1_2 failed");
  71         Asserts.assertTrue(test1(MyValue1.class.asValueType(), MyValue1.class.asValueType()), "test1_3 failed");
  72         Asserts.assertTrue(test1(MyValue1.class.asBoxType(), MyValue1.class.asValueType()), "test1_4 failed");
  73         Asserts.assertTrue(test1(MyValue1.class.asValueType(), MyValue1.class.asBoxType()), "test1_5 failed");
  74         Asserts.assertTrue(test1(Object.class, java.util.ArrayList.class), "test1_6 failed");
  75         Asserts.assertTrue(test1(Object.class, MyValue1.class.asBoxType()), "test1_7 failed");
  76         Asserts.assertTrue(test1(Object.class, MyValue1.class.asValueType()), "test1_8 failed");
  77         Asserts.assertTrue(!test1(MyValue1.class.asBoxType(), Object.class), "test1_9 failed");
  78         Asserts.assertTrue(!test1(MyValue1.class.asValueType(), Object.class), "test1_10 failed");
  79     }
  80 
  81     // Verify that Class::isAssignableFrom checks with statically known classes are folded
  82     @Test(failOn = LOADK)
  83     public boolean test2() {
  84         boolean check1 = java.util.AbstractList.class.isAssignableFrom(java.util.ArrayList.class);
  85         boolean check2 = MyValue1.class.asBoxType().isAssignableFrom(MyValue1.class.asBoxType());
  86         boolean check3 = MyValue1.class.asValueType().isAssignableFrom(MyValue1.class.asValueType());
  87         boolean check4 = MyValue1.class.asBoxType().isAssignableFrom(MyValue1.class.asValueType());
  88         boolean check5 = MyValue1.class.asValueType().isAssignableFrom(MyValue1.class.asBoxType());
  89         boolean check6 = Object.class.isAssignableFrom(java.util.ArrayList.class);
  90         boolean check7 = Object.class.isAssignableFrom(MyValue1.class.asBoxType());
  91         boolean check8 = Object.class.isAssignableFrom(MyValue1.class.asValueType());
  92         boolean check9 = !MyValue1.class.asBoxType().isAssignableFrom(Object.class);
  93         boolean check10 = !MyValue1.class.asValueType().isAssignableFrom(Object.class);
  94         return check1 && check2 && check3 && check4 && check5 && check6 && check7 && check8 && check9 && check10;
  95     }
  96 
  97     public void test2_verifier(boolean warmup) {
  98         Asserts.assertTrue(test2(), "test2 failed");
  99     }
 100 
 101     // Test correctness of the Class::getSuperclass intrinsic
 102     @Test()
 103     public Class<?> test3(Class<?> cls) {
 104         return cls.getSuperclass();
 105     }
 106 
 107     public void test3_verifier(boolean warmup) {
 108         Asserts.assertTrue(test3(Object.class) == null, "test3_1 failed");
 109         Asserts.assertTrue(test3(MyValue1.class.asBoxType()) == Object.class, "test3_2 failed");
 110         Asserts.assertTrue(test3(MyValue1.class.asValueType()) == Object.class, "test3_3 failed");
 111         Asserts.assertTrue(test3(Class.class) == Object.class, "test3_4 failed");
 112     }
 113 
 114     // Verify that Class::getSuperclass checks with statically known classes are folded
 115     @Test(failOn = LOADK)
 116     public boolean test4() {
 117         boolean check1 = Object.class.getSuperclass() == null;
 118         boolean check2 = MyValue1.class.asBoxType().getSuperclass() == Object.class;
 119         boolean check3 = MyValue1.class.asValueType().getSuperclass() == Object.class;
 120         boolean check4 = Class.class.getSuperclass() == Object.class;
 121         return check1 && check2 && check3 && check4;
 122     }
 123 
 124     public void test4_verifier(boolean warmup) {
 125         Asserts.assertTrue(test4(), "test4 failed");
 126     }
 127 
 128     // Test toString() method
 129     @Test()
 130     public String test5(MyValue1 v) {
 131         return v.toString();
 132     }
 133 
 134     @DontCompile
 135     public void test5_verifier(boolean warmup) {
 136         MyValue1 v = MyValue1.createDefaultInline();
 137         test5(v);
 138     }
 139 
 140     // Test hashCode() method
 141     @Test()
 142     public int test6(MyValue1 v) {
 143         return v.hashCode();
 144     }
 145 
 146     @DontCompile
 147     public void test6_verifier(boolean warmup) {
 148         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 149         int res = test6(v);
 150         Asserts.assertEQ(res, v.hashCode());
 151     }
 152 
 153     // Test default value type array creation via reflection
 154     @Test()
 155     public Object[] test7(Class<?> componentType, int len) {
 156         Object[] va = (Object[])Array.newInstance(componentType, len);
 157         return va;
 158     }
 159 
 160     @DontCompile
 161     public void test7_verifier(boolean warmup) {
 162         int len = Math.abs(rI) % 42;
 163         long hash = MyValue1.createDefaultDontInline().hashPrimitive();
 164         Object[] va = test7(MyValue1.class.asValueType(), len);
 165         for (int i = 0; i < len; ++i) {
 166             Asserts.assertEQ(((MyValue1)va[i]).hashPrimitive(), hash);
 167         }
 168     }
 169 
 170     // Class.isInstance
 171     @Test()
 172     public boolean test8(Class c, MyValue1 vt) {
 173         return c.isInstance(vt);
 174     }
 175 
 176     @DontCompile
 177     public void test8_verifier(boolean warmup) {
 178         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 179         boolean result = test8(MyValue1.class.asValueType(), vt);
 180         Asserts.assertTrue(result);
 181         result = test8(MyValue1.class.asBoxType(), vt);
 182         Asserts.assertTrue(result);
 183     }
 184 
 185     @Test()
 186     public boolean test9(Class c, MyValue1 vt) {
 187         return c.isInstance(vt);
 188     }
 189 
 190     @DontCompile
 191     public void test9_verifier(boolean warmup) {
 192         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 193         boolean result = test9(MyValue2.class.asValueType(), vt);
 194         Asserts.assertFalse(result);
 195         result = test9(MyValue2.class.asBoxType(), vt);
 196         Asserts.assertFalse(result);
 197     }
 198 
 199     // Class.cast
 200     @Test()
 201     public Object test10(Class c, MyValue1 vt) {
 202         return c.cast(vt);
 203     }
 204 
 205     @DontCompile
 206     public void test10_verifier(boolean warmup) {
 207         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 208         Object result = test10(MyValue1.class.asValueType(), vt);
 209         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 210     }
 211 
 212     @Test()
 213     public Object test11(Class c, MyValue1 vt) {
 214         return c.cast(vt);
 215     }
 216 
 217     @DontCompile
 218     public void test11_verifier(boolean warmup) {
 219         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 220         try {
 221             test11(MyValue2.class.asValueType(), vt);
 222             throw new RuntimeException("should have thrown");
 223         } catch (ClassCastException cce) {
 224         }
 225     }
 226 
 227     @Test()
 228     public Object test12(MyValue1 vt) {
 229         return MyValue1.class.asValueType().cast(vt);
 230     }
 231 
 232     @DontCompile
 233     public void test12_verifier(boolean warmup) {
 234         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 235         Object result = test12(vt);
 236         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 237     }
 238 
 239     @Test()
 240     public Object test13(MyValue1 vt) {
 241         return MyValue2.class.asValueType().cast(vt);
 242     }
 243 
 244     @DontCompile
 245     public void test13_verifier(boolean warmup) {
 246         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 247         try {
 248             test13(vt);
 249             throw new RuntimeException("should have thrown");
 250         } catch (ClassCastException cce) {
 251         }
 252     }
 253 
 254     // value type array creation via reflection
 255     @Test()
 256     public void test14(int len, long hash) {
 257         Object[] va = (Object[])Array.newInstance(MyValue1.class.asValueType().asBoxType().asValueType(), len);
 258         for (int i = 0; i < len; ++i) {
 259             Asserts.assertEQ(((MyValue1)va[i]).hashPrimitive(), hash);
 260         }
 261     }
 262 
 263     @DontCompile
 264     public void test14_verifier(boolean warmup) {
 265         int len = Math.abs(rI) % 42;
 266         long hash = MyValue1.createDefaultDontInline().hashPrimitive();
 267         test14(len, hash);
 268     }
 269 
 270     // Test hashCode() method
 271     @Test()
 272     public int test15(Object v) {
 273         return v.hashCode();
 274     }
 275 
 276     @DontCompile
 277     public void test15_verifier(boolean warmup) {
 278         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 279         int res = test15(v);
 280         Asserts.assertEQ(res, v.hashCode());
 281     }
 282 
 283     @Test()
 284     public int test16(Object v) {
 285         return System.identityHashCode(v);
 286     }
 287 
 288     @DontCompile
 289     public void test16_verifier(boolean warmup) {
 290         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 291         int res = test16(v);
 292         Asserts.assertEQ(res, System.identityHashCode((Object)v));
 293     }
 294 
 295     @Test()
 296     public int test17(Object v) {
 297         return System.identityHashCode(v);
 298     }
 299 
 300     @DontCompile
 301     public void test17_verifier(boolean warmup) {
 302         Integer v = new Integer(rI);
 303         int res = test17(v);
 304         Asserts.assertEQ(res, System.identityHashCode(v));
 305     }
 306 
 307     @Test()
 308     public int test18(Object v) {
 309         return System.identityHashCode(v);
 310     }
 311 
 312     @DontCompile
 313     public void test18_verifier(boolean warmup) {
 314         Object v = null;
 315         int res = test18(v);
 316         Asserts.assertEQ(res, System.identityHashCode(v));
 317     }
 318 
 319     // hashCode() and toString() with different value types
 320     @Test()
 321     public int test19(MyValue1 vt1, MyValue1 vt2, boolean b) {
 322         MyValue1 res = b ? vt1 : vt2;
 323         return res.hashCode();
 324     }
 325 
 326     @DontCompile
 327     public void test19_verifier(boolean warmup) {
 328         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 329         int res = test19(vt, vt, true);
 330         Asserts.assertEQ(res, vt.hashCode());
 331         res = test19(vt, vt, false);
 332         Asserts.assertEQ(res, vt.hashCode());
 333     }
 334 
 335     @Test()
 336     public String test20(MyValue1 vt1, MyValue1 vt2, boolean b) {
 337         MyValue1 res = b ? vt1 : vt2;
 338         return res.toString();
 339     }
 340 
 341     @DontCompile
 342     public void test20_verifier(boolean warmup) {
 343         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 344         String res = test20(vt, vt, true);
 345         Asserts.assertEQ(res, vt.toString());
 346         res = test20(vt, vt, false);
 347         Asserts.assertEQ(res, vt.toString());
 348     }
 349 
 350     private static final Unsafe U = Unsafe.getUnsafe();
 351     private static final long X_OFFSET;
 352     private static final long Y_OFFSET;
 353     private static final long V1_OFFSET;
 354     private static final boolean V1_FLATTENED;
 355     static {
 356         try {
 357             Field xField = MyValue1.class.asValueType().getDeclaredField("x");
 358             X_OFFSET = U.objectFieldOffset(xField);
 359             Field yField = MyValue1.class.asValueType().getDeclaredField("y");
 360             Y_OFFSET = U.objectFieldOffset(yField);
 361             Field v1Field = MyValue1.class.asValueType().getDeclaredField("v1");
 362             V1_OFFSET = U.objectFieldOffset(v1Field);
 363             V1_FLATTENED = U.isFlattened(v1Field);
 364         } catch (Exception e) {
 365             throw new RuntimeException(e);
 366         }
 367     }
 368 
 369     protected static final String CALL_Unsafe = START + "CallStaticJava" + MID + "# Static  jdk.internal.misc.Unsafe::" + END;
 370 
 371     @Test(failOn=CALL_Unsafe)
 372     public int test21(MyValue1 v) {
 373        return U.getInt(v, X_OFFSET);
 374     }
 375 
 376     @DontCompile
 377     public void test21_verifier(boolean warmup) {
 378         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 379         int res = test21(v);
 380         Asserts.assertEQ(res, v.x);
 381     }
 382 
 383     MyValue1 test22_vt;
 384     @Test(failOn=CALL_Unsafe + ALLOC)
 385     public void test22(MyValue1 v) {
 386         v = U.makePrivateBuffer(v);
 387         U.putInt(v, X_OFFSET, rI);
 388         v = U.finishPrivateBuffer(v);
 389         test22_vt = v;
 390     }
 391 
 392     @DontCompile
 393     public void test22_verifier(boolean warmup) {
 394         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 395         test22(v.setX(v, 0));
 396         Asserts.assertEQ(test22_vt.hash(), v.hash());
 397     }
 398 
 399     @Test(failOn=CALL_Unsafe)
 400     public int test23(MyValue1 v, long offset) {
 401         return U.getInt(v, offset);
 402     }
 403 
 404     @DontCompile
 405     public void test23_verifier(boolean warmup) {
 406         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 407         int res = test23(v, X_OFFSET);
 408         Asserts.assertEQ(res, v.x);
 409     }
 410 
 411     MyValue1 test24_vt = MyValue1.createWithFieldsInline(rI, rL);
 412 
 413     @Test(failOn=CALL_Unsafe)
 414     public int test24(long offset) {
 415         return U.getInt(test24_vt, offset);
 416     }
 417 
 418     @DontCompile
 419     public void test24_verifier(boolean warmup) {
 420         int res = test24(X_OFFSET);
 421         Asserts.assertEQ(res, test24_vt.x);
 422     }
 423 
 424     // Test copyOf intrinsic with allocated value type in it's debug information
 425     final inline class Test25Value {
 426         final int x;
 427         public Test25Value() {
 428             this.x = 42;
 429         }
 430     }
 431 
 432     final Test25Value[] test25Array = new Test25Value[10];
 433 
 434     @Test
 435     public Test25Value[] test25(Test25Value element) {
 436         Test25Value[] newArray = Arrays.copyOf(test25Array, test25Array.length + 1);
 437         newArray[test25Array.length] = element;
 438         return newArray;
 439     }
 440 
 441     @DontCompile
 442     public void test25_verifier(boolean warmup) {
 443         Test25Value vt = new Test25Value();
 444         test25(vt);
 445     }
 446 
 447     @Test
 448     public Object test26() {
 449         Class<?>[] ca = new Class<?>[1];
 450         for (int i = 0; i < 1; ++i) {
 451           // Folds during loop opts
 452           ca[i] = MyValue1.class.asValueType();
 453         }
 454         return Array.newInstance(ca[0], 1);
 455     }
 456 
 457     @DontCompile
 458     public void test26_verifier(boolean warmup) {
 459         Object[] res = (Object[])test26();
 460         Asserts.assertEQ(((MyValue1)res[0]).hashPrimitive(), MyValue1.createDefaultInline().hashPrimitive());
 461     }
 462 
 463     // Load non-flattenable value type field with unsafe
 464     MyValue1? test27_vt = MyValue1.createWithFieldsInline(rI, rL);
 465     private static final long TEST27_OFFSET;
 466     static {
 467         try {
 468             Field field = TestIntrinsics.class.getDeclaredField("test27_vt");
 469             TEST27_OFFSET = U.objectFieldOffset(field);
 470         } catch (Exception e) {
 471             throw new RuntimeException(e);
 472         }
 473     }
 474 
 475     @Test(failOn=CALL_Unsafe)
 476     public MyValue1 test27() {
 477         return (MyValue1)U.getReference(this, TEST27_OFFSET);
 478     }
 479 
 480     @DontCompile
 481     public void test27_verifier(boolean warmup) {
 482         MyValue1 res = test27();
 483         Asserts.assertEQ(res.hash(), test24_vt.hash());
 484     }
 485 
 486     // Mismatched type
 487     @Test(failOn=CALL_Unsafe)
 488     public int test28(MyValue1 v) {
 489         return U.getByte(v, X_OFFSET);
 490     }
 491 
 492     @DontCompile
 493     public void test28_verifier(boolean warmup) {
 494         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 495         int res = test28(v);
 496         if (java.nio.ByteOrder.nativeOrder() == java.nio.ByteOrder.LITTLE_ENDIAN) {
 497             Asserts.assertEQ(res, (int)((byte)v.x));
 498         } else {
 499             Asserts.assertEQ(res, (int)((byte)Integer.reverseBytes(v.x)));
 500         }
 501     }
 502 
 503     // Wrong alignment
 504     @Test(failOn=CALL_Unsafe)
 505     public long test29(MyValue1 v) {
 506         // Read the field that's guaranteed to not be last in the
 507         // value so we don't read out of the value
 508         if (X_OFFSET < Y_OFFSET) {
 509             return U.getInt(v, X_OFFSET+1);
 510         }
 511         return U.getLong(v, Y_OFFSET+1);
 512     }
 513 
 514     @DontCompile
 515     public void test29_verifier(boolean warmup) {
 516         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 517         long res = test29(v);
 518         if (java.nio.ByteOrder.nativeOrder() == java.nio.ByteOrder.LITTLE_ENDIAN) {
 519             if (X_OFFSET < Y_OFFSET) {
 520                 Asserts.assertEQ(((int)res) << 8, (v.x >> 8) << 8);
 521             } else {
 522                 Asserts.assertEQ(res << 8, (v.y >> 8) << 8);
 523             }
 524         } else {
 525             if (X_OFFSET < Y_OFFSET) {
 526                 Asserts.assertEQ(((int)res), v.x >>> 8);
 527             } else {
 528                 Asserts.assertEQ(res, v.y >>> 8);
 529             }
 530         }
 531     }
 532 
 533     // getValue to retrieve flattened field from value
 534     @Test(failOn=CALL_Unsafe)
 535     public MyValue2 test30(MyValue1 v) {
 536         if (V1_FLATTENED) {
 537             return U.getValue(v, V1_OFFSET, MyValue2.class.asValueType().asBoxType().asValueType());
 538         }
 539         return (MyValue2)U.getReference(v, V1_OFFSET);
 540     }
 541 
 542     @DontCompile
 543     public void test30_verifier(boolean warmup) {
 544         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 545         MyValue2 res = test30(v);
 546         Asserts.assertEQ(res.hash(), v.v1.hash());
 547     }
 548 
 549     MyValue1 test31_vt;
 550     private static final long TEST31_VT_OFFSET;
 551     private static final boolean TEST31_VT_FLATTENED;
 552     static {
 553         try {
 554             Field test31_vt_Field = TestIntrinsics.class.getDeclaredField("test31_vt");
 555             TEST31_VT_OFFSET = U.objectFieldOffset(test31_vt_Field);
 556             TEST31_VT_FLATTENED = U.isFlattened(test31_vt_Field);
 557         } catch (Exception e) {
 558             throw new RuntimeException(e);
 559         }
 560     }
 561 
 562     // getValue to retrieve flattened field from object
 563     @Test(failOn=CALL_Unsafe)
 564     public MyValue1 test31() {
 565         if (TEST31_VT_FLATTENED) {
 566             return U.getValue(this, TEST31_VT_OFFSET, MyValue1.class.asValueType().asBoxType().asValueType());
 567         }
 568         return (MyValue1)U.getReference(this, TEST31_VT_OFFSET);
 569     }
 570 
 571     @DontCompile
 572     public void test31_verifier(boolean warmup) {
 573         test31_vt = MyValue1.createWithFieldsInline(rI, rL);
 574         MyValue1 res = test31();
 575         Asserts.assertEQ(res.hash(), test31_vt.hash());
 576     }
 577 
 578     // putValue to set flattened field in object
 579     @Test(failOn=CALL_Unsafe)
 580     public void test32(MyValue1 vt) {
 581         if (TEST31_VT_FLATTENED) {
 582             U.putValue(this, TEST31_VT_OFFSET, MyValue1.class.asValueType().asBoxType().asValueType(), vt);
 583         } else {
 584             U.putReference(this, TEST31_VT_OFFSET, vt);
 585         }
 586     }
 587 
 588     @DontCompile
 589     public void test32_verifier(boolean warmup) {
 590         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 591         test31_vt = MyValue1.createDefaultInline();
 592         test32(vt);
 593         Asserts.assertEQ(vt.hash(), test31_vt.hash());
 594     }
 595 
 596     private static final int TEST33_BASE_OFFSET;
 597     private static final int TEST33_INDEX_SCALE;
 598     private static final boolean TEST33_FLATTENED_ARRAY;
 599     static {
 600         try {
 601             TEST33_BASE_OFFSET = U.arrayBaseOffset(MyValue1[].class);
 602             TEST33_INDEX_SCALE = U.arrayIndexScale(MyValue1[].class);
 603             TEST33_FLATTENED_ARRAY = U.isFlattenedArray(MyValue1[].class);
 604         } catch (Exception e) {
 605             throw new RuntimeException(e);
 606         }
 607     }
 608     // getValue to retrieve flattened field from array
 609     @Test(failOn=CALL_Unsafe)
 610     public MyValue1 test33(MyValue1[] arr) {
 611         if (TEST33_FLATTENED_ARRAY) {
 612             return U.getValue(arr, TEST33_BASE_OFFSET + TEST33_INDEX_SCALE, MyValue1.class.asValueType().asBoxType().asValueType());
 613         }
 614         return (MyValue1)U.getReference(arr, TEST33_BASE_OFFSET + TEST33_INDEX_SCALE);
 615     }
 616 
 617     @DontCompile
 618     public void test33_verifier(boolean warmup) {
 619         MyValue1[] arr = new MyValue1[2];
 620         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 621         arr[1] = vt;
 622         MyValue1 res = test33(arr);
 623         Asserts.assertEQ(res.hash(), vt.hash());
 624     }
 625 
 626     // putValue to set flattened field in array
 627     @Test(failOn=CALL_Unsafe)
 628     public void test34(MyValue1[] arr, MyValue1 vt) {
 629         if (TEST33_FLATTENED_ARRAY) {
 630             U.putValue(arr, TEST33_BASE_OFFSET + TEST33_INDEX_SCALE, MyValue1.class.asValueType().asBoxType().asValueType(), vt);
 631         } else {
 632             U.putReference(arr, TEST33_BASE_OFFSET + TEST33_INDEX_SCALE, vt);
 633         }
 634     }
 635 
 636     @DontCompile
 637     public void test34_verifier(boolean warmup) {
 638         MyValue1[] arr = new MyValue1[2];
 639         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 640         test34(arr, vt);
 641         Asserts.assertEQ(arr[1].hash(), vt.hash());
 642     }
 643 
 644     // getValue to retrieve flattened field from object with unknown
 645     // container type
 646     @Test(failOn=CALL_Unsafe)
 647     public MyValue1 test35(Object o) {
 648         if (TEST31_VT_FLATTENED) {
 649             return U.getValue(o, TEST31_VT_OFFSET, MyValue1.class.asValueType().asBoxType().asValueType());
 650         }
 651         return (MyValue1)U.getReference(o, TEST31_VT_OFFSET);
 652     }
 653 
 654     @DontCompile
 655     public void test35_verifier(boolean warmup) {
 656         test31_vt = MyValue1.createWithFieldsInline(rI, rL);
 657         MyValue1 res = test35(this);
 658         Asserts.assertEQ(res.hash(), test31_vt.hash());
 659     }
 660 
 661     // getValue to retrieve flattened field from object at unknown
 662     // offset
 663     @Test(failOn=CALL_Unsafe)
 664     public MyValue1 test36(long offset) {
 665         if (TEST31_VT_FLATTENED) {
 666             return U.getValue(this, offset, MyValue1.class.asValueType().asBoxType().asValueType());
 667         }
 668         return (MyValue1)U.getReference(this, offset);
 669     }
 670 
 671     @DontCompile
 672     public void test36_verifier(boolean warmup) {
 673         test31_vt = MyValue1.createWithFieldsInline(rI, rL);
 674         MyValue1 res = test36(TEST31_VT_OFFSET);
 675         Asserts.assertEQ(res.hash(), test31_vt.hash());
 676     }
 677 
 678     // putValue to set flattened field in object with unknown
 679     // container
 680     @Test(failOn=CALL_Unsafe)
 681     public void test37(Object o, MyValue1 vt) {
 682         if (TEST31_VT_FLATTENED) {
 683             U.putValue(o, TEST31_VT_OFFSET, MyValue1.class.asValueType().asBoxType().asValueType(), vt);
 684         } else {
 685             U.putReference(o, TEST31_VT_OFFSET, vt);
 686         }
 687     }
 688 
 689     @DontCompile
 690     public void test37_verifier(boolean warmup) {
 691         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 692         test31_vt = MyValue1.createDefaultInline();
 693         test37(this, vt);
 694         Asserts.assertEQ(vt.hash(), test31_vt.hash());
 695     }
 696 
 697     // putValue to set flattened field in object, non value argument
 698     // to store
 699     @Test(match = { CALL_Unsafe }, matchCount = { 1 })
 700     public void test38(Object o) {
 701         if (TEST31_VT_FLATTENED) {
 702             U.putValue(this, TEST31_VT_OFFSET, MyValue1.class.asValueType().asBoxType().asValueType(), o);
 703         } else {
 704             U.putReference(this, TEST31_VT_OFFSET, o);
 705         }
 706     }
 707 
 708     @DontCompile
 709     public void test38_verifier(boolean warmup) {
 710         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 711         test31_vt = MyValue1.createDefaultInline();
 712         test38(vt);
 713         Asserts.assertEQ(vt.hash(), test31_vt.hash());
 714     }
 715 
 716     @Test(failOn=CALL_Unsafe)
 717     public MyValue1 test39(MyValue1 v) {
 718         v = U.makePrivateBuffer(v);
 719         U.putInt(v, X_OFFSET, rI);
 720         v = U.finishPrivateBuffer(v);
 721         return v;
 722     }
 723 
 724     @DontCompile
 725     public void test39_verifier(boolean warmup) {
 726         MyValue1 v = MyValue1.createWithFieldsInline(rI, rL);
 727         MyValue1 res = test39(v.setX(v, 0));
 728         Asserts.assertEQ(res.hash(), v.hash());
 729     }
 730 
 731     // Test default value type array creation via reflection
 732     @Test()
 733     public Object[] test40(Class<?> componentType, int len) {
 734         Object[] va = (Object[])Array.newInstance(componentType, len);
 735         return va;
 736     }
 737 
 738     @DontCompile
 739     public void test40_verifier(boolean warmup) {
 740         int len = Math.abs(rI) % 42;
 741         Object[] va = test40(MyValue1.class.asBoxType(), len);
 742         for (int i = 0; i < len; ++i) {
 743             Asserts.assertEQ(va[i], null);
 744         }
 745     }
 746 
 747     // Class.isInstance
 748     @Test()
 749     public boolean test41(Class c, MyValue1? vt) {
 750         return c.isInstance(vt);
 751     }
 752 
 753     @DontCompile
 754     public void test41_verifier(boolean warmup) {
 755         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 756         boolean result = test41(MyValue1.class.asBoxType(), vt);
 757         Asserts.assertTrue(result);
 758         result = test41(MyValue1.class.asValueType(), vt);
 759         Asserts.assertTrue(result);
 760     }
 761 
 762     @Test()
 763     public boolean test42(Class c, MyValue1? vt) {
 764         return c.isInstance(vt);
 765     }
 766 
 767     @DontCompile
 768     public void test42_verifier(boolean warmup) {
 769         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 770         boolean result = test42(MyValue2.class.asBoxType(), vt);
 771         Asserts.assertFalse(result);
 772         result = test42(MyValue2.class.asValueType(), vt);
 773         Asserts.assertFalse(result);
 774     }
 775 
 776     // Class.cast
 777     @Test()
 778     public Object test43(Class c, MyValue1? vt) {
 779         return c.cast(vt);
 780     }
 781 
 782     @DontCompile
 783     public void test43_verifier(boolean warmup) {
 784         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 785         Object result = test43(MyValue1.class.asBoxType(), vt);
 786         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 787         result = test43(MyValue1.class.asBoxType(), null);
 788         Asserts.assertEQ(result, null);
 789     }
 790 
 791     @Test()
 792     public Object test44(Class c, MyValue1? vt) {
 793         return c.cast(vt);
 794     }
 795 
 796     @DontCompile
 797     public void test44_verifier(boolean warmup) {
 798         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 799         try {
 800             test44(MyValue2.class.asBoxType(), vt);
 801             throw new RuntimeException("should have thrown");
 802         } catch (ClassCastException cce) {
 803         }
 804     }
 805 
 806     @Test()
 807     public Object test45(MyValue1? vt) {
 808         return MyValue1.class.asBoxType().cast(vt);
 809     }
 810 
 811     @DontCompile
 812     public void test45_verifier(boolean warmup) {
 813         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 814         Object result = test45(vt);
 815         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 816         result = test45(null);
 817         Asserts.assertEQ(result, null);
 818     }
 819 
 820     @Test()
 821     public Object test46(MyValue1? vt) {
 822         return MyValue2.class.asBoxType().cast(vt);
 823     }
 824 
 825     @DontCompile
 826     public void test46_verifier(boolean warmup) {
 827         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 828         test46(null);
 829         try {
 830             test46(vt);
 831             throw new RuntimeException("should have thrown");
 832         } catch (ClassCastException cce) {
 833         }
 834     }
 835 
 836     @Test()
 837     public Object test47(MyValue1? vt) {
 838         return MyValue1.class.asValueType().cast(vt);
 839     }
 840 
 841     @DontCompile
 842     public void test47_verifier(boolean warmup) {
 843         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 844         Object result = test47(vt);
 845         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 846         try {
 847             test47(null);
 848             throw new RuntimeException("should have thrown");
 849         } catch (NullPointerException npe) {
 850         }
 851     }
 852 
 853     @Test()
 854     public Object test48(Class c, MyValue1? vt) {
 855         return c.cast(vt);
 856     }
 857 
 858     @DontCompile
 859     public void test48_verifier(boolean warmup) {
 860         MyValue1? vt = MyValue1.createWithFieldsInline(rI, rL);
 861         Object result = test48(MyValue1.class.asValueType(), vt);
 862         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 863         try {
 864             test48(MyValue1.class.asValueType(), null);
 865             throw new RuntimeException("should have thrown");
 866         } catch (NullPointerException npe) {
 867         }
 868     }
 869 
 870     @Test()
 871     public Object test49(MyValue1 vt) {
 872         return MyValue1.class.asBoxType().cast(vt);
 873     }
 874 
 875     @DontCompile
 876     public void test49_verifier(boolean warmup) {
 877         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 878         Object result = test49(vt);
 879         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 880     }
 881 
 882     @Test()
 883     public Object test50(Class c, Object obj) {
 884         return c.cast(obj);
 885     }
 886 
 887     @DontCompile
 888     public void test50_verifier(boolean warmup) {
 889         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
 890         MyValue1[] va  = new MyValue1[42];
 891         MyValue1?[] vba = new MyValue1?[42];
 892         Object result = test50(MyValue1.class.asValueType(), vt);
 893         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 894         result = test50(MyValue1.class.asBoxType(), vt);
 895         Asserts.assertEQ(((MyValue1)result).hash(), vt.hash());
 896         result = test50(MyValue1[].class, va);
 897         Asserts.assertEQ(result, va);
 898         result = test50(MyValue1?[].class, vba);
 899         Asserts.assertEQ(result, vba);
 900         result = test50(MyValue1?[].class, va);
 901         Asserts.assertEQ(result, va);
 902         try {
 903             test50(MyValue1.class.asValueType(), null);
 904             throw new RuntimeException("should have thrown");
 905         } catch (NullPointerException npe) {
 906         }
 907         try {
 908             test50(MyValue1[].class, vba);
 909             throw new RuntimeException("should have thrown");
 910         } catch (ClassCastException cce) {
 911         }
 912     }
 913 
 914     // value type array creation via reflection
 915     @Test()
 916     public void test51(int len) {
 917         Object[] va = (Object[])Array.newInstance(MyValue1.class.asBoxType().asValueType().asBoxType(), len);
 918         for (int i = 0; i < len; ++i) {
 919             Asserts.assertEQ(va[i], null);
 920         }
 921     }
 922 
 923     @DontCompile
 924     public void test51_verifier(boolean warmup) {
 925         int len = Math.abs(rI) % 42;
 926         test51(len);
 927     }
 928 
 929     // multidimensional value type array creation via reflection
 930     @Test()
 931     public Object[][] test52(int len, int val) {
 932         MyValue1[][] va1 = (MyValue1[][])Array.newInstance(MyValue1[].class, len);
 933         MyValue1?[][] va2 = (MyValue1?[][])Array.newInstance(MyValue1?[].class, len);
 934         Object[][] result;
 935         if (val == 1) {
 936             va1[0] = new MyValue1[1];
 937             result = va1;
 938         } else {
 939             va2[0] = new MyValue1?[1];
 940             result = va2;
 941         }
 942         if (val == 1) {
 943             Asserts.assertEQ(va1[0][0].hash(), ((MyValue1)result[0][0]).hash());
 944         } else {
 945             Asserts.assertEQ(result[0][0], null);
 946             result[0][0] = null;
 947         }
 948         return result;
 949     }
 950 
 951     @DontCompile
 952     public void test52_verifier(boolean warmup) {
 953         test52(1, 1);
 954         test52(1, 2);
 955     }
 956 
 957     @Test()
 958     public Object[][] test53(Class<?> c1, Class<?> c2, int len, int val) {
 959         MyValue1[][] va1 = (MyValue1[][])Array.newInstance(MyValue1[].class, len);
 960         MyValue1?[][] va2 = (MyValue1?[][])Array.newInstance(MyValue1?[].class, len);
 961         Object[][] va3 = (Object[][])Array.newInstance(c1, len);
 962         Object[][] va4 = (Object[][])Array.newInstance(c2, len);
 963         for (int i = 0; i < len; ++i) {
 964             Asserts.assertEQ(va1[i], null);
 965             Asserts.assertEQ(va2[i], null);
 966             Asserts.assertEQ(va3[i], null);
 967             Asserts.assertEQ(va4[i], null);
 968             va1[i] = new MyValue1[1];
 969             va2[i] = new MyValue1?[1];
 970             va3[i] = new MyValue1[1];
 971             va4[i] = new MyValue1?[1];
 972             Asserts.assertEQ(va1[i][0].hash(), ((MyValue1)va3[i][0]).hash());
 973             Asserts.assertEQ(va2[i][0], null);
 974             Asserts.assertEQ(va4[i][0], null);
 975         }
 976         Object[][] result;
 977         if (val == 1) {
 978             result = va1;
 979         } else if (val == 2) {
 980             result = va2;
 981         } else if (val == 3) {
 982             result = va3;
 983         } else {
 984             result = va4;
 985         }
 986         if ((val == 1 || val == 3) && len > 0) {
 987             Asserts.assertEQ(va1[0][0].hash(), ((MyValue1)result[0][0]).hash());
 988         } else if (len > 0) {
 989             Asserts.assertEQ(result[0][0], null);
 990             result[0][0] = null;
 991         }
 992         return result;
 993     }
 994 
 995     @DontCompile
 996     public void test53_verifier(boolean warmup) {
 997         int len = Math.abs(rI) % 42;
 998         test53(MyValue1[].class, MyValue1?[].class, len, 1);
 999         test53(MyValue1[].class, MyValue1?[].class, len, 2);
1000         test53(MyValue1[].class, MyValue1?[].class, len, 3);
1001         test53(MyValue1[].class, MyValue1?[].class, len, 4);
1002     }
1003 }