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