AssemblerTest.java revision 12657:6ef01bd40ce2
138494Sobrien/*
2174294Sobrien * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
338494Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
438494Sobrien *
538494Sobrien * This code is free software; you can redistribute it and/or modify it
638494Sobrien * under the terms of the GNU General Public License version 2 only, as
738494Sobrien * published by the Free Software Foundation.
838494Sobrien *
938494Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1038494Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1138494Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1238494Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1338494Sobrien * accompanied this code).
1438494Sobrien *
1538494Sobrien * You should have received a copy of the GNU General Public License version
1638494Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1738494Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1838494Sobrien *
1938494Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2042629Sobrien * or visit www.oracle.com if you need additional information or have any
2138494Sobrien * questions.
2238494Sobrien */
2338494Sobrienpackage org.graalvm.compiler.asm.test;
2438494Sobrien
2538494Sobrienimport static org.graalvm.compiler.core.common.CompilationRequestIdentifier.asCompilationRequest;
2638494Sobrien
2738494Sobrienimport java.lang.reflect.Method;
2838494Sobrien
2938494Sobrienimport org.junit.Assert;
3038494Sobrien
3138494Sobrienimport org.graalvm.compiler.api.test.Graal;
3238494Sobrienimport org.graalvm.compiler.code.CompilationResult;
3338494Sobrienimport org.graalvm.compiler.code.DisassemblerProvider;
3438494Sobrienimport org.graalvm.compiler.core.common.CompilationIdentifier;
3538494Sobrienimport org.graalvm.compiler.core.target.Backend;
3638494Sobrienimport org.graalvm.compiler.debug.Debug;
3738494Sobrienimport org.graalvm.compiler.debug.Debug.Scope;
3838494Sobrienimport org.graalvm.compiler.nodes.StructuredGraph;
3938494Sobrienimport org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
40174294Sobrienimport org.graalvm.compiler.runtime.RuntimeProvider;
4138494Sobrienimport org.graalvm.compiler.serviceprovider.GraalServices;
4238494Sobrienimport org.graalvm.compiler.test.GraalTest;
4338494Sobrien
4438494Sobrienimport jdk.vm.ci.code.CallingConvention;
4538494Sobrienimport jdk.vm.ci.code.CodeCacheProvider;
4638494Sobrienimport jdk.vm.ci.code.InstalledCode;
4738494Sobrienimport jdk.vm.ci.code.InvalidInstalledCodeException;
4838494Sobrienimport jdk.vm.ci.code.RegisterConfig;
4938494Sobrienimport jdk.vm.ci.code.TargetDescription;
5038494Sobrienimport jdk.vm.ci.meta.MetaAccessProvider;
5138494Sobrienimport jdk.vm.ci.meta.ResolvedJavaMethod;
5238494Sobrienimport jdk.vm.ci.runtime.JVMCI;
5338494Sobrienimport jdk.vm.ci.runtime.JVMCIBackend;
5438494Sobrien
5538494Sobrienpublic abstract class AssemblerTest extends GraalTest {
5638494Sobrien
5738494Sobrien    private final MetaAccessProvider metaAccess;
5838494Sobrien    protected final CodeCacheProvider codeCache;
5938494Sobrien    private final Backend backend;
6038494Sobrien
6138494Sobrien    public interface CodeGenTest {
6238494Sobrien        byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc);
6338494Sobrien    }
6438494Sobrien
6538494Sobrien    public AssemblerTest() {
6638494Sobrien        JVMCIBackend providers = JVMCI.getRuntime().getHostJVMCIBackend();
6738494Sobrien        this.metaAccess = providers.getMetaAccess();
6838494Sobrien        this.codeCache = providers.getCodeCache();
6938494Sobrien        this.backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend();
7038494Sobrien    }
7138494Sobrien
7238494Sobrien    public MetaAccessProvider getMetaAccess() {
7338494Sobrien        return metaAccess;
7438494Sobrien    }
7538494Sobrien
7638494Sobrien    @SuppressWarnings("try")
7738494Sobrien    protected InstalledCode assembleMethod(Method m, CodeGenTest test) {
7838494Sobrien        ResolvedJavaMethod method = getMetaAccess().lookupJavaMethod(m);
7938494Sobrien        try (Scope s = Debug.scope("assembleMethod", method, codeCache)) {
8038494Sobrien            RegisterConfig registerConfig = codeCache.getRegisterConfig();
8138494Sobrien            CompilationIdentifier compilationId = backend.getCompilationIdentifier(method);
8238494Sobrien            CallingConvention cc = backend.newLIRGenerationResult(compilationId, null, null, new StructuredGraph(method, AllowAssumptions.NO, compilationId), null).getCallingConvention();
8338494Sobrien
8438494Sobrien            CompilationResult compResult = new CompilationResult();
8538494Sobrien            byte[] targetCode = test.generateCode(compResult, codeCache.getTarget(), registerConfig, cc);
8638494Sobrien            compResult.setTargetCode(targetCode, targetCode.length);
8738494Sobrien            compResult.setTotalFrameSize(0);
8838494Sobrien            compResult.close();
8938494Sobrien
9038494Sobrien            InstalledCode code = backend.addInstalledCode(method, asCompilationRequest(compilationId), compResult);
9138494Sobrien
9238494Sobrien            for (DisassemblerProvider dis : GraalServices.load(DisassemblerProvider.class)) {
9338494Sobrien                String disasm1 = dis.disassembleCompiledCode(codeCache, compResult);
9438494Sobrien                Assert.assertTrue(compResult.toString(), disasm1 == null || disasm1.length() > 0);
9538494Sobrien                String disasm2 = dis.disassembleInstalledCode(codeCache, compResult, code);
9638494Sobrien                Assert.assertTrue(code.toString(), disasm2 == null || disasm2.length() > 0);
9738494Sobrien            }
9838494Sobrien            return code;
9938494Sobrien        } catch (Throwable e) {
10038494Sobrien            throw Debug.handle(e);
10138494Sobrien        }
10238494Sobrien    }
10338494Sobrien
10438494Sobrien    protected Object runTest(String methodName, CodeGenTest test, Object... args) {
10538494Sobrien        Method method = getMethod(methodName);
10638494Sobrien        InstalledCode code = assembleMethod(method, test);
10738494Sobrien        try {
10838494Sobrien            return code.executeVarargs(args);
10938494Sobrien        } catch (InvalidInstalledCodeException e) {
11038494Sobrien            throw new RuntimeException(e);
11138494Sobrien        }
11238494Sobrien    }
11338494Sobrien
114174294Sobrien    protected void assertReturn(String methodName, CodeGenTest test, Object expected, Object... args) {
11538494Sobrien        Object actual = runTest(methodName, test, args);
11638494Sobrien        Assert.assertEquals("unexpected return value", expected, actual);
11738494Sobrien    }
118174294Sobrien}
11938494Sobrien