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