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 primitive casts.
 27  * @modules jdk.incubator.code
 28  * @build PrimitiveCastTest
 29  * @build CodeReflectionTester
 30  * @run main CodeReflectionTester PrimitiveCastTest
 31  */
 32 
 33 import jdk.incubator.code.CodeReflection;
 34 
 35 public class PrimitiveCastTest {
 36 
 37     @CodeReflection
 38     @IR("""
 39             func @"testFromDouble" (%0 : PrimitiveCastTest, %1 : double)void -> {
 40                 %2 : Var<double> = var %1 @"v";
 41                 %3 : double = var.load %2;
 42                 %4 : Var<double> = var %3 @"d";
 43                 %5 : double = var.load %2;
 44                 %6 : float = conv %5;
 45                 %7 : Var<float> = var %6 @"f";
 46                 %8 : double = var.load %2;
 47                 %9 : long = conv %8;
 48                 %10 : Var<long> = var %9 @"l";
 49                 %11 : double = var.load %2;
 50                 %12 : int = conv %11;
 51                 %13 : Var<int> = var %12 @"i";
 52                 %14 : double = var.load %2;
 53                 %15 : short = conv %14;
 54                 %16 : Var<short> = var %15 @"s";
 55                 %17 : double = var.load %2;
 56                 %18 : char = conv %17;
 57                 %19 : Var<char> = var %18 @"c";
 58                 %20 : double = var.load %2;
 59                 %21 : byte = conv %20;
 60                 %22 : Var<byte> = var %21 @"b";
 61                 return;
 62             };
 63             """)
 64     @SuppressWarnings("cast")
 65     void testFromDouble(double v) {
 66         double d = (double) v;
 67         float f = (float) v;
 68         long l = (long) v;
 69         int i = (int) v;
 70         short s = (short) v;
 71         char c = (char) v;
 72         byte b = (byte) v;
 73     }
 74 
 75     @CodeReflection
 76     @IR("""
 77             func @"testFromFloat" (%0 : PrimitiveCastTest, %1 : float)void -> {
 78                 %2 : Var<float> = var %1 @"v";
 79                 %3 : float = var.load %2;
 80                 %4 : double = conv %3;
 81                 %5 : Var<double> = var %4 @"d";
 82                 %6 : float = var.load %2;
 83                 %7 : Var<float> = var %6 @"f";
 84                 %8 : float = var.load %2;
 85                 %9 : long = conv %8;
 86                 %10 : Var<long> = var %9 @"l";
 87                 %11 : float = var.load %2;
 88                 %12 : int = conv %11;
 89                 %13 : Var<int> = var %12 @"i";
 90                 %14 : float = var.load %2;
 91                 %15 : short = conv %14;
 92                 %16 : Var<short> = var %15 @"s";
 93                 %17 : float = var.load %2;
 94                 %18 : char = conv %17;
 95                 %19 : Var<char> = var %18 @"c";
 96                 %20 : float = var.load %2;
 97                 %21 : byte = conv %20;
 98                 %22 : Var<byte> = var %21 @"b";
 99                 return;
100             };
101             """)
102     @SuppressWarnings("cast")
103     void testFromFloat(float v) {
104         double d = (double) v;
105         float f = (float) v;
106         long l = (long) v;
107         int i = (int) v;
108         short s = (short) v;
109         char c = (char) v;
110         byte b = (byte) v;
111     }
112 
113     @CodeReflection
114     @IR("""
115             func @"testFromLong" (%0 : PrimitiveCastTest, %1 : long)void -> {
116                 %2 : Var<long> = var %1 @"v";
117                 %3 : long = var.load %2;
118                 %4 : double = conv %3;
119                 %5 : Var<double> = var %4 @"d";
120                 %6 : long = var.load %2;
121                 %7 : float = conv %6;
122                 %8 : Var<float> = var %7 @"f";
123                 %9 : long = var.load %2;
124                 %10 : Var<long> = var %9 @"l";
125                 %11 : long = var.load %2;
126                 %12 : int = conv %11;
127                 %13 : Var<int> = var %12 @"i";
128                 %14 : long = var.load %2;
129                 %15 : short = conv %14;
130                 %16 : Var<short> = var %15 @"s";
131                 %17 : long = var.load %2;
132                 %18 : char = conv %17;
133                 %19 : Var<char> = var %18 @"c";
134                 %20 : long = var.load %2;
135                 %21 : byte = conv %20;
136                 %22 : Var<byte> = var %21 @"b";
137                 return;
138             };
139             """)
140     @SuppressWarnings("cast")
141     void testFromLong(long v) {
142         double d = (double) v;
143         float f = (float) v;
144         long l = (long) v;
145         int i = (int) v;
146         short s = (short) v;
147         char c = (char) v;
148         byte b = (byte) v;
149     }
150 
151     @CodeReflection
152     @IR("""
153             func @"testFromInt" (%0 : PrimitiveCastTest, %1 : int)void -> {
154                 %2 : Var<int> = var %1 @"v";
155                 %3 : int = var.load %2;
156                 %4 : double = conv %3;
157                 %5 : Var<double> = var %4 @"d";
158                 %6 : int = var.load %2;
159                 %7 : float = conv %6;
160                 %8 : Var<float> = var %7 @"f";
161                 %9 : int = var.load %2;
162                 %10 : long = conv %9;
163                 %11 : Var<long> = var %10 @"l";
164                 %12 : int = var.load %2;
165                 %13 : Var<int> = var %12 @"i";
166                 %14 : int = var.load %2;
167                 %15 : short = conv %14;
168                 %16 : Var<short> = var %15 @"s";
169                 %17 : int = var.load %2;
170                 %18 : char = conv %17;
171                 %19 : Var<char> = var %18 @"c";
172                 %20 : int = var.load %2;
173                 %21 : byte = conv %20;
174                 %22 : Var<byte> = var %21 @"b";
175                 return;
176             };
177             """)
178     @SuppressWarnings("cast")
179     void testFromInt(int v) {
180         double d = (double) v;
181         float f = (float) v;
182         long l = (long) v;
183         int i = (int) v;
184         short s = (short) v;
185         char c = (char) v;
186         byte b = (byte) v;
187     }
188 
189     @CodeReflection
190     @IR("""
191             func @"testFromShort" (%0 : PrimitiveCastTest, %1 : short)void -> {
192                 %2 : Var<short> = var %1 @"v";
193                 %3 : short = var.load %2;
194                 %4 : double = conv %3;
195                 %5 : Var<double> = var %4 @"d";
196                 %6 : short = var.load %2;
197                 %7 : float = conv %6;
198                 %8 : Var<float> = var %7 @"f";
199                 %9 : short = var.load %2;
200                 %10 : long = conv %9;
201                 %11 : Var<long> = var %10 @"l";
202                 %12 : short = var.load %2;
203                 %13 : int = conv %12;
204                 %14 : Var<int> = var %13 @"i";
205                 %15 : short = var.load %2;
206                 %16 : Var<short> = var %15 @"s";
207                 %17 : short = var.load %2;
208                 %18 : char = conv %17;
209                 %19 : Var<char> = var %18 @"c";
210                 %20 : short = var.load %2;
211                 %21 : byte = conv %20;
212                 %22 : Var<byte> = var %21 @"b";
213                 return;
214             };
215             """)
216     @SuppressWarnings("cast")
217     void testFromShort(short v) {
218         double d = (double) v;
219         float f = (float) v;
220         long l = (long) v;
221         int i = (int) v;
222         short s = (short) v;
223         char c = (char) v;
224         byte b = (byte) v;
225     }
226 
227     @CodeReflection
228     @IR("""
229             func @"testFromChar" (%0 : PrimitiveCastTest, %1 : char)void -> {
230                 %2 : Var<char> = var %1 @"v";
231                 %3 : char = var.load %2;
232                 %4 : double = conv %3;
233                 %5 : Var<double> = var %4 @"d";
234                 %6 : char = var.load %2;
235                 %7 : float = conv %6;
236                 %8 : Var<float> = var %7 @"f";
237                 %9 : char = var.load %2;
238                 %10 : long = conv %9;
239                 %11 : Var<long> = var %10 @"l";
240                 %12 : char = var.load %2;
241                 %13 : int = conv %12;
242                 %14 : Var<int> = var %13 @"i";
243                 %15 : char = var.load %2;
244                 %16 : short = conv %15;
245                 %17 : Var<short> = var %16 @"s";
246                 %18 : char = var.load %2;
247                 %19 : Var<char> = var %18 @"c";
248                 %20 : char = var.load %2;
249                 %21 : byte = conv %20;
250                 %22 : Var<byte> = var %21 @"b";
251                 return;
252             };
253             """)
254     @SuppressWarnings("cast")
255     void testFromChar(char v) {
256         double d = (double) v;
257         float f = (float) v;
258         long l = (long) v;
259         int i = (int) v;
260         short s = (short) v;
261         char c = (char) v;
262         byte b = (byte) v;
263     }
264 
265     @CodeReflection
266     @IR("""
267             func @"testFromByte" (%0 : PrimitiveCastTest, %1 : byte)void -> {
268                 %2 : Var<byte> = var %1 @"v";
269                 %3 : byte = var.load %2;
270                 %4 : double = conv %3;
271                 %5 : Var<double> = var %4 @"d";
272                 %6 : byte = var.load %2;
273                 %7 : float = conv %6;
274                 %8 : Var<float> = var %7 @"f";
275                 %9 : byte = var.load %2;
276                 %10 : long = conv %9;
277                 %11 : Var<long> = var %10 @"l";
278                 %12 : byte = var.load %2;
279                 %13 : int = conv %12;
280                 %14 : Var<int> = var %13 @"i";
281                 %15 : byte = var.load %2;
282                 %16 : short = conv %15;
283                 %17 : Var<short> = var %16 @"s";
284                 %18 : byte = var.load %2;
285                 %19 : char = conv %18;
286                 %20 : Var<char> = var %19 @"c";
287                 %21 : byte = var.load %2;
288                 %22 : Var<byte> = var %21 @"b";
289                 return;
290             };
291             """)
292     @SuppressWarnings("cast")
293     void testFromByte(byte v) {
294         double d = (double) v;
295         float f = (float) v;
296         long l = (long) v;
297         int i = (int) v;
298         short s = (short) v;
299         char c = (char) v;
300         byte b = (byte) v;
301     }
302 }