--- /dev/null 2016-05-10 17:52:27.219330981 +0300 +++ new/test/testlibrary/jittester/src/jdk/test/lib/jittester/utils/FixedTrees.java 2016-05-12 04:25:00.674334322 +0300 @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.test.lib.jittester.utils; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import jdk.test.lib.jittester.BinaryOperator; +import jdk.test.lib.jittester.Block; +import jdk.test.lib.jittester.CatchBlock; +import jdk.test.lib.jittester.IRNode; +import jdk.test.lib.jittester.Literal; +import jdk.test.lib.jittester.LocalVariable; +import jdk.test.lib.jittester.NonStaticMemberVariable; +import jdk.test.lib.jittester.Nothing; +import jdk.test.lib.jittester.Operator; +import jdk.test.lib.jittester.OperatorKind; +import jdk.test.lib.jittester.PrintVariables; +import jdk.test.lib.jittester.ProductionFailedException; +import jdk.test.lib.jittester.Statement; +import jdk.test.lib.jittester.StaticMemberVariable; +import jdk.test.lib.jittester.Symbol; +import jdk.test.lib.jittester.TryCatchBlock; +import jdk.test.lib.jittester.Type; +import jdk.test.lib.jittester.TypeList; +import jdk.test.lib.jittester.VariableInfo; +import jdk.test.lib.jittester.VariableInitialization; +import jdk.test.lib.jittester.functions.ArgumentDeclaration; +import jdk.test.lib.jittester.functions.Function; +import jdk.test.lib.jittester.functions.FunctionDefinition; +import jdk.test.lib.jittester.functions.FunctionInfo; +import jdk.test.lib.jittester.functions.Return; +import jdk.test.lib.jittester.jtreg.Printer; +import jdk.test.lib.jittester.loops.CounterInitializer; +import jdk.test.lib.jittester.loops.CounterManipulator; +import jdk.test.lib.jittester.loops.For; +import jdk.test.lib.jittester.loops.Loop; +import jdk.test.lib.jittester.loops.LoopingCondition; +import jdk.test.lib.jittester.types.TypeArray; +import jdk.test.lib.jittester.types.TypeKlass; + +public class FixedTrees { + public static FunctionDefinition printVariablesAsFunction(PrintVariables node) { + TypeKlass owner = node.getOwner(); + + ArrayList nodes = new ArrayList<>(); + + VariableInfo resultInfo = new VariableInfo("result", node.getOwner(), TypeList.STRING, VariableInfo.LOCAL); + nodes.add(new Statement(new VariableInitialization(resultInfo, new Literal("[", TypeList.STRING)), true)); + LocalVariable resultVar = new LocalVariable(resultInfo); + + List vars = node.getVars(); + + TypeKlass printerKlass = new TypeKlass(Printer.class.getName()); + Literal EOL = new Literal("\n", TypeList.STRING); + VariableInfo thisInfo = new VariableInfo("this", node.getOwner(), + node.getOwner(), VariableInfo.LOCAL | VariableInfo.INITIALIZED); + + LocalVariable thisVar = new LocalVariable(thisInfo); + + for (int i = 0; i < vars.size(); i++) { + Symbol v = vars.get(i); + nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, + new Literal(v.owner.getName() + "." + v.name + " = ", TypeList.STRING)), true)); + VariableInfo argInfo = new VariableInfo("arg", printerKlass, + v.type instanceof TypeKlass ? TypeList.OBJECT : v.type, + VariableInfo.LOCAL | VariableInfo.INITIALIZED); + FunctionInfo printInfo = new FunctionInfo("print", printerKlass, + TypeList.STRING, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC, argInfo); + Function call = new Function(owner, printInfo, null); + VariableInfo varInfo = new VariableInfo(v.name, v.owner, v.type, v.flags); + if (v.isStatic()) { + call.addChild(new StaticMemberVariable(v.owner, varInfo)); + } else { + call.addChild(new NonStaticMemberVariable(thisVar, varInfo)); + } + nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, + call), true)); + if (i < vars.size() - 1) { + nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, + EOL), true)); + } + } + nodes.add(new Statement( + new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, new Literal("]\n", TypeList.STRING)), + true)); + + Block block = new Block(node.getOwner(), TypeList.STRING, nodes, 1); + FunctionInfo toStringInfo = new FunctionInfo("toString", owner, TypeList.STRING, 0L, FunctionInfo.PUBLIC, thisInfo); + return new FunctionDefinition(toStringInfo, new ArrayList<>(), block, new Return(resultVar)); + } + public static FunctionDefinition generateMainOrExecuteMethod(TypeKlass owner, boolean isMain) { + Nothing nothing = new Nothing(); + ArrayList testCallNodeContent = new ArrayList<>(); + VariableInfo tInfo = new VariableInfo("t", owner, owner, VariableInfo.LOCAL); + LocalVariable tVar = new LocalVariable(tInfo); + Function testCallNode = new Function(owner, new FunctionInfo("test", owner, TypeList.VOID, + 0L, FunctionInfo.PRIVATE, tInfo), null); + testCallNode.addChild(tVar); + testCallNodeContent.add(new Statement(testCallNode, true)); + // { t.test(); } + Block testCallNodeBlock = new Block(owner, TypeList.VOID, testCallNodeContent, 4); + + IRNode tryNode = testCallNodeBlock; + if (isMain) { + VariableInfo iInfo = new VariableInfo("i", owner, TypeList.INT, VariableInfo.LOCAL); + LocalVariable iVar = new LocalVariable(iInfo); + Operator increaseCounter = new BinaryOperator(OperatorKind.ASSIGN, TypeList.INT, + iVar, + new BinaryOperator(OperatorKind.ADD, TypeList.INT, + iVar, new Literal(1, TypeList.INT))); + Loop loop = new Loop(); + Block emptyBlock = new Block(owner, TypeList.VOID, new LinkedList<>(), 3); + loop.initialization = new CounterInitializer(iInfo, new Literal(0, TypeList.INT)); + loop.manipulator = new CounterManipulator(new Statement(increaseCounter, false)); + loop.condition = new LoopingCondition(new BinaryOperator(OperatorKind.LT, TypeList.BOOLEAN, iVar, + new Literal(150000, TypeList.INT))); + For forNode = new For(4, loop, 150000, emptyBlock, new Statement(nothing, false), + new Statement(nothing, false), testCallNodeBlock, emptyBlock, emptyBlock); + tryNode = forNode; + } + + FunctionInfo constrInfo = new FunctionInfo(owner.getName(), owner, owner, 0, FunctionInfo.PUBLIC); + Function testConstructor = new Function(owner, constrInfo, null); + // Test t = new Test() + VariableInitialization testInit = new VariableInitialization(tInfo, testConstructor); + + TypeKlass throwableKlass = new TypeKlass("java.lang.Throwable"); + List throwables = new ArrayList<>(); + throwables.add(throwableKlass); + + VariableInfo exInfo = new VariableInfo("ex", owner, throwableKlass, + VariableInfo.LOCAL | VariableInfo.INITIALIZED); + FunctionInfo printStackTraceInfo = new FunctionInfo("printStackTrace", throwableKlass, + TypeList.VOID, 0, FunctionInfo.PUBLIC, exInfo); + Function printStackTraceCall = new Function(throwableKlass, printStackTraceInfo, null); + printStackTraceCall.addChild(new LocalVariable(exInfo)); + ArrayList printStackTraceCallBlockContent = new ArrayList<>(); + // { ex.printStackTrace(); } + printStackTraceCallBlockContent.add(new Statement(printStackTraceCall, true)); + + Block printStackTraceCallBlock = new Block(owner, TypeList.VOID, printStackTraceCallBlockContent, 3); + List catchBlocks1 = new ArrayList<>(); + catchBlocks1.add(new CatchBlock(printStackTraceCallBlock, throwables, 3)); + List catchBlocks2 = new ArrayList<>(); + catchBlocks2.add(new CatchBlock(printStackTraceCallBlock, throwables, 3)); + List catchBlocks3 = new ArrayList<>(); + catchBlocks3.add(new CatchBlock(printStackTraceCallBlock, throwables, 2)); + + TryCatchBlock tryCatch1 = new TryCatchBlock(tryNode, nothing, catchBlocks1, 3); + TypeKlass printStreamKlass = new TypeKlass("java.io.PrintStream"); + TypeKlass systemKlass = new TypeKlass("java.lang.System"); + FunctionInfo systemOutPrintlnInfo = new FunctionInfo("println", printStreamKlass, + TypeList.VOID, 0, FunctionInfo.PUBLIC, + new VariableInfo("this", owner, printStreamKlass, VariableInfo.LOCAL | VariableInfo.INITIALIZED), + new VariableInfo("t", owner, TypeList.OBJECT, + VariableInfo.LOCAL | VariableInfo.INITIALIZED)); + List printlnArgs = new ArrayList<>(); + VariableInfo systemOutInfo = new VariableInfo("out", systemKlass, printStreamKlass, + VariableInfo.STATIC | VariableInfo.PUBLIC); + StaticMemberVariable systemOutVar = new StaticMemberVariable(owner, systemOutInfo); + printlnArgs.add(systemOutVar); + printlnArgs.add(tVar); + Function println = new Function(printStreamKlass, systemOutPrintlnInfo, printlnArgs); + ArrayList printlnBlockContent = new ArrayList<>(); + printlnBlockContent.add(new Statement(println, true)); + Block printlnBlock = new Block(owner, TypeList.VOID, printlnBlockContent, 3); + TryCatchBlock tryCatch2 = new TryCatchBlock(printlnBlock, nothing, catchBlocks2, 3); + + List mainTryCatchBlockContent = new ArrayList<>(); + mainTryCatchBlockContent.add(new Statement(testInit, true)); + mainTryCatchBlockContent.add(tryCatch1); + mainTryCatchBlockContent.add(tryCatch2); + Block mainTryCatchBlock = new Block(owner, TypeList.VOID, mainTryCatchBlockContent, 2); + TryCatchBlock mainTryCatch = new TryCatchBlock(mainTryCatchBlock, nothing, catchBlocks3, 2); + ArrayList bodyContent = new ArrayList<>(); + bodyContent.add(mainTryCatch); + Block funcBody = new Block(owner, TypeList.VOID, bodyContent, 1); + + // static main(String[] args)V or static execute()V + VariableInfo mainArgs = new VariableInfo("args", owner, + new TypeArray(TypeList.STRING, 1), VariableInfo.LOCAL); + FunctionInfo fInfo = isMain + ? new FunctionInfo("main", owner, TypeList.VOID, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC, mainArgs) + : new FunctionInfo("execute", owner, TypeList.VOID, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC); + ArrayList argDecl = new ArrayList<>(); + if (isMain) { + argDecl.add(new ArgumentDeclaration(mainArgs)); + } + return new FunctionDefinition(fInfo, argDecl, funcBody, new Return(nothing)); + } +}