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 jdk.test.lib.jittester.IRNode; 27 import jdk.test.lib.jittester.ProductionFailedException; 28 import jdk.test.lib.jittester.SymbolTable; 29 import jdk.test.lib.jittester.TernaryOperator; 30 import jdk.test.lib.jittester.Type; 31 import jdk.test.lib.jittester.TypeList; 32 import jdk.test.lib.jittester.types.TypeKlass; 33 import jdk.test.lib.jittester.utils.PseudoRandom; 34 35 class TernaryOperatorFactory extends OperatorFactory<TernaryOperator> { 36 private final Type resultType; 37 private final TypeKlass ownerClass; 38 39 TernaryOperatorFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass, 40 Type resultType, boolean exceptionSafe, boolean noconsts) { 41 super(2, complexityLimit, operatorLimit, exceptionSafe, noconsts); 42 this.resultType = resultType; 43 this.ownerClass = ownerClass; 44 } 45 46 private TernaryOperator generateProduction() throws ProductionFailedException { 47 int leftOpLimit = (int) (PseudoRandom.random() * 0.3 * (operatorLimit - 1)); 48 int rightOpLimit = (int) (PseudoRandom.random() * 0.3 * (operatorLimit - 1)); 49 int condOpLimit = operatorLimit - 1 - leftOpLimit - rightOpLimit; 50 long leftComplLimit = (long) (PseudoRandom.random() * 0.3 * (complexityLimit - 1)); 51 long rightComplLimit = (long) (PseudoRandom.random() * 0.3 * (complexityLimit - 1)); 52 long condComplLimit = complexityLimit - 1 - leftComplLimit - rightComplLimit; 53 if (leftComplLimit == 0 || rightComplLimit == 0 || condComplLimit == 0 54 || leftOpLimit == 0 || rightOpLimit == 0 || condOpLimit == 0) { 55 throw new ProductionFailedException(); 56 } 57 IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass) 58 .setExceptionSafe(exceptionSafe); 59 IRNode conditionalExp = builder.setComplexityLimit(condComplLimit) 60 .setOperatorLimit(condOpLimit) 61 .setResultType(TypeList.BOOLEAN) 62 .setNoConsts(noconsts) 63 .getExpressionFactory() 64 .produce(); 65 // Ignore initializations performed in left and right branches: 66 IRNode leftExp; 67 SymbolTable.push(); 68 try { 69 leftExp = builder.setComplexityLimit(leftComplLimit) 70 .setOperatorLimit(leftOpLimit) 71 .setResultType(resultType) 72 .setNoConsts(false) 73 .getExpressionFactory() 74 .produce(); 75 } finally { 76 SymbolTable.pop(); 77 } 78 IRNode rightExp; 79 SymbolTable.push(); 80 try { 81 rightExp = builder.setComplexityLimit(rightComplLimit) 82 .setOperatorLimit(rightOpLimit) 83 .setResultType(resultType) 84 .setNoConsts(false) 85 .getExpressionFactory() 86 .produce(); 87 } finally { 88 SymbolTable.pop(); 89 } 90 return new TernaryOperator(conditionalExp, leftExp, rightExp); 91 } 92 93 @Override 94 public TernaryOperator produce() throws ProductionFailedException { 95 try { 96 SymbolTable.push(); 97 TernaryOperator result = generateProduction(); 98 SymbolTable.merge(); 99 return result; 100 } catch (ProductionFailedException e) { 101 SymbolTable.pop(); 102 throw e; 103 } 104 } 105 }