VirtualObjectDebugInfoTest.java revision 11707:ad7af1afda7a
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.simpleArch == "x64" | vm.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 * @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
39package jdk.vm.ci.code.test;
40
41import jdk.vm.ci.code.Register;
42import jdk.vm.ci.code.VirtualObject;
43import jdk.vm.ci.hotspot.HotSpotConstant;
44import jdk.vm.ci.meta.JavaConstant;
45import jdk.vm.ci.meta.JavaKind;
46import jdk.vm.ci.meta.JavaValue;
47import jdk.vm.ci.meta.ResolvedJavaField;
48import jdk.vm.ci.meta.ResolvedJavaType;
49import org.junit.Assert;
50import org.junit.Test;
51
52import java.util.ArrayList;
53import java.util.Objects;
54
55public 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    private VirtualObject[] compileBuildObject(TestAssembler asm, JavaValue[] values) {
107        TestClass template = new TestClass();
108        ArrayList<VirtualObject> vobjs = new ArrayList<>();
109
110        ResolvedJavaType retType = metaAccess.lookupJavaType(TestClass.class);
111        VirtualObject ret = VirtualObject.get(retType, vobjs.size());
112        vobjs.add(ret);
113        values[0] = ret;
114
115        ResolvedJavaType arrayType = metaAccess.lookupJavaType(Object[].class);
116        VirtualObject array = VirtualObject.get(arrayType, vobjs.size());
117        vobjs.add(array);
118
119        // build array for ret.arrayField
120        ResolvedJavaType integerType = metaAccess.lookupJavaType(Integer.class);
121        JavaValue[] arrayContent = new JavaValue[template.arrayField.length];
122        JavaKind[] arrayKind = new JavaKind[template.arrayField.length];
123        for (int i = 0; i < arrayContent.length; i++) {
124            arrayKind[i] = JavaKind.Object;
125            if (template.arrayField[i] == null) {
126                arrayContent[i] = JavaConstant.NULL_POINTER;
127            } else if (template.arrayField[i] == template) {
128                arrayContent[i] = ret;
129            } else if (template.arrayField[i] instanceof Integer) {
130                int value = (Integer) template.arrayField[i];
131                VirtualObject boxed = VirtualObject.get(integerType, vobjs.size());
132                vobjs.add(boxed);
133                arrayContent[i] = boxed;
134                boxed.setValues(new JavaValue[]{JavaConstant.forInt(value)}, new JavaKind[]{JavaKind.Int});
135            } else if (template.arrayField[i] instanceof String) {
136                String value = (String) template.arrayField[i];
137                Register reg = asm.emitLoadPointer((HotSpotConstant) constantReflection.forString(value));
138                arrayContent[i] = reg.asValue(asm.getValueKind(JavaKind.Object));
139            } else {
140                Assert.fail("unexpected value");
141            }
142        }
143        array.setValues(arrayContent, arrayKind);
144
145        // build return object
146        ResolvedJavaField[] fields = retType.getInstanceFields(true);
147        JavaValue[] retContent = new JavaValue[fields.length];
148        JavaKind[] retKind = new JavaKind[fields.length];
149        for (int i = 0; i < fields.length; i++) {
150            retKind[i] = fields[i].getJavaKind();
151            switch (retKind[i]) {
152                case Long: // template.longField
153                    retContent[i] = JavaConstant.forLong(template.longField);
154                    break;
155                case Int: // template.intField
156                    Register intReg = asm.emitLoadInt(template.intField);
157                    retContent[i] = asm.emitIntToStack(intReg);
158                    break;
159                case Float: // template.floatField
160                    Register fReg = asm.emitLoadFloat(template.floatField);
161                    retContent[i] = fReg.asValue(asm.getValueKind(JavaKind.Float));
162                    break;
163                case Object: // template.arrayField
164                    retContent[i] = array;
165                    break;
166                default:
167                    Assert.fail("unexpected field");
168            }
169        }
170        ret.setValues(retContent, retKind);
171
172        return vobjs.toArray(new VirtualObject[0]);
173    }
174
175    @Test
176    public void testBuildObject() {
177        test(this::compileBuildObject, getMethod("buildObject"), 7, JavaKind.Object);
178    }
179}
180