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 : java.type:"WhileLoopTest")java.type:"void" -> {
39 %1 : java.type:"int" = constant @0;
40 %2 : Var<java.type:"int"> = var %1 @"i";
41 java.while
42 ()java.type:"boolean" -> {
43 %3 : java.type:"int" = var.load %2;
44 %4 : java.type:"int" = constant @10;
45 %5 : java.type:"boolean" = lt %3 %4;
46 yield %5;
47 }
48 ()java.type:"void" -> {
49 %6 : java.type:"java.io.PrintStream" = field.load @java.ref:"java.lang.System::out:java.io.PrintStream";
50 %7 : java.type:"int" = var.load %2;
51 invoke %6 %7 @java.ref:"java.io.PrintStream::println(int):void";
52 %8 : java.type:"int" = var.load %2;
53 %9 : java.type:"int" = constant @1;
54 %10 : java.type:"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 : java.type:"WhileLoopTest")java.type:"int" -> {
72 %1 : java.type:"int" = constant @0;
73 %2 : Var<java.type:"int"> = var %1 @"i";
74 java.while
75 ()java.type:"boolean" -> {
76 %3 : java.type:"int" = var.load %2;
77 %4 : java.type:"int" = constant @10;
78 %5 : java.type:"boolean" = lt %3 %4;
79 yield %5;
80 }
81 ()java.type:"void" -> {
82 %6 : java.type:"int" = var.load %2;
83 return %6;
84 };
85 %7 : java.type:"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 : java.type:"WhileLoopTest")java.type:"void" -> {
100 %1 : java.type:"int" = constant @0;
101 %2 : Var<java.type:"int"> = var %1 @"i";
102 java.do.while
103 ()java.type:"void" -> {
104 %3 : java.type:"java.io.PrintStream" = field.load @java.ref:"java.lang.System::out:java.io.PrintStream";
105 %4 : java.type:"int" = var.load %2;
106 invoke %3 %4 @java.ref:"java.io.PrintStream::println(int):void";
107 %5 : java.type:"int" = var.load %2;
108 %6 : java.type:"int" = constant @1;
109 %7 : java.type:"int" = add %5 %6;
110 var.store %2 %7;
111 java.continue;
112 }
113 ()java.type:"boolean" -> {
114 %8 : java.type:"int" = var.load %2;
115 %9 : java.type:"int" = constant @10;
116 %10 : java.type:"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" ()java.type:"void" -> {
133 %0 : java.type:"boolean" = constant @true;
134 %1 : java.type:"java.lang.Boolean" = invoke %0 @java.ref:"java.lang.Boolean::valueOf(boolean):java.lang.Boolean";
135 %2 : Var<java.type:"java.lang.Boolean"> = var %1 @"b";
136 %3 : java.type:"int" = constant @0;
137 %4 : Var<java.type:"int"> = var %3 @"i";
138 java.while
139 ()java.type:"boolean" -> {
140 %5 : java.type:"java.lang.Boolean" = var.load %2;
141 %6 : java.type:"boolean" = invoke %5 @java.ref:"java.lang.Boolean::booleanValue():boolean";
142 yield %6;
143 }
144 ()java.type:"void" -> {
145 %7 : java.type:"int" = var.load %4;
146 %8 : java.type:"int" = constant @1;
147 %9 : java.type:"int" = add %7 %8;
148 var.store %4 %9;
149 %10 : java.type:"int" = var.load %4;
150 %11 : java.type:"int" = constant @10;
151 %12 : java.type:"boolean" = lt %10 %11;
152 %13 : java.type:"java.lang.Boolean" = invoke %12 @java.ref:"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 : java.type:"int")java.type:"void" -> {
171 %1 : Var<java.type:"int"> = var %0 @"i";
172 %2 : Var<java.type:"java.lang.Boolean"> = var @"b";
173 java.do.while
174 ()java.type:"void" -> {
175 %3 : java.type:"int" = var.load %1;
176 %4 : java.type:"int" = constant @10;
177 %5 : java.type:"boolean" = lt %3 %4;
178 %6 : java.type:"java.lang.Boolean" = invoke %5 @java.ref:"java.lang.Boolean::valueOf(boolean):java.lang.Boolean";
179 var.store %2 %6;
180 java.continue;
181 }
182 ()java.type:"boolean" -> {
183 %7 : java.type:"java.lang.Boolean" = var.load %2;
184 %8 : java.type:"boolean" = invoke %7 @java.ref:"java.lang.Boolean::booleanValue():boolean";
185 yield %8;
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 }