1 /*
  2  * Copyright (c) 2017, 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 package compiler.valhalla.inlinetypes;
 25 
 26 import compiler.lib.ir_framework.DontInline;
 27 import compiler.lib.ir_framework.ForceInline;
 28 
 29 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 30 import jdk.internal.vm.annotation.LooselyConsistentValue;
 31 import jdk.internal.vm.annotation.NullRestricted;
 32 
 33 @ImplicitlyConstructible
 34 @LooselyConsistentValue
 35 value class MyValue2Inline {
 36     double d;
 37     long l;
 38 
 39     @ForceInline
 40     public MyValue2Inline(double d, long l) {
 41         this.d = d;
 42         this.l = l;
 43     }
 44 
 45     @ForceInline
 46     static MyValue2Inline setD(MyValue2Inline v, double d) {
 47         return new MyValue2Inline(d, v.l);
 48     }
 49 
 50     @ForceInline
 51     static MyValue2Inline setL(MyValue2Inline v, long l) {
 52         return new MyValue2Inline(v.d, l);
 53     }
 54 
 55     @ForceInline
 56     public static MyValue2Inline createDefault() {
 57         return new MyValue2Inline(0, 0);
 58     }
 59 
 60     @ForceInline
 61     public static MyValue2Inline createWithFieldsInline(double d, long l) {
 62         MyValue2Inline v = MyValue2Inline.createDefault();
 63         v = MyValue2Inline.setD(v, d);
 64         v = MyValue2Inline.setL(v, l);
 65         return v;
 66     }
 67 }
 68 
 69 @ImplicitlyConstructible
 70 @LooselyConsistentValue
 71 public value class MyValue2 extends MyAbstract {
 72     int x;
 73     byte y;
 74     @NullRestricted
 75     MyValue2Inline v;
 76 
 77     @ForceInline
 78     public MyValue2(int x, byte y, MyValue2Inline v) {
 79         this.x = x;
 80         this.y = y;
 81         this.v = v;
 82     }
 83 
 84     @ForceInline
 85     public static MyValue2 createDefaultInline() {
 86         return new MyValue2(0, (byte)0, MyValue2Inline.createDefault());
 87     }
 88 
 89     @ForceInline
 90     public static MyValue2 createWithFieldsInline(int x, long y, double d) {
 91         MyValue2 v = createDefaultInline();
 92         v = setX(v, x);
 93         v = setY(v, (byte)x);
 94         v = setV(v, MyValue2Inline.createWithFieldsInline(d, y));
 95         return v;
 96     }
 97 
 98     @ForceInline
 99     public static MyValue2 createWithFieldsInline(int x, double d) {
100         MyValue2 v = createDefaultInline();
101         v = setX(v, x);
102         v = setY(v, (byte)x);
103         v = setV(v, MyValue2Inline.createWithFieldsInline(d, InlineTypes.rL));
104         return v;
105     }
106 
107     @DontInline
108     public static MyValue2 createWithFieldsDontInline(int x, double d) {
109         MyValue2 v = createDefaultInline();
110         v = setX(v, x);
111         v = setY(v, (byte)x);
112         v = setV(v, MyValue2Inline.createWithFieldsInline(d, InlineTypes.rL));
113         return v;
114     }
115 
116     @ForceInline
117     public long hash() {
118         return x + y + (long)v.d + v.l;
119     }
120 
121     @DontInline
122     public long hashInterpreted() {
123         return x + y + (long)v.d + v.l;
124     }
125 
126     @ForceInline
127     public void print() {
128         System.out.print("x=" + x + ", y=" + y + ", d=" + v.d + ", l=" + v.l);
129     }
130 
131     @ForceInline
132     static MyValue2 setX(MyValue2 v, int x) {
133         return new MyValue2(x, v.y, v.v);
134     }
135 
136     @ForceInline
137     static MyValue2 setY(MyValue2 v, byte y) {
138         return new MyValue2(v.x, y, v.v);
139     }
140 
141     @ForceInline
142     static MyValue2 setV(MyValue2 v, MyValue2Inline vi) {
143         return new MyValue2(v.x, v.y, vi);
144     }
145 }