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 java.util.stream.Collectors; 30 import jdk.test.lib.jittester.Block; 31 import jdk.test.lib.jittester.IRNode; 32 import jdk.test.lib.jittester.ProductionFailedException; 33 import jdk.test.lib.jittester.ProductionParams; 34 import jdk.test.lib.jittester.Rule; 35 import jdk.test.lib.jittester.Type; 36 import jdk.test.lib.jittester.classes.ClassDefinitionBlock; 37 import jdk.test.lib.jittester.classes.Klass; 38 import jdk.test.lib.jittester.types.TypeKlass; 39 import jdk.test.lib.jittester.utils.PseudoRandom; 40 41 class ClassDefinitionBlockFactory extends Factory<ClassDefinitionBlock> { 42 private final String prefix; 43 private final long complexityLimit; 44 private final int classesLimit; 45 private final int statementLimit; 46 private final int operatorLimit; 47 private final int memberFunctionsLimit; 48 private final int memberFunctionsArgLimit; 49 private final int level; 50 51 ClassDefinitionBlockFactory(String prefix, int classesLimit, int memberFunctionsLimit, 52 int memberFunctionsArgLimit, long complexityLimit, int statementLimit, 53 int operatorLimit, int level) { 54 this.prefix = prefix; 55 this.classesLimit = classesLimit; 56 this.memberFunctionsLimit = memberFunctionsLimit; 57 this.memberFunctionsArgLimit = memberFunctionsArgLimit; 58 this.complexityLimit = complexityLimit; 59 this.statementLimit = statementLimit; 60 this.operatorLimit = operatorLimit; 61 this.level = level; 62 } 63 64 @Override 65 public ClassDefinitionBlock produce() throws ProductionFailedException { 66 ArrayList<IRNode> content = new ArrayList<>(); 67 int limit = (int) Math.ceil(PseudoRandom.random() * classesLimit); 68 if (limit > 0) { 69 long classCompl = complexityLimit / limit; 70 IRNodeBuilder builder = new IRNodeBuilder().setLevel(level) 71 .setMemberFunctionsArgLimit(memberFunctionsArgLimit) 72 .setStatementLimit(statementLimit) 73 .setOperatorLimit(operatorLimit) 74 .setComplexityLimit(classCompl); 75 for (int i = 0; i < limit; i++) { 76 try { 77 Rule<IRNode> rule = new Rule<>("class"); 78 rule.add("basic_class", builder.setName(prefix + "_Class_" + i) 79 .setMemberFunctionsLimit(memberFunctionsLimit) 80 .getKlassFactory()); 81 if (!ProductionParams.disableInterfaces.value()) { 82 rule.add("interface", builder.setName(prefix + "_Interface_" + i) 83 .setMemberFunctionsLimit((int) (memberFunctionsLimit * 0.2)) 84 .getInterfaceFactory(), 0.1); 85 } 86 // TODO: Add enums 87 content.add(rule.produce()); 88 } catch (ProductionFailedException e) { 89 } 90 } 91 } 92 ensureMinDepth(content); 93 ensureMaxDepth(content); 94 return new ClassDefinitionBlock(content, level); 95 } 96 97 private void ensureMinDepth(Collection<IRNode> content) throws ProductionFailedException { 98 int minDepth = ProductionParams.minCfgDepth.value(); 99 List<IRNode> childs = content.stream() 100 .filter(c -> c instanceof Klass) 101 .collect(Collectors.toList()); 102 addMoreChildren(childs, content, minDepth); 103 } 104 105 private void addMoreChildren(List<IRNode> children, Collection<IRNode> content, int minDepth) 106 throws ProductionFailedException { 107 /* check situation when no stackable leaves available in all children */ 108 if (IRNode.getModifiableNodesCount(children) == 0L) { 109 return; 110 } 111 /* now let's try to add children */ 112 while (!children.isEmpty() && IRNode.countDepth(content) < minDepth) { 113 PseudoRandom.shuffle(children); 114 IRNode randomChild = children.get(0); 115 List<IRNode> leaves = randomChild.getStackableLeaves(); 116 if (!leaves.isEmpty()) { 117 Block randomLeaf = (Block) leaves.get(PseudoRandom.randomNotNegative(leaves.size())); 118 TypeKlass owner = randomChild.getOwner(); 119 int newLevel = randomLeaf.getLevel() + 1; 120 Type retType = randomLeaf.getResultType(); 121 IRNodeBuilder b = new IRNodeBuilder() 122 .setOwnerKlass(owner) 123 .setResultType(retType) 124 .setComplexityLimit(complexityLimit) 125 .setStatementLimit(statementLimit) 126 .setOperatorLimit(operatorLimit) 127 .setLevel(newLevel); 128 IRNode newBlock = b.getBlockFactory().produce(); 129 List<IRNode> siblings = randomLeaf.getChildren(); 130 // to avoid break; 131 int index = PseudoRandom.randomNotZero(siblings.size() - 1); 132 siblings.add(index, newBlock); 133 } 134 } 135 } 136 137 private void ensureMaxDepth(Collection<IRNode> content) { 138 int maxDepth = ProductionParams.maxCfgDepth.value(); 139 List<IRNode> childrenClasses = content.stream() 140 .filter(c -> c instanceof Klass && c.countDepth() > maxDepth) 141 .collect(Collectors.toList()); 142 /* now attempt to reduce depth by removing optional parts of control deviation 143 blocks in case IRTree has oversized depth */ 144 IRNode.tryToReduceNodesDepth(childrenClasses, maxDepth); 145 } 146 }