1/*
2 * Copyright (c) 2015, 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.core.test;
24
25import org.graalvm.compiler.code.CompilationResult;
26import org.graalvm.compiler.nodes.StructuredGraph;
27import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
28
29import jdk.vm.ci.code.Architecture;
30import jdk.vm.ci.code.InstalledCode;
31import jdk.vm.ci.meta.Assumptions.Assumption;
32import jdk.vm.ci.meta.ResolvedJavaMethod;
33import jdk.vm.ci.meta.ResolvedJavaType;
34
35public abstract class GraalCompilerAssumptionsTest extends GraalCompilerTest {
36
37    public GraalCompilerAssumptionsTest() {
38        super();
39    }
40
41    public GraalCompilerAssumptionsTest(Class<? extends Architecture> arch) {
42        super(arch);
43    }
44
45    protected void testAssumptionInvalidate(String methodName, Assumption expected, String classToLoad) {
46        testAssumption(methodName, expected, classToLoad, true);
47    }
48
49    /**
50     * Checks the behavior of class loading on {@link Assumption invalidation}. {@code methodName}
51     * is compiled and the resulting graph is checked for {@code expectedAssumption}. The code is
52     * installed and optionally {@code classToLoad} is loaded. The class is assumed to be an inner
53     * class of the test class and the name of the class to load is constructed relative to that.
54     *
55     * @param methodName the method to compile
56     * @param expectedAssumption expected {@link Assumption} instance to find in graph
57     * @param classToLoad an optional class to load to trigger an invalidation check
58     * @param willInvalidate true if loading {@code classToLoad} should invalidate the method
59     */
60    protected void testAssumption(String methodName, Assumption expectedAssumption, String classToLoad, boolean willInvalidate) {
61        ResolvedJavaMethod javaMethod = getResolvedJavaMethod(methodName);
62
63        StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.YES);
64        assertTrue(!graph.getAssumptions().isEmpty());
65        checkGraph(expectedAssumption, graph);
66
67        CompilationResult compilationResult = compile(javaMethod, graph);
68        final InstalledCode installedCode = getBackend().createDefaultInstalledCode(javaMethod, compilationResult);
69        assertTrue(installedCode.isValid());
70        if (classToLoad != null) {
71            String fullName = getClass().getName() + "$" + classToLoad;
72            try {
73                Class.forName(fullName);
74            } catch (ClassNotFoundException e) {
75                fail("Can't find class %s", fullName);
76            }
77            assertTrue(!willInvalidate == installedCode.isValid(), "method should be %s", willInvalidate ? "invalid" : "valid");
78        }
79    }
80
81    protected void checkGraph(Assumption expectedAssumption, StructuredGraph graph) {
82        boolean found = false;
83        for (Assumption a : graph.getAssumptions()) {
84            if (expectedAssumption.equals(a)) {
85                found = true;
86            }
87        }
88        assertTrue(found, "Can't find assumption %s", expectedAssumption);
89    }
90
91    /**
92     * Converts a {@link Class} to an initialized {@link ResolvedJavaType}.
93     */
94    protected ResolvedJavaType resolveAndInitialize(Class<?> clazz) {
95        ResolvedJavaType type = getMetaAccess().lookupJavaType(clazz);
96        type.initialize();
97        return type;
98    }
99}
100