1 /*
  2  * Copyright (c) 2022, 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.DontCompile;
 27 import compiler.lib.ir_framework.DontInline;
 28 import compiler.lib.ir_framework.ForceCompileClassInitializer;
 29 import compiler.lib.ir_framework.ForceInline;
 30 
 31 import java.util.Arrays;
 32 
 33 @ForceCompileClassInitializer
 34 public value class MyValueClass1 extends MyAbstract {
 35     static int s;
 36     static long sf = InlineTypes.rL;
 37     int x;
 38     long y;
 39     short z;
 40     Integer o;
 41     int[] oa;
 42     MyValueClass2 v1;
 43     MyValueClass2 v2;
 44     static MyValueClass2 v3 = MyValueClass2.createWithFieldsInline(InlineTypes.rI, InlineTypes.rD);
 45     MyValueClass2 v4;
 46     int c;
 47 
 48     @ForceInline
 49     public MyValueClass1(int x, long y, short z, Integer o, int[] oa, MyValueClass2 v1, MyValueClass2 v2, MyValueClass2 v4, int c) {
 50         s = 0;
 51         this.x = x;
 52         this.y = y;
 53         this.z = z;
 54         this.o = o;
 55         this.oa = oa;
 56         this.v1 = v1;
 57         this.v2 = v2;
 58         this.v4 = v4;
 59         this.c = c;
 60     }
 61 
 62     @DontInline
 63     static MyValueClass1 createDefaultDontInline() {
 64         return createDefaultInline();
 65     }
 66 
 67     @ForceInline
 68     static MyValueClass1 createDefaultInline() {
 69         return new MyValueClass1(0, 0, (short)0, null, null, null, null, null, 0);
 70     }
 71 
 72     @DontInline
 73     static MyValueClass1 createWithFieldsDontInline(int x, long y) {
 74         return createWithFieldsInline(x, y);
 75     }
 76 
 77     @ForceInline
 78     static MyValueClass1 createWithFieldsInline(int x, long y) {
 79         MyValueClass1 v = createDefaultInline();
 80         v = setX(v, x);
 81         v = setY(v, y);
 82         v = setZ(v, (short)x);
 83         // Don't use Integer.valueOf here to avoid control flow added by Integer cache check
 84         v = setO(v, new Integer(x));
 85         int[] oa = {x};
 86         v = setOA(v, oa);
 87         v = setV1(v, MyValueClass2.createWithFieldsInline(x, y, InlineTypes.rD));
 88         v = setV2(v, MyValueClass2.createWithFieldsInline(x + 1, y + 1, InlineTypes.rD + 1));
 89         v = setV4(v, MyValueClass2.createWithFieldsInline(x + 2, y + 2, InlineTypes.rD + 2));
 90         v = setC(v, (int)(x+y));
 91         return v;
 92     }
 93 
 94     // Hash only primitive and inline type fields to avoid NullPointerException
 95     @ForceInline
 96     public long hashPrimitive() {
 97         return s + sf + x + y + z + c + v1.hash() + v2.hash() + v3.hash();
 98     }
 99 
100     @ForceInline
101     public long hash() {
102         long res = hashPrimitive();
103         try {
104             res += o;
105         } catch (NullPointerException npe) {}
106         try {
107             res += oa[0];
108         } catch (NullPointerException npe) {}
109         try {
110             res += v4.hash();
111         } catch (NullPointerException npe) {}
112         return res;
113     }
114 
115     @DontCompile
116     public long hashInterpreted() {
117         return s + sf + x + y + z + o + oa[0] + c + v1.hashInterpreted() + v2.hashInterpreted() + v3.hashInterpreted() + v4.hashInterpreted();
118     }
119 
120     @ForceInline
121     public void print() {
122         System.out.print("s=" + s + ", sf=" + sf + ", x=" + x + ", y=" + y + ", z=" + z + ", o=" + (o != null ? (Integer)o : "NULL") + ", oa=" + (oa != null ? oa[0] : "NULL") + ", v1[");
123         v1.print();
124         System.out.print("], v2[");
125         v2.print();
126         System.out.print("], v3[");
127         v3.print();
128         System.out.print("], v4[");
129         v4.print();
130         System.out.print("], c=" + c);
131     }
132 
133     @ForceInline
134     static MyValueClass1 setX(MyValueClass1 v, int x) {
135         return new MyValueClass1(x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v.c);
136     }
137 
138     @ForceInline
139     static MyValueClass1 setY(MyValueClass1 v, long y) {
140         return new MyValueClass1(v.x, y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v.c);
141     }
142 
143     @ForceInline
144     static MyValueClass1 setZ(MyValueClass1 v, short z) {
145         return new MyValueClass1(v.x, v.y, z, v.o, v.oa, v.v1, v.v2, v.v4, v.c);
146     }
147 
148     @ForceInline
149     static MyValueClass1 setO(MyValueClass1 v, Integer o) {
150         return new MyValueClass1(v.x, v.y, v.z, o, v.oa, v.v1, v.v2, v.v4, v.c);
151     }
152 
153     @ForceInline
154     static MyValueClass1 setOA(MyValueClass1 v, int[] oa) {
155         return new MyValueClass1(v.x, v.y, v.z, v.o, oa, v.v1, v.v2, v.v4, v.c);
156     }
157 
158     @ForceInline
159     static MyValueClass1 setC(MyValueClass1 v, int c) {
160         return new MyValueClass1(v.x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, c);
161     }
162 
163     @ForceInline
164     static MyValueClass1 setV1(MyValueClass1 v, MyValueClass2 v1) {
165         return new MyValueClass1(v.x, v.y, v.z, v.o, v.oa, v1, v.v2, v.v4, v.c);
166     }
167 
168     @ForceInline
169     static MyValueClass1 setV2(MyValueClass1 v, MyValueClass2 v2) {
170         return new MyValueClass1(v.x, v.y, v.z, v.o, v.oa, v.v1, v2, v.v4, v.c);
171     }
172 
173     @ForceInline
174     static MyValueClass1 setV4(MyValueClass1 v, MyValueClass2 v4) {
175         return new MyValueClass1(v.x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v4, v.c);
176     }
177 
178     @DontInline
179     void dontInline(MyValueClass1 arg) {
180 
181     }
182 
183     @Override
184     public String toString() {
185         return "MyValueClass1[s=" + s + ", sf=" + sf + ", x=" + x + ", y=" + y + ", z=" + z + ", o=" + o + ", oa=" + Arrays.toString(oa) +
186                 ", v1=" + v1 + ", v2=" + v2 + ", v4=" + v4 + ", c=" + c + "]";
187     }
188 }