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