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