# HG changeset patch # User Roland Schatz # Date 1462884940 -7200 # Node ID f102ee538647d48c081ae0e3c6e071679c9ff4cc # Parent 0226d6bcb0d29de678b7b7c09aaf07a71ad6a96a Remove stable constant handling from ConstantReflectionProvider (JDK-8156552). diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotConstantReflectionProvider.java --- a/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotConstantReflectionProvider.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotConstantReflectionProvider.java Tue May 10 14:55:40 2016 +0200 @@ -22,18 +22,13 @@ */ package jdk.vm.ci.hotspot; -import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset; -import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale; - import java.lang.reflect.Array; import jdk.vm.ci.common.JVMCIError; -import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option; import jdk.vm.ci.meta.Constant; import jdk.vm.ci.meta.ConstantReflectionProvider; import jdk.vm.ci.meta.JavaConstant; import jdk.vm.ci.meta.JavaKind; -import jdk.vm.ci.meta.JavaType; import jdk.vm.ci.meta.MemoryAccessProvider; import jdk.vm.ci.meta.MethodHandleAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; @@ -87,50 +82,6 @@ return Array.getLength(arrayObject); } - public JavaConstant readConstantArrayElement(JavaConstant array, int index) { - if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) { - JavaConstant element = readArrayElement(array, index); - if (element != null && (((HotSpotObjectConstantImpl) array).isDefaultStable() || !element.isDefaultForKind())) { - return element; - } - } - return null; - } - - /** - * Try to convert {@code offset} into an an index into {@code array}. - * - * @return the computed index or -1 if the offset isn't within the array - */ - private int indexForOffset(JavaConstant array, long offset) { - if (array.getJavaKind() != JavaKind.Object || array.isNull()) { - return -1; - } - Class componentType = ((HotSpotObjectConstantImpl) array).object().getClass().getComponentType(); - JavaKind kind = runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType(componentType).getJavaKind(); - int arraybase = getArrayBaseOffset(kind); - int scale = getArrayIndexScale(kind); - if (offset < arraybase) { - return -1; - } - long index = offset - arraybase; - if (index % scale != 0) { - return -1; - } - long result = index / scale; - if (result >= Integer.MAX_VALUE) { - return -1; - } - return (int) result; - } - - public JavaConstant readConstantArrayElementForOffset(JavaConstant array, long offset) { - if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) { - return readConstantArrayElement(array, indexForOffset(array, offset)); - } - return null; - } - @Override public JavaConstant readArrayElement(JavaConstant array, int index) { if (array.getJavaKind() != JavaKind.Object || array.isNull()) { @@ -144,11 +95,7 @@ if (a instanceof Object[]) { Object element = ((Object[]) a)[index]; - if (((HotSpotObjectConstantImpl) array).getStableDimension() > 1) { - return HotSpotObjectConstantImpl.forStableArray(element, ((HotSpotObjectConstantImpl) array).getStableDimension() - 1, ((HotSpotObjectConstantImpl) array).isDefaultStable()); - } else { - return HotSpotObjectConstantImpl.forObject(element); - } + return HotSpotObjectConstantImpl.forObject(element); } else { return JavaConstant.forBoxedPrimitive(Array.get(a, index)); } @@ -226,103 +173,8 @@ return null; } - private static final String SystemClassName = "Ljava/lang/System;"; - - /** - * Determines if a static field is constant for the purpose of - * {@link #readConstantFieldValue(ResolvedJavaField, JavaConstant)}. - */ - protected boolean isStaticFieldConstant(HotSpotResolvedJavaField staticField) { - if (staticField.isFinal() || (staticField.isStable() && runtime.getConfig().foldStableValues)) { - ResolvedJavaType holder = staticField.getDeclaringClass(); - if (holder.isInitialized() && !holder.getName().equals(SystemClassName)) { - return true; - } - } - return false; - } - - /** - * Determines if a value read from a {@code final} instance field is considered constant. The - * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is - * not the {@link JavaConstant#isDefaultForKind default value} for its kind or if - * {@link Option#TrustFinalDefaultFields} is true. - * - * @param value a value read from a {@code final} instance field - * @param receiverClass the {@link Object#getClass() class} of object from which the - * {@code value} was read - */ - protected boolean isFinalInstanceFieldValueConstant(JavaConstant value, Class receiverClass) { - return !value.isDefaultForKind() || Option.TrustFinalDefaultFields.getBoolean(); - } - - /** - * Determines if a value read from a {@link Stable} instance field is considered constant. The - * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is - * not the {@link JavaConstant#isDefaultForKind default value} for its kind. - * - * @param value a value read from a {@link Stable} field - * @param receiverClass the {@link Object#getClass() class} of object from which the - * {@code value} was read - */ - protected boolean isStableInstanceFieldValueConstant(JavaConstant value, Class receiverClass) { - return !value.isDefaultForKind(); - } - - public JavaConstant readConstantFieldValue(ResolvedJavaField field, JavaConstant receiver) { - HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; - - if (hotspotField.isStatic()) { - if (isStaticFieldConstant(hotspotField)) { - JavaConstant value = readFieldValue(field, receiver); - if (hotspotField.isFinal() || !value.isDefaultForKind()) { - return value; - } - } - } else { - /* - * for non-static final fields, we must assume that they are only initialized if they - * have a non-default value. - */ - Object object = receiver.isNull() ? null : ((HotSpotObjectConstantImpl) receiver).object(); - - // Canonicalization may attempt to process an unsafe read before - // processing a guard (e.g. a null check or a type check) for this read - // so we need to check the object being read - if (object != null) { - if (hotspotField.isFinal()) { - if (hotspotField.isInObject(object)) { - JavaConstant value = readFieldValue(field, receiver); - if (isFinalInstanceFieldValueConstant(value, object.getClass())) { - return value; - } - } - } else if (hotspotField.isStable() && runtime.getConfig().foldStableValues) { - if (hotspotField.isInObject(object)) { - JavaConstant value = readFieldValue(field, receiver); - if (isStableInstanceFieldValueConstant(value, object.getClass())) { - return value; - } - } - } - } - } - return null; - } - public JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver) { HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; - if (!hotspotField.isStable()) { - return readNonStableFieldValue(field, receiver); - } else if (runtime.getConfig().foldStableValues) { - return readStableFieldValue(field, receiver, hotspotField.isDefaultStable()); - } else { - return null; - } - } - - private JavaConstant readNonStableFieldValue(ResolvedJavaField field, JavaConstant receiver) { - HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; if (hotspotField.isStatic()) { HotSpotResolvedJavaType holder = (HotSpotResolvedJavaType) hotspotField.getDeclaringClass(); if (holder.isInitialized()) { @@ -336,27 +188,6 @@ return null; } - public JavaConstant readStableFieldValue(ResolvedJavaField field, JavaConstant receiver, boolean isDefaultStable) { - JavaConstant fieldValue = readNonStableFieldValue(field, receiver); - if (fieldValue != null && fieldValue.isNonNull()) { - JavaType declaredType = field.getType(); - if (declaredType.getComponentType() != null) { - int stableDimension = getArrayDimension(declaredType); - return HotSpotObjectConstantImpl.forStableArray(((HotSpotObjectConstantImpl) fieldValue).object(), stableDimension, isDefaultStable); - } - } - return fieldValue; - } - - private static int getArrayDimension(JavaType type) { - int dimensions = 0; - JavaType componentType = type; - while ((componentType = componentType.getComponentType()) != null) { - dimensions++; - } - return dimensions; - } - @Override public JavaConstant asJavaClass(ResolvedJavaType type) { return HotSpotObjectConstantImpl.forObject(((HotSpotResolvedJavaType) type).mirror()); diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java --- a/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java Tue May 10 14:55:40 2016 +0200 @@ -101,8 +101,7 @@ PrintConfig(boolean.class, false, "Prints all HotSpotVMConfig fields."), PrintFlags(boolean.class, false, "Prints all JVMCI flags and exits."), ShowFlags(boolean.class, false, "Prints all JVMCI flags and continues."), - TraceMethodDataFilter(String.class, null, ""), - TrustFinalDefaultFields(boolean.class, true, "Determines whether to treat final fields with default values as constant."); + TraceMethodDataFilter(String.class, null, ""); /** * The prefix for system properties that are JVMCI options. diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotObjectConstantImpl.java --- a/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotObjectConstantImpl.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotObjectConstantImpl.java Tue May 10 14:55:40 2016 +0200 @@ -53,15 +53,6 @@ } } - static JavaConstant forStableArray(Object object, int stableDimension, boolean isDefaultStable) { - if (object == null) { - return JavaConstant.NULL_POINTER; - } else { - assert object.getClass().isArray(); - return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable); - } - } - public static JavaConstant forBoxedValue(JavaKind kind, Object value) { if (kind == JavaKind.Object) { return HotSpotObjectConstantImpl.forObject(value); @@ -82,22 +73,11 @@ private final Object object; private final boolean compressed; - private final byte stableDimension; - private final boolean isDefaultStable; - private HotSpotObjectConstantImpl(Object object, boolean compressed, int stableDimension, boolean isDefaultStable) { + private HotSpotObjectConstantImpl(Object object, boolean compressed) { this.object = object; this.compressed = compressed; - this.stableDimension = (byte) stableDimension; - this.isDefaultStable = isDefaultStable; assert object != null; - assert stableDimension == 0 || (object != null && object.getClass().isArray()); - assert stableDimension >= 0 && stableDimension <= 255; - assert !isDefaultStable || stableDimension > 0; - } - - private HotSpotObjectConstantImpl(Object object, boolean compressed) { - this(object, compressed, 0, false); } @Override @@ -118,12 +98,12 @@ public JavaConstant compress() { assert !compressed; - return new HotSpotObjectConstantImpl(object, true, stableDimension, isDefaultStable); + return new HotSpotObjectConstantImpl(object, true); } public JavaConstant uncompress() { assert compressed; - return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable); + return new HotSpotObjectConstantImpl(object, false); } public HotSpotResolvedObjectType getType() { @@ -248,7 +228,7 @@ return true; } else if (o instanceof HotSpotObjectConstantImpl) { HotSpotObjectConstantImpl other = (HotSpotObjectConstantImpl) o; - return object == other.object && compressed == other.compressed && stableDimension == other.stableDimension && isDefaultStable == other.isDefaultStable; + return object == other.object && compressed == other.compressed; } return false; } @@ -266,19 +246,4 @@ public String toString() { return (compressed ? "NarrowOop" : getJavaKind().getJavaName()) + "[" + JavaKind.Object.format(object) + "]"; } - - /** - * Number of stable dimensions if this constant is a stable array. - */ - public int getStableDimension() { - return stableDimension & 0xff; - } - - /** - * Returns {@code true} if this is a stable array constant and its elements should be considered - * as stable regardless of whether they are default values. - */ - public boolean isDefaultStable() { - return isDefaultStable; - } } diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.meta/src/jdk/vm/ci/meta/ConstantReflectionProvider.java --- a/jvmci/jdk.vm.ci.meta/src/jdk/vm/ci/meta/ConstantReflectionProvider.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.meta/src/jdk/vm/ci/meta/ConstantReflectionProvider.java Tue May 10 14:55:40 2016 +0200 @@ -56,39 +56,10 @@ JavaConstant readArrayElement(JavaConstant array, int index); /** - * Reads a value from the given array at the given index if it is a stable array. Returns - * {@code null} if the constant is not a stable array, if it is a default value, if the index is - * out of bounds, or if the value is not available at this point. - */ - JavaConstant readConstantArrayElement(JavaConstant array, int index); - - /** - * Reads a value from the given array at the given offset if it is a stable array. The offset - * will be decoded relative to the platform addressing into an index into the array. Returns - * {@code null} if the constant is not a stable array, if it is a default value, if the offset - * is out of bounds, or if the value is not available at this point. - */ - JavaConstant readConstantArrayElementForOffset(JavaConstant array, long offset); - - /** - * Gets the constant value of this field. Note that a {@code static final} field may not be - * considered constant if its declaring class is not yet initialized or if it is a well known - * field that can be updated via other means (e.g., {@link System#setOut(java.io.PrintStream)}). - * - * @param receiver object from which this field's value is to be read. This value is ignored if - * this field is static. - * @return the constant value of this field or {@code null} if this field is not considered - * constant by the runtime - */ - JavaConstant readConstantFieldValue(ResolvedJavaField field, JavaConstant receiver); - - /** * Gets the current value of this field for a given object, if available. * * There is no guarantee that the same value will be returned by this method for a field unless - * the field is considered to be - * {@linkplain #readConstantFieldValue(ResolvedJavaField, JavaConstant) constant} by the - * runtime. + * the field is considered to be constant by the runtime. * * @param receiver object from which this field's value is to be read. This value is ignored if * this field is static. @@ -98,23 +69,6 @@ JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver); /** - * Gets the current value of this field for a given object, if available. Like - * {@link #readFieldValue(ResolvedJavaField, JavaConstant)} but treats array fields as stable. - * - * There is no guarantee that the same value will be returned by this method for a field unless - * the field is considered to be - * {@linkplain #readConstantFieldValue(ResolvedJavaField, JavaConstant) constant} by the - * runtime. - * - * @param receiver object from which this field's value is to be read. This value is ignored if - * this field is static. - * @param isDefaultStable if {@code true}, default values are considered stable - * @return the value of this field or {@code null} if the value is not available (e.g., because - * the field holder is not yet initialized). - */ - JavaConstant readStableFieldValue(ResolvedJavaField field, JavaConstant receiver, boolean isDefaultStable); - - /** * Converts the given {@link JavaKind#isPrimitive() primitive} constant to a boxed * {@link JavaKind#Object object} constant, according to the Java boxing rules. Returns * {@code null} if the source is is not a primitive constant, or the boxed value is not diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaField.java --- a/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaField.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaField.java Tue May 10 14:55:40 2016 +0200 @@ -32,7 +32,6 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.lang.annotation.Annotation; @@ -40,17 +39,15 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; -import jdk.vm.ci.meta.JavaConstant; +import org.junit.Test; + import jdk.vm.ci.meta.LocationIdentity; import jdk.vm.ci.meta.ResolvedJavaField; import jdk.vm.ci.meta.ResolvedJavaMethod; -import org.junit.Test; - /** * Tests for {@link ResolvedJavaField}. */ @@ -106,35 +103,6 @@ } } - static class ReadConstantValueTestConstants { - String stringField = "field"; - final String constantStringField = "constantField"; - - static final Object CONST1 = new ReadConstantValueTestConstants(); - static final Object CONST2 = null; - static final Object CONST3 = new String(); - } - - @Test - public void readConstantValueTest() throws NoSuchFieldException { - ResolvedJavaField field = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("stringField")); - List receiverConstants = readConstants(ReadConstantValueTestConstants.class); - for (ConstantValue receiver : receiverConstants) { - JavaConstant value = constantReflection.readConstantFieldValue(field, receiver.value); - assertNull(value); - } - - ResolvedJavaField constField = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("constantStringField")); - for (ConstantValue receiver : receiverConstants) { - JavaConstant value = constantReflection.readConstantFieldValue(constField, receiver.value); - if (value != null) { - Object expected = "constantField"; - String actual = ((ReadConstantValueTestConstants) receiver.boxed).constantStringField; - assertTrue(actual + " != " + expected, actual == expected); - } - } - } - private Method findTestMethod(Method apiMethod) { String testName = apiMethod.getName() + "Test"; for (Method m : getClass().getDeclaredMethods()) { diff -r 0226d6bcb0d2 -r f102ee538647 jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TypeUniverse.java --- a/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TypeUniverse.java Mon May 02 14:57:11 2016 +0200 +++ b/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TypeUniverse.java Tue May 10 14:55:40 2016 +0200 @@ -182,7 +182,7 @@ if (boxed instanceof JavaConstant) { res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed)); } else { - JavaConstant value = constantReflection.readConstantFieldValue(javaField, null); + JavaConstant value = constantReflection.readFieldValue(javaField, null); if (value != null) { res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed)); if (boxed instanceof Object[]) {