001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.asm.test;
024
025import java.lang.reflect.*;
026
027import jdk.internal.jvmci.code.*;
028import com.oracle.graal.debug.*;
029import com.oracle.graal.debug.Debug.*;
030import jdk.internal.jvmci.meta.*;
031import jdk.internal.jvmci.runtime.*;
032import jdk.internal.jvmci.service.*;
033
034import org.junit.*;
035
036import com.oracle.graal.code.*;
037import com.oracle.graal.test.*;
038
039public abstract class AssemblerTest extends GraalTest {
040
041    private final MetaAccessProvider metaAccess;
042    protected final CodeCacheProvider codeCache;
043
044    public interface CodeGenTest {
045        byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc);
046    }
047
048    public AssemblerTest() {
049        JVMCIBackend providers = JVMCI.getRuntime().getHostJVMCIBackend();
050        this.metaAccess = providers.getMetaAccess();
051        this.codeCache = providers.getCodeCache();
052    }
053
054    public MetaAccessProvider getMetaAccess() {
055        return metaAccess;
056    }
057
058    protected InstalledCode assembleMethod(Method m, CodeGenTest test) {
059        ResolvedJavaMethod method = getMetaAccess().lookupJavaMethod(m);
060        try (Scope s = Debug.scope("assembleMethod", method, codeCache)) {
061            RegisterConfig registerConfig = codeCache.getRegisterConfig();
062            CallingConvention cc = CodeUtil.getCallingConvention(codeCache, CallingConvention.Type.JavaCallee, method, false);
063
064            CompilationResult compResult = new CompilationResult();
065            byte[] targetCode = test.generateCode(compResult, codeCache.getTarget(), registerConfig, cc);
066            compResult.setTargetCode(targetCode, targetCode.length);
067            compResult.setTotalFrameSize(0);
068
069            InstalledCode code = codeCache.addMethod(method, compResult, null, null);
070
071            for (DisassemblerProvider dis : Services.load(DisassemblerProvider.class)) {
072                String disasm1 = dis.disassembleCompiledCode(codeCache, compResult);
073                Assert.assertTrue(compResult.toString(), disasm1 == null || disasm1.length() > 0);
074                String disasm2 = dis.disassembleInstalledCode(codeCache, compResult, code);
075                Assert.assertTrue(code.toString(), disasm2 == null || disasm2.length() > 0);
076            }
077            return code;
078        } catch (Throwable e) {
079            throw Debug.handle(e);
080        }
081    }
082
083    protected Object runTest(String methodName, CodeGenTest test, Object... args) {
084        Method method = getMethod(methodName);
085        InstalledCode code = assembleMethod(method, test);
086        try {
087            return code.executeVarargs(args);
088        } catch (InvalidInstalledCodeException e) {
089            throw new RuntimeException(e);
090        }
091    }
092
093    protected void assertReturn(String methodName, CodeGenTest test, Object expected, Object... args) {
094        Object actual = runTest(methodName, test, args);
095        Assert.assertEquals("unexpected return value", expected, actual);
096    }
097}