1 /*
   2  * Copyright (c) 2018, 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 runtime.valhalla.valuetypes;
  25 
  26 import jdk.test.lib.Asserts;
  27 
  28 /*
  29  * @test QuickeningTest
  30  * @summary Test quickening of getfield and putfield applied to value fields
  31  * @library /test/lib
  32  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator Point.java JumboValue.java QuickeningTest.java
  33  * @run main/othervm -Xint -XX:+EnableValhalla runtime.valhalla.valuetypes.QuickeningTest
  34  * @run main/othervm -Xcomp -XX:+EnableValhalla runtime.valhalla.valuetypes.QuickeningTest
  35  */
  36 
  37 public class QuickeningTest {
  38 
  39     static class Parent {
  40     Point.box nfp;        /* Not flattenable value field */
  41     Point.val fp;         /* Flattenable and flattened value field */
  42     JumboValue.val fj;    /* Flattenable not flattened value field */
  43 
  44         public void setNfp(Point p) { nfp = p; }
  45         public void setFp(Point p) { fp = p; }
  46         public void setFj(JumboValue j) { fj = j; }
  47     }
  48 
  49     static class Child extends Parent {
  50         // This class inherited fields from the Parent class
  51         Point.box nfp2;      /* Not flattenable value field */
  52         Point.val fp2;        /* Flattenable and flattened value field */
  53         JumboValue.val fj2;   /* Flattenable not flattene value field */
  54 
  55         public void setNfp2(Point p) { nfp2 = p; }
  56         public void setFp2(Point p)  { fp2 = p; }
  57         public void setFj2(JumboValue j) { fj2 = j; }
  58     }
  59 
  60     static final value class Value {
  61         final Point.box nfp;       /* Not flattenable value field */
  62         final Point.val fp;         /* Flattenable and flattened value field */
  63         final JumboValue.val fj;    /* Flattenable not flattene value field */
  64 
  65         private Value() {
  66             nfp = Point.createPoint(0, 0);
  67             fp = Point.createPoint(0, 0);
  68             fj = JumboValue.createJumboValue();
  69         }
  70 
  71         public static Value create() {
  72             return Value.default;
  73         }
  74     }
  75 
  76     static void testUninitializedFields() {
  77         Parent p = new Parent();
  78         Asserts.assertEquals(p.nfp, null, "invalid uninitialized not flattenable");
  79         Asserts.assertEquals(p.fp.x, 0, "invalid value for uninitialized flattened field");
  80         Asserts.assertEquals(p.fp.y, 0, "invalid value for uninitialized flattened field");
  81         Asserts.assertEquals(p.fj.l0, 0L, "invalid value for uninitialized flattened field");
  82         Asserts.assertEquals(p.fj.l1, 0L, "invalid value for uninitialized flattened field");
  83 
  84         Child c = new Child();
  85         Asserts.assertEquals(c.nfp, null, "invalid uninitialized not flattenable field");
  86         Asserts.assertEquals(c.fp.x, 0, "invalid value for uninitialized flattened field");
  87         Asserts.assertEquals(c.fp.y, 0, "invalid value for uninitialized flattened field");
  88         Asserts.assertEquals(c.fj.l0, 0L, "invalid value for uninitialized flattened field");
  89         Asserts.assertEquals(c.fj.l1, 0L, "invalid value for uninitialized flattened field");
  90         Asserts.assertEquals(c.nfp2, null, "invalid uninitialized not flattenable");
  91         Asserts.assertEquals(c.fp2.x, 0, "invalid value for uninitialized flattened field");
  92         Asserts.assertEquals(c.fp2.y, 0, "invalid value for uninitialized flattened field");
  93         Asserts.assertEquals(c.fj2.l0, 0L, "invalid value for uninitialized not flattened field");
  94         Asserts.assertEquals(c.fj2.l1, 0L, "invalid value for uninitialized not flattened field");
  95 
  96         Value v = Value.create();
  97         Asserts.assertEquals(v.nfp, null, "invalid uninitialized not flattenable");
  98         Asserts.assertEquals(v.fp.x, 0, "invalid value for uninitialized flattened field");
  99         Asserts.assertEquals(v.fp.y, 0, "invalid value for uninitialized flattened field");
 100         Asserts.assertEquals(v.fj.l0, 0L, "invalid value for uninitialized not flattened field");
 101         Asserts.assertEquals(v.fj.l1, 0L, "invalid value for uninitialized not flattened field");
 102     }
 103 
 104     static void testPutfieldAndGetField() {
 105         Point p1 = Point.createPoint(16, 47);
 106         Point p2 = Point.createPoint(32, 64);
 107 
 108         JumboValue j1 = JumboValue.createJumboValue().update(4, 5);
 109         JumboValue j2 = JumboValue.createJumboValue().update(7, 9);
 110 
 111         Parent p = new Parent();
 112         // executing each setter twice to test quickened bytecodes
 113         p.setNfp(p1);
 114         p.setNfp(p2);
 115         p.setFp(p2);
 116         p.setFp(p1);
 117         p.setFj(j1);
 118         p.setFj(j2);
 119 
 120         Asserts.assertTrue(p.nfp.equals(p2), "invalid updated not flattenable field");
 121         Asserts.assertEquals(p.fp.x, 16, "invalid value for updated flattened field");
 122         Asserts.assertEquals(p.fp.y, 47, "invalid value for updated flattened field");
 123         Asserts.assertTrue(p.fj.equals(j2), "invalid value for updated not flattened field");
 124 
 125         Child c = new Child();
 126         c.setNfp(p1);
 127         c.setNfp(p2);
 128         c.setFp(p2);
 129         c.setFp(p1);
 130         c.setFj(j1);
 131         c.setFj(j2);
 132         c.setNfp2(p2);
 133         c.setNfp2(p1);
 134         c.setFp2(p1);
 135         c.setFp2(p2);
 136         c.setFj2(j2);
 137         c.setFj2(j1);
 138 
 139         Asserts.assertTrue(c.nfp.equals(p2), "invalid updated not flattenable field");
 140         Asserts.assertEquals(c.fp.x, 16, "invalid value for updated flattened field");
 141         Asserts.assertEquals(c.fp.y, 47, "invalid value for updated flattened field");
 142         Asserts.assertTrue(c.fj.equals(j2), "invalid value for updated not flattened field");
 143 
 144         Asserts.assertTrue(c.nfp2.equals(p1), "invalid updated not flattenable field");
 145         Asserts.assertEquals(c.fp2.x, 32, "invalid value for updated flattened field");
 146         Asserts.assertEquals(c.fp2.y, 64, "invalid value for updated flattened field");
 147         Asserts.assertTrue(c.fj2.equals(j1), "invalid value for updated not flattened field");
 148     }
 149 
 150     public static void main(String[] args) {
 151         testUninitializedFields();
 152         testUninitializedFields(); // run twice to test quickened bytecodes
 153         testPutfieldAndGetField();
 154     }
 155 }