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.DataPatchTest
  37  */
  38 
  39 package jdk.vm.ci.code.test;
  40 
  41 import org.junit.Assume;
  42 import org.junit.Test;
  43 
  44 import jdk.vm.ci.code.Register;
  45 import jdk.vm.ci.code.site.DataSectionReference;
  46 import jdk.vm.ci.hotspot.HotSpotConstant;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 
  49 /**
  50  * Test code installation with data patches.
  51  */
  52 public class DataPatchTest extends CodeInstallationTest {
  53 
  54     public static Class<?> getConstClass() {
  55         return DataPatchTest.class;
  56     }
  57 
  58     private void test(TestCompiler compiler) {
  59         test(compiler, getMethod("getConstClass"));
  60     }
  61 
  62     @Test
  63     public void testInlineObject() {
  64         test(asm -> {
  65             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  66             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  67             Register ret = asm.emitLoadPointer(c);
  68             asm.emitPointerRet(ret);
  69         });
  70     }
  71 
  72     @Test
  73     public void testInlineNarrowObject() {
  74         Assume.assumeTrue(config.useCompressedOops);
  75         test(asm -> {
  76             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  77             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  78             Register compressed = asm.emitLoadPointer((HotSpotConstant) c.compress());
  79             Register ret = asm.emitUncompressPointer(compressed, config.narrowOopBase, config.narrowOopShift);
  80             asm.emitPointerRet(ret);
  81         });
  82     }
  83 
  84     @Test
  85     public void testDataSectionReference() {
  86         test(asm -> {
  87             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  88             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  89             DataSectionReference ref = asm.emitDataItem(c);
  90             Register ret = asm.emitLoadPointer(ref);
  91             asm.emitPointerRet(ret);
  92         });
  93     }
  94 
  95     @Test
  96     public void testNarrowDataSectionReference() {
  97         Assume.assumeTrue(config.useCompressedOops);
  98         test(asm -> {
  99             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 100             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
 101             HotSpotConstant cCompressed = (HotSpotConstant) c.compress();
 102             DataSectionReference ref = asm.emitDataItem(cCompressed);
 103             Register compressed = asm.emitLoadNarrowPointer(ref);
 104             Register ret = asm.emitUncompressPointer(compressed, config.narrowOopBase, config.narrowOopShift);
 105             asm.emitPointerRet(ret);
 106         });
 107     }
 108 
 109     @Test
 110     public void testInlineMetadata() {
 111         test(asm -> {
 112             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 113             Register klass = asm.emitLoadPointer((HotSpotConstant) constantReflection.asObjectHub(type));
 114             Register ret = asm.emitLoadPointer(klass, config.classMirrorOffset);
 115             asm.emitPointerRet(ret);
 116         });
 117     }
 118 
 119     @Test
 120     public void testInlineNarrowMetadata() {
 121         Assume.assumeTrue(config.useCompressedClassPointers);
 122         test(asm -> {
 123             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 124             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 125             Register narrowKlass = asm.emitLoadPointer((HotSpotConstant) hub.compress());
 126             Register klass = asm.emitUncompressPointer(narrowKlass, config.narrowKlassBase, config.narrowKlassShift);
 127             Register ret = asm.emitLoadPointer(klass, config.classMirrorOffset);
 128             asm.emitPointerRet(ret);
 129         });
 130     }
 131 
 132     @Test
 133     public void testMetadataInDataSection() {
 134         test(asm -> {
 135             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 136             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 137             DataSectionReference ref = asm.emitDataItem(hub);
 138             Register klass = asm.emitLoadPointer(ref);
 139             Register ret = asm.emitLoadPointer(klass, config.classMirrorOffset);
 140             asm.emitPointerRet(ret);
 141         });
 142     }
 143 
 144     @Test
 145     public void testNarrowMetadataInDataSection() {
 146         Assume.assumeTrue(config.useCompressedClassPointers);
 147         test(asm -> {
 148             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 149             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 150             HotSpotConstant narrowHub = (HotSpotConstant) hub.compress();
 151             DataSectionReference ref = asm.emitDataItem(narrowHub);
 152             Register narrowKlass = asm.emitLoadNarrowPointer(ref);
 153             Register klass = asm.emitUncompressPointer(narrowKlass, config.narrowKlassBase, config.narrowKlassShift);
 154             Register ret = asm.emitLoadPointer(klass, config.classMirrorOffset);
 155             asm.emitPointerRet(ret);
 156         });
 157     }
 158 }