45 * -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI -XX:+EnableValhalla 46 * compiler.valhalla.valuetypes.ValueTypeTest 47 * compiler.valhalla.valuetypes.TestMethodHandles 48 */ 49 public class TestMethodHandles extends ValueTypeTest { 50 // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters() 51 @Override 52 public String[] getExtraVMParameters(int scenario) { 53 switch (scenario) { 54 case 3: return new String[] {"-XX:-ValueArrayFlatten"}; 55 } 56 return null; 57 } 58 59 static { 60 try { 61 Class<?> clazz = TestMethodHandles.class; 62 ClassLoader loader = clazz.getClassLoader(); 63 MethodHandles.Lookup lookup = MethodHandles.lookup(); 64 65 MethodType mt = MethodType.methodType(MyValue3.class); 66 test1_mh = lookup.findVirtual(clazz, "test1_target", mt); 67 test2_mh = lookup.findVirtual(clazz, "test2_target", mt); 68 test3_mh = lookup.findVirtual(clazz, "test3_target", mt); 69 70 MethodType test4_mt1 = MethodType.methodType(int.class, MyValue1.class); 71 MethodType test4_mt2 = MethodType.methodType(MyValue1.class); 72 MethodHandle test4_mh1 = lookup.findStatic(clazz, "test4_helper1", test4_mt1); 73 MethodHandle test4_mh2 = lookup.findStatic(clazz, "test4_helper2", test4_mt2); 74 test4_mh = MethodHandles.filterReturnValue(test4_mh2, test4_mh1); 75 76 MethodType test5_mt = MethodType.methodType(int.class, MyValue1.class); 77 test5_mh = lookup.findVirtual(clazz, "test5_target", test5_mt); 78 79 MethodType test6_mt = MethodType.methodType(MyValue3.class); 80 MethodHandle test6_mh1 = lookup.findVirtual(clazz, "test6_target1", test6_mt); 81 MethodHandle test6_mh2 = lookup.findVirtual(clazz, "test6_target2", test6_mt); 82 MethodType boolean_mt = MethodType.methodType(boolean.class); 83 MethodHandle test6_mh_test = lookup.findVirtual(clazz, "test6_test", boolean_mt); 84 test6_mh = MethodHandles.guardWithTest(test6_mh_test, test6_mh1, test6_mh2); 85 86 MethodType myvalue2_mt = MethodType.methodType(MyValue2.class); 87 test7_mh1 = lookup.findStatic(clazz, "test7_target1", myvalue2_mt); 88 MethodHandle test7_mh2 = lookup.findStatic(clazz, "test7_target2", myvalue2_mt); 89 MethodHandle test7_mh_test = lookup.findStatic(clazz, "test7_test", boolean_mt); 90 test7_mh = MethodHandles.guardWithTest(test7_mh_test, 91 MethodHandles.invoker(myvalue2_mt), 92 MethodHandles.dropArguments(test7_mh2, 0, MethodHandle.class)); 93 94 MethodHandle test8_mh1 = lookup.findStatic(clazz, "test8_target1", myvalue2_mt); 95 test8_mh2 = lookup.findStatic(clazz, "test8_target2", myvalue2_mt); 96 MethodHandle test8_mh_test = lookup.findStatic(clazz, "test8_test", boolean_mt); 97 test8_mh = MethodHandles.guardWithTest(test8_mh_test, 98 MethodHandles.dropArguments(test8_mh1, 0, MethodHandle.class), 99 MethodHandles.invoker(myvalue2_mt)); 100 101 MethodType test9_mt = MethodType.methodType(MyValue3.class); 102 MethodHandle test9_mh1 = lookup.findVirtual(clazz, "test9_target1", test9_mt); 103 MethodHandle test9_mh2 = lookup.findVirtual(clazz, "test9_target2", test9_mt); 104 MethodHandle test9_mh3 = lookup.findVirtual(clazz, "test9_target3", test9_mt); 105 MethodType test9_mt2 = MethodType.methodType(boolean.class); 106 MethodHandle test9_mh_test1 = lookup.findVirtual(clazz, "test9_test1", test9_mt2); 107 MethodHandle test9_mh_test2 = lookup.findVirtual(clazz, "test9_test2", test9_mt2); 108 test9_mh = MethodHandles.guardWithTest(test9_mh_test1, 109 test9_mh1, 110 MethodHandles.guardWithTest(test9_mh_test2, test9_mh2, test9_mh3)); 111 112 MethodType test10_mt = MethodType.methodType(MyValue2.class); 113 MethodHandle test10_mh1 = lookup.findStatic(clazz, "test10_target1", test10_mt); 114 test10_mh2 = lookup.findStatic(clazz, "test10_target2", test10_mt); 115 test10_mh3 = lookup.findStatic(clazz, "test10_target3", test10_mt); 116 MethodType test10_mt2 = MethodType.methodType(boolean.class); 117 MethodType test10_mt3 = MethodType.methodType(MyValue2.class); 118 MethodHandle test10_mh_test1 = lookup.findStatic(clazz, "test10_test1", test10_mt2); 119 MethodHandle test10_mh_test2 = lookup.findStatic(clazz, "test10_test2", test10_mt2); 120 test10_mh = MethodHandles.guardWithTest(test10_mh_test1, 121 MethodHandles.dropArguments(test10_mh1, 0, MethodHandle.class, MethodHandle.class), 122 MethodHandles.guardWithTest(test10_mh_test2, 123 MethodHandles.dropArguments(MethodHandles.invoker(test10_mt3), 1, MethodHandle.class), 124 MethodHandles.dropArguments(MethodHandles.invoker(test10_mt3), 0, MethodHandle.class)) 125 ); 126 127 MethodHandle test11_mh1 = lookup.findStatic(clazz, "test11_target1", myvalue2_mt); 128 test11_mh2 = lookup.findStatic(clazz, "test11_target2", myvalue2_mt); 129 MethodHandle test11_mh_test = lookup.findStatic(clazz, "test11_test", boolean_mt); 130 test11_mh = MethodHandles.guardWithTest(test11_mh_test, 131 MethodHandles.dropArguments(test11_mh1, 0, MethodHandle.class), 132 MethodHandles.invoker(myvalue2_mt)); 133 } catch (NoSuchMethodException | IllegalAccessException e) { 134 e.printStackTrace(); 135 throw new RuntimeException("Method handle lookup failed"); 136 } 137 } 138 139 public static void main(String[] args) throws Throwable { 140 TestMethodHandles test = new TestMethodHandles(); 141 test.run(args, MyValue1.class, MyValue2.class, MyValue2Inline.class, MyValue3.class, MyValue3Inline.class); 142 } 143 144 // Everything inlined 145 final MyValue3 test1_vt = MyValue3.create(); 146 147 @ForceInline 148 MyValue3 test1_target() { 149 return test1_vt; 150 } 151 152 static final MethodHandle test1_mh; 153 154 @Test(valid = ValueTypeReturnedAsFieldsOn, failOn = ALLOC + STORE + CALL) 155 @Test(valid = ValueTypeReturnedAsFieldsOff, match = { ALLOC, STORE }, matchCount = { 1, 12 }) 156 public MyValue3 test1() throws Throwable { 157 return (MyValue3)test1_mh.invokeExact(this); 158 } 159 160 @DontCompile 161 public void test1_verifier(boolean warmup) throws Throwable { | 45 * -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI -XX:+EnableValhalla 46 * compiler.valhalla.valuetypes.ValueTypeTest 47 * compiler.valhalla.valuetypes.TestMethodHandles 48 */ 49 public class TestMethodHandles extends ValueTypeTest { 50 // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters() 51 @Override 52 public String[] getExtraVMParameters(int scenario) { 53 switch (scenario) { 54 case 3: return new String[] {"-XX:-ValueArrayFlatten"}; 55 } 56 return null; 57 } 58 59 static { 60 try { 61 Class<?> clazz = TestMethodHandles.class; 62 ClassLoader loader = clazz.getClassLoader(); 63 MethodHandles.Lookup lookup = MethodHandles.lookup(); 64 65 MethodType mt = MethodType.methodType(MyValue3.class.asValueType()); 66 test1_mh = lookup.findVirtual(clazz, "test1_target", mt); 67 test2_mh = lookup.findVirtual(clazz, "test2_target", mt); 68 test3_mh = lookup.findVirtual(clazz, "test3_target", mt); 69 70 MethodType test4_mt1 = MethodType.methodType(int.class, MyValue1.class.asValueType()); 71 MethodType test4_mt2 = MethodType.methodType(MyValue1.class.asValueType()); 72 MethodHandle test4_mh1 = lookup.findStatic(clazz, "test4_helper1", test4_mt1); 73 MethodHandle test4_mh2 = lookup.findStatic(clazz, "test4_helper2", test4_mt2); 74 test4_mh = MethodHandles.filterReturnValue(test4_mh2, test4_mh1); 75 76 MethodType test5_mt = MethodType.methodType(int.class, MyValue1.class.asValueType()); 77 test5_mh = lookup.findVirtual(clazz, "test5_target", test5_mt); 78 79 MethodType test6_mt = MethodType.methodType(MyValue3.class.asValueType()); 80 MethodHandle test6_mh1 = lookup.findVirtual(clazz, "test6_target1", test6_mt); 81 MethodHandle test6_mh2 = lookup.findVirtual(clazz, "test6_target2", test6_mt); 82 MethodType boolean_mt = MethodType.methodType(boolean.class); 83 MethodHandle test6_mh_test = lookup.findVirtual(clazz, "test6_test", boolean_mt); 84 test6_mh = MethodHandles.guardWithTest(test6_mh_test, test6_mh1, test6_mh2); 85 86 MethodType myvalue2_mt = MethodType.methodType(MyValue2.class.asValueType()); 87 test7_mh1 = lookup.findStatic(clazz, "test7_target1", myvalue2_mt); 88 MethodHandle test7_mh2 = lookup.findStatic(clazz, "test7_target2", myvalue2_mt); 89 MethodHandle test7_mh_test = lookup.findStatic(clazz, "test7_test", boolean_mt); 90 test7_mh = MethodHandles.guardWithTest(test7_mh_test, 91 MethodHandles.invoker(myvalue2_mt), 92 MethodHandles.dropArguments(test7_mh2, 0, MethodHandle.class)); 93 94 MethodHandle test8_mh1 = lookup.findStatic(clazz, "test8_target1", myvalue2_mt); 95 test8_mh2 = lookup.findStatic(clazz, "test8_target2", myvalue2_mt); 96 MethodHandle test8_mh_test = lookup.findStatic(clazz, "test8_test", boolean_mt); 97 test8_mh = MethodHandles.guardWithTest(test8_mh_test, 98 MethodHandles.dropArguments(test8_mh1, 0, MethodHandle.class), 99 MethodHandles.invoker(myvalue2_mt)); 100 101 MethodType test9_mt = MethodType.methodType(MyValue3.class.asValueType()); 102 MethodHandle test9_mh1 = lookup.findVirtual(clazz, "test9_target1", test9_mt); 103 MethodHandle test9_mh2 = lookup.findVirtual(clazz, "test9_target2", test9_mt); 104 MethodHandle test9_mh3 = lookup.findVirtual(clazz, "test9_target3", test9_mt); 105 MethodType test9_mt2 = MethodType.methodType(boolean.class); 106 MethodHandle test9_mh_test1 = lookup.findVirtual(clazz, "test9_test1", test9_mt2); 107 MethodHandle test9_mh_test2 = lookup.findVirtual(clazz, "test9_test2", test9_mt2); 108 test9_mh = MethodHandles.guardWithTest(test9_mh_test1, 109 test9_mh1, 110 MethodHandles.guardWithTest(test9_mh_test2, test9_mh2, test9_mh3)); 111 112 MethodType test10_mt = MethodType.methodType(MyValue2.class.asValueType()); 113 MethodHandle test10_mh1 = lookup.findStatic(clazz, "test10_target1", test10_mt); 114 test10_mh2 = lookup.findStatic(clazz, "test10_target2", test10_mt); 115 test10_mh3 = lookup.findStatic(clazz, "test10_target3", test10_mt); 116 MethodType test10_mt2 = MethodType.methodType(boolean.class); 117 MethodType test10_mt3 = MethodType.methodType(MyValue2.class.asValueType()); 118 MethodHandle test10_mh_test1 = lookup.findStatic(clazz, "test10_test1", test10_mt2); 119 MethodHandle test10_mh_test2 = lookup.findStatic(clazz, "test10_test2", test10_mt2); 120 test10_mh = MethodHandles.guardWithTest(test10_mh_test1, 121 MethodHandles.dropArguments(test10_mh1, 0, MethodHandle.class, MethodHandle.class), 122 MethodHandles.guardWithTest(test10_mh_test2, 123 MethodHandles.dropArguments(MethodHandles.invoker(test10_mt3), 1, MethodHandle.class), 124 MethodHandles.dropArguments(MethodHandles.invoker(test10_mt3), 0, MethodHandle.class)) 125 ); 126 127 MethodHandle test11_mh1 = lookup.findStatic(clazz, "test11_target1", myvalue2_mt); 128 test11_mh2 = lookup.findStatic(clazz, "test11_target2", myvalue2_mt); 129 MethodHandle test11_mh_test = lookup.findStatic(clazz, "test11_test", boolean_mt); 130 test11_mh = MethodHandles.guardWithTest(test11_mh_test, 131 MethodHandles.dropArguments(test11_mh1, 0, MethodHandle.class), 132 MethodHandles.invoker(myvalue2_mt)); 133 } catch (NoSuchMethodException | IllegalAccessException e) { 134 e.printStackTrace(); 135 throw new RuntimeException("Method handle lookup failed"); 136 } 137 } 138 139 public static void main(String[] args) throws Throwable { 140 TestMethodHandles test = new TestMethodHandles(); 141 test.run(args, MyValue1.class.asValueType(), MyValue2.class.asValueType(), MyValue2Inline.class.asValueType(), MyValue3.class.asValueType(), MyValue3Inline.class.asValueType()); 142 } 143 144 // Everything inlined 145 final MyValue3 test1_vt = MyValue3.create(); 146 147 @ForceInline 148 MyValue3 test1_target() { 149 return test1_vt; 150 } 151 152 static final MethodHandle test1_mh; 153 154 @Test(valid = ValueTypeReturnedAsFieldsOn, failOn = ALLOC + STORE + CALL) 155 @Test(valid = ValueTypeReturnedAsFieldsOff, match = { ALLOC, STORE }, matchCount = { 1, 12 }) 156 public MyValue3 test1() throws Throwable { 157 return (MyValue3)test1_mh.invokeExact(this); 158 } 159 160 @DontCompile 161 public void test1_verifier(boolean warmup) throws Throwable { |