43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.jtt.backend;
|
|
26 |
|
46344
|
27 |
import static org.objectweb.asm.Opcodes.ACC_FINAL;
|
|
28 |
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
|
29 |
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
|
30 |
import static org.objectweb.asm.Opcodes.ACC_SUPER;
|
|
31 |
import static org.objectweb.asm.Opcodes.ILOAD;
|
|
32 |
import static org.objectweb.asm.Opcodes.LRETURN;
|
43972
|
33 |
|
46344
|
34 |
import java.lang.reflect.Method;
|
|
35 |
import java.util.ArrayList;
|
|
36 |
import java.util.Collection;
|
|
37 |
import java.util.List;
|
|
38 |
|
|
39 |
import org.graalvm.compiler.debug.GraalError;
|
|
40 |
import org.graalvm.compiler.jtt.JTTTest;
|
58877
|
41 |
import org.graalvm.compiler.api.test.ExportingClassLoader;
|
46344
|
42 |
import org.junit.Before;
|
43972
|
43 |
import org.junit.Test;
|
46344
|
44 |
import org.junit.runner.RunWith;
|
|
45 |
import org.junit.runners.Parameterized;
|
|
46 |
import org.junit.runners.Parameterized.Parameter;
|
|
47 |
import org.junit.runners.Parameterized.Parameters;
|
|
48 |
import org.objectweb.asm.ClassWriter;
|
|
49 |
import org.objectweb.asm.Label;
|
|
50 |
import org.objectweb.asm.MethodVisitor;
|
|
51 |
import org.objectweb.asm.Opcodes;
|
43972
|
52 |
|
46344
|
53 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
54 |
import junit.framework.AssertionFailedError;
|
43972
|
55 |
|
46344
|
56 |
/**
|
|
57 |
* This test let the compiler deal with a large amount of constant data in a method. This data is
|
|
58 |
* stored typically in the constant section of the native method. Especially on the SPARC platform
|
|
59 |
* the backend can address only 8k of memory with an immediate offset. Beyond this barrier, a
|
|
60 |
* different addressing mode must be used.
|
|
61 |
*
|
|
62 |
* In order to do this this test generates a large method containing a large switch statement in
|
|
63 |
* form of
|
|
64 |
*
|
|
65 |
* <code>
|
|
66 |
* static long run(long a) {
|
|
67 |
* switch(a) {
|
|
68 |
* case 1:
|
|
69 |
* return 0xF0F0F0F0F0L + 1;
|
|
70 |
* case 2:
|
|
71 |
* return 0xF0F0F0F0F0L + 2;
|
|
72 |
* ....
|
|
73 |
* default:
|
|
74 |
* return 0;
|
|
75 |
* }
|
|
76 |
*
|
|
77 |
* }
|
|
78 |
* </code>
|
|
79 |
*
|
|
80 |
*/
|
|
81 |
@RunWith(Parameterized.class)
|
43972
|
82 |
public class LargeConstantSectionTest extends JTTTest {
|
|
83 |
private static final String NAME = "LargeConstantSection";
|
|
84 |
private static final long LARGE_CONSTANT = 0xF0F0F0F0F0L;
|
|
85 |
private static LargeConstantClassLoader LOADER;
|
46344
|
86 |
@Parameter(value = 0) public int numberBlocks;
|
43972
|
87 |
|
46344
|
88 |
@Parameters(name = "{0}")
|
|
89 |
public static Collection<Object[]> data() {
|
|
90 |
List<Object[]> parameters = new ArrayList<>();
|
|
91 |
for (int i = 4; i < 13; i += 2) {
|
|
92 |
parameters.add(new Object[]{1 << i});
|
|
93 |
}
|
|
94 |
return parameters;
|
|
95 |
}
|
|
96 |
|
|
97 |
@Before
|
|
98 |
public void before() {
|
43972
|
99 |
LOADER = new LargeConstantClassLoader(LargeConstantSectionTest.class.getClassLoader());
|
|
100 |
}
|
|
101 |
|
46344
|
102 |
public class LargeConstantClassLoader extends ExportingClassLoader {
|
43972
|
103 |
public LargeConstantClassLoader(ClassLoader parent) {
|
|
104 |
super(parent);
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
|
109 |
if (name.equals(NAME)) {
|
|
110 |
ClassWriter cw = new ClassWriter(0);
|
|
111 |
MethodVisitor mv;
|
46344
|
112 |
cw.visit(52, ACC_PUBLIC + ACC_SUPER, NAME, null, "java/lang/Object", null);
|
43972
|
113 |
|
46344
|
114 |
mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "run", "(I)J", null, null);
|
43972
|
115 |
mv.visitCode();
|
46344
|
116 |
Label begin = new Label();
|
|
117 |
mv.visitLabel(begin);
|
|
118 |
mv.visitVarInsn(ILOAD, 0);
|
|
119 |
Label[] labels = new Label[numberBlocks];
|
|
120 |
int[] keys = new int[numberBlocks];
|
|
121 |
for (int i = 0; i < labels.length; i++) {
|
|
122 |
labels[i] = new Label();
|
|
123 |
keys[i] = i;
|
|
124 |
}
|
|
125 |
Label defaultLabel = new Label();
|
|
126 |
mv.visitLookupSwitchInsn(defaultLabel, keys, labels);
|
|
127 |
for (int i = 0; i < labels.length; i++) {
|
|
128 |
mv.visitLabel(labels[i]);
|
|
129 |
mv.visitFrame(Opcodes.F_NEW, 1, new Object[]{Opcodes.INTEGER}, 0, new Object[]{});
|
51436
|
130 |
mv.visitLdcInsn(Long.valueOf(LARGE_CONSTANT + i));
|
43972
|
131 |
mv.visitInsn(LRETURN);
|
|
132 |
}
|
46344
|
133 |
mv.visitLabel(defaultLabel);
|
|
134 |
mv.visitFrame(Opcodes.F_NEW, 1, new Object[]{Opcodes.INTEGER}, 0, new Object[]{});
|
51436
|
135 |
mv.visitLdcInsn(Long.valueOf(3L));
|
43972
|
136 |
mv.visitInsn(LRETURN);
|
46344
|
137 |
Label end = new Label();
|
|
138 |
mv.visitLabel(end);
|
|
139 |
mv.visitLocalVariable("a", "I", null, begin, end, 0);
|
|
140 |
mv.visitMaxs(2, 1);
|
43972
|
141 |
mv.visitEnd();
|
|
142 |
byte[] bytes = cw.toByteArray();
|
|
143 |
return defineClass(name, bytes, 0, bytes.length);
|
|
144 |
} else {
|
|
145 |
return super.findClass(name);
|
|
146 |
}
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
@Test
|
|
151 |
@SuppressWarnings("try")
|
|
152 |
public void run0() throws Exception {
|
46344
|
153 |
test("run", numberBlocks - 3);
|
|
154 |
}
|
|
155 |
|
|
156 |
@Override
|
|
157 |
protected ResolvedJavaMethod getResolvedJavaMethod(String methodName) {
|
|
158 |
try {
|
|
159 |
for (Method method : LOADER.findClass(NAME).getDeclaredMethods()) {
|
|
160 |
if (method.getName().equals(methodName)) {
|
|
161 |
return asResolvedJavaMethod(method);
|
|
162 |
}
|
|
163 |
}
|
|
164 |
} catch (ClassNotFoundException e) {
|
|
165 |
throw new AssertionFailedError("Cannot find class " + NAME);
|
43972
|
166 |
}
|
46344
|
167 |
throw GraalError.shouldNotReachHere();
|
43972
|
168 |
}
|
|
169 |
}
|