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                 return %2;
 55             };
 56             """)
 57     static int test2(int v) {
 58         return +v;
 59     }
 60 
 61     @Reflect
 62     @IR("""
 63             func @"test3" (%0 : java.type:"int")java.type:"java.lang.Integer" -> {
 64                 %1 : Var<java.type:"int"> = var %0 @"v";
 65                 %2 : java.type:"int" = var.load %1;
 66                 %3 : java.type:"java.lang.Integer" = invoke %2 @java.ref:"java.lang.Integer::valueOf(int):java.lang.Integer";
 67                 return %3;
 68             };
 69             """)
 70     // Tests that numeric promotion occurs
 71     static Integer test3(int v) {
 72         return +v;
 73     }
 74 
 75     @Reflect
 76     @IR("""
 77             func @"test4" (%0 : java.type:"java.lang.Integer")java.type:"java.lang.Integer" -> {
 78                 %1 : Var<java.type:"java.lang.Integer"> = var %0 @"v";
 79                 %2 : java.type:"java.lang.Integer" = var.load %1;
 80                 %3 : java.type:"int" = invoke %2 @java.ref:"java.lang.Integer::intValue():int";
 81                 %4 : java.type:"java.lang.Integer" = invoke %3 @java.ref:"java.lang.Integer::valueOf(int):java.lang.Integer";
 82                 return %4;
 83             };
 84             """)
 85     // Tests that numeric promotion is retained
 86     static Integer test4(Integer v) {
 87         return +v;
 88     }
 89 
 90     @Reflect
 91     @IR("""
 92             func @"test5" (%0 : java.type:"int")java.type:"int" -> {
 93                 %1 : Var<java.type:"int"> = var %0 @"v";
 94                 %2 : java.type:"int" = var.load %1;
 95                 %3 : java.type:"int" = compl %2;
 96                 return %3;
 97             };
 98             """)
 99     static int test5(int v) {
100         return ~v;
101     }
102 
103     @IR("""
104             func @"test6" (%0 : java.type:"byte")java.type:"void" -> {
105                 %1 : Var<java.type:"byte"> = var %0 @"b";
106                 %2 : java.type:"byte" = var.load %1;
107                 %3 : java.type:"int" = constant @1;
108                 %4 : java.type:"int" = conv %2;
109                 %5 : java.type:"int" = add %4 %3;
110                 %6 : java.type:"byte" = conv %5;
111                 var.store %1 %6;
112                 %7 : java.type:"byte" = var.load %1;
113                 %8 : java.type:"int" = constant @1;
114                 %9 : java.type:"int" = conv %7;
115                 %10 : java.type:"int" = sub %9 %8;
116                 %11 : java.type:"byte" = conv %10;
117                 var.store %1 %11;
118                 %12 : java.type:"byte" = var.load %1;
119                 %13 : java.type:"int" = constant @1;
120                 %14 : java.type:"int" = conv %12;
121                 %15 : java.type:"int" = add %14 %13;
122                 %16 : java.type:"byte" = conv %15;
123                 var.store %1 %16;
124                 %17 : java.type:"byte" = var.load %1;
125                 %18 : java.type:"int" = constant @1;
126                 %19 : java.type:"int" = conv %17;
127                 %20 : java.type:"int" = sub %19 %18;
128                 %21 : java.type:"byte" = conv %20;
129                 var.store %1 %21;
130                 return;
131             };
132             """)
133     @Reflect
134     static void test6(byte b) {
135         b++;
136         b--;
137         ++b;
138         --b;
139     }
140 
141     @IR("""
142             func @"test7" (%0 : java.type:"short")java.type:"void" -> {
143                 %1 : Var<java.type:"short"> = var %0 @"s";
144                 %2 : java.type:"short" = var.load %1;
145                 %3 : java.type:"int" = constant @1;
146                 %4 : java.type:"int" = conv %2;
147                 %5 : java.type:"int" = add %4 %3;
148                 %6 : java.type:"short" = conv %5;
149                 var.store %1 %6;
150                 %7 : java.type:"short" = var.load %1;
151                 %8 : java.type:"int" = constant @1;
152                 %9 : java.type:"int" = conv %7;
153                 %10 : java.type:"int" = sub %9 %8;
154                 %11 : java.type:"short" = conv %10;
155                 var.store %1 %11;
156                 %12 : java.type:"short" = var.load %1;
157                 %13 : java.type:"int" = constant @1;
158                 %14 : java.type:"int" = conv %12;
159                 %15 : java.type:"int" = add %14 %13;
160                 %16 : java.type:"short" = conv %15;
161                 var.store %1 %16;
162                 %17 : java.type:"short" = var.load %1;
163                 %18 : java.type:"int" = constant @1;
164                 %19 : java.type:"int" = conv %17;
165                 %20 : java.type:"int" = sub %19 %18;
166                 %21 : java.type:"short" = conv %20;
167                 var.store %1 %21;
168                 return;
169             };
170             """)
171     @Reflect
172     static void test7(short s) {
173         s++;
174         s--;
175         ++s;
176         --s;
177     }
178 }