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