001/*
002 * Copyright (c) 2012, 2014, 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.hotspot.test;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.hotspot.*;
027import jdk.internal.jvmci.meta.*;
028import static java.lang.reflect.Modifier.*;
029
030import org.junit.*;
031
032import com.oracle.graal.compiler.test.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
035
036public class InstalledCodeExecuteHelperTest extends GraalCompilerTest {
037
038    private static final int ITERATIONS = 100000;
039    Object[] argsToBind;
040
041    @Test
042    public void test1() throws InvalidInstalledCodeException {
043        final ResolvedJavaMethod fooMethod = getResolvedJavaMethod("foo");
044        final HotSpotInstalledCode fooCode = (HotSpotInstalledCode) getCode(fooMethod);
045
046        argsToBind = new Object[]{fooCode};
047
048        final ResolvedJavaMethod benchmarkMethod = getResolvedJavaMethod("benchmark");
049        final HotSpotInstalledCode installedBenchmarkCode = (HotSpotInstalledCode) getCode(benchmarkMethod);
050
051        Assert.assertEquals(Integer.valueOf(42), benchmark(fooCode));
052
053        Assert.assertEquals(Integer.valueOf(42), installedBenchmarkCode.executeVarargs(argsToBind[0]));
054
055    }
056
057    public static Integer benchmark(HotSpotInstalledCode code) throws InvalidInstalledCodeException {
058        int val = 0;
059        for (int j = 0; j < 100; j++) {
060            for (int i = 0; i < ITERATIONS; i++) {
061                val = (Integer) code.executeVarargs();
062            }
063        }
064        return val;
065    }
066
067    public static Integer foo() {
068        return 42;
069    }
070
071    @Override
072    protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions) {
073        StructuredGraph graph = super.parseEager(m, allowAssumptions);
074        if (argsToBind != null) {
075            Object receiver = isStatic(m.getModifiers()) ? null : this;
076            Object[] args = argsWithReceiver(receiver, argsToBind);
077            JavaType[] parameterTypes = m.toParameterTypes();
078            assert parameterTypes.length == args.length;
079            for (int i = 0; i < argsToBind.length; i++) {
080                ParameterNode param = graph.getParameter(i);
081                JavaConstant c = HotSpotObjectConstantImpl.forBoxedValue(parameterTypes[i].getKind(), argsToBind[i]);
082                ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph);
083                param.replaceAtUsages(replacement);
084            }
085        }
086        return graph;
087    }
088}