TypeCheckTest.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2011, 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.replacements.test;
24
25import org.graalvm.compiler.core.common.CompilationIdentifier;
26import org.graalvm.compiler.core.test.GraalCompilerTest;
27import org.graalvm.compiler.nodes.StructuredGraph;
28import org.graalvm.compiler.options.OptionValues;
29
30import jdk.vm.ci.code.InstalledCode;
31import jdk.vm.ci.meta.JavaTypeProfile;
32import jdk.vm.ci.meta.JavaTypeProfile.ProfiledType;
33import jdk.vm.ci.meta.ResolvedJavaMethod;
34import jdk.vm.ci.meta.TriState;
35
36/**
37 * Base class for instanceof test classes.
38 */
39public abstract class TypeCheckTest extends GraalCompilerTest {
40
41    protected abstract void replaceProfile(StructuredGraph graph, JavaTypeProfile profile);
42
43    protected JavaTypeProfile currentProfile;
44
45    @Override
46    protected StructuredGraph parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options) {
47        StructuredGraph graph = super.parseForCompile(method, compilationId, options);
48        if (currentProfile != null) {
49            replaceProfile(graph, currentProfile);
50        }
51        return graph;
52    }
53
54    @Override
55    protected InstalledCode getCode(final ResolvedJavaMethod method, final StructuredGraph graph, boolean ignore, boolean installAsDefault, OptionValues options) {
56        return super.getCode(method, graph, currentProfile != null, installAsDefault, options);
57    }
58
59    protected JavaTypeProfile profile(Class<?>... types) {
60        return profile(TriState.FALSE, types);
61    }
62
63    protected JavaTypeProfile profile(TriState nullSeen, Class<?>... types) {
64        if (types.length == 0) {
65            return null;
66        }
67        ProfiledType[] ptypes = new ProfiledType[types.length];
68        for (int i = 0; i < types.length; i++) {
69            ptypes[i] = new ProfiledType(getMetaAccess().lookupJavaType(types[i]), 1.0D / types.length);
70        }
71        return new JavaTypeProfile(nullSeen, 0.0D, ptypes);
72    }
73
74    protected void test(String name, JavaTypeProfile profile, Object... args) {
75        assert currentProfile == null;
76        currentProfile = profile;
77        try {
78            super.test(name, args);
79        } finally {
80            currentProfile = null;
81        }
82    }
83}
84