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