-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SubstrateMethod.java
490 lines (409 loc) 路 16.6 KB
/
SubstrateMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.graal.meta;
import static com.oracle.svm.core.util.VMError.intentionallyUnimplemented;
import static com.oracle.svm.core.util.VMError.shouldNotReachHere;
import static com.oracle.svm.core.util.VMError.shouldNotReachHereAtRuntime;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import com.oracle.graal.pointsto.meta.AnalysisMethod;
import com.oracle.svm.core.BuildPhaseProvider.AfterHeapLayout;
import com.oracle.svm.core.BuildPhaseProvider.ReadyForCompilation;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.code.CodeInfo;
import com.oracle.svm.core.code.ImageCodeInfo;
import com.oracle.svm.core.deopt.Deoptimizer;
import com.oracle.svm.core.graal.code.ExplicitCallingConvention;
import com.oracle.svm.core.graal.code.StubCallingConvention;
import com.oracle.svm.core.graal.code.SubstrateCallingConventionKind;
import com.oracle.svm.core.graal.meta.SharedRuntimeMethod;
import com.oracle.svm.core.graal.phases.SubstrateSafepointInsertionPhase;
import com.oracle.svm.core.heap.UnknownObjectField;
import com.oracle.svm.core.heap.UnknownPrimitiveField;
import com.oracle.svm.core.snippets.SubstrateForeignCallTarget;
import com.oracle.svm.core.util.HostedStringDeduplication;
import com.oracle.svm.core.util.VMError;
import jdk.graal.compiler.api.replacements.Snippet;
import jdk.graal.compiler.core.common.util.TypeConversion;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.DefaultProfilingInfo;
import jdk.vm.ci.meta.ExceptionHandler;
import jdk.vm.ci.meta.LineNumberTable;
import jdk.vm.ci.meta.LocalVariableTable;
import jdk.vm.ci.meta.ProfilingInfo;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.Signature;
import jdk.vm.ci.meta.SpeculationLog;
import jdk.vm.ci.meta.TriState;
public class SubstrateMethod implements SharedRuntimeMethod {
private static final int FLAG_BIT_BRIDGE = 0;
private static final int FLAG_BIT_NEVER_INLINE = 1;
private static final int FLAG_BIT_UNINTERRUPTIBLE = 2;
private static final int FLAG_BIT_NEEDS_SAFEPOINT_CHECK = 3;
private static final int FLAG_BIT_ENTRY_POINT = 4;
private static final int FLAG_BIT_SNIPPET = 5;
private static final int FLAG_BIT_FOREIGN_CALL_TARGET = 6;
private static final int FLAG_BIT_CALLING_CONVENTION_KIND = 7;
private static final int NUM_BITS_CALLING_CONVENTION_KIND = 2;
private static final int FLAG_BIT_CALLEE_SAVED_REGISTERS = 9;
private final int flags;
private final byte[] encodedLineNumberTable;
private final int modifiers;
private final String name;
private final int hashCode;
private SubstrateType declaringClass;
private int encodedGraphStartOffset;
@UnknownPrimitiveField(availability = AfterHeapLayout.class) private int vTableIndex;
/**
* A metadata object describing the image code that contains the compiled code of this method.
* This is not a {@link CodeInfo} structure because those are not available until runtime.
*/
private final ImageCodeInfo imageCodeInfo;
/**
* The offset of the first instruction of the compiled code in the image code described by
* {@link #imageCodeInfo}. Used to compute the destination address in a direct call.
*/
@UnknownPrimitiveField(availability = AfterHeapLayout.class) private int imageCodeOffset;
/**
* The offset of the deoptimization target code in the image code described by
* {@link #imageCodeInfo}. Used to compute the destination address for deoptimization. This is
* only != 0 if there actually is a deoptimization target method in the image for this method.
*/
@UnknownPrimitiveField(availability = AfterHeapLayout.class) private int imageCodeDeoptOffset;
@UnknownObjectField(types = {SubstrateMethod[].class, SubstrateMethod.class}, canBeNull = true, availability = ReadyForCompilation.class)//
protected Object implementations;
private SubstrateSignature signature;
@Platforms(Platform.HOSTED_ONLY.class)
public SubstrateMethod(AnalysisMethod original, ImageCodeInfo codeInfo, HostedStringDeduplication stringTable) {
imageCodeInfo = codeInfo;
encodedLineNumberTable = EncodedLineNumberTable.encode(original.getLineNumberTable());
assert original.getAnnotation(CEntryPoint.class) == null : "Can't compile entry point method";
modifiers = original.getModifiers();
name = stringTable.deduplicate(original.getName(), true);
/*
* AnalysisMethods of snippets are stored in a hash map of SubstrateReplacements. The
* GraalObjectReplacer replaces them with SubstrateMethods. Therefore we have to preserve
* the hashCode of the original AnalysisMethod. Note that this is only required because it
* is a replaced object. For not replaced objects the hash code is preserved automatically
* in a synthetic hash-code field (see NativeImageHeap.ObjectInfo.identityHashCode).
*/
hashCode = original.hashCode();
encodedGraphStartOffset = -1;
SubstrateCallingConventionKind callingConventionKind = ExplicitCallingConvention.Util.getCallingConventionKind(original, original.isEntryPoint());
flags = makeFlag(original.isBridge(), FLAG_BIT_BRIDGE) |
makeFlag(original.hasNeverInlineDirective(), FLAG_BIT_NEVER_INLINE) |
makeFlag(Uninterruptible.Utils.isUninterruptible(original), FLAG_BIT_UNINTERRUPTIBLE) |
makeFlag(SubstrateSafepointInsertionPhase.needSafepointCheck(original), FLAG_BIT_NEEDS_SAFEPOINT_CHECK) |
makeFlag(original.isEntryPoint(), FLAG_BIT_ENTRY_POINT) |
makeFlag(original.isAnnotationPresent(Snippet.class), FLAG_BIT_SNIPPET) |
makeFlag(original.isAnnotationPresent(SubstrateForeignCallTarget.class), FLAG_BIT_FOREIGN_CALL_TARGET) |
makeFlag(callingConventionKind.ordinal(), FLAG_BIT_CALLING_CONVENTION_KIND, NUM_BITS_CALLING_CONVENTION_KIND) |
makeFlag(StubCallingConvention.Utils.hasStubCallingConvention(original), FLAG_BIT_CALLEE_SAVED_REGISTERS);
}
private static int makeFlag(boolean value, int flagBit) {
return value ? 1 << flagBit : 0;
}
private static int makeFlag(int value, int flagBit, int numBits) {
VMError.guarantee(value >= 0 && value < (1 << numBits), "flag value out of range");
return value << flagBit;
}
private boolean getFlag(int flagBit) {
return (flags & (1 << flagBit)) != 0;
}
private int getFlag(int flagBit, int numBits) {
return (flags >> flagBit) & ((1 << numBits) - 1);
}
public byte[] getEncodedLineNumberTable() {
return encodedLineNumberTable;
}
/**
* Returns the hashCode of the original AnalysisMethod.
*/
@Override
public int hashCode() {
return hashCode;
}
public void setLinks(SubstrateSignature signature, SubstrateType declaringClass) {
this.signature = signature;
this.declaringClass = declaringClass;
}
public void setImplementations(SubstrateMethod[] rawImplementations) {
if (rawImplementations.length == 0) {
implementations = null;
} else if (rawImplementations.length == 1) {
implementations = rawImplementations[0];
} else {
implementations = rawImplementations;
}
}
@Platforms(Platform.HOSTED_ONLY.class)
public Object getRawImplementations() {
return implementations;
}
public void setSubstrateData(int vTableIndex, int imageCodeOffset, int imageCodeDeoptOffset) {
this.vTableIndex = vTableIndex;
this.imageCodeOffset = imageCodeOffset;
this.imageCodeDeoptOffset = imageCodeDeoptOffset;
}
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public ImageCodeInfo getImageCodeInfo() {
return imageCodeInfo;
}
@Override
public boolean hasImageCodeOffset() {
return imageCodeOffset != 0;
}
@Override
public int getImageCodeOffset() {
assert imageCodeOffset != 0;
return imageCodeOffset;
}
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getImageCodeDeoptOffset() {
return imageCodeDeoptOffset;
}
@Override
public boolean forceIndirectCall() {
return false;
}
@Override
public int getEncodedGraphStartOffset() {
return encodedGraphStartOffset;
}
public void setEncodedGraphStartOffset(long encodedGraphStartOffset) {
this.encodedGraphStartOffset = TypeConversion.asS4(encodedGraphStartOffset);
}
@Override
public SubstrateCallingConventionKind getCallingConventionKind() {
return SubstrateCallingConventionKind.values()[getFlag(FLAG_BIT_CALLING_CONVENTION_KIND, NUM_BITS_CALLING_CONVENTION_KIND)];
}
@Override
public boolean hasCalleeSavedRegisters() {
return getFlag(FLAG_BIT_CALLEE_SAVED_REGISTERS);
}
@Override
public SubstrateMethod[] getImplementations() {
if (implementations == null) {
return new SubstrateMethod[0];
} else if (implementations instanceof SubstrateMethod) {
return new SubstrateMethod[]{(SubstrateMethod) implementations};
} else {
return (SubstrateMethod[]) implementations;
}
}
@Override
public boolean isDeoptTarget() {
return false;
}
@Override
public boolean canDeoptimize() {
return true;
}
@Override
public boolean isUninterruptible() {
return getFlag(FLAG_BIT_UNINTERRUPTIBLE);
}
@Override
public boolean needSafepointCheck() {
return getFlag(FLAG_BIT_NEEDS_SAFEPOINT_CHECK);
}
@Override
public boolean isEntryPoint() {
return getFlag(FLAG_BIT_ENTRY_POINT);
}
@Override
public boolean isSnippet() {
return getFlag(FLAG_BIT_SNIPPET);
}
@Override
public boolean isForeignCallTarget() {
return getFlag(FLAG_BIT_FOREIGN_CALL_TARGET);
}
@Override
public int getVTableIndex() {
if (vTableIndex < 0) {
throw shouldNotReachHere("no vtable index");
}
return vTableIndex;
}
@Override
public Deoptimizer.StubType getDeoptStubType() {
return Deoptimizer.StubType.NoDeoptStub;
}
@Override
public String getName() {
return name;
}
@Override
public Signature getSignature() {
return signature;
}
@Override
public byte[] getCode() {
return null;
}
@Override
public int getCodeSize() {
return 0;
}
@Override
public SubstrateType getDeclaringClass() {
return declaringClass;
}
@Override
public int getMaxLocals() {
return getSignature().getParameterCount(!Modifier.isStatic(getModifiers())) * 2;
}
@Override
public int getMaxStackSize() {
// A dummy number for now.
return 2;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public boolean isClassInitializer() {
assert !("<clinit>".equals(name) && isStatic()) : "class initializers are executed during native image generation and are never in the native image";
return false;
}
@Override
public boolean isConstructor() {
return "<init>".equals(name) && !isStatic();
}
@Override
public boolean canBeStaticallyBound() {
/*
* If the method has only a single implementation we have to return true. This let's a
* virtual call be canonicalized to a special call. This is not just an optimization but a
* requirement, because such methods don't get a vtable index assigned in the
* UniverseBuilder.
*/
return this.equals(implementations);
}
@Override
public ExceptionHandler[] getExceptionHandlers() {
throw shouldNotReachHereAtRuntime(); // ExcludeFromJacocoGeneratedReport
}
@Override
public StackTraceElement asStackTraceElement(int bci) {
int lineNumber = EncodedLineNumberTable.getLineNumber(bci, encodedLineNumberTable);
return new StackTraceElement(getDeclaringClass().toClassName(), getName(), getDeclaringClass().getSourceFileName(), lineNumber);
}
@Override
public ProfilingInfo getProfilingInfo(boolean includeNormal, boolean includeOSR) {
return DefaultProfilingInfo.get(TriState.UNKNOWN);
}
@Override
public void reprofile() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public ConstantPool getConstantPool() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public Annotation[] getAnnotations() {
throw VMError.unimplemented("Annotations are not available for JIT compilation at image run time");
}
@Override
public Annotation[] getDeclaredAnnotations() {
throw VMError.unimplemented("Annotations are not available for JIT compilation at image run time");
}
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
throw VMError.unimplemented("Annotations are not available for JIT compilation at image run time");
}
@Override
public Annotation[][] getParameterAnnotations() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public Type[] getGenericParameterTypes() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public boolean canBeInlined() {
return !hasNeverInlineDirective();
}
@Override
public boolean hasNeverInlineDirective() {
// If there is no graph in the image, then the method must never be considered
// for inlining (because any attempt to inline it would fail).
return getFlag(FLAG_BIT_NEVER_INLINE) || encodedGraphStartOffset < 0;
}
@Override
public boolean shouldBeInlined() {
return false;
}
@Override
public LineNumberTable getLineNumberTable() {
return EncodedLineNumberTable.decode(encodedLineNumberTable);
}
@Override
public LocalVariableTable getLocalVariableTable() {
return null;
}
@Override
public Constant getEncoding() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public boolean isInVirtualMethodTable(ResolvedJavaType resolved) {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public boolean isSynthetic() {
return false;
}
@Override
public boolean isVarArgs() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public boolean isBridge() {
return getFlag(FLAG_BIT_BRIDGE);
}
@Override
public boolean isDefault() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public SpeculationLog getSpeculationLog() {
throw intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
@Override
public String toString() {
return "SubstrateMethod<" + format("%h.%n") + ">";
}
}