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.CastOperator; 28 import jdk.test.lib.jittester.IRNode; 29 import jdk.test.lib.jittester.ProductionFailedException; 30 import jdk.test.lib.jittester.SymbolTable; 31 import jdk.test.lib.jittester.Type; 32 import jdk.test.lib.jittester.TypeList; 33 import jdk.test.lib.jittester.types.TypeKlass; 34 import jdk.test.lib.jittester.utils.PseudoRandom; 35 36 class CastOperatorFactory extends OperatorFactory { 37 private final Type resultType; 38 private final Type ownerClass; 39 40 CastOperatorFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass, 41 Type resultType, boolean exceptionSafe, boolean noconsts) { 42 super(13, complexityLimit, operatorLimit, exceptionSafe, noconsts); 43 this.resultType = resultType; 44 this.ownerClass = ownerClass; 45 } 46 47 @Override 48 public IRNode produce() throws ProductionFailedException { 49 ArrayList<Type> argType = new ArrayList<>(TypeList.getAll()); 50 PseudoRandom.shuffle(argType); 51 for (Type type : argType) { 52 try { 53 ExpressionFactory expressionFactory = new IRNodeBuilder() 54 .setComplexityLimit(complexityLimit - 1) 55 .setOperatorLimit(operatorLimit - 1) 56 .setOwnerKlass((TypeKlass) ownerClass) 57 .setExceptionSafe(exceptionSafe) 58 .setNoConsts(noconsts) 59 .setResultType(type) 60 .getExpressionFactory(); 61 SymbolTable.push(); 62 if (type.equals(resultType)) { 63 IRNode expr = expressionFactory.produce(); 64 SymbolTable.merge(); 65 return expr; 66 } else if ((!exceptionSafe || exceptionSafe && !(type instanceof TypeKlass)) 67 && type.canExplicitlyCastTo(resultType)) { 68 // In safe mode we cannot explicitly cast an object, because it may throw. 69 IRNode castOperator = new CastOperator(resultType, expressionFactory.produce()); 70 SymbolTable.merge(); 71 return castOperator; 72 } 73 throw new ProductionFailedException(); 74 } catch (ProductionFailedException e) { 75 SymbolTable.pop(); 76 } 77 } 78 throw new ProductionFailedException(); 79 } 80 }