|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +/* |
| 25 | + * @test |
| 26 | + * @summary Example test to use the Compile Framework together with the IR Framework (i.e. TestFramework), |
| 27 | + * and the VectorAPI. |
| 28 | + * @modules java.base/jdk.internal.misc |
| 29 | + * @modules jdk.incubator.vector |
| 30 | + * @library /test/lib / |
| 31 | + * @compile ../../../compiler/lib/ir_framework/TestFramework.java |
| 32 | + * @run driver compile_framework.examples.IRFrameworkWithVectorAPIExample |
| 33 | + */ |
| 34 | + |
| 35 | +package compile_framework.examples; |
| 36 | + |
| 37 | +import compiler.lib.compile_framework.*; |
| 38 | +import jdk.test.lib.Utils; |
| 39 | +import jdk.incubator.vector.IntVector; |
| 40 | +import jdk.test.lib.Platform; |
| 41 | +import java.lang.reflect.InvocationTargetException; |
| 42 | + |
| 43 | +/** |
| 44 | + * This test shows that the IR verification can be done on code compiled by the Compile Framework. |
| 45 | + * The "@compile" command for JTREG is required so that the IRFramework is compiled, other javac |
| 46 | + * might not compile it because it is not present in the class, only in the dynamically compiled |
| 47 | + * code. |
| 48 | + * <p> |
| 49 | + * Additionally, we must set the classpath for the Test-VM, so that it has access to all compiled |
| 50 | + * classes (see {@link CompileFramework#getEscapedClassPathOfCompiledClasses}). |
| 51 | + */ |
| 52 | +public class IRFrameworkWithVectorAPIExample { |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | + // Create a new CompileFramework instance. |
| 56 | + CompileFramework comp = new CompileFramework(); |
| 57 | + |
| 58 | + // Add a java source file. |
| 59 | + comp.addJavaSourceCode("InnerTest", generateInnerTest(comp)); |
| 60 | + |
| 61 | + // Compile the source file. "javac" needs to know that it is ok to compile with the |
| 62 | + // VectorAPI module. |
| 63 | + comp.compile("--add-modules=jdk.incubator.vector"); |
| 64 | + |
| 65 | + // InnerTest.main(); |
| 66 | + comp.invoke("InnerTest", "main", new Object[] {null}); |
| 67 | + } |
| 68 | + |
| 69 | + // Generate a source java file as String |
| 70 | + public static String generateInnerTest(CompileFramework comp) { |
| 71 | + return String.format(""" |
| 72 | + import compiler.lib.ir_framework.*; |
| 73 | + import jdk.incubator.vector.*; |
| 74 | +
|
| 75 | + public class InnerTest { |
| 76 | + public static void main(String args[]) { |
| 77 | + TestFramework framework = new TestFramework(InnerTest.class); |
| 78 | + // Also the TestFramework must allow the test VM to see the VectorAPI module. |
| 79 | + framework.addFlags("-classpath", "%s", "--add-modules=jdk.incubator.vector"); |
| 80 | + framework.start(); |
| 81 | + } |
| 82 | +
|
| 83 | + @Test |
| 84 | + static Object test() { |
| 85 | + return IntVector.broadcast(IntVector.SPECIES_64, 42); |
| 86 | + } |
| 87 | + } |
| 88 | + """, comp.getEscapedClassPathOfCompiledClasses()); |
| 89 | + } |
| 90 | +} |
0 commit comments