1 /* 2 * Copyright (c) 2016, 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.utils; 25 26 import java.util.ArrayList; 27 import java.util.LinkedList; 28 import java.util.List; 29 import jdk.test.lib.jittester.BinaryOperator; 30 import jdk.test.lib.jittester.Block; 31 import jdk.test.lib.jittester.CatchBlock; 32 import jdk.test.lib.jittester.IRNode; 33 import jdk.test.lib.jittester.Literal; 34 import jdk.test.lib.jittester.LocalVariable; 35 import jdk.test.lib.jittester.NonStaticMemberVariable; 36 import jdk.test.lib.jittester.Nothing; 37 import jdk.test.lib.jittester.Operator; 38 import jdk.test.lib.jittester.OperatorKind; 39 import jdk.test.lib.jittester.PrintVariables; 40 import jdk.test.lib.jittester.Statement; 41 import jdk.test.lib.jittester.StaticMemberVariable; 42 import jdk.test.lib.jittester.Symbol; 43 import jdk.test.lib.jittester.TryCatchBlock; 44 import jdk.test.lib.jittester.Type; 45 import jdk.test.lib.jittester.TypeList; 46 import jdk.test.lib.jittester.VariableInfo; 47 import jdk.test.lib.jittester.VariableInitialization; 48 import jdk.test.lib.jittester.functions.ArgumentDeclaration; 49 import jdk.test.lib.jittester.functions.Function; 50 import jdk.test.lib.jittester.functions.FunctionDefinition; 51 import jdk.test.lib.jittester.functions.FunctionInfo; 52 import jdk.test.lib.jittester.functions.Return; 53 import jdk.test.lib.jittester.jtreg.Printer; 54 import jdk.test.lib.jittester.loops.CounterInitializer; 55 import jdk.test.lib.jittester.loops.CounterManipulator; 56 import jdk.test.lib.jittester.loops.For; 57 import jdk.test.lib.jittester.loops.Loop; 58 import jdk.test.lib.jittester.loops.LoopingCondition; 59 import jdk.test.lib.jittester.types.TypeArray; 60 import jdk.test.lib.jittester.types.TypeKlass; 61 62 public class FixedTrees { 63 public static FunctionDefinition printVariablesAsFunction(PrintVariables node) { 64 TypeKlass owner = node.getOwner(); 65 66 ArrayList<IRNode> nodes = new ArrayList<>(); 67 68 VariableInfo resultInfo = new VariableInfo("result", node.getOwner(), TypeList.STRING, VariableInfo.LOCAL); 69 nodes.add(new Statement(new VariableInitialization(resultInfo, new Literal("[", TypeList.STRING)), true)); 70 LocalVariable resultVar = new LocalVariable(resultInfo); 71 72 List<Symbol> vars = node.getVars(); 73 74 TypeKlass printerKlass = new TypeKlass(Printer.class.getName()); 75 Literal EOL = new Literal("\n", TypeList.STRING); 76 VariableInfo thisInfo = new VariableInfo("this", node.getOwner(), 77 node.getOwner(), VariableInfo.LOCAL | VariableInfo.INITIALIZED); 78 79 LocalVariable thisVar = new LocalVariable(thisInfo); 80 81 for (int i = 0; i < vars.size(); i++) { 82 Symbol v = vars.get(i); 83 nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, 84 new Literal(v.owner.getName() + "." + v.name + " = ", TypeList.STRING)), true)); 85 VariableInfo argInfo = new VariableInfo("arg", printerKlass, 86 v.type instanceof TypeKlass ? TypeList.OBJECT : v.type, 87 VariableInfo.LOCAL | VariableInfo.INITIALIZED); 88 FunctionInfo printInfo = new FunctionInfo("print", printerKlass, 89 TypeList.STRING, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC, argInfo); 90 Function call = new Function(owner, printInfo, null); 91 VariableInfo varInfo = new VariableInfo(v.name, v.owner, v.type, v.flags); 92 if (v.isStatic()) { 93 call.addChild(new StaticMemberVariable(v.owner, varInfo)); 94 } else { 95 call.addChild(new NonStaticMemberVariable(thisVar, varInfo)); 96 } 97 nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, 98 call), true)); 99 if (i < vars.size() - 1) { 100 nodes.add(new Statement(new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, 101 EOL), true)); 102 } 103 } 104 nodes.add(new Statement( 105 new BinaryOperator(OperatorKind.COMPOUND_ADD, TypeList.STRING, resultVar, new Literal("]\n", TypeList.STRING)), 106 true)); 107 108 Block block = new Block(node.getOwner(), TypeList.STRING, nodes, 1); 109 FunctionInfo toStringInfo = new FunctionInfo("toString", owner, TypeList.STRING, 0L, FunctionInfo.PUBLIC, thisInfo); 110 return new FunctionDefinition(toStringInfo, new ArrayList<>(), block, new Return(resultVar)); 111 } 112 public static FunctionDefinition generateMainOrExecuteMethod(TypeKlass owner, boolean isMain) { 113 Nothing nothing = new Nothing(); 114 ArrayList<IRNode> testCallNodeContent = new ArrayList<>(); 115 VariableInfo tInfo = new VariableInfo("t", owner, owner, VariableInfo.LOCAL); 116 LocalVariable tVar = new LocalVariable(tInfo); 117 Function testCallNode = new Function(owner, new FunctionInfo("test", owner, TypeList.VOID, 118 0L, FunctionInfo.PRIVATE, tInfo), null); 119 testCallNode.addChild(tVar); 120 testCallNodeContent.add(new Statement(testCallNode, true)); 121 // { t.test(); } 122 Block testCallNodeBlock = new Block(owner, TypeList.VOID, testCallNodeContent, 4); 123 124 IRNode tryNode = testCallNodeBlock; 125 if (isMain) { 126 VariableInfo iInfo = new VariableInfo("i", owner, TypeList.INT, VariableInfo.LOCAL); 127 LocalVariable iVar = new LocalVariable(iInfo); 128 Operator increaseCounter = new BinaryOperator(OperatorKind.ASSIGN, TypeList.INT, 129 iVar, 130 new BinaryOperator(OperatorKind.ADD, TypeList.INT, 131 iVar, new Literal(1, TypeList.INT))); 132 Loop loop = new Loop(); 133 Block emptyBlock = new Block(owner, TypeList.VOID, new LinkedList<>(), 3); 134 loop.initialization = new CounterInitializer(iInfo, new Literal(0, TypeList.INT)); 135 loop.manipulator = new CounterManipulator(new Statement(increaseCounter, false)); 136 loop.condition = new LoopingCondition(new BinaryOperator(OperatorKind.LT, TypeList.BOOLEAN, iVar, 137 new Literal(150000, TypeList.INT))); 138 For forNode = new For(4, loop, 150000, emptyBlock, new Statement(nothing, false), 139 new Statement(nothing, false), testCallNodeBlock, emptyBlock, emptyBlock); 140 tryNode = forNode; 141 } 142 143 FunctionInfo constrInfo = new FunctionInfo(owner.getName(), owner, owner, 0, FunctionInfo.PUBLIC); 144 Function testConstructor = new Function(owner, constrInfo, null); 145 // Test t = new Test() 146 VariableInitialization testInit = new VariableInitialization(tInfo, testConstructor); 147 148 TypeKlass throwableKlass = new TypeKlass("java.lang.Throwable"); 149 List<Type> throwables = new ArrayList<>(); 150 throwables.add(throwableKlass); 151 152 VariableInfo exInfo = new VariableInfo("ex", owner, throwableKlass, 153 VariableInfo.LOCAL | VariableInfo.INITIALIZED); 154 FunctionInfo printStackTraceInfo = new FunctionInfo("printStackTrace", throwableKlass, 155 TypeList.VOID, 0, FunctionInfo.PUBLIC, exInfo); 156 Function printStackTraceCall = new Function(throwableKlass, printStackTraceInfo, null); 157 printStackTraceCall.addChild(new LocalVariable(exInfo)); 158 ArrayList<IRNode> printStackTraceCallBlockContent = new ArrayList<>(); 159 // { ex.printStackTrace(); } 160 printStackTraceCallBlockContent.add(new Statement(printStackTraceCall, true)); 161 162 Block printStackTraceCallBlock = new Block(owner, TypeList.VOID, printStackTraceCallBlockContent, 3); 163 List<CatchBlock> catchBlocks1 = new ArrayList<>(); 164 catchBlocks1.add(new CatchBlock(printStackTraceCallBlock, throwables, 3)); 165 List<CatchBlock> catchBlocks2 = new ArrayList<>(); 166 catchBlocks2.add(new CatchBlock(printStackTraceCallBlock, throwables, 3)); 167 List<CatchBlock> catchBlocks3 = new ArrayList<>(); 168 catchBlocks3.add(new CatchBlock(printStackTraceCallBlock, throwables, 2)); 169 170 TryCatchBlock tryCatch1 = new TryCatchBlock(tryNode, nothing, catchBlocks1, 3); 171 TypeKlass printStreamKlass = new TypeKlass("java.io.PrintStream"); 172 TypeKlass systemKlass = new TypeKlass("java.lang.System"); 173 FunctionInfo systemOutPrintInfo = new FunctionInfo("print", printStreamKlass, 174 TypeList.VOID, 0, FunctionInfo.PUBLIC, 175 new VariableInfo("this", owner, printStreamKlass, VariableInfo.LOCAL | VariableInfo.INITIALIZED), 176 new VariableInfo("t", owner, TypeList.OBJECT, 177 VariableInfo.LOCAL | VariableInfo.INITIALIZED)); 178 List<IRNode> printArgs = new ArrayList<>(); 179 VariableInfo systemOutInfo = new VariableInfo("out", systemKlass, printStreamKlass, 180 VariableInfo.STATIC | VariableInfo.PUBLIC); 181 StaticMemberVariable systemOutVar = new StaticMemberVariable(owner, systemOutInfo); 182 printArgs.add(systemOutVar); 183 printArgs.add(tVar); 184 Function print = new Function(printStreamKlass, systemOutPrintInfo, printArgs); 185 ArrayList<IRNode> printBlockContent = new ArrayList<>(); 186 printBlockContent.add(new Statement(print, true)); 187 Block printBlock = new Block(owner, TypeList.VOID, printBlockContent, 3); 188 TryCatchBlock tryCatch2 = new TryCatchBlock(printBlock, nothing, catchBlocks2, 3); 189 190 List<IRNode> mainTryCatchBlockContent = new ArrayList<>(); 191 mainTryCatchBlockContent.add(new Statement(testInit, true)); 192 mainTryCatchBlockContent.add(tryCatch1); 193 mainTryCatchBlockContent.add(tryCatch2); 194 Block mainTryCatchBlock = new Block(owner, TypeList.VOID, mainTryCatchBlockContent, 2); 195 TryCatchBlock mainTryCatch = new TryCatchBlock(mainTryCatchBlock, nothing, catchBlocks3, 2); 196 ArrayList<IRNode> bodyContent = new ArrayList<>(); 197 bodyContent.add(mainTryCatch); 198 Block funcBody = new Block(owner, TypeList.VOID, bodyContent, 1); 199 200 // static main(String[] args)V or static execute()V 201 VariableInfo mainArgs = new VariableInfo("args", owner, 202 new TypeArray(TypeList.STRING, 1), VariableInfo.LOCAL); 203 FunctionInfo fInfo = isMain 204 ? new FunctionInfo("main", owner, TypeList.VOID, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC, mainArgs) 205 : new FunctionInfo("execute", owner, TypeList.VOID, 0, FunctionInfo.PUBLIC | FunctionInfo.STATIC); 206 ArrayList<ArgumentDeclaration> argDecl = new ArrayList<>(); 207 if (isMain) { 208 argDecl.add(new ArgumentDeclaration(mainArgs)); 209 } 210 return new FunctionDefinition(fInfo, argDecl, funcBody, new Return(nothing)); 211 } 212 }