1/*
2 * Copyright (c) 2015, 2016, 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 jdk.test.lib.util.Pair;
27import jdk.test.lib.jittester.BinaryOperator;
28import jdk.test.lib.jittester.IRNode;
29import jdk.test.lib.jittester.OperatorKind;
30import jdk.test.lib.jittester.ProductionFailedException;
31import jdk.test.lib.jittester.SymbolTable;
32import jdk.test.lib.jittester.Type;
33import jdk.test.lib.jittester.types.TypeKlass;
34import jdk.test.lib.jittester.utils.PseudoRandom;
35
36abstract class BinaryOperatorFactory extends OperatorFactory<BinaryOperator> {
37    protected final OperatorKind opKind;
38    protected final Type resultType;
39    protected final Type ownerClass;
40
41    protected BinaryOperatorFactory(OperatorKind opKind, long complexityLimit, int operatorLimit,
42            Type ownerClass, Type resultType, boolean exceptionSafe, boolean noconsts) {
43        super(opKind.priority, complexityLimit, operatorLimit, exceptionSafe, noconsts);
44        this.opKind = opKind;
45        this.resultType = resultType;
46        this.ownerClass = ownerClass;
47    }
48
49    protected abstract boolean isApplicable(Type resultType);
50
51    protected abstract Pair<Type, Type> generateTypes();
52
53    protected BinaryOperator generateProduction(Type leftType, Type rightType) throws ProductionFailedException {
54        int leftOpLimit = (int) (PseudoRandom.random() * (operatorLimit - 1));
55        int rightOpLimit = operatorLimit - 1 - leftOpLimit;
56        long leftComplLimit = (long) (PseudoRandom.random() * (complexityLimit - 1));
57        long rightComplLimit = complexityLimit - 1 - leftComplLimit;
58        if (leftOpLimit == 0 || rightOpLimit == 0 || leftComplLimit == 0 || rightComplLimit == 0) {
59            throw new ProductionFailedException();
60        }
61        boolean swap = PseudoRandom.randomBoolean();
62        IRNodeBuilder builder = new IRNodeBuilder().setExceptionSafe(exceptionSafe)
63                .setOwnerKlass((TypeKlass) ownerClass)
64                .setNoConsts(!swap && noconsts);
65        IRNode leftExpr = builder.setComplexityLimit(leftComplLimit)
66                .setOperatorLimit(leftOpLimit)
67                .setResultType(leftType)
68                .getExpressionFactory()
69                .produce();
70        IRNode rightExpr = builder.setComplexityLimit(rightComplLimit)
71                .setOperatorLimit(rightOpLimit)
72                .setResultType(rightType)
73                .getExpressionFactory()
74                .produce();
75        return new BinaryOperator(opKind, resultType, leftExpr, rightExpr);
76    }
77
78    @Override
79    public final BinaryOperator produce() throws ProductionFailedException {
80        if (!isApplicable(resultType)) {
81            //avoid implicit use of resultType.toString()
82            throw new ProductionFailedException("Type " + resultType.getName() + " is not applicable by " + getClass().getName());
83        }
84
85        Pair<Type, Type> types;
86        try {
87            types = generateTypes();
88        } catch (RuntimeException ex) {
89            throw new ProductionFailedException(ex.getMessage());
90        }
91
92        try {
93            SymbolTable.push();
94            BinaryOperator p = generateProduction(types.first, types.second);
95            SymbolTable.merge();
96            return p;
97        } catch (ProductionFailedException e) {
98            SymbolTable.pop();
99            throw e;
100        }
101    }
102}
103