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 jdk.test.lib.jittester.IRNode;
  27 import jdk.test.lib.jittester.If;
  28 import jdk.test.lib.jittester.ProductionFailedException;
  29 import jdk.test.lib.jittester.Type;
  30 import jdk.test.lib.jittester.types.TypeKlass;
  31 import jdk.test.lib.jittester.types.TypeBoolean;
  32 import jdk.test.lib.jittester.utils.PseudoRandom;
  33 
  34 class IfFactory extends SafeFactory {
  35     protected long complexityLimit;
  36     protected int statementLimit;
  37     protected int operatorLimit;
  38     protected boolean canHaveBreaks;
  39     protected boolean canHaveContinues;
  40     protected boolean canHaveReturn;
  41     protected final TypeKlass ownerClass;
  42     protected final Type returnType;
  43     protected final int level;
  44 
  45     IfFactory(TypeKlass ownerClass, Type returnType, long complexityLimit, int statementLimit,
  46             int operatorLimit, int level, boolean canHaveBreaks, boolean canHaveContinues,
  47             boolean canHaveReturn) {
  48         this.ownerClass = ownerClass;
  49         this.returnType = returnType;
  50         this.complexityLimit = complexityLimit;
  51         this.statementLimit = statementLimit;
  52         this.operatorLimit = operatorLimit;
  53         this.level = level;
  54         this.canHaveBreaks = canHaveBreaks;
  55         this.canHaveContinues = canHaveContinues;
  56         this.canHaveReturn = canHaveReturn;
  57     }
  58 
  59     @Override
  60     public IRNode sproduce() throws ProductionFailedException {
  61         // resizeUpChildren(If.IfPart.values().length);
  62         if (statementLimit > 0 && complexityLimit > 0) {
  63             long conditionComplLimit = (long) (0.01 * PseudoRandom.random() * (complexityLimit - 1));
  64             IRNodeBuilder builder = new IRNodeBuilder()
  65                     .setOwnerKlass(ownerClass)
  66                     .setOperatorLimit(operatorLimit);
  67             IRNode condition = builder.setComplexityLimit(conditionComplLimit)
  68                     .setResultType(new TypeBoolean())
  69                     .setExceptionSafe(false)
  70                     .setNoConsts(false)
  71                     .getLimitedExpressionFactory()
  72                     .produce();
  73             // setChild(If.IfPart.CONDITION.ordinal(), condition);
  74             long remainder = complexityLimit - 1 - condition.complexity();
  75             long ifBlockComplLimit = (long) (PseudoRandom.random() * remainder);
  76             long elseBlockComplLimit = remainder - ifBlockComplLimit;
  77             int ifBlockLimit = (int) (PseudoRandom.random() * statementLimit);
  78             int elseBlockLimit = statementLimit - ifBlockLimit;
  79             If.IfPart controlDeviation;
  80             if (ifBlockLimit > 0 && elseBlockLimit <= 0) {
  81                 controlDeviation = If.IfPart.THEN;
  82             } else {
  83                 controlDeviation = PseudoRandom.randomBoolean() ? If.IfPart.THEN : If.IfPart.ELSE;
  84             }
  85             if (ifBlockLimit > 0 && ifBlockComplLimit > 0) {
  86                 IRNode thenBlock = null;
  87                 builder.setResultType(returnType)
  88                         .setLevel(level)
  89                         .setComplexityLimit(ifBlockComplLimit)
  90                         .setStatementLimit(ifBlockLimit);
  91                 if (controlDeviation == If.IfPart.THEN) {
  92                     thenBlock = builder.setSubBlock(false)
  93                             .setCanHaveBreaks(canHaveBreaks)
  94                             .setCanHaveContinues(canHaveContinues)
  95                             .setCanHaveReturn(canHaveReturn)
  96                             .getBlockFactory()
  97                             .produce();
  98                 } else {
  99                     thenBlock = builder.setSubBlock(false)
 100                             .setCanHaveBreaks(false)
 101                             .setCanHaveContinues(false)
 102                             .setCanHaveReturn(false)
 103                             .getBlockFactory()
 104                             .produce();
 105                 }
 106                 // setChild(If.IfPart.THEN.ordinal(), thenBlock);
 107                 IRNode elseBlock = null;
 108                 if (elseBlockLimit > 0 && elseBlockComplLimit > 0) {
 109                     builder.setComplexityLimit(elseBlockComplLimit)
 110                             .setStatementLimit(elseBlockLimit);
 111                     if (controlDeviation == If.IfPart.ELSE) {
 112                         elseBlock = builder.setSubBlock(false)
 113                             .setCanHaveBreaks(canHaveBreaks)
 114                             .setCanHaveContinues(canHaveContinues)
 115                             .setCanHaveReturn(canHaveReturn)
 116                             .getBlockFactory()
 117                             .produce();
 118                     } else {
 119                         elseBlock = builder.setSubBlock(false)
 120                             .setCanHaveBreaks(false)
 121                             .setCanHaveContinues(false)
 122                             .setCanHaveReturn(false)
 123                             .getBlockFactory()
 124                             .produce();
 125                     }
 126                 }
 127                 // setChild(If.IfPart.ELSE.ordinal(), elseBlock);
 128                 return new If(condition, thenBlock, elseBlock, level);
 129             }
 130         }
 131         throw new ProductionFailedException();
 132     }
 133 }