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 package jdk.vm.ci.code.test;
  24 
  25 import jdk.vm.ci.amd64.AMD64;
  26 import jdk.vm.ci.code.Architecture;
  27 import jdk.vm.ci.code.CodeCacheProvider;
  28 import jdk.vm.ci.code.InstalledCode;
  29 import jdk.vm.ci.code.TargetDescription;
  30 import jdk.vm.ci.code.test.amd64.AMD64TestAssembler;
  31 import jdk.vm.ci.code.test.sparc.SPARCTestAssembler;
  32 import jdk.vm.ci.hotspot.HotSpotCompiledCode;
  33 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  34 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  35 import jdk.vm.ci.meta.ConstantReflectionProvider;
  36 import jdk.vm.ci.meta.MetaAccessProvider;
  37 import jdk.vm.ci.runtime.JVMCI;
  38 import jdk.vm.ci.runtime.JVMCIBackend;
  39 import jdk.vm.ci.sparc.SPARC;
  40 import org.junit.Assert;
  41 
  42 import java.lang.reflect.Method;
  43 
  44 /**
  45  * Base class for code installation tests.
  46  */
  47 public class CodeInstallationTest {
  48 
  49     protected final MetaAccessProvider metaAccess;
  50     protected final CodeCacheProvider codeCache;
  51     protected final TargetDescription target;
  52     protected final ConstantReflectionProvider constantReflection;
  53     protected final TestHotSpotVMConfig config;
  54 
  55     public CodeInstallationTest() {
  56         JVMCIBackend backend = JVMCI.getRuntime().getHostJVMCIBackend();
  57         metaAccess = backend.getMetaAccess();
  58         codeCache = backend.getCodeCache();
  59         target = backend.getTarget();
  60         constantReflection = backend.getConstantReflection();
  61         config = new TestHotSpotVMConfig(HotSpotJVMCIRuntime.runtime().getConfigStore());
  62     }
  63 
  64     protected interface TestCompiler {
  65 
  66         void compile(TestAssembler asm);
  67     }
  68 
  69     private TestAssembler createAssembler() {
  70         Architecture arch = codeCache.getTarget().arch;
  71         if (arch instanceof AMD64) {
  72             return new AMD64TestAssembler(codeCache, config);
  73         } else if (arch instanceof SPARC) {
  74             return new SPARCTestAssembler(codeCache, config);
  75         } else {
  76             Assert.fail("unsupported architecture");
  77             return null;
  78         }
  79     }
  80 
  81     protected Method getMethod(String name, Class<?>... args) {
  82         try {
  83             return getClass().getMethod(name, args);
  84         } catch (NoSuchMethodException e) {
  85             Assert.fail("method not found");
  86             return null;
  87         }
  88     }
  89 
  90     protected void test(TestCompiler compiler, Method method, Object... args) {
  91         try {
  92             HotSpotResolvedJavaMethod resolvedMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(method);
  93             TestAssembler asm = createAssembler();
  94 
  95             asm.emitPrologue();
  96             compiler.compile(asm);
  97             asm.emitEpilogue();
  98 
  99             HotSpotCompiledCode code = asm.finish(resolvedMethod);
 100             InstalledCode installed = codeCache.addCode(resolvedMethod, code, null, null);
 101 
 102             Object expected = method.invoke(null, args);
 103             Object actual = installed.executeVarargs(args);
 104             Assert.assertEquals(expected, actual);
 105         } catch (Exception e) {
 106             e.printStackTrace();
 107             Assert.fail(e.toString());
 108         }
 109     }
 110 }