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; 25 26 import jdk.test.lib.jittester.visitors.ByteCodeVisitor; 27 28 import java.io.FileOutputStream; 29 import java.io.FileWriter; 30 import java.io.IOException; 31 import java.io.PrintWriter; 32 import java.nio.file.Files; 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 import java.nio.file.StandardOpenOption; 36 import java.util.function.BiFunction; 37 38 /** 39 * Generates class files from bytecode 40 */ 41 class ByteCodeGenerator implements BiFunction<IRNode, IRNode, String> { 42 private final Path testbase = Paths.get(ProductionParams.testbaseDir.value(), 43 "bytecode_tests"); 44 45 public void writeJtregBytecodeRunner(String name) { 46 try (FileWriter file = new FileWriter(testbase.resolve(name + ".java").toFile())) { 47 file.write(Automatic.getJtregHeader(name, false)); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 53 public String apply(IRNode mainClass, IRNode privateClasses) { 54 Automatic.ensureExisting(testbase); 55 try { 56 ByteCodeVisitor vis = new ByteCodeVisitor(); 57 if (privateClasses != null) { 58 privateClasses.accept(vis); 59 } 60 mainClass.accept(vis); 61 62 Path mainClassPath = testbase.resolve(mainClass.getName() + ".class"); 63 writeToClassFile(mainClassPath, vis.getByteCode(mainClass.getName())); 64 if (privateClasses != null) { 65 privateClasses.getChildren().forEach(c -> { 66 String name = c.getName(); 67 Path classPath = testbase.resolve(name + ".class"); 68 writeToClassFile(classPath, vis.getByteCode(name)); 69 }); 70 } 71 return mainClassPath.toString(); 72 } catch (Throwable t) { 73 Path errFile = testbase.resolve(mainClass.getName() + ".err"); 74 try (PrintWriter pw = new PrintWriter(Files.newOutputStream(errFile, 75 StandardOpenOption.CREATE_NEW))) { 76 t.printStackTrace(pw); 77 } catch (IOException e) { 78 t.printStackTrace(); 79 throw new Error("can't write error to error file " + errFile, e); 80 } 81 return null; 82 } 83 } 84 85 public Path getTestbase() { 86 return testbase; 87 } 88 89 private void writeToClassFile(Path path, byte[] bytecode) { 90 try (FileOutputStream file = new FileOutputStream(path.toString())) { 91 file.write(bytecode); 92 } catch (IOException ex) { 93 ex.printStackTrace(); 94 } 95 } 96 }