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