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 /*
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.CodeReflection;
34
35 public class UnaryopTest {
36 @CodeReflection
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 @CodeReflection
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 @CodeReflection
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 @CodeReflection
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 @CodeReflection
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:"byte" = conv %3;
109 %5 : java.type:"byte" = add %2 %4;
110 var.store %1 %5;
111 %6 : java.type:"byte" = var.load %1;
112 %7 : java.type:"int" = constant @1;
113 %8 : java.type:"byte" = conv %7;
114 %9 : java.type:"byte" = sub %6 %8;
115 var.store %1 %9;
116 %10 : java.type:"byte" = var.load %1;
117 %11 : java.type:"int" = constant @1;
118 %12 : java.type:"byte" = conv %11;
119 %13 : java.type:"byte" = add %10 %12;
120 var.store %1 %13;
121 %14 : java.type:"byte" = var.load %1;
122 %15 : java.type:"int" = constant @1;
123 %16 : java.type:"byte" = conv %15;
124 %17 : java.type:"byte" = sub %14 %16;
125 var.store %1 %17;
126 return;
127 };
128 """)
129 @CodeReflection
130 static void test6(byte b) {
131 b++;
132 b--;
133 ++b;
134 --b;
135 }
136
137 @IR("""
138 func @"test7" (%0 : java.type:"short")java.type:"void" -> {
139 %1 : Var<java.type:"short"> = var %0 @"s";
140 %2 : java.type:"short" = var.load %1;
141 %3 : java.type:"int" = constant @1;
142 %4 : java.type:"short" = conv %3;
143 %5 : java.type:"short" = add %2 %4;
144 var.store %1 %5;
145 %6 : java.type:"short" = var.load %1;
146 %7 : java.type:"int" = constant @1;
147 %8 : java.type:"short" = conv %7;
148 %9 : java.type:"short" = sub %6 %8;
149 var.store %1 %9;
150 %10 : java.type:"short" = var.load %1;
151 %11 : java.type:"int" = constant @1;
152 %12 : java.type:"short" = conv %11;
153 %13 : java.type:"short" = add %10 %12;
154 var.store %1 %13;
155 %14 : java.type:"short" = var.load %1;
156 %15 : java.type:"int" = constant @1;
157 %16 : java.type:"short" = conv %15;
158 %17 : java.type:"short" = sub %14 %16;
159 var.store %1 %17;
160 return;
161 };
162 """)
163 @CodeReflection
164 static void test7(short s) {
165 s++;
166 s--;
167 ++s;
168 --s;
169 }
170 }