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 java.lang.runtime.CodeReflection;
 25 
 26 /*
 27  * @test
 28  * @summary Smoke test for code reflection with while loops.
 29  * @build WhileLoopTest
 30  * @build CodeReflectionTester
 31  * @run main CodeReflectionTester WhileLoopTest
 32  */
 33 
 34 public class WhileLoopTest {
 35     @CodeReflection
 36     @IR("""
 37             func @"test1" (%0 : WhileLoopTest)void -> {
 38                 %1 : int = constant @"0";
 39                 %2 : Var<int> = var %1 @"i";
 40                 java.while
 41                     ^cond()boolean -> {
 42                         %3 : int = var.load %2;
 43                         %4 : int = constant @"10";
 44                         %5 : boolean = lt %3 %4;
 45                         yield %5;
 46                     }
 47                     ^body()void -> {
 48                         %6 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream";
 49                         %7 : int = var.load %2;
 50                         invoke %6 %7 @"java.io.PrintStream::println(int)void";
 51                         %8 : int = var.load %2;
 52                         %9 : int = constant @"1";
 53                         %10 : int = add %8 %9;
 54                         var.store %2 %10;
 55                         java.continue;
 56                     };
 57                 return;
 58             };
 59             """)
 60     void test1() {
 61         int i = 0;
 62         while (i < 10) {
 63             System.out.println(i);
 64             i = i + 1;
 65         }
 66     }
 67 
 68     @CodeReflection
 69     @IR("""
 70             func @"test2" (%0 : WhileLoopTest)int -> {
 71               %1 : int = constant @"0";
 72               %2 : Var<int> = var %1 @"i";
 73               java.while
 74                   ^cond()boolean -> {
 75                       %3 : int = var.load %2;
 76                       %4 : int = constant @"10";
 77                       %5 : boolean = lt %3 %4;
 78                       yield %5;
 79                   }
 80                   ^body()void -> {
 81                       %6 : int = var.load %2;
 82                       return %6;
 83                   };
 84               %7 : int = constant @"-1";
 85               return %7;
 86             };
 87             """)
 88     int test2() {
 89         int i = 0;
 90         while (i < 10) {
 91             return i;
 92         }
 93         return -1;
 94     }
 95 
 96     @CodeReflection
 97     @IR("""
 98             func @"test3" (%0 : WhileLoopTest)void -> {
 99                 %1 : int = constant @"0";
100                 %2 : Var<int> = var %1 @"i";
101                 java.do.while
102                     ^body()void -> {
103                         %3 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream";
104                         %4 : int = var.load %2;
105                         invoke %3 %4 @"java.io.PrintStream::println(int)void";
106                         %5 : int = var.load %2;
107                         %6 : int = constant @"1";
108                         %7 : int = add %5 %6;
109                         var.store %2 %7;
110                         java.continue;
111                     }
112                     ^cond()boolean -> {
113                         %8 : int = var.load %2;
114                         %9 : int = constant @"10";
115                         %10 : boolean = lt %8 %9;
116                         yield %10;
117                     };
118                 return;
119             };
120             """)
121     void test3() {
122         int i = 0;
123         do {
124             System.out.println(i);
125             i = i + 1;
126         } while (i < 10);
127     }
128 }