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