1 /* 2 * Copyright (c) 2014, 2018, 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 24 25 package org.graalvm.compiler.hotspot.meta; 26 27 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; 28 29 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 30 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 31 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 32 import org.graalvm.compiler.word.WordTypes; 33 34 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; 35 import jdk.vm.ci.hotspot.HotSpotObjectConstant; 36 import jdk.vm.ci.meta.JavaConstant; 37 import jdk.vm.ci.meta.JavaKind; 38 import jdk.vm.ci.meta.ResolvedJavaType; 39 40 public class HotSpotSnippetReflectionProvider implements SnippetReflectionProvider { 41 42 private final HotSpotGraalRuntimeProvider runtime; 43 private final HotSpotConstantReflectionProvider constantReflection; 44 private final WordTypes wordTypes; 45 46 public HotSpotSnippetReflectionProvider(HotSpotGraalRuntimeProvider runtime, HotSpotConstantReflectionProvider constantReflection, WordTypes wordTypes) { 47 this.runtime = runtime; 48 this.constantReflection = constantReflection; 49 this.wordTypes = wordTypes; 50 } 51 52 @Override 53 public JavaConstant forObject(Object object) { 54 return constantReflection.forObject(object); 55 } 56 57 @Override 58 public <T> T asObject(Class<T> type, JavaConstant constant) { 59 if (constant.isNull()) { 60 return null; 61 } 62 if (constant instanceof HotSpotObjectConstant) { 63 HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant; 64 return hsConstant.asObject(type); 65 } 66 return null; 67 } 68 69 @Override 70 public JavaConstant forBoxed(JavaKind kind, Object value) { 71 if (kind == JavaKind.Object) { 72 return forObject(value); 73 } else { 74 return JavaConstant.forBoxedPrimitive(value); 75 } 76 } 77 78 // Lazily initialized 79 private Class<?> wordTypesType; 80 private Class<?> runtimeType; 81 private Class<?> configType; 82 83 @Override 84 public <T> T getInjectedNodeIntrinsicParameter(Class<T> type) { 85 // Need to test all fields since there no guarantee under the JMM 86 // about the order in which these fields are written. 87 GraalHotSpotVMConfig config = runtime.getVMConfig(); 88 if (configType == null || wordTypesType == null || runtimeType == null) { 89 wordTypesType = wordTypes.getClass(); 90 runtimeType = runtime.getClass(); 91 configType = config.getClass(); 92 } 93 94 if (type.isAssignableFrom(wordTypesType)) { 95 return type.cast(wordTypes); 96 } 97 if (type.isAssignableFrom(runtimeType)) { 98 return type.cast(runtime); 99 } 100 if (type.isAssignableFrom(configType)) { 101 return type.cast(config); 102 } 103 return null; 104 } 105 106 @Override 107 public Class<?> originalClass(ResolvedJavaType type) { 108 return runtime().getMirror(type); 109 } 110 }