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 jdk.test.lib.jittester.IRNode;
  28 import jdk.test.lib.jittester.Literal;
  29 import jdk.test.lib.jittester.ProductionFailedException;
  30 import jdk.test.lib.jittester.ProductionParams;
  31 import jdk.test.lib.jittester.Type;
  32 import jdk.test.lib.jittester.TypeList;
  33 import jdk.test.lib.jittester.VariableDeclaration;
  34 import jdk.test.lib.jittester.arrays.ArrayCreation;
  35 import jdk.test.lib.jittester.types.TypeArray;
  36 import jdk.test.lib.jittester.types.TypeKlass;
  37 import jdk.test.lib.jittester.utils.PseudoRandom;
  38 
  39 class ArrayCreationFactory extends SafeFactory<ArrayCreation> {
  40     private final long complexityLimit;
  41     private final int operatorLimit;
  42     private final Type resultType;
  43     private final boolean exceptionSafe;
  44     private final boolean noconsts;
  45     private final TypeKlass ownerClass;
  46 
  47     ArrayCreationFactory(long complexityLimit, int operatorLimit,
  48             TypeKlass ownerClass, Type resultType, boolean exceptionSafe, boolean noconsts) {
  49         this.complexityLimit = complexityLimit;
  50         this.operatorLimit = operatorLimit;
  51         this.ownerClass = ownerClass;
  52         this.resultType = resultType;
  53         this.exceptionSafe = exceptionSafe;
  54         this.noconsts = noconsts;
  55     }
  56 
  57     @Override
  58     protected ArrayCreation sproduce() throws ProductionFailedException {
  59         if (resultType instanceof TypeArray) {
  60             TypeArray arrayResultType = (TypeArray) resultType;
  61             if (arrayResultType.type.equals(TypeList.VOID)) {
  62                 arrayResultType = arrayResultType.produce();
  63             }
  64             IRNodeBuilder builder = new IRNodeBuilder()
  65                     .setComplexityLimit(complexityLimit)
  66                     .setOwnerKlass(ownerClass)
  67                     .setResultType(TypeList.BYTE)
  68                     .setExceptionSafe(exceptionSafe)
  69                     .setNoConsts(noconsts);
  70             double chanceExpression = ProductionParams.chanceExpressionIndex.value() / 100;
  71             ArrayList<IRNode> dims = new ArrayList<>(arrayResultType.dimensions);
  72             for (int i = 0; i < arrayResultType.dimensions; i++) {
  73                 if (PseudoRandom.randomBoolean(chanceExpression)) {
  74                     dims.add(builder.setOperatorLimit((int) (PseudoRandom.random()
  75                                 * operatorLimit / arrayResultType.dimensions))
  76                             .getExpressionFactory()
  77                             .produce());
  78                 } else {
  79                     Literal dimension = builder.getLiteralFactory().produce();
  80                     while (Integer.valueOf(dimension.getValue().toString()) < 1) {
  81                         dimension = builder.getLiteralFactory().produce();
  82                     }
  83                     dims.add(dimension);
  84                 }
  85             }
  86             VariableDeclaration var = builder
  87                     .setOwnerKlass(ownerClass)
  88                     .setResultType(arrayResultType)
  89                     .setIsLocal(true)
  90                     .setIsStatic(false)
  91                     .getVariableDeclarationFactory()
  92                     .produce();
  93             return new ArrayCreation(var, arrayResultType, dims);
  94         }
  95         throw new ProductionFailedException();
  96     }
  97 }