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