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