Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand Down Expand Up @@ -113,6 +114,13 @@ enum Kind {
* @since 22.3
*/
FieldOffset,
/**
* The static field base Object as it would be computed by
* {@link sun.misc.Unsafe#staticFieldBase(Field)}.
*
* @since 23.0
*/
StaticFieldBase,
/**
* The int or long field is set to the offset of the first array element of the array class
* {@link #declClass}, as it would be computed by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
//Checkstyle: stop

import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.AtomicFieldUpdaterOffset;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.FieldOffset;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.Reset;

import java.lang.ref.ReferenceQueue;
Expand Down Expand Up @@ -341,7 +340,6 @@ public static int getCommonPoolParallelism() {
private static Unsafe U;

@Alias @TargetElement(onlyWith = JDK19OrLater.class) //
@RecomputeFieldValue(kind = FieldOffset, name = "poolIds") //
private static long POOLIDS;

@Substitute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.AtomicFieldUpdaterOffset;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.FieldOffset;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.StaticFieldBase;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.TranslateFieldOffset;
import static com.oracle.svm.core.util.VMError.guarantee;
import static com.oracle.svm.core.util.VMError.shouldNotReachHere;
Expand All @@ -48,6 +49,7 @@
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
import com.oracle.graal.pointsto.util.GraalAccess;
import com.oracle.svm.core.BuildPhaseProvider;
import com.oracle.svm.core.StaticFieldsSupport;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.fieldvaluetransformer.FieldValueTransformerWithAvailability;
Expand Down Expand Up @@ -133,11 +135,11 @@ public ComputedValueField(ResolvedJavaField original, ResolvedJavaField annotate
constantValue = JavaConstant.defaultForKind(getJavaKind());
break;
case FieldOffset:
try {
f = targetClass.getDeclaredField(targetName);
} catch (NoSuchFieldException e) {
throw shouldNotReachHere("could not find target field " + targetClass.getName() + "." + targetName + " for alias " + annotated.format("%H.%n"));
}
f = getField(annotated, targetClass, targetName);
break;
case StaticFieldBase:
f = getField(annotated, targetClass, targetName);
UserError.guarantee(Modifier.isStatic(f.getModifiers()), "Target field must be static for %s computation of %s", StaticFieldBase, fieldFormat());
break;
case Custom:
if (initialTransformer != null) {
Expand All @@ -154,15 +156,24 @@ public ComputedValueField(ResolvedJavaField original, ResolvedJavaField annotate
}
}
boolean isOffsetField = isOffsetRecomputation(kind);
boolean isStaticFieldBase = kind == StaticFieldBase;
guarantee(!isFinal || !isOffsetField);
this.isValueAvailableBeforeAnalysis = customValueAvailableBeforeAnalysis && !isOffsetField;
this.isValueAvailableOnlyAfterAnalysis = customValueAvailableOnlyAfterAnalysis || isOffsetField;
this.isValueAvailableBeforeAnalysis = customValueAvailableBeforeAnalysis && !isOffsetField && !isStaticFieldBase;
this.isValueAvailableOnlyAfterAnalysis = customValueAvailableOnlyAfterAnalysis || isOffsetField || isStaticFieldBase;
this.isValueAvailableOnlyAfterCompilation = customValueAvailableOnlyAfterCompilation;
this.targetField = f;
this.fieldValueTransformer = transformer;
this.valueCache = EconomicMap.create();
}

private static Field getField(ResolvedJavaField annotated, Class<?> targetClass, String targetName) {
try {
return targetClass.getDeclaredField(targetName);
} catch (NoSuchFieldException e) {
throw UserError.abort("Could not find target field " + targetClass.getName() + "." + targetName + " for alias " + annotated.format("%H.%n"));
}
}

public static boolean isOffsetRecomputation(RecomputeFieldValue.Kind kind) {
return offsetComputationKinds.contains(kind);
}
Expand Down Expand Up @@ -282,6 +293,10 @@ public JavaConstant readValue(MetaAccessProvider metaAccess, JavaConstant receiv
case ArrayIndexShift:
constantValue = asConstant(ConfigurationValues.getObjectLayout().getArrayIndexShift(JavaKind.fromJavaClass(targetClass.getComponentType())));
return constantValue;
case StaticFieldBase:
Object staticFieldsArray = targetField.getType().isPrimitive() ? StaticFieldsSupport.getStaticPrimitiveFields() : StaticFieldsSupport.getStaticObjectFields();
constantValue = GraalAccess.getOriginalSnippetReflection().forObject(staticFieldsArray);
return constantValue;
}

ReadLock readLock = valueCacheLock.readLock();
Expand Down Expand Up @@ -342,9 +357,9 @@ private JavaConstant computeValue(MetaAccessProvider metaAccess, JavaConstant re
originalValue = fetchOriginalValue(metaAccess, receiver, originalSnippetReflection);
Object newValue = fieldValueTransformer.transform(receiverValue, originalValue);
checkValue(newValue);
result = originalSnippetReflection.forBoxed(annotated.getJavaKind(), newValue);
result = originalSnippetReflection.forBoxed(original.getJavaKind(), newValue);

assert result.getJavaKind() == annotated.getJavaKind();
assert result.getJavaKind() == original.getJavaKind();
break;
default:
throw shouldNotReachHere("Field recomputation of kind " + kind + " for " + fieldFormat() + " not yet supported");
Expand Down
Loading