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 array access.
 29  * @build ArrayAccessTest
 30  * @build CodeReflectionTester
 31  * @run main CodeReflectionTester ArrayAccessTest
 32  */
 33 
 34 public class ArrayAccessTest {
 35     @CodeReflection
 36     @IR("""
 37             func @"test1" (%0 : ArrayAccessTest, %1 : int[])int -> {
 38                 %2 : Var<int[]> = var %1 @"ia";
 39                 %3 : int[] = var.load %2;
 40                 %4 : int = constant @"0";
 41                 %5 : int = array.load %3 %4;
 42                 return %5;
 43             };
 44             """)
 45     int test1(int[] ia) {
 46         return ia[0];
 47     }
 48 
 49     @CodeReflection
 50     @IR("""
 51             func @"test2" (%0 : ArrayAccessTest, %1 : int[], %2 : int)int -> {
 52                 %3 : Var<int[]> = var %1 @"ia";
 53                 %4 : Var<int> = var %2 @"i";
 54                 %5 : int[] = var.load %3;
 55                 %6 : int = var.load %4;
 56                 %7 : int = constant @"1";
 57                 %8 : int = add %6 %7;
 58                 %9 : int = array.load %5 %8;
 59                 return %9;
 60             };
 61             """)
 62     int test2(int[] ia, int i) {
 63         return ia[i + 1];
 64     }
 65 
 66     @CodeReflection
 67     @IR("""
 68             func @"test3" (%0 : ArrayAccessTest, %1 : int[])void -> {
 69                 %2 : Var<int[]> = var %1 @"ia";
 70                 %3 : int[] = var.load %2;
 71                 %4 : int = constant @"0";
 72                 %5 : int = constant @"1";
 73                 array.store %3 %4 %5;
 74                 return;
 75             };
 76             """)
 77     void test3(int[] ia) {
 78         ia[0] = 1;
 79     }
 80 
 81     @CodeReflection
 82     @IR("""
 83             func @"test4" (%0 : ArrayAccessTest, %1 : int[], %2 : int)void -> {
 84                 %3 : Var<int[]> = var %1 @"ia";
 85                 %4 : Var<int> = var %2 @"i";
 86                 %5 : int[] = var.load %3;
 87                 %6 : int = var.load %4;
 88                 %7 : int = constant @"1";
 89                 %8 : int = add %6 %7;
 90                 %9 : int = constant @"1";
 91                 array.store %5 %8 %9;
 92                 return;
 93             };
 94             """)
 95     void test4(int[] ia, int i) {
 96         ia[i + 1] = 1;
 97     }
 98 
 99     @CodeReflection
100     @IR("""
101             func @"test5" (%0 : ArrayAccessTest, %1 : int[][], %2 : int)int -> {
102                 %3 : Var<int[][]> = var %1 @"ia";
103                 %4 : Var<int> = var %2 @"i";
104                 %5 : int[][] = var.load %3;
105                 %6 : int = var.load %4;
106                 %7 : int = constant @"1";
107                 %8 : int = add %6 %7;
108                 %9 : int[] = array.load %5 %8;
109                 %10 : int = var.load %4;
110                 %11 : int = constant @"2";
111                 %12 : int = add %10 %11;
112                 %13 : int = array.load %9 %12;
113                 return %13;
114             };
115             """)
116     int test5(int[][] ia, int i) {
117         return ia[i + 1][i + 2];
118     }
119 
120     @CodeReflection
121     @IR("""
122             func @"test6" (%0 : ArrayAccessTest, %1 : int[][], %2 : int)void -> {
123                 %3 : Var<int[][]> = var %1 @"ia";
124                 %4 : Var<int> = var %2 @"i";
125                 %5 : int[][] = var.load %3;
126                 %6 : int = var.load %4;
127                 %7 : int = constant @"1";
128                 %8 : int = add %6 %7;
129                 %9 : int[] = array.load %5 %8;
130                 %10 : int = var.load %4;
131                 %11 : int = constant @"2";
132                 %12 : int = add %10 %11;
133                 %13 : int = constant @"1";
134                 array.store %9 %12 %13;
135                 return;
136             };
137             """)
138     void test6(int[][] ia, int i) {
139         ia[i + 1][i + 2] = 1;
140     }
141 
142     int[] ia;
143 
144     @CodeReflection
145     @IR("""
146             func @"test7" (%0 : ArrayAccessTest)int -> {
147                 %1 : int[] = field.load %0 @"ArrayAccessTest::ia()int[]";
148                 %2 : int = constant @"0";
149                 %3 : int = array.load %1 %2;
150                 return %3;
151             };
152             """)
153     int test7() {
154         return ia[0];
155     }
156 
157     @CodeReflection
158     @IR("""
159             func @"test8" (%0 : ArrayAccessTest)int -> {
160                 %1 : int[] = field.load %0 @"ArrayAccessTest::ia()int[]";
161                 %2 : int = constant @"0";
162                 %3 : int = array.load %1 %2;
163                 return %3;
164             };
165             """)
166     int test8() {
167         return this.ia[0];
168     }
169 
170     static class A {
171         int i;
172     }
173 
174     @CodeReflection
175     @IR("""
176             func @"test9" (%0 : ArrayAccessTest, %1 : ArrayAccessTest$A[])int -> {
177                 %2 : Var<ArrayAccessTest$A[]> = var %1 @"aa";
178                 %3 : ArrayAccessTest$A[] = var.load %2;
179                 %4 : int = constant @"0";
180                 %5 : ArrayAccessTest$A = array.load %3 %4;
181                 %6 : int = field.load %5 @"ArrayAccessTest$A::i()int";
182                 return %6;
183             };
184             """)
185     int test9(A[] aa) {
186         return aa[0].i;
187     }
188 
189     @CodeReflection
190     @IR("""
191             func @"test10" (%0 : ArrayAccessTest, %1 : ArrayAccessTest$A[])void -> {
192                 %2 : Var<ArrayAccessTest$A[]> = var %1 @"aa";
193                 %3 : ArrayAccessTest$A[] = var.load %2;
194                 %4 : int = constant @"0";
195                 %5 : ArrayAccessTest$A = array.load %3 %4;
196                 %6 : int = constant @"1";
197                 field.store %5 %6 @"ArrayAccessTest$A::i()int";
198                 return;
199             };
200             """)
201     void test10(A[] aa) {
202         aa[0].i = 1;
203     }
204 
205     @CodeReflection
206     @IR("""
207             func @"test11" (%0 : ArrayAccessTest, %1 : int[])void -> {
208                 %2 : Var<int[]> = var %1 @"ia";
209                 %3 : int[] = var.load %2;
210                 %4 : int = constant @"0";
211                 %5 : int = array.load %3 %4;
212                 %6 : int = constant @"1";
213                 %7 : int = add %5 %6;
214                 array.store %3 %4 %7;
215                 return;
216             };
217             """)
218     void test11(int[] ia) {
219         ia[0] += 1;
220     }
221 
222     @CodeReflection
223     @IR("""
224             func @"test12" (%0 : ArrayAccessTest, %1 : int[], %2 : int)void -> {
225                 %3 : Var<int[]> = var %1 @"ia";
226                 %4 : Var<int> = var %2 @"i";
227                 %5 : int[] = var.load %3;
228                 %6 : int = constant @"1";
229                 %7 : int[] = var.load %3;
230                 %8 : int = var.load %4;
231                 %9 : int = constant @"2";
232                 %10 : int = add %8 %9;
233                 %11 : int = array.load %7 %10;
234                 %12 : int = constant @"1";
235                 %13 : int = add %11 %12;
236                 array.store %7 %10 %13;
237                 array.store %5 %6 %13;
238                 return;
239             };
240             """)
241     void test12(int[] ia, int i) {
242         ia[1] = ia[i + 2] += 1;
243     }
244 
245     @CodeReflection
246     @IR("""
247             func @"test13" (%0 : ArrayAccessTest, %1 : int[], %2 : int)void -> {
248                 %3 : Var<int[]> = var %1 @"ia";
249                 %4 : Var<int> = var %2 @"i";
250                 %5 : int[] = var.load %3;
251                 %6 : int = constant @"1";
252                 %7 : int = array.load %5 %6;
253                 %8 : int[] = var.load %3;
254                 %9 : int = var.load %4;
255                 %10 : int = constant @"2";
256                 %11 : int = add %9 %10;
257                 %12 : int = array.load %8 %11;
258                 %13 : int = constant @"1";
259                 %14 : int = add %12 %13;
260                 array.store %8 %11 %14;
261                 %15 : int = add %7 %14;
262                 array.store %5 %6 %15;
263                 return;
264             };
265             """)
266     void test13(int[] ia, int i) {
267         ia[1] += ia[i + 2] += 1;
268     }
269 
270 
271     @CodeReflection
272     @IR("""
273             func @"test14" (%0 : ArrayAccessTest, %1 : int[])void -> {
274                 %2 : Var<int[]> = var %1 @"ia";
275                 %3 : int[] = var.load %2;
276                 %4 : int = constant @"0";
277                 %5 : int = array.load %3 %4;
278                 %6 : int = constant @"1";
279                 %7 : int = add %5 %6;
280                 array.store %3 %4 %7;
281                 %8 : Var<int> = var %5 @"x";
282                 %9 : int[] = var.load %2;
283                 %10 : int = constant @"0";
284                 %11 : int = array.load %9 %10;
285                 %12 : int = constant @"1";
286                 %13 : int = sub %11 %12;
287                 array.store %9 %10 %13;
288                 %14 : Var<int> = var %11 @"y";
289                 return;
290             };
291             """)
292     void test14(int[] ia) {
293         int x = ia[0]++;
294         int y = ia[0]--;
295     }
296 
297     @CodeReflection
298     @IR("""
299             func @"test15" (%0 : ArrayAccessTest, %1 : int[])void -> {
300                 %2 : Var<int[]> = var %1 @"ia";
301                 %3 : int[] = var.load %2;
302                 %4 : int = constant @"0";
303                 %5 : int = array.load %3 %4;
304                 %6 : int = constant @"1";
305                 %7 : int = add %5 %6;
306                 array.store %3 %4 %7;
307                 %8 : Var<int> = var %7 @"x";
308                 %9 : int[] = var.load %2;
309                 %10 : int = constant @"0";
310                 %11 : int = array.load %9 %10;
311                 %12 : int = constant @"1";
312                 %13 : int = sub %11 %12;
313                 array.store %9 %10 %13;
314                 %14 : Var<int> = var %13 @"y";
315                 return;
316             };
317             """)
318     void test15(int[] ia) {
319         int x = ++ia[0];
320         int y = --ia[0];
321     }
322 
323     @CodeReflection
324     @IR("""
325             func @"test16" (%0 : ArrayAccessTest, %1 : int[])int -> {
326                 %2 : Var<int[]> = var %1 @"ia";
327                 %3 : int[] = var.load %2;
328                 %4 : int = array.length %3;
329                 %5 : int[] = var.load %2;
330                 %6 : int = invoke %5 @"java.lang.Object::hashCode()int";
331                 %7 : int = add %4 %6;
332                 return %7;
333             };
334             """)
335     int test16(int[] ia) {
336         return ia.length + ia.hashCode();
337     }
338 
339 }