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