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 local variables.
 29  * @modules jdk.incubator.code
 30  * @build LocalVarTest
 31  * @build CodeReflectionTester
 32  * @run main CodeReflectionTester LocalVarTest
 33  */
 34 
 35 public class LocalVarTest {
 36 
 37     @CodeReflection
 38     @IR("""
 39             func @"test1" (%0 : LocalVarTest)int -> {
 40                 %1 : int = constant @"1";
 41                 %2 : Var<int> = var %1 @"x";
 42                 %3 : int = constant @"2";
 43                 %4 : Var<int> = var %3 @"y";
 44                 %5 : int = var.load %2;
 45                 %6 : int = var.load %4;
 46                 %7 : int = add %5 %6;
 47                 return %7;
 48             };
 49             """)
 50     int test1() {
 51         int x = 1;
 52         int y = 2;
 53         return x + y;
 54     }
 55 
 56     @CodeReflection
 57     @IR("""
 58             func @"test2" (%0 : LocalVarTest, %1 : int, %2 : int)int -> {
 59                 %3 : Var<int> = var %1 @"x";
 60                 %4 : Var<int> = var %2 @"y";
 61                 %5 : int = var.load %3;
 62                 %6 : int = var.load %4;
 63                 %7 : int = add %5 %6;
 64                 return %7;
 65             };
 66             """)
 67     int test2(int x, int y) {
 68         return x + y;
 69     }
 70 
 71     @CodeReflection
 72     @IR("""
 73             func @"test3" (%0 : LocalVarTest)int -> {
 74                 %2 : Var<int> = var @"x";
 75                 %4 : Var<int> = var @"y";
 76                 %5 : int = constant @"1";
 77                 var.store %2 %5;
 78                 %6 : int = constant @"2";
 79                 var.store %4 %6;
 80                 %7 : int = var.load %2;
 81                 %8 : int = var.load %4;
 82                 %9 : int = add %7 %8;
 83                 return %9;
 84             };
 85             """)
 86     int test3() {
 87         int x;
 88         int y;
 89         x = 1;
 90         y = 2;
 91         return x + y;
 92     }
 93 
 94     @CodeReflection
 95     @IR("""
 96             func @"test4" (%0 : LocalVarTest)int -> {
 97                 %1 : int = constant @"1";
 98                 %2 : Var<int> = var %1 @"x";
 99                 %3 : int = var.load %2;
100                 %4 : int = constant @"1";
101                 %5 : int = add %3 %4;
102                 %6 : Var<int> = var %5 @"y";
103                 %7 : int = var.load %6;
104                 return %7;
105             };
106             """)
107     int test4() {
108         int x = 1;
109         int y = x + 1;
110         return y;
111     }
112 
113     @CodeReflection
114     @IR("""
115             func @"test5" (%0 : LocalVarTest)int -> {
116                 %1 : int = constant @"1";
117                 %2 : Var<int> = var %1 @"x";
118                 %3 : int = var.load %2;
119                 %4 : Var<int> = var %3 @"y";
120                 %5 : int = var.load %4;
121                 return %5;
122             };
123             """)
124     int test5() {
125         int x = 1;
126         int y = x;
127         return y;
128     }
129 
130     @CodeReflection
131     @IR("""
132             func @"test6" (%0 : LocalVarTest)int -> {
133                 %1 : int = constant @"1";
134                 %2 : Var<int> = var %1 @"x";
135                 %3 : int = constant @"1";
136                 %4 : Var<int> = var %3 @"y";
137                 %5 : int = constant @"1";
138                 %6 : Var<int> = var %5 @"z";
139                 %7 : int = var.load %2;
140                 var.store %4 %7;
141                 var.store %6 %7;
142                 %8 : int = var.load %6;
143                 return %8;
144             };
145             """)
146     int test6() {
147         int x = 1;
148         int y = 1;
149         int z = 1;
150         z = y = x;
151         return z;
152     }
153 
154     @CodeReflection
155     @IR("""
156             func @"test7" (%0 : LocalVarTest)int -> {
157                 %1 : int = constant @"1";
158                 %2 : Var<int> = var %1 @"x";
159                 %3 : int = var.load %2;
160                 %4 : int = constant @"2";
161                 %5 : int = add %3 %4;
162                 var.store %2 %5;
163                 %6 : Var<int> = var %5 @"y";
164                 %7 : int = var.load %6;
165                 %8 : int = constant @"3";
166                 %9 : int = add %7 %8;
167                 var.store %6 %9;
168                 %10 : int = var.load %2;
169                 %11 : int = constant @"4";
170                 %12 : int = add %10 %11;
171                 var.store %2 %12;
172                 %13 : int = add %9 %12;
173                 return %13;
174             };
175             """)
176     int test7() {
177         int x = 1;
178         int y = x += 2;
179         return (y += 3) + (x += 4);
180     }
181 
182     @CodeReflection
183     @IR("""
184             func @"test8" (%0 : LocalVarTest, %1 : int)void -> {
185                 %2 : Var<int> = var %1 @"i";
186                 %3 : int = var.load %2;
187                 %4 : int = constant @"1";
188                 %5 : int = add %3 %4;
189                 var.store %2 %5;
190                 %6 : Var<int> = var %3 @"x";
191                 %7 : int = var.load %2;
192                 %8 : int = constant @"1";
193                 %9 : int = sub %7 %8;
194                 var.store %2 %9;
195                 %10 : Var<int> = var %7 @"y";
196                 return;
197             };
198             """)
199     void test8(int i) {
200         int x = i++;
201         int y = i--;
202     }
203 
204     @CodeReflection
205     @IR("""
206             func @"test9" (%0 : LocalVarTest, %1 : int)void -> {
207                 %2 : Var<int> = var %1 @"i";
208                 %3 : int = var.load %2;
209                 %4 : int = constant @"1";
210                 %5 : int = add %3 %4;
211                 var.store %2 %5;
212                 %6 : Var<int> = var %5 @"x";
213                 %7 : int = var.load %2;
214                 %8 : int = constant @"1";
215                 %9 : int = sub %7 %8;
216                 var.store %2 %9;
217                 %10 : Var<int> = var %9 @"y";
218                 return;
219             };
220             """)
221     void test9(int i) {
222         int x = ++i;
223         int y = --i;
224     }
225 }