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