|
1 /* |
|
2 * Copyright (c) 2014, 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 import java.lang.reflect.Constructor; |
|
25 import java.lang.reflect.Method; |
|
26 |
|
27 import jdk.internal.org.objectweb.asm.ClassWriter; |
|
28 import jdk.internal.org.objectweb.asm.Label; |
|
29 import jdk.internal.org.objectweb.asm.MethodVisitor; |
|
30 import static jdk.internal.org.objectweb.asm.Opcodes.*; |
|
31 |
|
32 /** |
|
33 * @test |
|
34 * @bug 8051344 |
|
35 * @summary Force OSR compilation with non-empty stack at the OSR entry point. |
|
36 * @compile -XDignore.symbol.file TestOSRWithNonEmptyStack.java |
|
37 * @run main/othervm -XX:CompileOnly=TestCase.test TestOSRWithNonEmptyStack |
|
38 */ |
|
39 public class TestOSRWithNonEmptyStack extends ClassLoader { |
|
40 private static final int CLASS_FILE_VERSION = 52; |
|
41 private static final String CLASS_NAME = "TestCase"; |
|
42 private static final String METHOD_NAME = "test"; |
|
43 private static final int ITERATIONS = 1_000_000; |
|
44 |
|
45 private static byte[] generateTestClass() { |
|
46 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); |
|
47 |
|
48 cw.visit(TestOSRWithNonEmptyStack.CLASS_FILE_VERSION, ACC_PUBLIC, |
|
49 TestOSRWithNonEmptyStack.CLASS_NAME, null, "java/lang/Object", |
|
50 null); |
|
51 |
|
52 TestOSRWithNonEmptyStack.generateConstructor(cw); |
|
53 TestOSRWithNonEmptyStack.generateTestMethod(cw); |
|
54 |
|
55 cw.visitEnd(); |
|
56 return cw.toByteArray(); |
|
57 } |
|
58 |
|
59 private static void generateConstructor(ClassWriter classWriter) { |
|
60 MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", |
|
61 null, null); |
|
62 |
|
63 mv.visitCode(); |
|
64 |
|
65 mv.visitVarInsn(ALOAD, 0); |
|
66 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", |
|
67 false); |
|
68 mv.visitInsn(RETURN); |
|
69 |
|
70 mv.visitMaxs(0, 0); |
|
71 mv.visitEnd(); |
|
72 } |
|
73 |
|
74 private static void generateTestMethod(ClassWriter classWriter) { |
|
75 MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC, |
|
76 TestOSRWithNonEmptyStack.METHOD_NAME, "()V", null, null); |
|
77 Label osrEntryPoint = new Label(); |
|
78 |
|
79 mv.visitCode(); |
|
80 // Push 'this' into stack before OSR entry point to bail out compilation |
|
81 mv.visitVarInsn(ALOAD, 0); |
|
82 // Setup loop counter |
|
83 mv.visitInsn(ICONST_0); |
|
84 mv.visitVarInsn(ISTORE, 1); |
|
85 // Begin loop |
|
86 mv.visitLabel(osrEntryPoint); |
|
87 // Increment loop counter |
|
88 mv.visitVarInsn(ILOAD, 1); |
|
89 mv.visitInsn(ICONST_1); |
|
90 mv.visitInsn(IADD); |
|
91 // Duplicate it for loop condition check |
|
92 mv.visitInsn(DUP); |
|
93 mv.visitVarInsn(ISTORE, 1); |
|
94 // Check loop condition |
|
95 mv.visitLdcInsn(TestOSRWithNonEmptyStack.ITERATIONS); |
|
96 mv.visitJumpInsn(IF_ICMPLT, osrEntryPoint); |
|
97 // Pop 'this'. |
|
98 mv.visitInsn(POP); |
|
99 mv.visitInsn(RETURN); |
|
100 |
|
101 mv.visitMaxs(0, 0); |
|
102 mv.visitEnd(); |
|
103 } |
|
104 |
|
105 private void run() { |
|
106 byte[] bytecode = TestOSRWithNonEmptyStack.generateTestClass(); |
|
107 |
|
108 try { |
|
109 Class klass = defineClass(TestOSRWithNonEmptyStack.CLASS_NAME, |
|
110 bytecode, 0, bytecode.length); |
|
111 |
|
112 Constructor ctor = klass.getConstructor(); |
|
113 Method method = klass.getDeclaredMethod( |
|
114 TestOSRWithNonEmptyStack.METHOD_NAME); |
|
115 |
|
116 Object testCase = ctor.newInstance(); |
|
117 method.invoke(testCase); |
|
118 } catch (Exception e) { |
|
119 throw new RuntimeException( |
|
120 "Test bug: generated class should be valid.", e); |
|
121 } |
|
122 } |
|
123 |
|
124 public static void main(String args[]) { |
|
125 new TestOSRWithNonEmptyStack().run(); |
|
126 } |
|
127 } |