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
24package jdk.test.lib.jittester.factories;
25
26import java.util.ArrayList;
27import jdk.test.lib.jittester.IRNode;
28import jdk.test.lib.jittester.Literal;
29import jdk.test.lib.jittester.ProductionFailedException;
30import jdk.test.lib.jittester.ProductionParams;
31import jdk.test.lib.jittester.Type;
32import jdk.test.lib.jittester.TypeList;
33import jdk.test.lib.jittester.arrays.ArrayCreation;
34import jdk.test.lib.jittester.arrays.ArrayExtraction;
35import jdk.test.lib.jittester.types.TypeArray;
36import jdk.test.lib.jittester.types.TypeKlass;
37import jdk.test.lib.jittester.utils.PseudoRandom;
38
39class ArrayExtractionFactory extends SafeFactory<ArrayExtraction> {
40    private final long complexityLimit;
41    private final int operatorLimit;
42    private final Type resultType;
43    private final TypeKlass ownerClass;
44    private final boolean exceptionSafe;
45    private final boolean noconsts;
46
47    ArrayExtractionFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass,
48            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    public ArrayExtraction sproduce() throws ProductionFailedException {
59        if (resultType instanceof TypeArray) {
60            TypeArray arrayType = (TypeArray) resultType;
61            int delta = PseudoRandom.randomNotZero(ProductionParams.dimensionsLimit.value()
62                    - arrayType.dimensions);
63            if (arrayType.dimensions + delta <= ProductionParams.dimensionsLimit.value()) {
64                long arrayComplLimit = (long) (complexityLimit * 0.5 * PseudoRandom.random());
65                int arrayOpLimit = (int) (operatorLimit * 0.5 * PseudoRandom.random());
66                IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass)
67                        .setExceptionSafe(exceptionSafe)
68                        .setNoConsts(noconsts);
69                IRNode arrayReturningExpression = builder
70                        .setComplexityLimit(arrayComplLimit)
71                        .setOperatorLimit(arrayOpLimit)
72                        .setResultType(new TypeArray(arrayType.type, arrayType.dimensions + delta))
73                        .getExpressionFactory().produce();
74                ArrayList<IRNode> perDimensionExpression = new ArrayList<>(delta);
75                long dimComplLimit = (long) ((complexityLimit - arrayComplLimit)
76                        * PseudoRandom.random()) / delta;
77                int dimOpLimit = (int) ((operatorLimit - arrayOpLimit - delta)
78                        * PseudoRandom.random()) / delta;
79                double chanceExpression = ProductionParams.chanceExpressionIndex.value() / 100.;
80                for (int i = 0; i < delta; i++) {
81                    if (PseudoRandom.randomBoolean(chanceExpression)) {
82                        perDimensionExpression.add(builder.setResultType(TypeList.BYTE)
83                                .setComplexityLimit(dimComplLimit)
84                                .setOperatorLimit(dimOpLimit)
85                                .getExpressionFactory()
86                                .produce());
87                    } else {
88                        byte dimLimit = 0;
89                        if (arrayReturningExpression instanceof ArrayCreation) {
90                            ArrayCreation arratCreation = (ArrayCreation) arrayReturningExpression;
91                            dimLimit = arratCreation.getDimensionSize(i);
92                        } else if (arrayReturningExpression instanceof ArrayExtraction) {
93                            ArrayExtraction arrayExtraction = (ArrayExtraction) arrayReturningExpression;
94                            if (i < arrayExtraction.getDimsNumber())
95                                dimLimit = arrayExtraction.getDim(i);
96                        }
97                        perDimensionExpression.add(new Literal((byte)PseudoRandom.randomNotNegative(dimLimit), TypeList.BYTE));
98                    }
99                }
100                return new ArrayExtraction(arrayReturningExpression, perDimensionExpression);
101            }
102        }
103        throw new ProductionFailedException();
104    }
105}
106