42650
|
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.tools.jaotc;
|
|
25 |
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.HashMap;
|
|
28 |
import java.util.Map;
|
|
29 |
|
|
30 |
import jdk.tools.jaotc.binformat.BinaryContainer;
|
|
31 |
import jdk.tools.jaotc.binformat.ReadOnlyDataContainer;
|
|
32 |
import jdk.tools.jaotc.binformat.Symbol.Binding;
|
|
33 |
import jdk.tools.jaotc.binformat.Symbol.Kind;
|
|
34 |
|
|
35 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
|
|
36 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
37 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
38 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Class encapsulating Graal-compiled output of a Java class. The compilation result of all methods
|
|
42 |
* of a class {@code className} are maintained in an array list.
|
|
43 |
*/
|
|
44 |
public class AOTCompiledClass {
|
|
45 |
|
|
46 |
public static class AOTKlassData {
|
|
47 |
int gotIndex; // Index (offset/8) to the got in the .metaspace.got section
|
|
48 |
int classId; // Unique ID
|
|
49 |
// Offset to compiled methods data in the .methods.offsets section.
|
|
50 |
int compiledMethodsOffset;
|
|
51 |
// Offset to dependent methods data.
|
|
52 |
int dependentMethodsOffset;
|
|
53 |
long fingerprint; // Class fingerprint
|
|
54 |
|
|
55 |
private final String name;
|
|
56 |
private boolean isArray;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* List of dependent compiled methods which have a reference to this class.
|
|
60 |
*/
|
|
61 |
private ArrayList<CompiledMethodInfo> dependentMethods;
|
|
62 |
|
|
63 |
public AOTKlassData(BinaryContainer binaryContainer, String name, long fingerprint, int classId) {
|
|
64 |
this.dependentMethods = new ArrayList<>();
|
|
65 |
this.classId = classId;
|
|
66 |
this.fingerprint = fingerprint;
|
|
67 |
this.gotIndex = binaryContainer.addTwoSlotMetaspaceSymbol(name);
|
|
68 |
this.compiledMethodsOffset = -1; // Not compiled classes do not have compiled methods.
|
|
69 |
this.dependentMethodsOffset = -1;
|
|
70 |
this.name = name;
|
|
71 |
this.isArray = name.length() > 0 && name.charAt(0) == '[';
|
|
72 |
}
|
|
73 |
|
|
74 |
public long getFingerprint() {
|
|
75 |
return fingerprint;
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Add a method to the list of dependent methods.
|
|
80 |
*/
|
|
81 |
public synchronized boolean addDependentMethod(CompiledMethodInfo cm) {
|
|
82 |
return dependentMethods.add(cm);
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Return the array list of dependent class methods.
|
|
87 |
*
|
|
88 |
* @return array list of dependent methods
|
|
89 |
*/
|
|
90 |
public ArrayList<CompiledMethodInfo> getDependentMethods() {
|
|
91 |
return dependentMethods;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Returns if this class has dependent methods.
|
|
96 |
*
|
|
97 |
* @return true if dependent methods exist, false otherwise
|
|
98 |
*/
|
|
99 |
public boolean hasDependentMethods() {
|
|
100 |
return !dependentMethods.isEmpty();
|
|
101 |
}
|
|
102 |
|
|
103 |
public void setCompiledMethodsOffset(int offset) {
|
|
104 |
compiledMethodsOffset = offset;
|
|
105 |
}
|
|
106 |
|
|
107 |
protected void putAOTKlassData(BinaryContainer binaryContainer, ReadOnlyDataContainer container) {
|
|
108 |
int cntDepMethods = dependentMethods.size();
|
|
109 |
// Create array of dependent methods IDs. First word is count.
|
|
110 |
ReadOnlyDataContainer dependenciesContainer = binaryContainer.getKlassesDependenciesContainer();
|
|
111 |
this.dependentMethodsOffset = binaryContainer.addMethodsCount(cntDepMethods, dependenciesContainer);
|
|
112 |
for (CompiledMethodInfo methodInfo : dependentMethods) {
|
|
113 |
dependenciesContainer.appendInt(methodInfo.getCodeId());
|
|
114 |
}
|
|
115 |
verify();
|
|
116 |
|
|
117 |
// @formatter:off
|
|
118 |
/*
|
|
119 |
* The offsets layout should match AOTKlassData structure in AOT JVM runtime
|
|
120 |
*/
|
|
121 |
int offset = container.getByteStreamSize();
|
|
122 |
container.createSymbol(offset, Kind.OBJECT, Binding.GLOBAL, 0, name);
|
|
123 |
// Add index (offset/8) to the got in the .metaspace.got section
|
|
124 |
container.appendInt(gotIndex).
|
|
125 |
// Add unique ID
|
|
126 |
appendInt(classId).
|
|
127 |
// Add the offset to compiled methods data in the .metaspace.offsets section.
|
|
128 |
appendInt(compiledMethodsOffset).
|
|
129 |
// Add the offset to dependent methods data in the .metaspace.offsets section.
|
|
130 |
appendInt(dependentMethodsOffset).
|
|
131 |
// Add fingerprint.
|
|
132 |
appendLong(fingerprint);
|
|
133 |
// @formatter:on
|
|
134 |
}
|
|
135 |
|
|
136 |
private void verify() {
|
|
137 |
assert gotIndex > 0 : "incorrect gotIndex: " + gotIndex + " for klass: " + name;
|
|
138 |
assert isArray || fingerprint != 0 : "incorrect fingerprint: " + fingerprint + " for klass: " + name;
|
|
139 |
assert compiledMethodsOffset >= -1 : "incorrect compiledMethodsOffset: " + compiledMethodsOffset + " for klass: " + name;
|
|
140 |
assert dependentMethodsOffset >= -1 : "incorrect dependentMethodsOffset: " + dependentMethodsOffset + " for klass: " + name;
|
|
141 |
assert classId >= 0 : "incorrect classId: " + classId + " for klass: " + name;
|
|
142 |
}
|
|
143 |
|
|
144 |
}
|
|
145 |
|
|
146 |
private final HotSpotResolvedObjectType resolvedJavaType;
|
|
147 |
|
|
148 |
/**
|
|
149 |
* List of all collected class data.
|
|
150 |
*/
|
|
151 |
private static Map<String, AOTKlassData> klassData = new HashMap<>();
|
|
152 |
|
|
153 |
/**
|
|
154 |
* List of all methods to be compiled.
|
|
155 |
*/
|
|
156 |
private ArrayList<ResolvedJavaMethod> methods = new ArrayList<>();
|
|
157 |
|
|
158 |
/**
|
|
159 |
* List of all compiled class methods.
|
|
160 |
*/
|
|
161 |
private ArrayList<CompiledMethodInfo> compiledMethods;
|
|
162 |
|
|
163 |
/**
|
|
164 |
* If this class represents Graal stub code.
|
|
165 |
*/
|
|
166 |
private final boolean representsStubs;
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Classes count used to generate unique global method id.
|
|
170 |
*/
|
|
171 |
private static int classesCount = 0;
|
|
172 |
|
|
173 |
/**
|
|
174 |
* Construct an object with compiled methods. Intended to be used for code with no corresponding
|
|
175 |
* Java method name in the user application.
|
|
176 |
*
|
|
177 |
* @param compiledMethods AOT compiled methods
|
|
178 |
*/
|
|
179 |
public AOTCompiledClass(ArrayList<CompiledMethodInfo> compiledMethods) {
|
|
180 |
this.resolvedJavaType = null;
|
|
181 |
this.compiledMethods = compiledMethods;
|
|
182 |
this.representsStubs = true;
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Construct an object with compiled versions of the named class.
|
|
187 |
*/
|
|
188 |
public AOTCompiledClass(ResolvedJavaType resolvedJavaType) {
|
|
189 |
this.resolvedJavaType = (HotSpotResolvedObjectType) resolvedJavaType;
|
|
190 |
this.compiledMethods = new ArrayList<>();
|
|
191 |
this.representsStubs = false;
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* @return the ResolvedJavaType of this class
|
|
196 |
*/
|
|
197 |
public ResolvedJavaType getResolvedJavaType() {
|
|
198 |
return resolvedJavaType;
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Get the list of methods which should be compiled.
|
|
203 |
*/
|
|
204 |
public ArrayList<ResolvedJavaMethod> getMethods() {
|
|
205 |
ArrayList<ResolvedJavaMethod> m = methods;
|
|
206 |
methods = null; // Free - it is not used after that.
|
|
207 |
return m;
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* Get the number of all AOT classes.
|
|
212 |
*/
|
|
213 |
public static int getClassesCount() {
|
|
214 |
return classesCount;
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* Get the number of methods which should be compiled.
|
|
219 |
*
|
|
220 |
* @return number of methods which should be compiled
|
|
221 |
*/
|
|
222 |
public int getMethodCount() {
|
|
223 |
return methods.size();
|
|
224 |
}
|
|
225 |
|
|
226 |
/**
|
|
227 |
* Add a method to the list of methods to be compiled.
|
|
228 |
*/
|
|
229 |
public void addMethod(ResolvedJavaMethod method) {
|
|
230 |
methods.add(method);
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* Returns if this class has methods which should be compiled.
|
|
235 |
*
|
|
236 |
* @return true if this class contains methods which should be compiled, false otherwise
|
|
237 |
*/
|
|
238 |
public boolean hasMethods() {
|
|
239 |
return !methods.isEmpty();
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Add a method to the list of compiled methods. This method needs to be thread-safe.
|
|
244 |
*/
|
|
245 |
public synchronized boolean addCompiledMethod(CompiledMethodInfo cm) {
|
|
246 |
return compiledMethods.add(cm);
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Return the array list of compiled class methods.
|
|
251 |
*
|
|
252 |
* @return array list of compiled methods
|
|
253 |
*/
|
|
254 |
public ArrayList<CompiledMethodInfo> getCompiledMethods() {
|
|
255 |
return compiledMethods;
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Returns if this class has successfully compiled methods.
|
|
260 |
*
|
|
261 |
* @return true if methods were compiled, false otherwise
|
|
262 |
*/
|
|
263 |
public boolean hasCompiledMethods() {
|
|
264 |
return !compiledMethods.isEmpty();
|
|
265 |
}
|
|
266 |
|
|
267 |
/**
|
|
268 |
* Add a klass data.
|
|
269 |
*/
|
|
270 |
public synchronized static AOTKlassData addAOTKlassData(BinaryContainer binaryContainer, HotSpotResolvedObjectType type) {
|
|
271 |
String name = type.getName();
|
|
272 |
long fingerprint = type.getFingerprint();
|
|
273 |
AOTKlassData data = klassData.get(name);
|
|
274 |
if (data != null) {
|
|
275 |
assert data.getFingerprint() == fingerprint : "incorrect fingerprint data for klass: " + name;
|
|
276 |
} else {
|
|
277 |
data = new AOTKlassData(binaryContainer, name, fingerprint, classesCount++);
|
|
278 |
klassData.put(name, data);
|
|
279 |
}
|
|
280 |
return data;
|
|
281 |
}
|
|
282 |
|
|
283 |
public synchronized static AOTKlassData getAOTKlassData(String name) {
|
|
284 |
return klassData.get(name);
|
|
285 |
}
|
|
286 |
|
|
287 |
public synchronized static AOTKlassData getAOTKlassData(HotSpotResolvedObjectType type) {
|
|
288 |
return getAOTKlassData(type.getName());
|
|
289 |
}
|
|
290 |
|
|
291 |
public void addAOTKlassData(BinaryContainer binaryContainer) {
|
|
292 |
for (CompiledMethodInfo methodInfo : compiledMethods) {
|
|
293 |
// Record methods holder
|
|
294 |
methodInfo.addDependentKlassData(binaryContainer, resolvedJavaType);
|
|
295 |
// Record inlinee classes
|
43435
|
296 |
ResolvedJavaMethod[] inlinees = methodInfo.getCompilationResult().getMethods();
|
|
297 |
if (inlinees != null) {
|
|
298 |
for (ResolvedJavaMethod m : inlinees) {
|
|
299 |
methodInfo.addDependentKlassData(binaryContainer, (HotSpotResolvedObjectType) m.getDeclaringClass());
|
|
300 |
}
|
42650
|
301 |
}
|
|
302 |
// Record classes of fields that were accessed
|
43435
|
303 |
ResolvedJavaField[] fields = methodInfo.getCompilationResult().getFields();
|
|
304 |
if (fields != null) {
|
|
305 |
for (ResolvedJavaField f : fields) {
|
|
306 |
methodInfo.addDependentKlassData(binaryContainer, (HotSpotResolvedObjectType) f.getDeclaringClass());
|
|
307 |
}
|
42650
|
308 |
}
|
|
309 |
}
|
|
310 |
}
|
|
311 |
|
|
312 |
public synchronized static AOTKlassData addFingerprintKlassData(BinaryContainer binaryContainer, HotSpotResolvedObjectType type) {
|
|
313 |
if (type.isArray()) {
|
|
314 |
return addAOTKlassData(binaryContainer, type);
|
|
315 |
}
|
|
316 |
assert type.getFingerprint() != 0 : "no fingerprint for " + type.getName();
|
|
317 |
AOTKlassData old = getAOTKlassData(type);
|
|
318 |
if (old != null) {
|
|
319 |
boolean assertsEnabled = false;
|
|
320 |
assert assertsEnabled = true;
|
|
321 |
if (assertsEnabled) {
|
|
322 |
HotSpotResolvedObjectType s = type.getSuperclass();
|
|
323 |
if (s != null) {
|
|
324 |
assert getAOTKlassData(s) != null : "fingerprint super " + s.getName() + " needed for " + type.getName();
|
|
325 |
}
|
|
326 |
for (HotSpotResolvedObjectType i : type.getInterfaces()) {
|
|
327 |
assert getAOTKlassData(i) != null : "fingerprint super " + i.getName() + " needed for " + type.getName();
|
|
328 |
}
|
|
329 |
}
|
|
330 |
return old;
|
|
331 |
}
|
|
332 |
|
|
333 |
// Fingerprinting requires super classes and super interfaces
|
|
334 |
HotSpotResolvedObjectType s = type.getSuperclass();
|
|
335 |
if (s != null) {
|
|
336 |
addFingerprintKlassData(binaryContainer, s);
|
|
337 |
}
|
|
338 |
for (HotSpotResolvedObjectType i : type.getInterfaces()) {
|
|
339 |
addFingerprintKlassData(binaryContainer, i);
|
|
340 |
}
|
|
341 |
|
|
342 |
return addAOTKlassData(binaryContainer, type);
|
|
343 |
}
|
|
344 |
|
|
345 |
/*
|
|
346 |
* Put methods data to contained.
|
|
347 |
*/
|
|
348 |
public void putMethodsData(BinaryContainer binaryContainer) {
|
|
349 |
ReadOnlyDataContainer container = binaryContainer.getMethodsOffsetsContainer();
|
|
350 |
int cntMethods = compiledMethods.size();
|
|
351 |
int startMethods = binaryContainer.addMethodsCount(cntMethods, container);
|
|
352 |
for (CompiledMethodInfo methodInfo : compiledMethods) {
|
|
353 |
methodInfo.addMethodOffsets(binaryContainer, container);
|
|
354 |
}
|
|
355 |
String name = resolvedJavaType.getName();
|
|
356 |
AOTKlassData data = klassData.get(name);
|
|
357 |
assert data != null : "missing data for klass: " + name;
|
|
358 |
assert data.getFingerprint() == resolvedJavaType.getFingerprint() : "incorrect fingerprint for klass: " + name;
|
|
359 |
int cntDepMethods = data.dependentMethods.size();
|
|
360 |
assert cntDepMethods > 0 : "no dependent methods for compiled klass: " + name;
|
|
361 |
data.setCompiledMethodsOffset(startMethods);
|
|
362 |
}
|
|
363 |
|
|
364 |
public static void putAOTKlassData(BinaryContainer binaryContainer) {
|
|
365 |
ReadOnlyDataContainer container = binaryContainer.getKlassesOffsetsContainer();
|
|
366 |
for (AOTKlassData data : klassData.values()) {
|
|
367 |
data.putAOTKlassData(binaryContainer, container);
|
|
368 |
}
|
|
369 |
}
|
|
370 |
|
|
371 |
public boolean representsStubs() {
|
|
372 |
return representsStubs;
|
|
373 |
}
|
|
374 |
|
|
375 |
public void clear() {
|
|
376 |
for (CompiledMethodInfo c : compiledMethods) {
|
|
377 |
c.clear();
|
|
378 |
}
|
|
379 |
this.compiledMethods = null;
|
|
380 |
this.methods = null;
|
|
381 |
}
|
|
382 |
|
|
383 |
}
|