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 30 import jdk.test.lib.jittester.IRNode; 31 import jdk.test.lib.jittester.ProductionFailedException; 32 import jdk.test.lib.jittester.Symbol; 33 import jdk.test.lib.jittester.SymbolTable; 34 import jdk.test.lib.jittester.Type; 35 import jdk.test.lib.jittester.TypeList; 36 import jdk.test.lib.jittester.VariableInfo; 37 import jdk.test.lib.jittester.functions.ArgumentDeclaration; 38 import jdk.test.lib.jittester.functions.FunctionDefinition; 39 import jdk.test.lib.jittester.functions.FunctionInfo; 40 import jdk.test.lib.jittester.types.TypeKlass; 41 import jdk.test.lib.jittester.types.TypeVoid; 42 import jdk.test.lib.jittester.utils.PseudoRandom; 43 44 class FunctionDefinitionFactory extends Factory { 45 private final Type resultType; 46 private final String name; 47 private final long complexityLimit; 48 private final int statementLimit; 49 private final int operatorLimit; 50 private final int memberFunctionsArgLimit; 51 private final int flags; 52 private final int level; 53 private final TypeKlass ownerClass; 54 55 FunctionDefinitionFactory(String name, TypeKlass ownerClass, Type resultType, 56 long complexityLimit, int statementLimit, int operatorLimit, 57 int memberFunctionsArgLimit, int level, int flags) { 58 this.name = name; 59 this.ownerClass = ownerClass; 60 this.resultType = resultType; 61 this.complexityLimit = complexityLimit; 62 this.statementLimit = statementLimit; 63 this.operatorLimit = operatorLimit; 64 this.memberFunctionsArgLimit = memberFunctionsArgLimit; 65 this.level = level; 66 this.flags = flags; 67 } 68 69 @Override 70 public IRNode produce() throws ProductionFailedException { 71 Type resType = resultType; 72 if (resType == null) { 73 List<Type> types = new ArrayList<>(TypeList.getAll()); 74 types.add(new TypeVoid()); 75 resType = PseudoRandom.randomElement(types); 76 } 77 int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit); 78 ArrayList<VariableInfo> argumentsInfo; 79 if ((flags & FunctionInfo.STATIC) > 0) { 80 argumentsInfo = new ArrayList<>(argNumber); 81 } else { 82 argumentsInfo = new ArrayList<>(argNumber + 1); 83 argumentsInfo.add(new VariableInfo("this", ownerClass, ownerClass, 84 VariableInfo.FINAL | VariableInfo.LOCAL | VariableInfo.INITIALIZED)); 85 } 86 ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber); 87 SymbolTable.push(); 88 IRNode body; 89 IRNode returnNode; 90 FunctionInfo functionInfo; 91 try { 92 IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClass); 93 int i = 0; 94 for (; i < argNumber; i++) { 95 ArgumentDeclaration d = builder.setVariableNumber(i).getArgumentDeclarationFactory() 96 .produce(); 97 argumentsDeclaration.add(d); 98 argumentsInfo.add(d.variableInfo); 99 } 100 Collection<Symbol> thisKlassFuncs = SymbolTable.getAllCombined(ownerClass, 101 FunctionInfo.class); 102 Collection<Symbol> parentFuncs = FunctionDefinition.getFuncsFromParents(ownerClass); 103 while (true) { 104 functionInfo = new FunctionInfo(name, ownerClass, resType, 0, flags, 105 argumentsInfo); 106 if (thisKlassFuncs.contains(functionInfo) 107 || FunctionDefinition.isInvalidOverride(functionInfo, parentFuncs)) { 108 // try changing the signature, and go checking again. 109 ArgumentDeclaration argDecl = builder.setVariableNumber(i++) 110 .getArgumentDeclarationFactory().produce(); 111 argumentsDeclaration.add(argDecl); 112 argumentsInfo.add(argDecl.variableInfo); 113 } else { 114 break; 115 } 116 } 117 long blockComplLimit = (long) (PseudoRandom.random() * complexityLimit); 118 body = builder.setOwnerKlass(ownerClass) 119 .setResultType(resType) 120 .setComplexityLimit(blockComplLimit) 121 .setStatementLimit(statementLimit) 122 .setOperatorLimit(operatorLimit) 123 .setLevel(level) 124 .setSubBlock(true) 125 .setCanHaveBreaks(false) 126 .setCanHaveContinues(false) 127 .setCanHaveReturn(true) 128 .getBlockFactory() 129 .produce(); 130 if (!resType.equals(new TypeVoid())) { 131 returnNode = builder.setComplexityLimit(complexityLimit - blockComplLimit) 132 .setExceptionSafe(false) 133 .getReturnFactory() 134 .produce(); 135 } else { 136 returnNode = null; 137 } 138 } finally { 139 SymbolTable.pop(); 140 } 141 // addChildren(argumentsDeclaration); // not neccessary while complexity() doesn't use it 142 functionInfo = new FunctionInfo(name, ownerClass, resType, body == null ? 0 : body.complexity(), 143 flags, argumentsInfo); 144 // If it's all ok, add the function to the symbol table. 145 SymbolTable.add(functionInfo); 146 return new FunctionDefinition(functionInfo, argumentsDeclaration, body, returnNode); 147 } 148 }