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.invoke.MethodHandles; 28 import jdk.incubator.code.OpTransformer; 29 import jdk.incubator.code.op.CoreOp; 30 import jdk.incubator.code.Op; 31 import jdk.incubator.code.interpreter.Interpreter; 32 import java.lang.reflect.Method; 33 import jdk.incubator.code.CodeReflection; 34 import java.util.Optional; 35 import java.util.stream.Stream; 36 37 /* 38 * @test 39 * @modules jdk.incubator.code 40 * @run testng TestWhileOp 41 */ 42 43 public class TestWhileOp { 44 45 @CodeReflection 46 public static int whileLoop() { 47 int i = 0; 48 while (i < 10) { 49 i++; 50 } 51 return i; 52 } 53 54 @Test 55 public void testWhileLoop() { 56 CoreOp.FuncOp f = getFuncOp("whileLoop"); 57 58 f.writeTo(System.out); 59 60 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 61 62 lf.writeTo(System.out); 63 64 Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), lf), whileLoop()); 65 } 66 67 @CodeReflection 68 public static int doWhileLoop() { 69 int i = 0; 70 do { 71 i++; 72 } while (i < 10); 73 return i; 74 } 75 76 @Test 77 public void testDpWhileLoop() { 78 CoreOp.FuncOp f = getFuncOp("doWhileLoop"); 79 80 f.writeTo(System.out); 81 82 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 83 84 lf.writeTo(System.out); 85 86 Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), lf), doWhileLoop()); 87 } 88 89 static CoreOp.FuncOp getFuncOp(String name) { 90 Optional<Method> om = Stream.of(TestWhileOp.class.getDeclaredMethods()) 91 .filter(m -> m.getName().equals(name)) 92 .findFirst(); 93 94 Method m = om.get(); 95 return Op.ofMethod(m).get(); 96 } 97 }