src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotConstantReflectionProvider.java

Print this page




  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 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale;
  27 
  28 import java.lang.reflect.Array;
  29 
  30 import jdk.vm.ci.meta.Constant;
  31 import jdk.vm.ci.meta.ConstantReflectionProvider;
  32 import jdk.vm.ci.meta.JavaConstant;
  33 import jdk.vm.ci.meta.JavaField;
  34 import jdk.vm.ci.meta.JavaKind;
  35 import jdk.vm.ci.meta.JavaType;
  36 import jdk.vm.ci.meta.MemoryAccessProvider;
  37 import jdk.vm.ci.meta.MethodHandleAccessProvider;

  38 import jdk.vm.ci.meta.ResolvedJavaType;
  39 import jdk.vm.ci.options.Option;
  40 import jdk.vm.ci.options.OptionType;
  41 import jdk.vm.ci.options.OptionValue;
  42 import jdk.vm.ci.options.StableOptionValue;
  43 
  44 /**
  45  * HotSpot implementation of {@link ConstantReflectionProvider}.
  46  */
  47 public class HotSpotConstantReflectionProvider implements ConstantReflectionProvider, HotSpotProxified {
  48 
  49     static class Options {
  50         //@formatter:off
  51         @Option(help = "Constant fold final fields with default values.", type = OptionType.Debug)
  52         public static final OptionValue<Boolean> TrustFinalDefaultFields = new OptionValue<>(true);
  53         //@formatter:on
  54     }
  55 
  56     protected final HotSpotJVMCIRuntimeProvider runtime;
  57     protected final HotSpotMethodHandleAccessProvider methodHandleAccess;
  58     protected final HotSpotMemoryAccessProviderImpl memoryAccess;
  59 
  60     public HotSpotConstantReflectionProvider(HotSpotJVMCIRuntimeProvider runtime) {
  61         this.runtime = runtime;
  62         this.methodHandleAccess = new HotSpotMethodHandleAccessProvider(this);
  63         this.memoryAccess = new HotSpotMemoryAccessProviderImpl(runtime);
  64     }
  65 
  66     public MethodHandleAccessProvider getMethodHandleAccess() {
  67         return methodHandleAccess;
  68     }
  69 
  70     @Override
  71     public MemoryAccessProvider getMemoryAccessProvider() {
  72         return memoryAccess;
  73     }
  74 


 222     public ResolvedJavaType asJavaType(Constant constant) {
 223         if (constant instanceof HotSpotObjectConstant) {
 224             Object obj = ((HotSpotObjectConstantImpl) constant).object();
 225             if (obj instanceof Class) {
 226                 return runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType((Class<?>) obj);
 227             }
 228         }
 229         if (constant instanceof HotSpotMetaspaceConstant) {
 230             MetaspaceWrapperObject obj = HotSpotMetaspaceConstantImpl.getMetaspaceObject(constant);
 231             if (obj instanceof HotSpotResolvedObjectTypeImpl) {
 232                 return (ResolvedJavaType) obj;
 233             }
 234         }
 235         return null;
 236     }
 237 
 238     private static final String SystemClassName = "Ljava/lang/System;";
 239 
 240     /**
 241      * Determines if a static field is constant for the purpose of
 242      * {@link #readConstantFieldValue(JavaField, JavaConstant)}.
 243      */
 244     protected boolean isStaticFieldConstant(HotSpotResolvedJavaField staticField) {
 245         if (staticField.isFinal() || staticField.isStable()) {
 246             ResolvedJavaType holder = staticField.getDeclaringClass();
 247             if (holder.isInitialized() && !holder.getName().equals(SystemClassName)) {
 248                 return true;
 249             }
 250         }
 251         return false;
 252     }
 253 
 254     /**
 255      * Determines if a value read from a {@code final} instance field is considered constant. The
 256      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 257      * not the {@link JavaConstant#isDefaultForKind default value} for its kind or if
 258      * {@link Options#TrustFinalDefaultFields} is true.
 259      *
 260      * @param value a value read from a {@code final} instance field
 261      * @param receiverClass the {@link Object#getClass() class} of object from which the
 262      *            {@code value} was read
 263      */
 264     protected boolean isFinalInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 265         return !value.isDefaultForKind() || Options.TrustFinalDefaultFields.getValue();
 266     }
 267 
 268     /**
 269      * Determines if a value read from a {@link Stable} instance field is considered constant. The
 270      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 271      * not the {@link JavaConstant#isDefaultForKind default value} for its kind.
 272      *
 273      * @param value a value read from a {@link Stable} field
 274      * @param receiverClass the {@link Object#getClass() class} of object from which the
 275      *            {@code value} was read
 276      */
 277     protected boolean isStableInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 278         return !value.isDefaultForKind();
 279     }
 280 
 281     /**
 282      * {@inheritDoc}
 283      * <p>
 284      * The {@code value} field in {@link OptionValue} is considered constant if the type of
 285      * {@code receiver} is (assignable to) {@link StableOptionValue}.
 286      */
 287     public JavaConstant readConstantFieldValue(JavaField field, JavaConstant receiver) {
 288         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 289 
 290         if (hotspotField.isStatic()) {
 291             if (isStaticFieldConstant(hotspotField)) {
 292                 JavaConstant value = readFieldValue(field, receiver);
 293                 if (hotspotField.isFinal() || !value.isDefaultForKind()) {
 294                     return value;
 295                 }
 296             }
 297         } else {
 298             /*
 299              * for non-static final fields, we must assume that they are only initialized if they
 300              * have a non-default value.
 301              */
 302             Object object = receiver.isNull() ? null : ((HotSpotObjectConstantImpl) receiver).object();
 303 
 304             // Canonicalization may attempt to process an unsafe read before
 305             // processing a guard (e.g. a null check or a type check) for this read
 306             // so we need to check the object being read
 307             if (object != null) {
 308                 if (hotspotField.isFinal()) {
 309                     if (hotspotField.isInObject(object)) {
 310                         JavaConstant value = readFieldValue(field, receiver);
 311                         if (isFinalInstanceFieldValueConstant(value, object.getClass())) {
 312                             return value;
 313                         }
 314                     }
 315                 } else if (hotspotField.isStable()) {
 316                     if (hotspotField.isInObject(object)) {
 317                         JavaConstant value = readFieldValue(field, receiver);
 318                         if (isStableInstanceFieldValueConstant(value, object.getClass())) {
 319                             return value;
 320                         }
 321                     }
 322                 } else {
 323                     Class<?> clazz = object.getClass();
 324                     if (StableOptionValue.class.isAssignableFrom(clazz)) {
 325                         if (hotspotField.isInObject(object) && hotspotField.getName().equals("value")) {
 326                             StableOptionValue<?> option = (StableOptionValue<?>) object;
 327                             return HotSpotObjectConstantImpl.forObject(option.getValue());
 328                         }
 329                     }
 330                 }
 331             }
 332         }
 333         return null;
 334     }
 335 
 336     public JavaConstant readFieldValue(JavaField field, JavaConstant receiver) {
 337         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 338         if (!hotspotField.isStable()) {
 339             return readNonStableFieldValue(field, receiver);
 340         } else {
 341             return readStableFieldValue(field, receiver, hotspotField.isDefaultStable());
 342         }
 343     }
 344 
 345     private JavaConstant readNonStableFieldValue(JavaField field, JavaConstant receiver) {
 346         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 347         if (hotspotField.isStatic()) {
 348             HotSpotResolvedJavaType holder = (HotSpotResolvedJavaType) hotspotField.getDeclaringClass();
 349             if (holder.isInitialized()) {
 350                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), HotSpotObjectConstantImpl.forObject(holder.mirror()), hotspotField.offset());
 351             }
 352         } else {
 353             if (receiver.isNonNull() && hotspotField.isInObject(((HotSpotObjectConstantImpl) receiver).object())) {
 354                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), receiver, hotspotField.offset());
 355             }
 356         }
 357         return null;
 358     }
 359 
 360     public JavaConstant readStableFieldValue(JavaField field, JavaConstant receiver, boolean isDefaultStable) {
 361         JavaConstant fieldValue = readNonStableFieldValue(field, receiver);
 362         if (fieldValue.isNonNull()) {
 363             JavaType declaredType = field.getType();
 364             if (declaredType.getComponentType() != null) {
 365                 int stableDimension = getArrayDimension(declaredType);
 366                 return HotSpotObjectConstantImpl.forStableArray(((HotSpotObjectConstantImpl) fieldValue).object(), stableDimension, isDefaultStable);
 367             }
 368         }
 369         return fieldValue;
 370     }
 371 
 372     private static int getArrayDimension(JavaType type) {
 373         int dimensions = 0;
 374         JavaType componentType = type;
 375         while ((componentType = componentType.getComponentType()) != null) {
 376             dimensions++;
 377         }
 378         return dimensions;
 379     }
 380 }


  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 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale;
  27 
  28 import java.lang.reflect.Array;
  29 
  30 import jdk.vm.ci.meta.Constant;
  31 import jdk.vm.ci.meta.ConstantReflectionProvider;
  32 import jdk.vm.ci.meta.JavaConstant;

  33 import jdk.vm.ci.meta.JavaKind;
  34 import jdk.vm.ci.meta.JavaType;
  35 import jdk.vm.ci.meta.MemoryAccessProvider;
  36 import jdk.vm.ci.meta.MethodHandleAccessProvider;
  37 import jdk.vm.ci.meta.ResolvedJavaField;
  38 import jdk.vm.ci.meta.ResolvedJavaType;




  39 
  40 /**
  41  * HotSpot implementation of {@link ConstantReflectionProvider}.
  42  */
  43 public class HotSpotConstantReflectionProvider implements ConstantReflectionProvider, HotSpotProxified {
  44 
  45     private static final String TrustFinalDefaultFieldsProperty = "jvmci.TrustFinalDefaultFields";
  46 
  47     /**
  48      * Determines whether to treat {@code final} fields with default values as constant.
  49      */
  50     private static final boolean TrustFinalDefaultFields = HotSpotJVMCIRuntime.getBooleanProperty(TrustFinalDefaultFieldsProperty, true);
  51 
  52     protected final HotSpotJVMCIRuntimeProvider runtime;
  53     protected final HotSpotMethodHandleAccessProvider methodHandleAccess;
  54     protected final HotSpotMemoryAccessProviderImpl memoryAccess;
  55 
  56     public HotSpotConstantReflectionProvider(HotSpotJVMCIRuntimeProvider runtime) {
  57         this.runtime = runtime;
  58         this.methodHandleAccess = new HotSpotMethodHandleAccessProvider(this);
  59         this.memoryAccess = new HotSpotMemoryAccessProviderImpl(runtime);
  60     }
  61 
  62     public MethodHandleAccessProvider getMethodHandleAccess() {
  63         return methodHandleAccess;
  64     }
  65 
  66     @Override
  67     public MemoryAccessProvider getMemoryAccessProvider() {
  68         return memoryAccess;
  69     }
  70 


 218     public ResolvedJavaType asJavaType(Constant constant) {
 219         if (constant instanceof HotSpotObjectConstant) {
 220             Object obj = ((HotSpotObjectConstantImpl) constant).object();
 221             if (obj instanceof Class) {
 222                 return runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType((Class<?>) obj);
 223             }
 224         }
 225         if (constant instanceof HotSpotMetaspaceConstant) {
 226             MetaspaceWrapperObject obj = HotSpotMetaspaceConstantImpl.getMetaspaceObject(constant);
 227             if (obj instanceof HotSpotResolvedObjectTypeImpl) {
 228                 return (ResolvedJavaType) obj;
 229             }
 230         }
 231         return null;
 232     }
 233 
 234     private static final String SystemClassName = "Ljava/lang/System;";
 235 
 236     /**
 237      * Determines if a static field is constant for the purpose of
 238      * {@link #readConstantFieldValue(ResolvedJavaField, JavaConstant)}.
 239      */
 240     protected boolean isStaticFieldConstant(HotSpotResolvedJavaField staticField) {
 241         if (staticField.isFinal() || staticField.isStable()) {
 242             ResolvedJavaType holder = staticField.getDeclaringClass();
 243             if (holder.isInitialized() && !holder.getName().equals(SystemClassName)) {
 244                 return true;
 245             }
 246         }
 247         return false;
 248     }
 249 
 250     /**
 251      * Determines if a value read from a {@code final} instance field is considered constant. The
 252      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 253      * not the {@link JavaConstant#isDefaultForKind default value} for its kind or if
 254      * {@link #TrustFinalDefaultFields} is true.
 255      *
 256      * @param value a value read from a {@code final} instance field
 257      * @param receiverClass the {@link Object#getClass() class} of object from which the
 258      *            {@code value} was read
 259      */
 260     protected boolean isFinalInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 261         return !value.isDefaultForKind() || TrustFinalDefaultFields;
 262     }
 263 
 264     /**
 265      * Determines if a value read from a {@link Stable} instance field is considered constant. The
 266      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 267      * not the {@link JavaConstant#isDefaultForKind default value} for its kind.
 268      *
 269      * @param value a value read from a {@link Stable} field
 270      * @param receiverClass the {@link Object#getClass() class} of object from which the
 271      *            {@code value} was read
 272      */
 273     protected boolean isStableInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 274         return !value.isDefaultForKind();
 275     }
 276 
 277     public JavaConstant readConstantFieldValue(ResolvedJavaField field, JavaConstant receiver) {






 278         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 279 
 280         if (hotspotField.isStatic()) {
 281             if (isStaticFieldConstant(hotspotField)) {
 282                 JavaConstant value = readFieldValue(field, receiver);
 283                 if (hotspotField.isFinal() || !value.isDefaultForKind()) {
 284                     return value;
 285                 }
 286             }
 287         } else {
 288             /*
 289              * for non-static final fields, we must assume that they are only initialized if they
 290              * have a non-default value.
 291              */
 292             Object object = receiver.isNull() ? null : ((HotSpotObjectConstantImpl) receiver).object();
 293 
 294             // Canonicalization may attempt to process an unsafe read before
 295             // processing a guard (e.g. a null check or a type check) for this read
 296             // so we need to check the object being read
 297             if (object != null) {
 298                 if (hotspotField.isFinal()) {
 299                     if (hotspotField.isInObject(object)) {
 300                         JavaConstant value = readFieldValue(field, receiver);
 301                         if (isFinalInstanceFieldValueConstant(value, object.getClass())) {
 302                             return value;
 303                         }
 304                     }
 305                 } else if (hotspotField.isStable()) {
 306                     if (hotspotField.isInObject(object)) {
 307                         JavaConstant value = readFieldValue(field, receiver);
 308                         if (isStableInstanceFieldValueConstant(value, object.getClass())) {
 309                             return value;
 310                         }
 311                     }








 312                 }
 313             }
 314         }
 315         return null;
 316     }
 317 
 318     public JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 319         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 320         if (!hotspotField.isStable()) {
 321             return readNonStableFieldValue(field, receiver);
 322         } else {
 323             return readStableFieldValue(field, receiver, hotspotField.isDefaultStable());
 324         }
 325     }
 326 
 327     private JavaConstant readNonStableFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 328         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 329         if (hotspotField.isStatic()) {
 330             HotSpotResolvedJavaType holder = (HotSpotResolvedJavaType) hotspotField.getDeclaringClass();
 331             if (holder.isInitialized()) {
 332                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), HotSpotObjectConstantImpl.forObject(holder.mirror()), hotspotField.offset());
 333             }
 334         } else {
 335             if (receiver.isNonNull() && hotspotField.isInObject(((HotSpotObjectConstantImpl) receiver).object())) {
 336                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), receiver, hotspotField.offset());
 337             }
 338         }
 339         return null;
 340     }
 341 
 342     public JavaConstant readStableFieldValue(ResolvedJavaField field, JavaConstant receiver, boolean isDefaultStable) {
 343         JavaConstant fieldValue = readNonStableFieldValue(field, receiver);
 344         if (fieldValue.isNonNull()) {
 345             JavaType declaredType = field.getType();
 346             if (declaredType.getComponentType() != null) {
 347                 int stableDimension = getArrayDimension(declaredType);
 348                 return HotSpotObjectConstantImpl.forStableArray(((HotSpotObjectConstantImpl) fieldValue).object(), stableDimension, isDefaultStable);
 349             }
 350         }
 351         return fieldValue;
 352     }
 353 
 354     private static int getArrayDimension(JavaType type) {
 355         int dimensions = 0;
 356         JavaType componentType = type;
 357         while ((componentType = componentType.getComponentType()) != null) {
 358             dimensions++;
 359         }
 360         return dimensions;
 361     }
 362 }