1 /*
   2  * Copyright (c) 2014, 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 
  26 package org.graalvm.compiler.hotspot.test;
  27 
  28 import org.graalvm.compiler.core.common.CompressEncoding;
  29 import org.graalvm.compiler.hotspot.nodes.HotSpotCompressionNode;
  30 import org.graalvm.compiler.nodes.ValueNode;
  31 import org.graalvm.compiler.nodes.debug.OpaqueNode;
  32 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  36 import org.junit.Assume;
  37 import org.junit.Test;
  38 
  39 import jdk.vm.ci.meta.JavaKind;
  40 import jdk.vm.ci.meta.ResolvedJavaMethod;
  41 
  42 public class DataPatchTest extends HotSpotGraalCompilerTest {
  43 
  44     public static double doubleSnippet() {
  45         return 84.72;
  46     }
  47 
  48     @Test
  49     public void doubleTest() {
  50         test("doubleSnippet");
  51     }
  52 
  53     public static Object oopSnippet() {
  54         return "asdf";
  55     }
  56 
  57     @Test
  58     public void oopTest() {
  59         test("oopSnippet");
  60     }
  61 
  62     private static Object compressUncompress(Object obj) {
  63         return obj;
  64     }
  65 
  66     public static Object narrowOopSnippet() {
  67         return compressUncompress("narrowAsdf");
  68     }
  69 
  70     @Test
  71     public void narrowOopTest() {
  72         Assume.assumeTrue("skipping narrow oop data patch test", runtime().getVMConfig().useCompressedOops);
  73         test("narrowOopSnippet");
  74     }
  75 
  76     @Override
  77     protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
  78         Registration r = new Registration(invocationPlugins, DataPatchTest.class);
  79         r.register1("compressUncompress", Object.class, new InvocationPlugin() {
  80             @Override
  81             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
  82                 CompressEncoding encoding = runtime().getVMConfig().getOopEncoding();
  83                 ValueNode compressed = b.add(HotSpotCompressionNode.compress(arg, encoding));
  84                 ValueNode proxy = b.add(new OpaqueNode(compressed));
  85                 b.addPush(JavaKind.Object, HotSpotCompressionNode.uncompress(proxy, encoding));
  86                 return true;
  87             }
  88         });
  89         super.registerInvocationPlugins(invocationPlugins);
  90     }
  91 }