Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/jvmci/common/CTVMUtilities.java
41155 views
1
/*
2
* Copyright (c) 2015, 2018, 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 compiler.jvmci.common;
25
26
import jdk.internal.org.objectweb.asm.ClassReader;
27
import jdk.internal.org.objectweb.asm.ClassVisitor;
28
import jdk.internal.org.objectweb.asm.ClassWriter;
29
import jdk.internal.org.objectweb.asm.Label;
30
import jdk.internal.org.objectweb.asm.MethodVisitor;
31
import jdk.internal.org.objectweb.asm.Opcodes;
32
import jdk.internal.org.objectweb.asm.tree.ClassNode;
33
import jdk.test.lib.Utils;
34
import jdk.vm.ci.code.InstalledCode;
35
import jdk.vm.ci.meta.ResolvedJavaMethod;
36
import jdk.vm.ci.hotspot.CompilerToVMHelper;
37
import jdk.vm.ci.hotspot.HotSpotNmethod;
38
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
39
40
import java.io.IOException;
41
import java.lang.reflect.Constructor;
42
import java.lang.reflect.Executable;
43
import java.lang.reflect.Field;
44
import java.lang.reflect.Method;
45
import java.lang.reflect.Modifier;
46
import java.lang.reflect.Parameter;
47
import java.util.HashMap;
48
import java.util.Map;
49
import java.util.TreeMap;
50
51
public class CTVMUtilities {
52
/*
53
* A method to return HotSpotResolvedJavaMethod object using class object
54
* and method as input
55
*/
56
public static HotSpotResolvedJavaMethod getResolvedMethod(Class<?> cls,
57
Executable method) {
58
if (!(method instanceof Method || method instanceof Constructor)) {
59
throw new Error("wrong executable type " + method.getClass());
60
}
61
return CompilerToVMHelper.asResolvedJavaMethod(method);
62
}
63
64
public static HotSpotResolvedJavaMethod getResolvedMethod(
65
Executable method) {
66
return getResolvedMethod(method.getDeclaringClass(), method);
67
}
68
69
public static InstalledCode getInstalledCode(ResolvedJavaMethod method, String name, long address, long entryPoint) {
70
return CompilerToVMHelper.getInstalledCode(method, name, address, entryPoint);
71
}
72
73
public static Map<Integer, Integer> getBciToLineNumber(Executable method) {
74
Map<Integer, Integer> lineNumbers = new TreeMap<>();
75
Class<?> aClass = method.getDeclaringClass();
76
ClassReader cr;
77
try {
78
Module aModule = aClass.getModule();
79
String name = aClass.getName();
80
cr = new ClassReader(aModule.getResourceAsStream(
81
name.replace('.', '/') + ".class"));
82
} catch (IOException e) {
83
throw new Error("TEST BUG: can read " + aClass.getName() + " : " + e, e);
84
}
85
ClassNode cn = new ClassNode();
86
cr.accept(cn, ClassReader.EXPAND_FRAMES);
87
88
Map<Label, Integer> labels = new HashMap<>();
89
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
90
ClassVisitor cv = new ClassVisitorForLabels(cw, labels, method);
91
cr.accept(cv, ClassReader.EXPAND_FRAMES);
92
labels.forEach((k, v) -> lineNumbers.put(k.getOffset(), v));
93
boolean isEmptyMethod = Modifier.isAbstract(method.getModifiers())
94
|| Modifier.isNative(method.getModifiers());
95
if (lineNumbers.isEmpty() && !isEmptyMethod) {
96
throw new Error(method + " doesn't contains the line numbers table "
97
+"(the method marked neither abstract nor native)");
98
}
99
return lineNumbers;
100
}
101
102
private static class ClassVisitorForLabels extends ClassVisitor {
103
private final Map<Label, Integer> lineNumbers;
104
private final String targetName;
105
private final String targetDesc;
106
107
public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,
108
Executable target) {
109
super(Opcodes.ASM7, cw);
110
this.lineNumbers = lines;
111
112
StringBuilder builder = new StringBuilder("(");
113
for (Parameter parameter : target.getParameters()) {
114
builder.append(Utils.toJVMTypeSignature(parameter.getType()));
115
}
116
builder.append(")");
117
if (target instanceof Constructor) {
118
targetName = "<init>";
119
builder.append("V");
120
} else {
121
targetName = target.getName();
122
builder.append(Utils.toJVMTypeSignature(
123
((Method) target).getReturnType()));
124
}
125
targetDesc = builder.toString();
126
}
127
128
@Override
129
public final MethodVisitor visitMethod(int access, String name,
130
String desc, String signature,
131
String[] exceptions) {
132
MethodVisitor mv = cv.visitMethod(access, name, desc, signature,
133
exceptions);
134
if (targetDesc.equals(desc) && targetName.equals(name)) {
135
return new MethodVisitor(Opcodes.ASM7, mv) {
136
@Override
137
public void visitLineNumber(int i, Label label) {
138
super.visitLineNumber(i, label);
139
lineNumbers.put(label, i);
140
}
141
};
142
}
143
return mv;
144
}
145
}
146
}
147
148