1 /* 2 * Copyright (c) 2015, 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 compiler.jvmci.compilerToVM; 26 27 import compiler.testlibrary.CompilerUtils; 28 import jdk.test.lib.Utils; 29 import sun.hotspot.WhiteBox; 30 import sun.hotspot.code.NMethod; 31 32 import java.lang.reflect.Executable; 33 import java.lang.reflect.Modifier; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.Collections; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 41 /** 42 * A test case for tests which require compiled code. 43 */ 44 public final class CompileCodeTestCase { 45 public static final Map<Class<?>, Object> RECEIVERS; 46 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 47 private static final int COMP_LEVEL; 48 static { 49 int[] levels = CompilerUtils.getAvailableCompilationLevels(); 50 if (levels.length == 0) { 51 throw new Error("TESTBUG: no compilers available"); 52 } 53 COMP_LEVEL = levels[levels.length - 1]; 54 } 55 private static final Class<?>[] CLASSES = { 56 Interface.class, 57 Dummy.class, 58 DummyEx.class}; 59 60 public final Executable executable; 61 public final int bci; 62 private final boolean isOsr; 63 64 public CompileCodeTestCase(Executable executable, int bci) { 65 this.executable = executable; 66 this.bci = bci; 67 isOsr = bci >= 0; 68 } 69 70 public NMethod compile() { 71 return compile(COMP_LEVEL); 72 } 73 74 public NMethod compile(int level) { 75 boolean enqueued = WB.enqueueMethodForCompilation(executable, 76 level, bci); 77 if (!enqueued) { 78 throw new Error(String.format( 79 "%s can't be enqueued for %scompilation on level %d", 80 executable, bci >= 0 ? "osr-" : "", level)); 81 } 82 Utils.waitForCondition(() -> WB.isMethodCompiled(executable, isOsr)); 83 return NMethod.get(executable, isOsr); 84 } 85 86 public static List<CompileCodeTestCase> generate(int bci) { 87 ArrayList<CompileCodeTestCase> result = new ArrayList<>(); 88 for (Class<?> aClass : CLASSES) { 89 for (Executable m : aClass.getDeclaredConstructors()) { 90 result.add(new CompileCodeTestCase(m, bci)); 91 } 92 Arrays.stream(aClass.getDeclaredMethods()) 93 .filter(m -> !Modifier.isAbstract(m.getModifiers())) 94 .filter(m -> !Modifier.isNative(m.getModifiers())) 95 .map(m -> new CompileCodeTestCase(m, bci)) 96 .forEach(result::add); 97 } 98 return result; 99 } 100 101 public NMethod toNMethod() { 102 return NMethod.get(executable, isOsr); 103 } 104 105 @Override 106 public String toString() { 107 return "CompileCodeTestCase{" + 108 "executable=" + executable + 109 ", bci=" + bci + 110 '}'; 111 } 112 113 public void deoptimize() { 114 WB.deoptimizeMethod(executable, isOsr); 115 } 116 117 public NMethod deoptimizeAndCompile() { 118 deoptimize(); 119 return compile(); 120 } 121 122 // classes which are used as "input" data in test cases 123 private static interface Interface { 124 Interface interfaceMethod(); 125 default Long defaultOverriddenMethod(Interface[] array) { 126 return array == null ? 0L : array.length; 127 } 128 default int defaultMethod(Object o) { 129 return o != null ? o.hashCode() : 0; 130 } 131 } 132 133 private static abstract class Dummy implements Interface { 134 protected Dummy() { 135 } 136 137 private static void staticMethod() { 138 } 139 140 Dummy instanceMethod(int i) { 141 return null; 142 } 143 144 abstract Object abstractMethod(double d); 145 146 @Override 147 public Long defaultOverriddenMethod(Interface[] array) { 148 return 0L; 149 } 150 } 151 152 public static class DummyEx extends Dummy { 153 @Override 154 public boolean equals(Object o) { 155 if (this == o) { 156 return true; 157 } 158 if (o == null || getClass() != o.getClass()) { 159 return false; 160 } 161 return true; 162 } 163 164 @Override 165 public int hashCode() { 166 return 0; 167 } 168 169 public DummyEx() { 170 } 171 172 protected Dummy instanceMethod(int i) { 173 if (i == 0) { 174 return this; 175 } 176 return null; 177 } 178 179 @Override 180 Object abstractMethod(double d) { 181 return this; 182 } 183 184 @Override 185 public Interface interfaceMethod() { 186 return null; 187 } 188 } 189 190 static { 191 Map<Class<?>, Object> map = new HashMap<>();; 192 map.put(CompileCodeTestCase.DummyEx.class, 193 new CompileCodeTestCase.DummyEx()); 194 map.put(CompileCodeTestCase.Dummy.class, 195 new CompileCodeTestCase.Dummy() { 196 @Override 197 public CompileCodeTestCase.Interface interfaceMethod() { 198 throw new AbstractMethodError(); 199 } 200 201 @Override 202 Object abstractMethod(double d) { 203 throw new AbstractMethodError(); 204 } 205 }); 206 map.put(CompileCodeTestCase.Interface.class, 207 new CompileCodeTestCase.Interface() { 208 @Override 209 public CompileCodeTestCase.Interface interfaceMethod() { 210 throw new AbstractMethodError(); 211 } 212 }); 213 RECEIVERS = Collections.unmodifiableMap(map); 214 } 215 216 }