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.IRNode;
  28 import jdk.test.lib.jittester.NonStaticMemberVariable;
  29 import jdk.test.lib.jittester.ProductionFailedException;
  30 import jdk.test.lib.jittester.Symbol;
  31 import jdk.test.lib.jittester.SymbolTable;
  32 import jdk.test.lib.jittester.Type;
  33 import jdk.test.lib.jittester.VariableInfo;
  34 import jdk.test.lib.jittester.types.TypeKlass;
  35 import jdk.test.lib.jittester.utils.PseudoRandom;
  36 
  37 class NonStaticMemberVariableFactory extends Factory {
  38     private final Type type;
  39     private final int flags;
  40     private final long complexityLimit;
  41     private final int operatorLimit;
  42     private final boolean exceptionSafe;
  43     private final Type ownerClass;
  44 
  45     NonStaticMemberVariableFactory(long complexityLimit, int operatorLimit,
  46             TypeKlass ownerClass, Type type, int flags, boolean exceptionSafe) {
  47         this.ownerClass = ownerClass;
  48         this.type = type;
  49         this.flags = flags;
  50         this.complexityLimit = complexityLimit;
  51         this.operatorLimit = operatorLimit;
  52         this.exceptionSafe = exceptionSafe;
  53     }
  54 
  55     @Override
  56     public IRNode produce() throws ProductionFailedException {
  57         // Get the variables of the requested type from SymbolTable
  58         ArrayList<Symbol> variables = new ArrayList<>(SymbolTable.get(type, VariableInfo.class));
  59         if (!variables.isEmpty()) {
  60             PseudoRandom.shuffle(variables);
  61             IRNodeBuilder builder = new IRNodeBuilder().setComplexityLimit(complexityLimit)
  62                     .setOperatorLimit(operatorLimit)
  63                     .setOwnerKlass((TypeKlass) ownerClass)
  64                     .setExceptionSafe(exceptionSafe)
  65                     .setNoConsts(false);
  66             for (Symbol symbol : variables) {
  67                 VariableInfo varInfo = (VariableInfo) symbol;
  68                 if ((varInfo.flags & VariableInfo.FINAL) == (flags & VariableInfo.FINAL)
  69                         && (varInfo.flags & VariableInfo.INITIALIZED) == (flags & VariableInfo.INITIALIZED)
  70                         && (varInfo.flags & VariableInfo.STATIC) == 0
  71                         && (varInfo.flags & VariableInfo.LOCAL) == 0) {
  72                     try {
  73                         IRNode object = builder.setResultType(varInfo.klass)
  74                                 .getExpressionFactory().produce();
  75                         return new NonStaticMemberVariable(object, varInfo);
  76                     } catch (ProductionFailedException e) {
  77                     }
  78                 }
  79             }
  80         }
  81         throw new ProductionFailedException();
  82     }
  83 }