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.Initialization;
  28 import jdk.test.lib.jittester.Literal;
  29 import jdk.test.lib.jittester.LocalVariable;
  30 import jdk.test.lib.jittester.Nothing;
  31 import jdk.test.lib.jittester.ProductionFailedException;
  32 import jdk.test.lib.jittester.SymbolTable;
  33 import jdk.test.lib.jittester.Type;
  34 import jdk.test.lib.jittester.loops.Loop;
  35 import jdk.test.lib.jittester.loops.While;
  36 import jdk.test.lib.jittester.types.TypeKlass;
  37 import jdk.test.lib.jittester.types.TypeInt;
  38 import jdk.test.lib.jittester.utils.PseudoRandom;
  39 
  40 class WhileFactory extends SafeFactory {
  41     private final Loop loop;
  42     private final long complexityLimit;
  43     private final int statementLimit;
  44     private final int operatorLimit;
  45     private final TypeKlass ownerClass;
  46     private final int level;
  47     private final Type returnType;
  48     private long thisLoopIterLimit = 0;
  49     private final boolean canHaveReturn;
  50 
  51     WhileFactory(TypeKlass ownerClass, Type returnType, long complexityLimit, int statementLimit,
  52             int operatorLimit, int level, boolean canHaveReturn) {
  53         this.ownerClass = ownerClass;
  54         this.returnType = returnType;
  55         loop = new Loop();
  56         this.complexityLimit = complexityLimit;
  57         this.statementLimit = statementLimit;
  58         this.operatorLimit = operatorLimit;
  59         this.level = level;
  60         this.canHaveReturn = canHaveReturn;
  61     }
  62 
  63     @Override
  64     protected IRNode sproduce() throws ProductionFailedException {
  65         if (statementLimit <= 0 || complexityLimit <= 0) {
  66             throw new ProductionFailedException();
  67         }
  68         long complexity = complexityLimit;
  69         // Loop header parameters
  70         long headerComplLimit = (long) (0.005 * complexity * PseudoRandom.random());
  71         complexity -= headerComplLimit;
  72         int headerStatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 3.0));
  73         // Loop body parameters
  74         thisLoopIterLimit = (long) (0.0001 * complexity * PseudoRandom.random());
  75         if (thisLoopIterLimit > Integer.MAX_VALUE || thisLoopIterLimit == 0) {
  76             throw new ProductionFailedException();
  77         }
  78         complexity = thisLoopIterLimit > 0 ? complexity / thisLoopIterLimit : 0;
  79         long condComplLimit = (long) (complexity * PseudoRandom.random());
  80         complexity -= condComplLimit;
  81         long body1ComplLimit = (long) (complexity * PseudoRandom.random());
  82         complexity -= body1ComplLimit;
  83         int body1StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
  84         long body2ComplLimit = (long) (complexity * PseudoRandom.random());
  85         complexity -= body2ComplLimit;
  86         int body2StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
  87         long body3ComplLimit = complexity;
  88         int body3StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
  89         // Production
  90         IRNodeBuilder builder =  new IRNodeBuilder().setOwnerKlass(ownerClass)
  91                 .setResultType(returnType)
  92                 .setOperatorLimit(operatorLimit);
  93         loop.initialization = builder.getCounterInitializerFactory(0).produce();
  94         IRNode header;
  95         try {
  96             header = builder.setComplexityLimit(headerComplLimit)
  97                     .setStatementLimit(headerStatementLimit)
  98                     .setLevel(level - 1)
  99                     .setSubBlock(true)
 100                     .setCanHaveBreaks(false)
 101                     .setCanHaveContinues(false)
 102                     .setCanHaveReturn(false)
 103                     .getBlockFactory()
 104                     .produce();
 105         } catch (ProductionFailedException e) {
 106             header = new Nothing();
 107         }
 108         LocalVariable counter = new LocalVariable(((Initialization) loop.initialization).get());
 109         Literal limiter = new Literal(Integer.valueOf((int) thisLoopIterLimit), new TypeInt());
 110         loop.condition = builder.setComplexityLimit(condComplLimit)
 111                 .setLocalVariable(counter)
 112                 .getLoopingConditionFactory(limiter)
 113                 .produce();
 114         IRNode body1;
 115         SymbolTable.push();
 116         try {
 117             body1 = builder.setComplexityLimit(body1ComplLimit)
 118                     .setStatementLimit(body1StatementLimit)
 119                     .setLevel(level)
 120                     .setSubBlock(true)
 121                     .setCanHaveBreaks(true)
 122                     .setCanHaveContinues(false)
 123                     .setCanHaveReturn(canHaveReturn)
 124                     .getBlockFactory()
 125                     .produce();
 126         } catch (ProductionFailedException e) {
 127             body1 = new Nothing();
 128         }
 129         loop.manipulator = builder.setLocalVariable(counter).getCounterManipulatorFactory().produce();
 130         IRNode body2;
 131         try {
 132             body2 = builder.setComplexityLimit(body2ComplLimit)
 133                     .setStatementLimit(body2StatementLimit)
 134                     .setLevel(level)
 135                     .setSubBlock(true)
 136                     .setCanHaveBreaks(true)
 137                     .setCanHaveContinues(true)
 138                     .setCanHaveReturn(canHaveReturn)
 139                     .getBlockFactory()
 140                     .produce();
 141         } catch (ProductionFailedException e) {
 142             body2 = new Nothing();
 143         }
 144         IRNode body3;
 145         try {
 146             body3 = builder.setComplexityLimit(body3ComplLimit)
 147                     .setStatementLimit(body3StatementLimit)
 148                     .setLevel(level)
 149                     .setSubBlock(true)
 150                     .setCanHaveBreaks(true)
 151                     .setCanHaveContinues(false)
 152                     .setCanHaveReturn(canHaveReturn)
 153                     .getBlockFactory()
 154                     .produce();
 155         } catch (ProductionFailedException e) {
 156             body3 = new Nothing();
 157         }
 158         SymbolTable.pop();
 159         return new While(level, loop, thisLoopIterLimit, header, body1, body2, body3);
 160     }
 161 }