1 /*
   2  * Copyright (c) 2015, 2016, 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 /**
  25  * @test
  26  * @requires vm.jvmci & (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9")
  27  * @library /
  28  * @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
  29  *          jdk.internal.vm.ci/jdk.vm.ci.meta
  30  *          jdk.internal.vm.ci/jdk.vm.ci.code
  31  *          jdk.internal.vm.ci/jdk.vm.ci.code.site
  32  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  33  *          jdk.internal.vm.ci/jdk.vm.ci.amd64
  34  *          jdk.internal.vm.ci/jdk.vm.ci.sparc
  35  * @compile CodeInstallationTest.java DebugInfoTest.java TestAssembler.java TestHotSpotVMConfig.java amd64/AMD64TestAssembler.java sparc/SPARCTestAssembler.java
  36  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.code.test.VirtualObjectDebugInfoTest
  37  */
  38 
  39 package jdk.vm.ci.code.test;
  40 
  41 import jdk.vm.ci.code.Register;
  42 import jdk.vm.ci.code.VirtualObject;
  43 import jdk.vm.ci.hotspot.HotSpotConstant;
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.JavaValue;
  47 import jdk.vm.ci.meta.ResolvedJavaField;
  48 import jdk.vm.ci.meta.ResolvedJavaType;
  49 import org.junit.Assert;
  50 import org.junit.Test;
  51 
  52 import java.util.ArrayList;
  53 import java.util.Objects;
  54 
  55 public class VirtualObjectDebugInfoTest extends DebugInfoTest {
  56 
  57     private static class TestClass {
  58 
  59         private long longField;
  60         private int intField;
  61         private float floatField;
  62         private Object[] arrayField;
  63 
  64         TestClass() {
  65             this.longField = 8472;
  66             this.intField = 42;
  67             this.floatField = 3.14f;
  68             this.arrayField = new Object[]{Integer.valueOf(58), this, null, Integer.valueOf(17), "Hello, World!"};
  69         }
  70 
  71         @Override
  72         public boolean equals(Object o) {
  73             if (!(o instanceof TestClass)) {
  74                 return false;
  75             }
  76 
  77             TestClass other = (TestClass) o;
  78             if (this.longField != other.longField || this.intField != other.intField || this.floatField != other.floatField || this.arrayField.length != other.arrayField.length) {
  79                 return false;
  80             }
  81 
  82             for (int i = 0; i < this.arrayField.length; i++) {
  83                 // break cycle
  84                 if (this.arrayField[i] == this && other.arrayField[i] == other) {
  85                     continue;
  86                 }
  87 
  88                 if (!Objects.equals(this.arrayField[i], other.arrayField[i])) {
  89                     return false;
  90                 }
  91             }
  92 
  93             return true;
  94         }
  95 
  96         @Override
  97         public int hashCode() {
  98             return super.hashCode();
  99         }
 100     }
 101 
 102     public static TestClass buildObject() {
 103         return new TestClass();
 104     }
 105 
 106     public static TestClass buildObjectStack() {
 107         return new TestClass();
 108     }
 109 
 110     boolean storeToStack;
 111 
 112     private VirtualObject[] compileBuildObject(TestAssembler asm, JavaValue[] values) {
 113         TestClass template = new TestClass();
 114         ArrayList<VirtualObject> vobjs = new ArrayList<>();
 115 
 116         ResolvedJavaType retType = metaAccess.lookupJavaType(TestClass.class);
 117         VirtualObject ret = VirtualObject.get(retType, vobjs.size());
 118         vobjs.add(ret);
 119         values[0] = ret;
 120 
 121         ResolvedJavaType arrayType = metaAccess.lookupJavaType(Object[].class);
 122         VirtualObject array = VirtualObject.get(arrayType, vobjs.size());
 123         vobjs.add(array);
 124 
 125         // build array for ret.arrayField
 126         ResolvedJavaType integerType = metaAccess.lookupJavaType(Integer.class);
 127         JavaValue[] arrayContent = new JavaValue[template.arrayField.length];
 128         JavaKind[] arrayKind = new JavaKind[template.arrayField.length];
 129         for (int i = 0; i < arrayContent.length; i++) {
 130             arrayKind[i] = JavaKind.Object;
 131             if (template.arrayField[i] == null) {
 132                 arrayContent[i] = JavaConstant.NULL_POINTER;
 133             } else if (template.arrayField[i] == template) {
 134                 arrayContent[i] = ret;
 135             } else if (template.arrayField[i] instanceof Integer) {
 136                 int value = (Integer) template.arrayField[i];
 137                 VirtualObject boxed = VirtualObject.get(integerType, vobjs.size());
 138                 vobjs.add(boxed);
 139                 arrayContent[i] = boxed;
 140                 boxed.setValues(new JavaValue[]{JavaConstant.forInt(value)}, new JavaKind[]{JavaKind.Int});
 141             } else if (template.arrayField[i] instanceof String) {
 142                 String value = (String) template.arrayField[i];
 143                 Register reg = asm.emitLoadPointer((HotSpotConstant) constantReflection.forString(value));
 144                 if (storeToStack) {
 145                     arrayContent[i] = asm.emitPointerToStack(reg);
 146                 } else {
 147                     arrayContent[i] = reg.asValue(asm.getValueKind(JavaKind.Object));
 148                 }
 149             } else {
 150                 Assert.fail("unexpected value");
 151             }
 152         }
 153         array.setValues(arrayContent, arrayKind);
 154 
 155         // build return object
 156         ResolvedJavaField[] fields = retType.getInstanceFields(true);
 157         JavaValue[] retContent = new JavaValue[fields.length];
 158         JavaKind[] retKind = new JavaKind[fields.length];
 159         for (int i = 0; i < fields.length; i++) {
 160             retKind[i] = fields[i].getJavaKind();
 161             switch (retKind[i]) {
 162                 case Long: // template.longField
 163                     retContent[i] = JavaConstant.forLong(template.longField);
 164                     break;
 165                 case Int: // template.intField
 166                     Register intReg = asm.emitLoadInt(template.intField);
 167                     retContent[i] = asm.emitIntToStack(intReg);
 168                     break;
 169                 case Float: // template.floatField
 170                     Register fReg = asm.emitLoadFloat(template.floatField);
 171                     retContent[i] = fReg.asValue(asm.getValueKind(JavaKind.Float));
 172                     break;
 173                 case Object: // template.arrayField
 174                     retContent[i] = array;
 175                     break;
 176                 default:
 177                     Assert.fail("unexpected field");
 178             }
 179         }
 180         ret.setValues(retContent, retKind);
 181 
 182         return vobjs.toArray(new VirtualObject[0]);
 183     }
 184 
 185     @Test
 186     public void testBuildObject() {
 187         storeToStack = false;
 188         test(this::compileBuildObject, getMethod("buildObject"), 7, JavaKind.Object);
 189     }
 190 
 191     @Test
 192     public void testBuildObjectStack() {
 193         storeToStack = true;
 194         test(this::compileBuildObject, getMethod("buildObjectStack"), 7, JavaKind.Object);
 195     }
 196 }
--- EOF ---