1 /* 2 * Copyright (c) 2013, 2018, 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 package org.graalvm.compiler.asm.test; 26 27 import static org.graalvm.compiler.core.common.CompilationRequestIdentifier.asCompilationRequest; 28 29 import java.lang.reflect.Method; 30 31 import org.graalvm.compiler.api.test.Graal; 32 import org.graalvm.compiler.code.CompilationResult; 33 import org.graalvm.compiler.code.DisassemblerProvider; 34 import org.graalvm.compiler.core.common.CompilationIdentifier; 35 import org.graalvm.compiler.core.target.Backend; 36 import org.graalvm.compiler.debug.DebugContext; 37 import org.graalvm.compiler.nodes.StructuredGraph; 38 import org.graalvm.compiler.options.OptionValues; 39 import org.graalvm.compiler.runtime.RuntimeProvider; 40 import org.graalvm.compiler.serviceprovider.GraalServices; 41 import org.graalvm.compiler.test.GraalTest; 42 import org.junit.Assert; 43 44 import jdk.vm.ci.code.CallingConvention; 45 import jdk.vm.ci.code.CodeCacheProvider; 46 import jdk.vm.ci.code.InstalledCode; 47 import jdk.vm.ci.code.InvalidInstalledCodeException; 48 import jdk.vm.ci.code.RegisterConfig; 49 import jdk.vm.ci.code.TargetDescription; 50 import jdk.vm.ci.meta.MetaAccessProvider; 51 import jdk.vm.ci.meta.ResolvedJavaMethod; 52 import jdk.vm.ci.runtime.JVMCI; 53 import jdk.vm.ci.runtime.JVMCIBackend; 54 55 public abstract class AssemblerTest extends GraalTest { 56 57 private final MetaAccessProvider metaAccess; 58 protected final CodeCacheProvider codeCache; 59 private final Backend backend; 60 61 public interface CodeGenTest { 62 byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc); 63 } 64 65 /** 66 * Gets the initial option values provided by the Graal runtime. These are option values 67 * typically parsed from the command line. 68 */ 69 public static OptionValues getInitialOptions() { 70 return Graal.getRequiredCapability(OptionValues.class); 71 } 72 73 public AssemblerTest() { 74 JVMCIBackend providers = JVMCI.getRuntime().getHostJVMCIBackend(); 75 this.metaAccess = providers.getMetaAccess(); 76 this.codeCache = providers.getCodeCache(); 77 this.backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend(); 78 } 79 80 public MetaAccessProvider getMetaAccess() { 81 return metaAccess; 82 } 83 84 @SuppressWarnings("try") 85 protected InstalledCode assembleMethod(Method m, CodeGenTest test) { 86 ResolvedJavaMethod method = getMetaAccess().lookupJavaMethod(m); 87 OptionValues options = getInitialOptions(); 88 DebugContext debug = getDebugContext(options); 89 try (DebugContext.Scope s = debug.scope("assembleMethod", method, codeCache)) { 90 RegisterConfig registerConfig = codeCache.getRegisterConfig(); 91 CompilationIdentifier compilationId = backend.getCompilationIdentifier(method); 92 StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).compilationId(compilationId).build(); 93 CallingConvention cc = backend.newLIRGenerationResult(compilationId, null, null, graph, null).getCallingConvention(); 94 95 CompilationResult compResult = new CompilationResult(graph.compilationId()); 96 byte[] targetCode = test.generateCode(compResult, codeCache.getTarget(), registerConfig, cc); 97 compResult.setTargetCode(targetCode, targetCode.length); 98 compResult.setTotalFrameSize(0); 99 compResult.close(); 100 101 InstalledCode code = backend.addInstalledCode(debug, method, asCompilationRequest(compilationId), compResult); 102 103 for (DisassemblerProvider dis : GraalServices.load(DisassemblerProvider.class)) { 104 String disasm1 = dis.disassembleCompiledCode(codeCache, compResult); 105 Assert.assertTrue(compResult.toString(), disasm1 == null || disasm1.length() > 0); 106 String disasm2 = dis.disassembleInstalledCode(codeCache, compResult, code); 107 Assert.assertTrue(code.toString(), disasm2 == null || disasm2.length() > 0); 108 } 109 return code; 110 } catch (Throwable e) { 111 throw debug.handle(e); 112 } 113 } 114 115 protected Object runTest(String methodName, CodeGenTest test, Object... args) { 116 Method method = getMethod(methodName); 117 InstalledCode code = assembleMethod(method, test); 118 try { 119 return code.executeVarargs(args); 120 } catch (InvalidInstalledCodeException e) { 121 throw new RuntimeException(e); 122 } 123 } 124 125 protected void assertReturn(String methodName, CodeGenTest test, Object expected, Object... args) { 126 Object actual = runTest(methodName, test, args); 127 Assert.assertEquals("unexpected return value", expected, actual); 128 } 129 }