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