1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 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.hotspot.HotSpotJVMCIRuntime.Option; 31 import jdk.vm.ci.meta.Constant; 32 import jdk.vm.ci.meta.ConstantReflectionProvider; 33 import jdk.vm.ci.meta.JavaConstant; 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.ResolvedJavaField; 39 import jdk.vm.ci.meta.ResolvedJavaType; 40 41 /** 42 * HotSpot implementation of {@link ConstantReflectionProvider}. 43 */ 44 public class HotSpotConstantReflectionProvider implements ConstantReflectionProvider, HotSpotProxified { 45 46 /** 47 * Determines whether to treat {@code final} fields with default values as constant. 48 */ 49 private static final boolean TrustFinalDefaultFields = HotSpotJVMCIRuntime.getBooleanProperty(Option.TrustFinalDefaultFields, true); 50 51 protected final HotSpotJVMCIRuntimeProvider runtime; 52 protected final HotSpotMethodHandleAccessProvider methodHandleAccess; 53 protected final HotSpotMemoryAccessProviderImpl memoryAccess; 54 55 public HotSpotConstantReflectionProvider(HotSpotJVMCIRuntimeProvider runtime) { 56 this.runtime = runtime; 57 this.methodHandleAccess = new HotSpotMethodHandleAccessProvider(this); 58 this.memoryAccess = new HotSpotMemoryAccessProviderImpl(runtime); 59 } 60 61 public MethodHandleAccessProvider getMethodHandleAccess() { 62 return methodHandleAccess; 63 } 64 65 @Override 66 public MemoryAccessProvider getMemoryAccessProvider() { 67 return memoryAccess; 68 } 69 70 @Override 71 public Boolean constantEquals(Constant x, Constant y) { 72 if (x == y) { 73 return true; 74 } else if (x instanceof HotSpotObjectConstantImpl) { 75 return y instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) x).object() == ((HotSpotObjectConstantImpl) y).object(); 76 } else { 77 return x.equals(y); 78 } 79 } 80 81 @Override 82 public Integer readArrayLength(JavaConstant array) { 83 if (array.getJavaKind() != JavaKind.Object || array.isNull()) { 84 return null; 85 } 86 87 Object arrayObject = ((HotSpotObjectConstantImpl) array).object(); 88 if (!arrayObject.getClass().isArray()) { 89 return null; 90 } 91 return Array.getLength(arrayObject); 92 } 93 94 public JavaConstant readConstantArrayElement(JavaConstant array, int index) { 95 if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) { 96 JavaConstant element = readArrayElement(array, index); 97 if (element != null && (((HotSpotObjectConstantImpl) array).isDefaultStable() || !element.isDefaultForKind())) { 98 return element; 99 } 100 } 101 return null; 102 } 103 104 /** 105 * Try to convert {@code offset} into an an index into {@code array}. 106 * 107 * @return the computed index or -1 if the offset isn't within the array 108 */ 109 private int indexForOffset(JavaConstant array, long offset) { 110 if (array.getJavaKind() != JavaKind.Object || array.isNull()) { 111 return -1; 112 } 113 Class<?> componentType = ((HotSpotObjectConstantImpl) array).object().getClass().getComponentType(); 114 JavaKind kind = runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType(componentType).getJavaKind(); 115 int arraybase = getArrayBaseOffset(kind); 116 int scale = getArrayIndexScale(kind); 117 if (offset < arraybase) { 118 return -1; 119 } 120 long index = offset - arraybase; 121 if (index % scale != 0) { 122 return -1; 123 } 124 long result = index / scale; 125 if (result >= Integer.MAX_VALUE) { 126 return -1; 127 } 128 return (int) result; 129 } 130 131 public JavaConstant readConstantArrayElementForOffset(JavaConstant array, long offset) { 132 if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) { 133 return readConstantArrayElement(array, indexForOffset(array, offset)); 134 } 135 return null; 136 } 137 138 @Override 139 public JavaConstant readArrayElement(JavaConstant array, int index) { 140 if (array.getJavaKind() != JavaKind.Object || array.isNull()) { 141 return null; 142 } 143 Object a = ((HotSpotObjectConstantImpl) array).object(); 144 145 if (index < 0 || index >= Array.getLength(a)) { 146 return null; 147 } 148 149 if (a instanceof Object[]) { 150 Object element = ((Object[]) a)[index]; 151 if (((HotSpotObjectConstantImpl) array).getStableDimension() > 1) { 152 return HotSpotObjectConstantImpl.forStableArray(element, ((HotSpotObjectConstantImpl) array).getStableDimension() - 1, ((HotSpotObjectConstantImpl) array).isDefaultStable()); 153 } else { 154 return HotSpotObjectConstantImpl.forObject(element); 155 } 156 } else { 157 return JavaConstant.forBoxedPrimitive(Array.get(a, index)); 158 } 159 } 160 161 /** 162 * Check if the constant is a boxed value that is guaranteed to be cached by the platform. 163 * Otherwise the generated code might be the only reference to the boxed value and since object 164 * references from nmethods are weak this can cause GC problems. 165 * 166 * @param source 167 * @return true if the box is cached 168 */ 169 private static boolean isBoxCached(JavaConstant source) { 170 switch (source.getJavaKind()) { 171 case Boolean: 172 return true; 173 case Char: 174 return source.asInt() <= 127; 175 case Byte: 176 case Short: 177 case Int: 178 return source.asInt() >= -128 && source.asInt() <= 127; 179 case Long: 180 return source.asLong() >= -128 && source.asLong() <= 127; 181 case Float: 182 case Double: 183 return false; 184 default: 185 throw new IllegalArgumentException("unexpected kind " + source.getJavaKind()); 186 } 187 } 188 189 @Override 190 public JavaConstant boxPrimitive(JavaConstant source) { 191 if (!source.getJavaKind().isPrimitive() || !isBoxCached(source)) { 192 return null; 193 } 194 return HotSpotObjectConstantImpl.forObject(source.asBoxedPrimitive()); 195 } 196 197 @Override 198 public JavaConstant unboxPrimitive(JavaConstant source) { 199 if (!source.getJavaKind().isObject()) { 200 return null; 201 } 202 if (source.isNull()) { 203 return null; 204 } 205 return JavaConstant.forBoxedPrimitive(((HotSpotObjectConstantImpl) source).object()); 206 } 207 208 public JavaConstant forString(String value) { 209 return HotSpotObjectConstantImpl.forObject(value); 210 } 211 212 public JavaConstant forObject(Object value) { 213 return HotSpotObjectConstantImpl.forObject(value); 214 } 215 216 @Override 217 public ResolvedJavaType asJavaType(Constant constant) { 218 if (constant instanceof HotSpotObjectConstant) { 219 Object obj = ((HotSpotObjectConstantImpl) constant).object(); 220 if (obj instanceof Class) { 221 return runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType((Class<?>) obj); 222 } 223 } 224 if (constant instanceof HotSpotMetaspaceConstant) { 225 MetaspaceWrapperObject obj = HotSpotMetaspaceConstantImpl.getMetaspaceObject(constant); 226 if (obj instanceof HotSpotResolvedObjectTypeImpl) { 227 return (ResolvedJavaType) obj; 228 } 229 } 230 return null; 231 } 232 233 private static final String SystemClassName = "Ljava/lang/System;"; 234 235 /** 236 * Determines if a static field is constant for the purpose of 237 * {@link #readConstantFieldValue(ResolvedJavaField, JavaConstant)}. 238 */ 239 protected boolean isStaticFieldConstant(HotSpotResolvedJavaField staticField) { 240 if (staticField.isFinal() || (staticField.isStable() && runtime.getConfig().foldStableValues)) { 241 ResolvedJavaType holder = staticField.getDeclaringClass(); 242 if (holder.isInitialized() && !holder.getName().equals(SystemClassName)) { 243 return true; 244 } 245 } 246 return false; 247 } 248 249 /** 250 * Determines if a value read from a {@code final} instance field is considered constant. The 251 * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is 252 * not the {@link JavaConstant#isDefaultForKind default value} for its kind or if 253 * {@link #TrustFinalDefaultFields} is true. 254 * 255 * @param value a value read from a {@code final} instance field 256 * @param receiverClass the {@link Object#getClass() class} of object from which the 257 * {@code value} was read 258 */ 259 protected boolean isFinalInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) { 260 return !value.isDefaultForKind() || TrustFinalDefaultFields; 261 } 262 263 /** 264 * Determines if a value read from a {@link Stable} instance field is considered constant. The 265 * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is 266 * not the {@link JavaConstant#isDefaultForKind default value} for its kind. 267 * 268 * @param value a value read from a {@link Stable} field 269 * @param receiverClass the {@link Object#getClass() class} of object from which the 270 * {@code value} was read 271 */ 272 protected boolean isStableInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) { 273 return !value.isDefaultForKind(); 274 } 275 276 public JavaConstant readConstantFieldValue(ResolvedJavaField field, JavaConstant receiver) { 277 HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; 278 279 if (hotspotField.isStatic()) { 280 if (isStaticFieldConstant(hotspotField)) { 281 JavaConstant value = readFieldValue(field, receiver); 282 if (hotspotField.isFinal() || !value.isDefaultForKind()) { 283 return value; 284 } 285 } 286 } else { 287 /* 288 * for non-static final fields, we must assume that they are only initialized if they 289 * have a non-default value. 290 */ 291 Object object = receiver.isNull() ? null : ((HotSpotObjectConstantImpl) receiver).object(); 292 293 // Canonicalization may attempt to process an unsafe read before 294 // processing a guard (e.g. a null check or a type check) for this read 295 // so we need to check the object being read 296 if (object != null) { 297 if (hotspotField.isFinal()) { 298 if (hotspotField.isInObject(object)) { 299 JavaConstant value = readFieldValue(field, receiver); 300 if (isFinalInstanceFieldValueConstant(value, object.getClass())) { 301 return value; 302 } 303 } 304 } else if (hotspotField.isStable() && runtime.getConfig().foldStableValues) { 305 if (hotspotField.isInObject(object)) { 306 JavaConstant value = readFieldValue(field, receiver); 307 if (isStableInstanceFieldValueConstant(value, object.getClass())) { 308 return value; 309 } 310 } 311 } 312 } 313 } 314 return null; 315 } 316 317 public JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver) { 318 HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; 319 if (!hotspotField.isStable()) { 320 return readNonStableFieldValue(field, receiver); 321 } else if (runtime.getConfig().foldStableValues) { 322 return readStableFieldValue(field, receiver, hotspotField.isDefaultStable()); 323 } else { 324 return null; 325 } 326 } 327 328 private JavaConstant readNonStableFieldValue(ResolvedJavaField field, JavaConstant receiver) { 329 HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; 330 if (hotspotField.isStatic()) { 331 HotSpotResolvedJavaType holder = (HotSpotResolvedJavaType) hotspotField.getDeclaringClass(); 332 if (holder.isInitialized()) { 333 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), HotSpotObjectConstantImpl.forObject(holder.mirror()), hotspotField.offset()); 334 } 335 } else { 336 if (receiver.isNonNull() && hotspotField.isInObject(((HotSpotObjectConstantImpl) receiver).object())) { 337 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), receiver, hotspotField.offset()); 338 } 339 } 340 return null; 341 } 342 343 public JavaConstant readStableFieldValue(ResolvedJavaField field, JavaConstant receiver, boolean isDefaultStable) { 344 JavaConstant fieldValue = readNonStableFieldValue(field, receiver); 345 if (fieldValue.isNonNull()) { 346 JavaType declaredType = field.getType(); 347 if (declaredType.getComponentType() != null) { 348 int stableDimension = getArrayDimension(declaredType); 349 return HotSpotObjectConstantImpl.forStableArray(((HotSpotObjectConstantImpl) fieldValue).object(), stableDimension, isDefaultStable); 350 } 351 } 352 return fieldValue; 353 } 354 355 private static int getArrayDimension(JavaType type) { 356 int dimensions = 0; 357 JavaType componentType = type; 358 while ((componentType = componentType.getComponentType()) != null) { 359 dimensions++; 360 } 361 return dimensions; 362 } 363 }