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> childs, Collection<IRNode> content, int minDepth)
 106         throws ProductionFailedException {
 107         while (!childs.isEmpty() && IRNode.countDepth(content) < minDepth) {
 108             PseudoRandom.shuffle(childs);
 109             IRNode randomChild = childs.get(0);
 110             List<IRNode> leaves = randomChild.getStackableLeaves();
 111             if (!leaves.isEmpty()) {
 112                 Block randomLeaf = (Block) leaves.get(PseudoRandom.randomNotNegative(leaves.size()));
 113                 TypeKlass owner = randomChild.getOwner();
 114                 int newLevel = randomLeaf.getLevel() + 1;
 115                 Type retType = randomLeaf.getResultType();
 116                 IRNodeBuilder b = new IRNodeBuilder()
 117                         .setOwnerKlass(owner)
 118                         .setResultType(retType)
 119                         .setComplexityLimit(complexityLimit)
 120                         .setStatementLimit(statementLimit)
 121                         .setOperatorLimit(operatorLimit)
 122                         .setLevel(newLevel);
 123                 IRNode newBlock = b.getBlockFactory().produce();
 124                 List<IRNode> siblings = randomLeaf.getChildren();
 125                 // to avoid break;
 126                 int index = PseudoRandom.randomNotZero(siblings.size() - 1);
 127                 siblings.add(index, newBlock);
 128             }
 129         }
 130     }
 131 
 132     private void ensureMaxDepth(Collection<IRNode> content) {
 133         int maxDepth = ProductionParams.maxCfgDepth.value();
 134         List<IRNode> childs = content.stream()
 135                 .filter(c -> c instanceof Klass && c.countDepth() > maxDepth)
 136                 .collect(Collectors.toList());
 137         for (IRNode ch : childs) {
 138             List<IRNode> leaves;
 139             do {
 140                 long depth = Math.max(ch.countDepth(), maxDepth + 1);
 141                 leaves = ch.getDeviantBlocks(depth);
 142                 if(leaves.size() > 0) {
 143                     leaves.get(0).removeSelf();
 144                 }
 145             } while (!leaves.isEmpty() && ch.countDepth() > maxDepth);
 146         }
 147     }
 148 }