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