1 /*
  2  * Copyright (c) 2017, 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 package compiler.valhalla.inlinetypes;
 25 
 26 import jdk.test.lib.Asserts;
 27 import jdk.test.lib.Utils;
 28 import compiler.lib.ir_framework.DontInline;
 29 import compiler.lib.ir_framework.ForceInline;
 30 
 31 import jdk.internal.vm.annotation.LooselyConsistentValue;
 32 import jdk.internal.vm.annotation.NullRestricted;
 33 import jdk.internal.vm.annotation.Strict;
 34 
 35 @LooselyConsistentValue
 36 value class MyValue3Inline {
 37     float f7;
 38     double f8;
 39 
 40     @ForceInline
 41     public MyValue3Inline(float f7, double f8) {
 42         this.f7 = f7;
 43         this.f8 = f8;
 44     }
 45 
 46     @ForceInline
 47     static MyValue3Inline setF7(MyValue3Inline v, float f7) {
 48         return new MyValue3Inline(f7, v.f8);
 49     }
 50 
 51     @ForceInline
 52     static MyValue3Inline setF8(MyValue3Inline v, double f8) {
 53         return new MyValue3Inline(v.f7, f8);
 54     }
 55 
 56     @ForceInline
 57     public static MyValue3Inline createDefault() {
 58         return new MyValue3Inline(0, 0);
 59     }
 60 
 61     @ForceInline
 62     public static MyValue3Inline createWithFieldsInline(float f7, double f8) {
 63         MyValue3Inline v = createDefault();
 64         v = setF7(v, f7);
 65         v = setF8(v, f8);
 66         return v;
 67     }
 68 
 69     @Override
 70     public String toString() {
 71         return "MyValue3Inline[f7=" + f7 + ", f8=" + f8 + "]";
 72     }
 73 }
 74 
 75 // Inline type definition to stress test return of an inline type in registers
 76 // (uses all registers of calling convention on x86_64)
 77 @LooselyConsistentValue
 78 public value class MyValue3 extends MyAbstract {
 79     char c;
 80     byte bb;
 81     short s;
 82     int i;
 83     long l;
 84     Object o;
 85     float f1;
 86     double f2;
 87     float f3;
 88     double f4;
 89     float f5;
 90     double f6;
 91     @Strict
 92     @NullRestricted
 93     MyValue3Inline v1;
 94 
 95     static final MyValue3 DEFAULT = new MyValue3((char)0, (byte)0, (short)0, 0, 0, null,
 96                                                  0, 0, 0, 0, 0, 0, new MyValue3Inline(0, 0));
 97 
 98     @ForceInline
 99     public MyValue3(char c, byte bb, short s, int i, long l, Object o,
100                     float f1, double f2, float f3, double f4, float f5, double f6,
101                     MyValue3Inline v1) {
102         this.c = c;
103         this.bb = bb;
104         this.s = s;
105         this.i = i;
106         this.l = l;
107         this.o = o;
108         this.f1 = f1;
109         this.f2 = f2;
110         this.f3 = f3;
111         this.f4 = f4;
112         this.f5 = f5;
113         this.f6 = f6;
114         this.v1 = v1;
115     }
116 
117     @ForceInline
118     static MyValue3 setC(MyValue3 v, char c) {
119         return new MyValue3(c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
120     }
121 
122     @ForceInline
123     static MyValue3 setBB(MyValue3 v, byte bb) {
124         return new MyValue3(v.c, bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
125     }
126 
127     @ForceInline
128     static MyValue3 setS(MyValue3 v, short s) {
129         return new MyValue3(v.c, v.bb, s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
130     }
131 
132     @ForceInline
133     static MyValue3 setI(MyValue3 v, int i) {
134         return new MyValue3(v.c, v.bb, v.s, i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
135     }
136 
137     @ForceInline
138     static MyValue3 setL(MyValue3 v, long l) {
139         return new MyValue3(v.c, v.bb, v.s, v.i, l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
140     }
141 
142     @ForceInline
143     static MyValue3 setO(MyValue3 v, Object o) {
144         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
145     }
146 
147     @ForceInline
148     static MyValue3 setF1(MyValue3 v, float f1) {
149         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, f1, v.f2, v.f3, v.f4, v.f5, v.f6, v.v1);
150     }
151 
152     @ForceInline
153     static MyValue3 setF2(MyValue3 v, double f2) {
154         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, f2, v.f3, v.f4, v.f5, v.f6, v.v1);
155     }
156 
157     @ForceInline
158     static MyValue3 setF3(MyValue3 v, float f3) {
159         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, f3, v.f4, v.f5, v.f6, v.v1);
160     }
161 
162     @ForceInline
163     static MyValue3 setF4(MyValue3 v, double f4) {
164         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, f4, v.f5, v.f6, v.v1);
165     }
166 
167     @ForceInline
168     static MyValue3 setF5(MyValue3 v, float f5) {
169         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, f5, v.f6, v.v1);
170     }
171 
172     @ForceInline
173     static MyValue3 setF6(MyValue3 v, double f6) {
174         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, f6, v.v1);
175     }
176 
177     @ForceInline
178     static MyValue3 setV1(MyValue3 v, MyValue3Inline v1) {
179         return new MyValue3(v.c, v.bb, v.s, v.i, v.l, v.o, v.f1, v.f2, v.f3, v.f4, v.f5, v.f6, v1);
180     }
181 
182     @ForceInline
183     public static MyValue3 createDefault() {
184         return new MyValue3((char)0, (byte)0, (short)0, 0, 0, null, 0, 0, 0, 0, 0, 0, MyValue3Inline.createDefault());
185     }
186 
187     @ForceInline
188     public static MyValue3 create() {
189         java.util.Random r = Utils.getRandomInstance();
190         MyValue3 v = createDefault();
191         v = setC(v, (char)r.nextInt());
192         v = setBB(v, (byte)r.nextInt());
193         v = setS(v, (short)r.nextInt());
194         v = setI(v, r.nextInt());
195         v = setL(v, r.nextLong());
196         v = setO(v, new Object());
197         v = setF1(v, r.nextFloat());
198         v = setF2(v, r.nextDouble());
199         v = setF3(v, r.nextFloat());
200         v = setF4(v, r.nextDouble());
201         v = setF5(v, r.nextFloat());
202         v = setF6(v, r.nextDouble());
203         v = setV1(v, MyValue3Inline.createWithFieldsInline(r.nextFloat(), r.nextDouble()));
204         return v;
205     }
206 
207     @DontInline
208     public static MyValue3 createDontInline() {
209         return create();
210     }
211 
212     @ForceInline
213     public static MyValue3 copy(MyValue3 other) {
214         MyValue3 v = createDefault();
215         v = setC(v, other.c);
216         v = setBB(v, other.bb);
217         v = setS(v, other.s);
218         v = setI(v, other.i);
219         v = setL(v, other.l);
220         v = setO(v, other.o);
221         v = setF1(v, other.f1);
222         v = setF2(v, other.f2);
223         v = setF3(v, other.f3);
224         v = setF4(v, other.f4);
225         v = setF5(v, other.f5);
226         v = setF6(v, other.f6);
227         v = setV1(v, other.v1);
228         return v;
229     }
230 
231     @DontInline
232     public void verify(MyValue3 other) {
233         Asserts.assertEQ(c, other.c);
234         Asserts.assertEQ(bb, other.bb);
235         Asserts.assertEQ(s, other.s);
236         Asserts.assertEQ(i, other.i);
237         Asserts.assertEQ(l, other.l);
238         Asserts.assertEQ(o, other.o);
239         Asserts.assertEQ(f1, other.f1);
240         Asserts.assertEQ(f2, other.f2);
241         Asserts.assertEQ(f3, other.f3);
242         Asserts.assertEQ(f4, other.f4);
243         Asserts.assertEQ(f5, other.f5);
244         Asserts.assertEQ(f6, other.f6);
245         Asserts.assertEQ(v1.f7, other.v1.f7);
246         Asserts.assertEQ(v1.f8, other.v1.f8);
247     }
248 
249     @ForceInline
250     public long hash() {
251         return c +
252             bb +
253             s +
254             i +
255             l +
256             o.hashCode() +
257             Float.hashCode(f1) +
258             Double.hashCode(f2) +
259             Float.hashCode(f3) +
260             Double.hashCode(f4) +
261             Float.hashCode(f5) +
262             Double.hashCode(f6) +
263             Float.hashCode(v1.f7) +
264             Double.hashCode(v1.f8);
265     }
266 
267     @Override
268     public String toString() {
269         return "MyValue3[c=" + c + ", bb=" + bb + ", s=" + s + ", i=" + i + ", l=" + l + ", o=" + o +
270                 ", f1=" + f1 + ", f2=" + f2 + ", f3=" + f3 + ", f4=" + f4 + ", f5=" + f5 + ", f6=" + f6 + ", v1=" + v1 + "]";
271     }
272 }
273