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.ProductionFailedException;
  29 import jdk.test.lib.jittester.ProductionParams;
  30 import jdk.test.lib.jittester.Symbol;
  31 import jdk.test.lib.jittester.SymbolTable;
  32 import jdk.test.lib.jittester.VariableInfo;
  33 import jdk.test.lib.jittester.functions.FunctionDefinitionBlock;
  34 import jdk.test.lib.jittester.functions.FunctionInfo;
  35 import jdk.test.lib.jittester.types.TypeKlass;
  36 import jdk.test.lib.jittester.utils.PseudoRandom;
  37 
  38 class FunctionDefinitionBlockFactory extends Factory<FunctionDefinitionBlock> {
  39     private final long complexityLimit;
  40     private final int statementLimit;
  41     private final int operatorLimit;
  42     private final int memberFunctionsLimit;
  43     private final int memberFunctionsArgLimit;
  44     private final int initialFlags;
  45     private final int level;
  46     private final TypeKlass ownerClass;
  47 
  48     FunctionDefinitionBlockFactory(TypeKlass ownerClass, int memberFunctionsLimit,
  49             int memberFunctionsArgLimit, long complexityLimit, int statementLimit,
  50             int operatorLimit, int level, int initialFlags) {
  51         this.ownerClass = ownerClass;
  52         this.memberFunctionsLimit = memberFunctionsLimit;
  53         this.memberFunctionsArgLimit = memberFunctionsArgLimit;
  54         this.complexityLimit = complexityLimit;
  55         this.statementLimit = statementLimit;
  56         this.operatorLimit = operatorLimit;
  57         this.level = level;
  58         this.initialFlags = initialFlags;
  59     }
  60 
  61     @Override
  62     public FunctionDefinitionBlock produce() throws ProductionFailedException {
  63         ArrayList<IRNode> content = new ArrayList<>();
  64         int memFunLimit = (int) (PseudoRandom.random() * memberFunctionsLimit);
  65         if (memFunLimit > 0) {
  66             long memFunCompl = complexityLimit / memFunLimit;
  67             IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass)
  68                     .setComplexityLimit(memFunCompl)
  69                     .setStatementLimit(statementLimit)
  70                     .setOperatorLimit(operatorLimit)
  71                     .setMemberFunctionsArgLimit(memberFunctionsArgLimit)
  72                     .setLevel(level);
  73             for (int i = 0; i < memFunLimit; i++) {
  74                 int flags = initialFlags;
  75                 if (PseudoRandom.randomBoolean()) {
  76                     flags |= FunctionInfo.STATIC;
  77                 }
  78                 if (!ProductionParams.disableFinalMethods.value() && PseudoRandom.randomBoolean()) {
  79                     flags |= FunctionInfo.FINAL;
  80                 }
  81                 if (PseudoRandom.randomBoolean()) {
  82                     flags |= FunctionInfo.NONRECURSIVE;
  83                 }
  84                 if (PseudoRandom.randomBoolean()) {
  85                     flags |= FunctionInfo.SYNCHRONIZED;
  86                 }
  87                 switch ((int) (PseudoRandom.random() * 4)) {
  88                     case 0:
  89                         flags |= FunctionInfo.PRIVATE;
  90                         break;
  91                     case 1:
  92                         flags |= FunctionInfo.PROTECTED;
  93                         break;
  94                     case 2:
  95                         flags |= FunctionInfo.DEFAULT;
  96                         break;
  97                     case 3:
  98                         flags |= FunctionInfo.PUBLIC;
  99                         break;
 100                 }
 101                 Symbol thisSymbol = null;
 102                 if ((flags & FunctionInfo.STATIC) > 0) {
 103                     thisSymbol = SymbolTable.get("this", VariableInfo.class);
 104                     SymbolTable.remove(thisSymbol);
 105                 }
 106                 try {
 107                     content.add(builder.setName("func_" + i)
 108                             .setFlags(flags)
 109                             .getFunctionDefinitionFactory()
 110                             .produce());
 111                 } catch (ProductionFailedException e) {
 112                 }
 113                 if ((flags & FunctionInfo.STATIC) > 0) {
 114                     SymbolTable.add(thisSymbol);
 115                 }
 116             }
 117         }
 118         return new FunctionDefinitionBlock(content, level, ownerClass);
 119     }
 120 }