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.arrays.ArrayCreation; 33 import jdk.test.lib.jittester.arrays.ArrayElement; 34 import jdk.test.lib.jittester.arrays.ArrayExtraction; 35 import jdk.test.lib.jittester.types.TypeArray; 36 import jdk.test.lib.jittester.types.TypeKlass; 37 import jdk.test.lib.jittester.types.TypeByte; 38 import jdk.test.lib.jittester.utils.PseudoRandom; 39 40 class ArrayElementFactory extends SafeFactory { 41 private final long complexityLimit; 42 private final int operatorLimit; 43 private final Type resultType; 44 private final TypeKlass ownerClass; 45 private final boolean exceptionSafe; 46 private final boolean noconsts; 47 48 ArrayElementFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass, 49 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 throw new ProductionFailedException(); 62 } 63 long arrayComplexityLimit = (long) (complexityLimit * 0.5 * PseudoRandom.random()); 64 int arrayOperatorLimit = (int) (operatorLimit * 0.5 * PseudoRandom.random()); 65 int dimensionsCount = PseudoRandom.randomNotZero(ProductionParams.dimensionsLimit.value()); 66 long complexityPerDimension = (long) ((complexityLimit - arrayComplexityLimit) 67 * PseudoRandom.random()) / dimensionsCount; 68 int operatorLimitPerDimension = (int) ((operatorLimit - arrayOperatorLimit - dimensionsCount) 69 * PseudoRandom.random()) / dimensionsCount; 70 IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass) 71 .setExceptionSafe(exceptionSafe) 72 .setNoConsts(noconsts); 73 IRNode arrayReturningExpression = builder 74 .setComplexityLimit(arrayComplexityLimit) 75 .setOperatorLimit(arrayOperatorLimit) 76 .setResultType(new TypeArray(resultType, dimensionsCount)) 77 .getExpressionFactory() 78 .produce(); 79 ExpressionFactory expressionFactory = builder 80 .setComplexityLimit(complexityPerDimension) 81 .setOperatorLimit(operatorLimitPerDimension) 82 .setResultType(new TypeByte()) 83 .getExpressionFactory(); 84 double chanceExpression = ProductionParams.chanceExpressionIndex.value() / 100.; 85 ArrayList<IRNode> perDimensionExpressions = new ArrayList<>(dimensionsCount); 86 for (int i = 0; i < dimensionsCount; i++) { 87 if (PseudoRandom.randomBoolean(chanceExpression)) { 88 perDimensionExpressions.add(expressionFactory.produce()); 89 } else { 90 byte dimLimit = 0; 91 if (arrayReturningExpression instanceof ArrayCreation) { 92 ArrayCreation arrayCreation = (ArrayCreation) arrayReturningExpression; 93 dimLimit = arrayCreation.getDimensionSize(i); 94 } else if (arrayReturningExpression instanceof ArrayExtraction) { 95 ArrayExtraction arrayExtraction = (ArrayExtraction) arrayReturningExpression; 96 if (i < arrayExtraction.getDimsNumber()) 97 dimLimit = arrayExtraction.getDim(i); 98 } 99 perDimensionExpressions.add(new Literal(PseudoRandom.randomNotNegative(dimLimit), new TypeByte())); 100 } 101 } 102 return new ArrayElement(arrayReturningExpression, perDimensionExpressions); 103 } 104 }