hotspot/test/compiler/jvmci/code/VirtualObjectDebugInfoTest.java
changeset 38126 c3706b502779
parent 37761 82b8d12a553f
parent 38079 fd24ad51113a
child 38127 2ab00cd4556e
equal deleted inserted replaced
37761:82b8d12a553f 38126:c3706b502779
     1 /*
       
     2  * Copyright (c) 2015, 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.hotspot
       
    29  *          jdk.vm.ci/jdk.vm.ci.meta
       
    30  *          jdk.vm.ci/jdk.vm.ci.code
       
    31  *          jdk.vm.ci/jdk.vm.ci.code.site
       
    32  *          jdk.vm.ci/jdk.vm.ci.runtime
       
    33  *          jdk.vm.ci/jdk.vm.ci.amd64
       
    34  *          jdk.vm.ci/jdk.vm.ci.sparc
       
    35  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI compiler.jvmci.code.VirtualObjectDebugInfoTest
       
    36  */
       
    37 
       
    38 package compiler.jvmci.code;
       
    39 
       
    40 import java.util.ArrayList;
       
    41 import java.util.Objects;
       
    42 
       
    43 import jdk.vm.ci.code.Register;
       
    44 import jdk.vm.ci.code.VirtualObject;
       
    45 import jdk.vm.ci.hotspot.HotSpotConstant;
       
    46 import jdk.vm.ci.meta.JavaConstant;
       
    47 import jdk.vm.ci.meta.JavaKind;
       
    48 import jdk.vm.ci.meta.JavaValue;
       
    49 import jdk.vm.ci.meta.ResolvedJavaField;
       
    50 import jdk.vm.ci.meta.ResolvedJavaType;
       
    51 
       
    52 import org.junit.Assert;
       
    53 import org.junit.Test;
       
    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         public 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
       
    79                 || this.intField != other.intField
       
    80                 || this.floatField != other.floatField
       
    81                 || this.arrayField.length != other.arrayField.length) {
       
    82                 return false;
       
    83             }
       
    84 
       
    85             for (int i = 0; i < this.arrayField.length; i++) {
       
    86                 // break cycle
       
    87                 if (this.arrayField[i] == this && other.arrayField[i] == other) {
       
    88                     continue;
       
    89                 }
       
    90 
       
    91                 if (!Objects.equals(this.arrayField[i], other.arrayField[i])) {
       
    92                     return false;
       
    93                 }
       
    94             }
       
    95 
       
    96             return true;
       
    97         }
       
    98     }
       
    99 
       
   100     public static TestClass buildObject() {
       
   101         return new TestClass();
       
   102     }
       
   103 
       
   104     private VirtualObject[] compileBuildObject(TestAssembler asm, JavaValue[] values) {
       
   105         TestClass template = new TestClass();
       
   106         ArrayList<VirtualObject> vobjs = new ArrayList<>();
       
   107 
       
   108         ResolvedJavaType retType = metaAccess.lookupJavaType(TestClass.class);
       
   109         VirtualObject ret = VirtualObject.get(retType, vobjs.size());
       
   110         vobjs.add(ret);
       
   111         values[0] = ret;
       
   112 
       
   113         ResolvedJavaType arrayType = metaAccess.lookupJavaType(Object[].class);
       
   114         VirtualObject array = VirtualObject.get(arrayType, vobjs.size());
       
   115         vobjs.add(array);
       
   116 
       
   117         // build array for ret.arrayField
       
   118         ResolvedJavaType integerType = metaAccess.lookupJavaType(Integer.class);
       
   119         JavaValue[] arrayContent = new JavaValue[template.arrayField.length];
       
   120         JavaKind[] arrayKind = new JavaKind[template.arrayField.length];
       
   121         for (int i = 0; i < arrayContent.length; i++) {
       
   122             arrayKind[i] = JavaKind.Object;
       
   123             if (template.arrayField[i] == null) {
       
   124                 arrayContent[i] = JavaConstant.NULL_POINTER;
       
   125             } else if (template.arrayField[i] == template) {
       
   126                 arrayContent[i] = ret;
       
   127             } else if (template.arrayField[i] instanceof Integer) {
       
   128                 int value = (Integer) template.arrayField[i];
       
   129                 VirtualObject boxed = VirtualObject.get(integerType, vobjs.size());
       
   130                 vobjs.add(boxed);
       
   131                 arrayContent[i] = boxed;
       
   132                 boxed.setValues(new JavaValue[]{JavaConstant.forInt(value)}, new JavaKind[]{JavaKind.Int});
       
   133             } else if (template.arrayField[i] instanceof String) {
       
   134                 String value = (String) template.arrayField[i];
       
   135                 Register reg = asm.emitLoadPointer((HotSpotConstant) constantReflection.forString(value));
       
   136                 arrayContent[i] = reg.asValue(target.getLIRKind(JavaKind.Object));
       
   137             } else {
       
   138                 Assert.fail("unexpected value");
       
   139             }
       
   140         }
       
   141         array.setValues(arrayContent, arrayKind);
       
   142 
       
   143         // build return object
       
   144         ResolvedJavaField[] fields = retType.getInstanceFields(true);
       
   145         JavaValue[] retContent = new JavaValue[fields.length];
       
   146         JavaKind[] retKind = new JavaKind[fields.length];
       
   147         for (int i = 0; i < fields.length; i++) {
       
   148             retKind[i] = fields[i].getJavaKind();
       
   149             switch (retKind[i]) {
       
   150                 case Long: // template.longField
       
   151                     retContent[i] = JavaConstant.forLong(template.longField);
       
   152                     break;
       
   153                 case Int: // template.intField
       
   154                     Register intReg = asm.emitLoadInt(template.intField);
       
   155                     retContent[i] = asm.emitIntToStack(intReg);
       
   156                     break;
       
   157                 case Float: // template.floatField
       
   158                     Register fReg = asm.emitLoadFloat(template.floatField);
       
   159                     retContent[i] = fReg.asValue(target.getLIRKind(JavaKind.Float));
       
   160                     break;
       
   161                 case Object: // template.arrayField
       
   162                     retContent[i] = array;
       
   163                     break;
       
   164                 default:
       
   165                     Assert.fail("unexpected field");
       
   166             }
       
   167         }
       
   168         ret.setValues(retContent, retKind);
       
   169 
       
   170         return vobjs.toArray(new VirtualObject[0]);
       
   171     }
       
   172 
       
   173     @Test
       
   174     public void testBuildObject() {
       
   175         test(this::compileBuildObject, getMethod("buildObject"), 7, JavaKind.Object);
       
   176     }
       
   177 }