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