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