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