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 package jdk.test.lib.jittester.factories; 25 26 import java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.List; 29 import jdk.test.lib.jittester.IRNode; 30 import jdk.test.lib.jittester.ProductionFailedException; 31 import jdk.test.lib.jittester.Symbol; 32 import jdk.test.lib.jittester.SymbolTable; 33 import jdk.test.lib.jittester.Type; 34 import jdk.test.lib.jittester.VariableInfo; 35 import jdk.test.lib.jittester.functions.Function; 36 import jdk.test.lib.jittester.functions.FunctionInfo; 37 import jdk.test.lib.jittester.types.TypeKlass; 38 import jdk.test.lib.jittester.utils.PseudoRandom; 39 40 class FunctionFactory extends SafeFactory<Function> { 41 private final FunctionInfo functionInfo; 42 private final int operatorLimit; 43 private final long complexityLimit; 44 private final boolean exceptionSafe; 45 private final TypeKlass ownerClass; 46 47 FunctionFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass, 48 Type resultType, boolean exceptionSafe) { 49 functionInfo = new FunctionInfo(); 50 this.complexityLimit = complexityLimit; 51 this.operatorLimit = operatorLimit; 52 this.ownerClass = ownerClass; 53 this.functionInfo.type = resultType; 54 this.exceptionSafe = exceptionSafe; 55 } 56 57 @Override 58 protected Function sproduce() throws ProductionFailedException { 59 // Currently no function is exception-safe 60 if (exceptionSafe) { 61 throw new ProductionFailedException(); 62 } 63 ArrayList<Symbol> allFunctions; 64 if (functionInfo.type == null) { 65 allFunctions = new ArrayList<>(SymbolTable.getAllCombined(FunctionInfo.class)); 66 } else { 67 allFunctions = new ArrayList<>(SymbolTable.get(functionInfo.type, FunctionInfo.class)); 68 } 69 if (!allFunctions.isEmpty()) { 70 PseudoRandom.shuffle(allFunctions); 71 Collection<TypeKlass> klassHierarchy = ownerClass.getAllParents(); 72 for (Symbol function : allFunctions) { 73 FunctionInfo functionInfo = (FunctionInfo) function; 74 // Don't try to construct abstract classes. 75 if (functionInfo.isConstructor() && functionInfo.owner.isAbstract()) { 76 continue; 77 } 78 // We don't call methods from the same class which are not final, because if we 79 // do this may produce an infinite recursion. Simple example: 80 // class A 81 // { 82 // f1() { } 83 // f2() { f1(); } 84 // } 85 // 86 // class B : A 87 // { 88 // f1() { f2(); } 89 // } 90 // 91 // However the same example is obviously safe for static and final functions 92 // Also we introduce a special flag NONRECURSIVE to mark functions that 93 // are not overrided. We may also call such functions. 94 95 // If it's a local call.. or it's a call using some variable to some object of some type in our hierarchy 96 boolean inHierarchy = false; 97 if (ownerClass.equals(functionInfo.owner) || (inHierarchy = klassHierarchy.contains(functionInfo.owner))) { 98 if ((functionInfo.flags & FunctionInfo.FINAL) == 0 && (functionInfo.flags & FunctionInfo.STATIC) == 0 99 && (functionInfo.flags & FunctionInfo.NONRECURSIVE) == 0) { 100 continue; 101 } 102 if (inHierarchy && (functionInfo.flags & FunctionInfo.PRIVATE) > 0) { 103 continue; 104 } 105 } else { 106 if ((functionInfo.flags & FunctionInfo.PUBLIC) == 0 107 && (functionInfo.flags & FunctionInfo.DEFAULT) == 0) { 108 continue; 109 } 110 } 111 if (functionInfo.complexity < complexityLimit - 1) { 112 try { 113 List<IRNode> accum = new ArrayList<>(); 114 if (!functionInfo.argTypes.isEmpty()) { 115 // Here we should do some analysis here to determine if 116 // there are any conflicting functions due to possible 117 // constant folding. 118 119 // For example the following can be done: 120 // Scan all the hieirachy where the class is declared. 121 // If there are function with a same name and same number of args, 122 // then disable usage of foldable expressions in the args. 123 boolean noconsts = false; 124 Collection<Symbol> allFuncsInKlass = SymbolTable.getAllCombined(functionInfo.owner, 125 FunctionInfo.class); 126 for (Symbol s2 : allFuncsInKlass) { 127 FunctionInfo i2 = (FunctionInfo) function; 128 if (!i2.equals(functionInfo) && i2.name.equals(functionInfo.name) 129 && i2.argTypes.size() == functionInfo.argTypes.size()) { 130 noconsts = true; 131 break; 132 } 133 } 134 long argComp = (complexityLimit - 1 - functionInfo.complexity) / functionInfo.argTypes.size(); 135 int argumentOperatorLimit = (operatorLimit - 1) / functionInfo.argTypes.size(); 136 IRNodeBuilder b = new IRNodeBuilder().setOwnerKlass(ownerClass) 137 .setComplexityLimit(argComp) 138 .setOperatorLimit(argumentOperatorLimit) 139 .setExceptionSafe(exceptionSafe) 140 .setNoConsts(noconsts); 141 for (VariableInfo argType : functionInfo.argTypes) { 142 accum.add(b.setResultType(argType.type) 143 .getExpressionFactory() 144 .produce()); 145 } 146 } 147 return new Function(ownerClass, functionInfo, accum); 148 } catch (ProductionFailedException e) { 149 // removeAllChildren(); 150 } 151 } 152 } 153 } 154 throw new ProductionFailedException(); 155 } 156 }