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 compiler.jvmci.code;
  24 
  25 import java.lang.reflect.Method;
  26 
  27 import jdk.vm.ci.amd64.AMD64;
  28 import jdk.vm.ci.code.Architecture;
  29 import jdk.vm.ci.code.CodeCacheProvider;
  30 import jdk.vm.ci.code.InstalledCode;
  31 import jdk.vm.ci.code.TargetDescription;
  32 import jdk.vm.ci.hotspot.HotSpotCompiledCode;
  33 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  34 import jdk.vm.ci.meta.ConstantReflectionProvider;
  35 import jdk.vm.ci.meta.MetaAccessProvider;
  36 import jdk.vm.ci.runtime.JVMCI;
  37 import jdk.vm.ci.runtime.JVMCIBackend;
  38 import jdk.vm.ci.sparc.SPARC;
  39 
  40 import org.junit.Assert;
  41 
  42 import compiler.jvmci.code.amd64.AMD64TestAssembler;
  43 import compiler.jvmci.code.sparc.SPARCTestAssembler;
  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 
  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     }
  62 
  63     protected interface TestCompiler {
  64 
  65         void compile(TestAssembler asm);
  66     }
  67 
  68     private TestAssembler createAssembler() {
  69         Architecture arch = codeCache.getTarget().arch;
  70         if (arch instanceof AMD64) {
  71             return new AMD64TestAssembler(codeCache);
  72         } else if (arch instanceof SPARC) {
  73             return new SPARCTestAssembler(codeCache);
  74         } else {
  75             Assert.fail("unsupported architecture");
  76             return null;
  77         }
  78     }
  79 
  80     protected Method getMethod(String name, Class<?>... args) {
  81         try {
  82             return getClass().getMethod(name, args);
  83         } catch (NoSuchMethodException e) {
  84             Assert.fail("method not found");
  85             return null;
  86         }
  87     }
  88 
  89     protected void test(TestCompiler compiler, Method method, Object... args) {
  90         HotSpotResolvedJavaMethod resolvedMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(method);
  91         TestAssembler asm = createAssembler();
  92 
  93         asm.emitPrologue();
  94         compiler.compile(asm);
  95 
  96         HotSpotCompiledCode code = asm.finish(resolvedMethod);
  97         InstalledCode installed = codeCache.addCode(resolvedMethod, code, null, null);
  98 
  99         try {
 100             Object expected = method.invoke(null, args);
 101             Object actual = installed.executeVarargs(args);
 102             Assert.assertEquals(expected, actual);
 103         } catch (Exception e) {
 104             e.printStackTrace();
 105             Assert.fail(e.toString());
 106         }
 107     }
 108 }