1 /*
2 * Copyright (c) 2024, 2025, 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 /*
25 * @test
26 * @summary Smoke test for code reflection with unary operations.
27 * @modules jdk.incubator.code
28 * @build UnaryopTest
29 * @build CodeReflectionTester
30 * @run main CodeReflectionTester UnaryopTest
31 */
32
33 import jdk.incubator.code.Reflect;
34
35 public class UnaryopTest {
36 @Reflect
37 @IR("""
38 func @"test" (%0 : java.type:"int")java.type:"int" -> {
39 %1 : Var<java.type:"int"> = var %0 @"v";
40 %2 : java.type:"int" = var.load %1;
41 %3 : java.type:"int" = neg %2;
42 return %3;
43 };
44 """)
45 static int test(int v) {
46 return -v;
47 }
48
49 @Reflect
50 @IR("""
51 func @"test2" (%0 : java.type:"int")java.type:"int" -> {
52 %1 : Var<java.type:"int"> = var %0 @"v";
53 %2 : java.type:"int" = var.load %1;
54 %3 : java.type:"int" = pos %2;
55 return %3;
56 };
57 """)
58 static int test2(int v) {
59 return +v;
60 }
61
62 @Reflect
63 @IR("""
64 func @"test2" (%0 : java.type:"byte")java.type:"int" -> {
65 %1 : Var<java.type:"byte"> = var %0 @"v";
66 %2 : java.type:"byte" = var.load %1;
67 %3 : java.type:"int" = conv %2;
68 %4 : java.type:"int" = pos %3;
69 return %4;
70 };
71 """)
72 static int test2(byte v) {
73 return +v;
74 }
75
76 @Reflect
77 @IR("""
78 func @"test2" (%0 : java.type:"long")java.type:"long" -> {
79 %1 : Var<java.type:"long"> = var %0 @"v";
80 %2 : java.type:"long" = var.load %1;
81 %3 : java.type:"long" = pos %2;
82 return %3;
83 };
84 """)
85 static long test2(long v) {
86 return +v;
87 }
88
89 @Reflect
90 @IR("""
91 func @"test3" (%0 : java.type:"int")java.type:"java.lang.Integer" -> {
92 %1 : Var<java.type:"int"> = var %0 @"v";
93 %2 : java.type:"int" = var.load %1;
94 %3 : java.type:"int" = pos %2;
95 %4 : java.type:"java.lang.Integer" = invoke %3 @java.ref:"java.lang.Integer::valueOf(int):java.lang.Integer";
96 return %4;
97 };
98 """)
99 // Tests that numeric promotion occurs
100 static Integer test3(int v) {
101 return +v;
102 }
103
104 @Reflect
105 @IR("""
106 func @"test4" (%0 : java.type:"java.lang.Integer")java.type:"java.lang.Integer" -> {
107 %1 : Var<java.type:"java.lang.Integer"> = var %0 @"v";
108 %2 : java.type:"java.lang.Integer" = var.load %1;
109 %3 : java.type:"int" = invoke %2 @java.ref:"java.lang.Integer::intValue():int";
110 %4 : java.type:"int" = pos %3;
111 %5 : java.type:"java.lang.Integer" = invoke %4 @java.ref:"java.lang.Integer::valueOf(int):java.lang.Integer";
112 return %5;
113 };
114 """)
115 // Tests that numeric promotion is retained
116 static Integer test4(Integer v) {
117 return +v;
118 }
119
120 @Reflect
121 @IR("""
122 func @"test5" (%0 : java.type:"int")java.type:"int" -> {
123 %1 : Var<java.type:"int"> = var %0 @"v";
124 %2 : java.type:"int" = var.load %1;
125 %3 : java.type:"int" = compl %2;
126 return %3;
127 };
128 """)
129 static int test5(int v) {
130 return ~v;
131 }
132
133 @IR("""
134 func @"test6" (%0 : java.type:"byte")java.type:"void" -> {
135 %1 : Var<java.type:"byte"> = var %0 @"b";
136 %2 : java.type:"byte" = var.load %1;
137 %3 : java.type:"int" = conv %2;
138 %4 : java.type:"int" = constant @1;
139 %5 : java.type:"int" = add %3 %4;
140 %6 : java.type:"byte" = conv %5;
141 var.store %1 %6;
142 %7 : java.type:"byte" = var.load %1;
143 %8 : java.type:"int" = conv %7;
144 %9 : java.type:"int" = constant @1;
145 %10 : java.type:"int" = sub %8 %9;
146 %11 : java.type:"byte" = conv %10;
147 var.store %1 %11;
148 %12 : java.type:"byte" = var.load %1;
149 %13 : java.type:"int" = conv %12;
150 %14 : java.type:"int" = constant @1;
151 %15 : java.type:"int" = add %13 %14;
152 %16 : java.type:"byte" = conv %15;
153 var.store %1 %16;
154 %17 : java.type:"byte" = var.load %1;
155 %18 : java.type:"int" = conv %17;
156 %19 : java.type:"int" = constant @1;
157 %20 : java.type:"int" = sub %18 %19;
158 %21 : java.type:"byte" = conv %20;
159 var.store %1 %21;
160 return;
161 };
162 """)
163 @Reflect
164 static void test6(byte b) {
165 b++;
166 b--;
167 ++b;
168 --b;
169 }
170
171 @IR("""
172 func @"test7" (%0 : java.type:"short")java.type:"void" -> {
173 %1 : Var<java.type:"short"> = var %0 @"s";
174 %2 : java.type:"short" = var.load %1;
175 %3 : java.type:"int" = conv %2;
176 %4 : java.type:"int" = constant @1;
177 %5 : java.type:"int" = add %3 %4;
178 %6 : java.type:"short" = conv %5;
179 var.store %1 %6;
180 %7 : java.type:"short" = var.load %1;
181 %8 : java.type:"int" = conv %7;
182 %9 : java.type:"int" = constant @1;
183 %10 : java.type:"int" = sub %8 %9;
184 %11 : java.type:"short" = conv %10;
185 var.store %1 %11;
186 %12 : java.type:"short" = var.load %1;
187 %13 : java.type:"int" = conv %12;
188 %14 : java.type:"int" = constant @1;
189 %15 : java.type:"int" = add %13 %14;
190 %16 : java.type:"short" = conv %15;
191 var.store %1 %16;
192 %17 : java.type:"short" = var.load %1;
193 %18 : java.type:"int" = conv %17;
194 %19 : java.type:"int" = constant @1;
195 %20 : java.type:"int" = sub %18 %19;
196 %21 : java.type:"short" = conv %20;
197 var.store %1 %21;
198 return;
199 };
200 """)
201 @Reflect
202 static void test7(short s) {
203 s++;
204 s--;
205 ++s;
206 --s;
207 }
208 }