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