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.Symbol;
  30 import jdk.test.lib.jittester.SymbolTable;
  31 import jdk.test.lib.jittester.VariableInfo;
  32 import jdk.test.lib.jittester.functions.ArgumentDeclaration;
  33 import jdk.test.lib.jittester.functions.ConstructorDefinition;
  34 import jdk.test.lib.jittester.functions.FunctionInfo;
  35 import jdk.test.lib.jittester.types.TypeKlass;
  36 import jdk.test.lib.jittester.types.TypeVoid;
  37 import jdk.test.lib.jittester.utils.PseudoRandom;
  38 
  39 class ConstructorDefinitionFactory extends Factory {
  40     private final long complexityLimit;
  41     private final int statementLimit;
  42     private final int operatorLimit;
  43     private final int memberFunctionsArgLimit;
  44     private final int level;
  45     private final TypeKlass ownerClass;
  46 
  47     ConstructorDefinitionFactory(TypeKlass ownerClass, long complexityLimit, int statementLimit,
  48             int operatorLimit, int memberFunctionsArgLimit, int level) {
  49         this.ownerClass = ownerClass;
  50         this.complexityLimit = complexityLimit;
  51         this.statementLimit = statementLimit;
  52         this.operatorLimit = operatorLimit;
  53         this.memberFunctionsArgLimit = memberFunctionsArgLimit;
  54         this.level = level;
  55     }
  56 
  57     @Override
  58     public IRNode produce() throws ProductionFailedException {
  59         int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit);
  60         ArrayList<VariableInfo> argumentsInfo = new ArrayList<>(argNumber);
  61         ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber);
  62         SymbolTable.push();
  63         IRNode body;
  64         FunctionInfo functionInfo;
  65         try {
  66             int i = 0;
  67             IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClass).setOwnerKlass(ownerClass);
  68             for (; i < argNumber; i++) {
  69                 ArgumentDeclaration d = builder.setVariableNumber(i).getArgumentDeclarationFactory()
  70                         .produce();
  71                 argumentsDeclaration.add(d);
  72                 argumentsInfo.add(d.variableInfo);
  73             }
  74             for (boolean dup = true; dup; i++) {
  75                 /* Check if these is a function with a same signature
  76                 (includes original class name) defined. */
  77                 functionInfo = new FunctionInfo(ownerClass.getName(), ownerClass,
  78                         ownerClass, 0, FunctionInfo.PUBLIC, argumentsInfo);
  79                 dup = false;
  80                 for (Symbol symbol : SymbolTable.get(ownerClass, FunctionInfo.class)) {
  81                     if (functionInfo.equals(symbol)) {
  82                         ArgumentDeclaration argDecl = builder.setVariableNumber(i)
  83                                 .getArgumentDeclarationFactory().produce();
  84                         argumentsDeclaration.add(argDecl);
  85                         argumentsInfo.add(argDecl.variableInfo);
  86                         dup = true;
  87                         break;
  88                     }
  89                 }
  90             }
  91             long blockComplLimit = (long) (PseudoRandom.random() * complexityLimit);
  92             try {
  93                 body = builder.setResultType(new TypeVoid())
  94                         .setComplexityLimit(blockComplLimit)
  95                         .setStatementLimit(statementLimit)
  96                         .setOperatorLimit(operatorLimit)
  97                         .setLevel(level)
  98                         .setSubBlock(true)
  99                         .getBlockFactory()
 100                         .produce();
 101             } catch (ProductionFailedException e) {
 102                 body = null;
 103             }
 104         } finally {
 105             SymbolTable.pop();
 106         }
 107         functionInfo = new FunctionInfo(ownerClass.getName(), ownerClass, ownerClass,
 108                 body != null ? body.complexity() : 0, FunctionInfo.PUBLIC, argumentsInfo);
 109         // If it's all ok, add the function to the symbol table.
 110         SymbolTable.add(functionInfo);
 111         return new ConstructorDefinition(functionInfo, argumentsDeclaration, body);
 112     }
 113 }