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