4646import com .oracle .graal .pointsto .util .AnalysisError ;
4747import com .oracle .svm .core .RuntimeAssertionsSupport ;
4848import com .oracle .svm .core .annotate .InjectAccessors ;
49+ import com .oracle .svm .core .annotate .RecomputeFieldValue ;
4950import com .oracle .svm .core .graal .meta .SharedConstantReflectionProvider ;
5051import com .oracle .svm .core .hub .DynamicHub ;
5152import com .oracle .svm .core .meta .ObjectConstantEquality ;
5455import com .oracle .svm .hosted .SVMHost ;
5556import com .oracle .svm .hosted .classinitialization .ClassInitializationSupport ;
5657import com .oracle .svm .hosted .classinitialization .SimulateClassInitializerSupport ;
58+ import com .oracle .svm .hosted .meta .HostedField ;
59+ import com .oracle .svm .hosted .meta .HostedLookupSnippetReflectionProvider ;
5760import com .oracle .svm .hosted .meta .HostedMetaAccess ;
61+ import com .oracle .svm .hosted .meta .RelocatableConstant ;
5862
5963import jdk .vm .ci .meta .Constant ;
6064import jdk .vm .ci .meta .JavaConstant ;
6165import jdk .vm .ci .meta .JavaKind ;
6266import jdk .vm .ci .meta .MemoryAccessProvider ;
6367import jdk .vm .ci .meta .MethodHandleAccessProvider ;
68+ import jdk .vm .ci .meta .PrimitiveConstant ;
6469import jdk .vm .ci .meta .ResolvedJavaField ;
6570import jdk .vm .ci .meta .ResolvedJavaType ;
6671
6772@ Platforms (Platform .HOSTED_ONLY .class )
6873public class AnalysisConstantReflectionProvider extends SharedConstantReflectionProvider {
6974 private final AnalysisUniverse universe ;
7075 private final UniverseMetaAccess metaAccess ;
76+ private HostedMetaAccess hMetaAccess ;
7177 private final ClassInitializationSupport classInitializationSupport ;
7278 private final AnalysisMethodHandleAccessProvider methodHandleAccess ;
7379 private SimulateClassInitializerSupport simulateClassInitializerSupport ;
@@ -79,6 +85,10 @@ public AnalysisConstantReflectionProvider(AnalysisUniverse universe, UniverseMet
7985 this .methodHandleAccess = new AnalysisMethodHandleAccessProvider (universe );
8086 }
8187
88+ public void setHostedMetaAccess (HostedMetaAccess hMetaAccess ) {
89+ this .hMetaAccess = hMetaAccess ;
90+ }
91+
8292 @ Override
8393 public Boolean constantEquals (Constant x , Constant y ) {
8494 if (x == y ) {
@@ -212,7 +222,7 @@ public JavaConstant readValue(UniverseMetaAccess suppliedMetaAccess, AnalysisFie
212222 }
213223
214224 /** Read the field value and wrap it in a value supplier without performing any replacements. */
215- public ValueSupplier <JavaConstant > readHostedFieldValue (AnalysisField field , HostedMetaAccess hMetaAccess , JavaConstant receiver , boolean returnSimulatedValues ) {
225+ public ValueSupplier <JavaConstant > readHostedFieldValue (AnalysisField field , JavaConstant receiver , boolean returnSimulatedValues ) {
216226 if (returnSimulatedValues ) {
217227 var simulatedValue = readSimulatedValue (field );
218228 if (simulatedValue != null ) {
@@ -233,7 +243,17 @@ public ValueSupplier<JavaConstant> readHostedFieldValue(AnalysisField field, Hos
233243 * during analysis or in a later phase. Attempts to materialize the value before it becomes
234244 * available will result in an error.
235245 */
236- return ValueSupplier .lazyValue (() -> doReadValue (field , receiver , hMetaAccess ), () -> ReadableJavaField .isValueAvailable (field ));
246+ return ValueSupplier .lazyValue (() -> doReadValue (field , receiver ), () -> ReadableJavaField .isValueAvailable (field ));
247+ }
248+
249+ /**
250+ * The {@link HostedMetaAccess} is used to access the {@link HostedField} in the re-computation
251+ * of {@link RecomputeFieldValue.Kind#AtomicFieldUpdaterOffset} and
252+ * {@link RecomputeFieldValue.Kind#TranslateFieldOffset} annotated fields .
253+ */
254+ private JavaConstant doReadValue (AnalysisField field , JavaConstant receiver ) {
255+ Objects .requireNonNull (hMetaAccess );
256+ return doReadValue (field , receiver , hMetaAccess );
237257 }
238258
239259 private JavaConstant doReadValue (AnalysisField field , JavaConstant receiver , UniverseMetaAccess access ) {
@@ -265,7 +285,7 @@ public JavaConstant interceptValue(UniverseMetaAccess suppliedMetaAccess, Analys
265285 result = filterInjectedAccessor (field , result );
266286 result = replaceObject (result );
267287 result = interceptAssertionStatus (field , result );
268- result = interceptWordType (suppliedMetaAccess , field , result );
288+ result = interceptWordField (suppliedMetaAccess , field , result );
269289 }
270290 return result ;
271291 }
@@ -321,23 +341,17 @@ private static JavaConstant interceptAssertionStatus(AnalysisField field, JavaCo
321341 }
322342
323343 /**
324- * Intercept {@link Word} types. They are boxed objects in the hosted world, but primitive
325- * values in the runtime world.
344+ * Intercept {@link Word} fields. {@link Word} values are boxed objects in the hosted world, but
345+ * primitive values in the runtime world, so the default value of {@link Word} fields is 0.
346+ *
347+ * {@link HostedLookupSnippetReflectionProvider} replaces relocatable pointers with
348+ * {@link RelocatableConstant} and regular {@link WordBase} values with
349+ * {@link PrimitiveConstant}. No other {@link WordBase} values can be reachable at this point.
326350 */
327- private JavaConstant interceptWordType (UniverseMetaAccess suppliedMetaAccess , AnalysisField field , JavaConstant value ) {
351+ private JavaConstant interceptWordField (UniverseMetaAccess suppliedMetaAccess , AnalysisField field , JavaConstant value ) {
328352 if (value .getJavaKind () == JavaKind .Object ) {
329- if (universe .hostVM ().isRelocatedPointer (suppliedMetaAccess , value )) {
330- /*
331- * Such pointers are subject to relocation therefore we don't know their values yet.
332- * Therefore there should not be a relocated pointer constant in a function which is
333- * compiled. RelocatedPointers are only allowed in non-constant fields. The caller
334- * of readValue is responsible of handling the returned value correctly.
335- */
336- return value ;
337- } else if (suppliedMetaAccess .isInstanceOf (value , WordBase .class )) {
338- Object originalObject = universe .getSnippetReflection ().asObject (Object .class , value );
339- return JavaConstant .forIntegerKind (universe .getWordKind (), ((WordBase ) originalObject ).rawValue ());
340- } else if (value .isNull () && field .getType ().isWordType ()) {
353+ VMError .guarantee (value instanceof RelocatableConstant || !suppliedMetaAccess .isInstanceOf (value , WordBase .class ));
354+ if (value .isNull () && field .getType ().isWordType ()) {
341355 return JavaConstant .forIntegerKind (universe .getWordKind (), 0 );
342356 }
343357 }
@@ -364,7 +378,7 @@ public AnalysisType asJavaType(Constant constant) {
364378
365379 @ Override
366380 public JavaConstant asJavaClass (ResolvedJavaType type ) {
367- return SubstrateObjectConstant . forObject (getHostVM ().dynamicHub (type ));
381+ return universe . getHeapScanner (). createImageHeapConstant ( super . forObject (getHostVM ().dynamicHub (type )), ObjectScanner . OtherReason . UNKNOWN );
368382 }
369383
370384 @ Override
0 commit comments