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