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 
 26 /*
 27  * @test
 28  * @summary Smoke test for code reflection with while loops.
 29  * @modules jdk.incubator.code
 30  * @build WhileLoopTest
 31  * @build CodeReflectionTester
 32  * @run main CodeReflectionTester WhileLoopTest
 33  */
 34 
 35 public class WhileLoopTest {
 36     @CodeReflection
 37     @IR("""
 38             func @"test1" (%0 : WhileLoopTest)void -> {
 39                 %1 : int = constant @"0";
 40                 %2 : Var<int> = var %1 @"i";
 41                 java.while
 42                     ^cond()boolean -> {
 43                         %3 : int = var.load %2;
 44                         %4 : int = constant @"10";
 45                         %5 : boolean = lt %3 %4;
 46                         yield %5;
 47                     }
 48                     ^body()void -> {
 49                         %6 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream";
 50                         %7 : int = var.load %2;
 51                         invoke %6 %7 @"java.io.PrintStream::println(int)void";
 52                         %8 : int = var.load %2;
 53                         %9 : int = constant @"1";
 54                         %10 : int = add %8 %9;
 55                         var.store %2 %10;
 56                         java.continue;
 57                     };
 58                 return;
 59             };
 60             """)
 61     void test1() {
 62         int i = 0;
 63         while (i < 10) {
 64             System.out.println(i);
 65             i = i + 1;
 66         }
 67     }
 68 
 69     @CodeReflection
 70     @IR("""
 71             func @"test2" (%0 : WhileLoopTest)int -> {
 72               %1 : int = constant @"0";
 73               %2 : Var<int> = var %1 @"i";
 74               java.while
 75                   ^cond()boolean -> {
 76                       %3 : int = var.load %2;
 77                       %4 : int = constant @"10";
 78                       %5 : boolean = lt %3 %4;
 79                       yield %5;
 80                   }
 81                   ^body()void -> {
 82                       %6 : int = var.load %2;
 83                       return %6;
 84                   };
 85               %7 : int = constant @"-1";
 86               return %7;
 87             };
 88             """)
 89     int test2() {
 90         int i = 0;
 91         while (i < 10) {
 92             return i;
 93         }
 94         return -1;
 95     }
 96 
 97     @CodeReflection
 98     @IR("""
 99             func @"test3" (%0 : WhileLoopTest)void -> {
100                 %1 : int = constant @"0";
101                 %2 : Var<int> = var %1 @"i";
102                 java.do.while
103                     ^body()void -> {
104                         %3 : java.io.PrintStream = field.load @"java.lang.System::out()java.io.PrintStream";
105                         %4 : int = var.load %2;
106                         invoke %3 %4 @"java.io.PrintStream::println(int)void";
107                         %5 : int = var.load %2;
108                         %6 : int = constant @"1";
109                         %7 : int = add %5 %6;
110                         var.store %2 %7;
111                         java.continue;
112                     }
113                     ^cond()boolean -> {
114                         %8 : int = var.load %2;
115                         %9 : int = constant @"10";
116                         %10 : boolean = lt %8 %9;
117                         yield %10;
118                     };
119                 return;
120             };
121             """)
122     void test3() {
123         int i = 0;
124         do {
125             System.out.println(i);
126             i = i + 1;
127         } while (i < 10);
128     }
129 
130 
131     @IR("""
132             func @"test4" ()void -> {
133                   %0 : boolean = constant @"true";
134                   %1 : java.lang.Boolean = invoke %0 @"java.lang.Boolean::valueOf(boolean)java.lang.Boolean";
135                   %2 : Var<java.lang.Boolean> = var %1 @"b";
136                   %3 : int = constant @"0";
137                   %4 : Var<int> = var %3 @"i";
138                   java.while
139                       ()boolean -> {
140                           %5 : java.lang.Boolean = var.load %2;
141                           %6 : boolean = invoke %5 @"java.lang.Boolean::booleanValue()boolean";
142                           yield %6;
143                       }
144                       ()void -> {
145                           %7 : int = var.load %4;
146                           %8 : int = constant @"1";
147                           %9 : int = add %7 %8;
148                           var.store %4 %9;
149                           %10 : int = var.load %4;
150                           %11 : int = constant @"10";
151                           %12 : boolean = lt %10 %11;
152                           %13 : java.lang.Boolean = invoke %12 @"java.lang.Boolean::valueOf(boolean)java.lang.Boolean";
153                           var.store %2 %13;
154                           java.continue;
155                       };
156                   return;
157               };
158             """)
159     @CodeReflection
160     static void test4() {
161         Boolean b = true;
162         int i = 0;
163         while (b) {
164             i++;
165             b = i < 10;
166         }
167     }
168 
169     @IR("""
170             func @"test5" (%0 : int)void -> {
171                 %1 : Var<int> = var %0 @"i";
172                 %3 : Var<java.lang.Boolean> = var @"b";
173                 java.do.while
174                     ()void -> {
175                         %4 : int = var.load %1;
176                         %5 : int = constant @"10";
177                         %6 : boolean = lt %4 %5;
178                         %7 : java.lang.Boolean = invoke %6 @"java.lang.Boolean::valueOf(boolean)java.lang.Boolean";
179                         var.store %3 %7;
180                         java.continue;
181                     }
182                     ()boolean -> {
183                         %8 : java.lang.Boolean = var.load %3;
184                         %9 : boolean = invoke %8 @"java.lang.Boolean::booleanValue()boolean";
185                         yield %9;
186                     };
187                 return;
188             };
189             """)
190     @CodeReflection
191     static void test5(int i) {
192         Boolean b;
193         do {
194             b = i < 10;
195         } while (b);
196     }
197 }