1 /* 2 * Copyright (c) 2024, 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 import org.testng.Assert; 25 import org.testng.annotations.Test; 26 27 import java.lang.reflect.code.OpTransformer; 28 import java.lang.reflect.code.op.CoreOp; 29 import java.lang.reflect.code.Op; 30 import java.lang.reflect.code.bytecode.BytecodeGenerator; 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.lang.reflect.Method; 34 import java.lang.runtime.CodeReflection; 35 import java.util.Optional; 36 import java.util.stream.Stream; 37 38 /* 39 * @test 40 * @run testng TestSlots 41 */ 42 43 public class TestSlots { 44 @CodeReflection 45 static double f(double i, double j) { 46 i = i + j; 47 48 double k = 4.0; 49 k += i; 50 return k; 51 } 52 53 @Test 54 public void testF() throws Throwable { 55 CoreOp.FuncOp f = getFuncOp("f"); 56 57 MethodHandle mh = generate(f); 58 59 Assert.assertEquals(f(1.0d, 2.0d), (double) mh.invoke(1.0d, 2.0d)); 60 } 61 62 @CodeReflection 63 static double f2(double x, double y) { 64 return x * (-Math.sin(x * y) + y) * 4.0d; 65 } 66 67 @Test 68 public void testF2() throws Throwable { 69 CoreOp.FuncOp f = getFuncOp("f2"); 70 71 MethodHandle mh = generate(f); 72 73 Assert.assertEquals(f2(1.0d, 2.0d), (double) mh.invoke(1.0d, 2.0d)); 74 } 75 76 @CodeReflection 77 static double f3(/* independent */ double x, int y) { 78 /* dependent */ 79 double o = 1.0; 80 for (int i = 0; i < y; i = i + 1) { 81 if (i > 1) { 82 if (i < 5) { 83 o = o * x; 84 } 85 } 86 } 87 return o; 88 } 89 90 @Test 91 public void testF3() throws Throwable { 92 CoreOp.FuncOp f = getFuncOp("f3"); 93 94 MethodHandle mh = generate(f); 95 96 for (int i = 0; i < 7; i++) { 97 Assert.assertEquals(f3(2.0d, i), (double) mh.invoke(2.0d, i)); 98 } 99 } 100 101 @CodeReflection 102 static int f4(/* Unused */ int a, int b) { 103 return b; 104 } 105 106 @Test 107 public void testF4() throws Throwable { 108 CoreOp.FuncOp f = getFuncOp("f4"); 109 110 MethodHandle mh; 111 try { 112 mh = generate(f); 113 } catch (VerifyError e) { 114 Assert.fail("invalid class file generated", e); 115 return; 116 } 117 118 Assert.assertEquals(f4(1, 2), (int) mh.invoke(1, 2)); 119 } 120 121 @CodeReflection 122 static double f5(/* Unused */ double a, double b) { 123 return b; 124 } 125 126 @Test 127 public void testF5() throws Throwable { 128 CoreOp.FuncOp f = getFuncOp("f5"); 129 130 MethodHandle mh; 131 try { 132 mh = generate(f); 133 } catch (VerifyError e) { 134 Assert.fail("invalid class file generated", e); 135 return; 136 } 137 138 Assert.assertEquals(f5(1.0, 2.0), (double) mh.invoke(1.0, 2.0)); 139 } 140 141 static MethodHandle generate(CoreOp.FuncOp f) { 142 f.writeTo(System.out); 143 144 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 145 lf.writeTo(System.out); 146 147 return BytecodeGenerator.generate(MethodHandles.lookup(), lf); 148 } 149 150 static CoreOp.FuncOp getFuncOp(String name) { 151 Optional<Method> om = Stream.of(TestSlots.class.getDeclaredMethods()) 152 .filter(m -> m.getName().equals(name)) 153 .findFirst(); 154 155 Method m = om.get(); 156 return m.getCodeModel().get(); 157 } 158 159 }