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 {
  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 IRNode 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 rule = new Rule("class");
  78                     rule.add("basic_class", builder.setName(prefix + "_Class_" + i)
  79                             .setPrinterName(prefix + ".Printer")
  80                             .setMemberFunctionsLimit(memberFunctionsLimit)
  81                             .getKlassFactory());
  82                     if (!ProductionParams.disableInterfaces.value()) {
  83                         rule.add("interface", builder.setName(prefix + "_Interface_" + i)
  84                                 .setMemberFunctionsLimit((int) (memberFunctionsLimit * 0.2))
  85                                 .getInterfaceFactory(), 0.1);
  86                     }
  87                     // TODO: Add enums
  88                     content.add(rule.produce());
  89                 } catch (ProductionFailedException e) {
  90                 }
  91             }
  92         }
  93         ensureMinDepth(content);
  94         ensureMaxDepth(content);
  95         return new ClassDefinitionBlock(content, level);
  96     }
  97 
  98     private void ensureMinDepth(Collection<IRNode> content) throws ProductionFailedException {
  99         int minDepth = ProductionParams.minCfgDepth.value();
 100         List<IRNode> childs = content.stream()
 101                 .filter(c -> c instanceof Klass)
 102                 .collect(Collectors.toList());
 103         addMoreChildren(childs, content, minDepth);
 104     }
 105 
 106     private void addMoreChildren(List<IRNode> childs, Collection<IRNode> content, int minDepth)
 107         throws ProductionFailedException {
 108         while (!childs.isEmpty() && IRNode.countDepth(content) < minDepth) {
 109             PseudoRandom.shuffle(childs);
 110             IRNode randomChild = childs.get(0);
 111             List<IRNode> leaves = randomChild.getStackableLeaves();
 112             if (!leaves.isEmpty()) {
 113                 PseudoRandom.shuffle(leaves);
 114                 Block randomLeaf = (Block) leaves.get(0);
 115                 TypeKlass klass = (TypeKlass) randomChild.getKlass();
 116                 int newLevel = randomLeaf.getLevel() + 1;
 117                 Type retType = randomLeaf.getReturnType();
 118                 IRNodeBuilder b = new IRNodeBuilder()
 119                         .setOwnerKlass(klass)
 120                         .setResultType(retType)
 121                         .setComplexityLimit(complexityLimit)
 122                         .setStatementLimit(statementLimit)
 123                         .setOperatorLimit(operatorLimit)
 124                         .setLevel(newLevel);
 125                 IRNode newBlock = b.getBlockFactory().produce();
 126                 List<IRNode> siblings = randomLeaf.getChildren();
 127                 // to avoid break;
 128                 int index = PseudoRandom.randomNotZero(siblings.size() - 1);
 129                 siblings.add(index, newBlock);
 130             }
 131         }
 132     }
 133 
 134     private void ensureMaxDepth(Collection<IRNode> content) {
 135         int maxDepth = ProductionParams.maxCfgDepth.value();
 136         List<IRNode> childs = content.stream()
 137                 .filter(c -> c instanceof Klass && c.countDepth() > maxDepth)
 138                 .collect(Collectors.toList());
 139         for (IRNode ch : childs) {
 140             List<IRNode> leaves = null;
 141             do {
 142                 long depth = Math.max(ch.countDepth(), maxDepth + 1);
 143                 leaves = ch.getDeviantBlocks(depth);
 144                 if(leaves.size() > 0) {
 145                     leaves.get(0).removeSelf();
 146                 }
 147             } while (!leaves.isEmpty() && ch.countDepth() > maxDepth);
 148         }
 149     }
 150 }