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  * @compile CodeInstallationTest.java TestAssembler.java amd64/AMD64TestAssembler.java sparc/SPARCTestAssembler.java
  28  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI compiler.jvmci.code.DataPatchTest
  29  */
  30 
  31 package compiler.jvmci.code;
  32 
  33 import jdk.vm.ci.code.Register;
  34 import jdk.vm.ci.code.site.DataSectionReference;
  35 import jdk.vm.ci.hotspot.HotSpotConstant;
  36 import jdk.vm.ci.hotspot.HotSpotMetaAccessProvider;
  37 import jdk.vm.ci.hotspot.HotSpotSymbol;
  38 import jdk.vm.ci.hotspot.HotSpotVMConfig;
  39 import jdk.vm.ci.meta.ResolvedJavaType;
  40 
  41 import org.junit.Assume;
  42 import org.junit.Test;
  43 
  44 /**
  45  * Test code installation with data patches.
  46  */
  47 public class DataPatchTest extends CodeInstallationTest {
  48 
  49     public static Class<?> getConstClass() {
  50         return DataPatchTest.class;
  51     }
  52 
  53     private void test(TestCompiler compiler) {
  54         test(compiler, getMethod("getConstClass"));
  55     }
  56 
  57     @Test
  58     public void testInlineObject() {
  59         test(asm -> {
  60             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  61             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  62             Register ret = asm.emitLoadPointer(c);
  63             asm.emitPointerRet(ret);
  64         });
  65     }
  66 
  67     @Test
  68     public void testInlineNarrowObject() {
  69         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedOops);
  70         test(asm -> {
  71             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  72             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  73             Register compressed = asm.emitLoadPointer((HotSpotConstant) c.compress());
  74             Register ret = asm.emitUncompressPointer(compressed, HotSpotVMConfig.config().narrowOopBase, HotSpotVMConfig.config().narrowOopShift);
  75             asm.emitPointerRet(ret);
  76         });
  77     }
  78 
  79     @Test
  80     public void testDataSectionReference() {
  81         test(asm -> {
  82             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  83             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  84             DataSectionReference ref = asm.emitDataItem(c);
  85             Register ret = asm.emitLoadPointer(ref);
  86             asm.emitPointerRet(ret);
  87         });
  88     }
  89 
  90     @Test
  91     public void testNarrowDataSectionReference() {
  92         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedOops);
  93         test(asm -> {
  94             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
  95             HotSpotConstant c = (HotSpotConstant) constantReflection.asJavaClass(type);
  96             HotSpotConstant cCompressed = (HotSpotConstant) c.compress();
  97             DataSectionReference ref = asm.emitDataItem(cCompressed);
  98             Register compressed = asm.emitLoadNarrowPointer(ref);
  99             Register ret = asm.emitUncompressPointer(compressed, HotSpotVMConfig.config().narrowOopBase, HotSpotVMConfig.config().narrowOopShift);
 100             asm.emitPointerRet(ret);
 101         });
 102     }
 103 
 104     @Test
 105     public void testInlineMetadata() {
 106         test(asm -> {
 107             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 108             Register klass = asm.emitLoadPointer((HotSpotConstant) constantReflection.asObjectHub(type));
 109             Register ret = asm.emitLoadPointer(klass, HotSpotVMConfig.config().classMirrorOffset);
 110             asm.emitPointerRet(ret);
 111         });
 112     }
 113 
 114     @Test
 115     public void testInlineNarrowMetadata() {
 116         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedClassPointers);
 117         test(asm -> {
 118             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 119             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 120             Register narrowKlass = asm.emitLoadPointer((HotSpotConstant) hub.compress());
 121             Register klass = asm.emitUncompressPointer(narrowKlass, HotSpotVMConfig.config().narrowKlassBase, HotSpotVMConfig.config().narrowKlassShift);
 122             Register ret = asm.emitLoadPointer(klass, HotSpotVMConfig.config().classMirrorOffset);
 123             asm.emitPointerRet(ret);
 124         });
 125     }
 126 
 127     @Test
 128     public void testMetadataInDataSection() {
 129         test(asm -> {
 130             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 131             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 132             DataSectionReference ref = asm.emitDataItem(hub);
 133             Register klass = asm.emitLoadPointer(ref);
 134             Register ret = asm.emitLoadPointer(klass, HotSpotVMConfig.config().classMirrorOffset);
 135             asm.emitPointerRet(ret);
 136         });
 137     }
 138 
 139     @Test
 140     public void testNarrowMetadataInDataSection() {
 141         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedClassPointers);
 142         test(asm -> {
 143             ResolvedJavaType type = metaAccess.lookupJavaType(getConstClass());
 144             HotSpotConstant hub = (HotSpotConstant) constantReflection.asObjectHub(type);
 145             HotSpotConstant narrowHub = (HotSpotConstant) hub.compress();
 146             DataSectionReference ref = asm.emitDataItem(narrowHub);
 147             Register narrowKlass = asm.emitLoadNarrowPointer(ref);
 148             Register klass = asm.emitUncompressPointer(narrowKlass, HotSpotVMConfig.config().narrowKlassBase, HotSpotVMConfig.config().narrowKlassShift);
 149             Register ret = asm.emitLoadPointer(klass, HotSpotVMConfig.config().classMirrorOffset);
 150             asm.emitPointerRet(ret);
 151         });
 152     }
 153 
 154 
 155     public static long getConstSymbol(HotSpotMetaAccessProvider meta) {
 156         HotSpotSymbol symbol = meta.lookupSymbol("java/lang/Object");
 157         return symbol.getMetaspacePointer();
 158     }
 159 
 160     private void testSymbol(TestCompiler compiler) {
 161         test(compiler, getMethod("getConstSymbol", HotSpotMetaAccessProvider.class), (HotSpotMetaAccessProvider) metaAccess);
 162     }
 163 
 164     @Test
 165     public void testInlineSymbol() {
 166         testSymbol(asm -> {
 167             HotSpotSymbol symbol = ((HotSpotMetaAccessProvider) metaAccess).lookupSymbol("java/lang/Object");
 168             Register ret = asm.emitLoadPointer((HotSpotConstant) symbol.asConstant());
 169             asm.emitPointerRet(ret);
 170         });
 171     }
 172 
 173     @Test
 174     public void testSymbolInDataSection() {
 175         testSymbol(asm -> {
 176             HotSpotSymbol symbol = ((HotSpotMetaAccessProvider) metaAccess).lookupSymbol("java/lang/Object");
 177             DataSectionReference ref = asm.emitDataItem((HotSpotConstant) symbol.asConstant());
 178             Register ret = asm.emitLoadPointer(ref);
 179             asm.emitPointerRet(ret);
 180         });
 181     }
 182 }