HotSpotSnippetReflectionProvider.java revision 12651:6ef01bd40ce2
1224135Sdim/* 2224135Sdim * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3224135Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4224135Sdim * 5224135Sdim * This code is free software; you can redistribute it and/or modify it 6224135Sdim * under the terms of the GNU General Public License version 2 only, as 7224135Sdim * published by the Free Software Foundation. 8224135Sdim * 9224135Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10224135Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11224135Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12224135Sdim * version 2 for more details (a copy is included in the LICENSE file that 13224135Sdim * accompanied this code). 14224135Sdim * 15224135Sdim * You should have received a copy of the GNU General Public License version 16224135Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17224135Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18224135Sdim * 19224135Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20224135Sdim * or visit www.oracle.com if you need additional information or have any 21224135Sdim * questions. 22224135Sdim */ 23224135Sdimpackage org.graalvm.compiler.hotspot.meta; 24224135Sdim 25224135Sdimimport org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 26224135Sdimimport org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 27224135Sdimimport org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 28224135Sdimimport org.graalvm.compiler.word.WordTypes; 29224135Sdim 30224135Sdimimport jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; 31224135Sdimimport jdk.vm.ci.hotspot.HotSpotObjectConstant; 32224135Sdimimport jdk.vm.ci.meta.JavaConstant; 33224135Sdimimport jdk.vm.ci.meta.JavaKind; 34224135Sdimimport jdk.vm.ci.meta.ResolvedJavaType; 35224135Sdim 36224135Sdimpublic class HotSpotSnippetReflectionProvider implements SnippetReflectionProvider { 37224135Sdim 38224135Sdim private final HotSpotGraalRuntimeProvider runtime; 39224135Sdim private final HotSpotConstantReflectionProvider constantReflection; 40224135Sdim private final WordTypes wordTypes; 41224135Sdim 42224135Sdim public HotSpotSnippetReflectionProvider(HotSpotGraalRuntimeProvider runtime, HotSpotConstantReflectionProvider constantReflection, WordTypes wordTypes) { 43224135Sdim this.runtime = runtime; 44263508Sdim this.constantReflection = constantReflection; 45224135Sdim this.wordTypes = wordTypes; 46224135Sdim } 47224135Sdim 48224135Sdim @Override 49224135Sdim public JavaConstant forObject(Object object) { 50224135Sdim return constantReflection.forObject(object); 51224135Sdim } 52224135Sdim 53224135Sdim @Override 54224135Sdim public Object asObject(ResolvedJavaType type, JavaConstant constant) { 55224135Sdim if (constant.isNull()) { 56224135Sdim return null; 57224135Sdim } 58224135Sdim HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant; 59224135Sdim return hsConstant.asObject(type); 60224135Sdim } 61224135Sdim 62224135Sdim @Override 63224135Sdim public <T> T asObject(Class<T> type, JavaConstant constant) { 64224135Sdim if (constant.isNull()) { 65224135Sdim return null; 66224135Sdim } 67224135Sdim HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant; 68224135Sdim return hsConstant.asObject(type); 69224135Sdim } 70224135Sdim 71224135Sdim @Override 72224135Sdim public JavaConstant forBoxed(JavaKind kind, Object value) { 73224135Sdim if (kind == JavaKind.Object) { 74224135Sdim return forObject(value); 75224135Sdim } else { 76224135Sdim return JavaConstant.forBoxedPrimitive(value); 77224135Sdim } 78224135Sdim } 79224135Sdim 80224135Sdim // Lazily initialized 81224135Sdim private Class<?> wordTypesType; 82224135Sdim private Class<?> runtimeType; 83224135Sdim private Class<?> configType; 84224135Sdim 85224135Sdim @Override 86224135Sdim public <T> T getInjectedNodeIntrinsicParameter(Class<T> type) { 87224135Sdim // Need to test all fields since there no guarantee under the JMM 88224135Sdim // about the order in which these fields are written. 89224135Sdim GraalHotSpotVMConfig config = runtime.getVMConfig(); 90224135Sdim if (configType == null || wordTypesType == null || configType == null) { 91224135Sdim wordTypesType = wordTypes.getClass(); 92224135Sdim runtimeType = runtime.getClass(); 93224135Sdim configType = config.getClass(); 94224135Sdim } 95224135Sdim 96224135Sdim if (type.isAssignableFrom(wordTypesType)) { 97224135Sdim return type.cast(wordTypes); 98224135Sdim } 99224135Sdim if (type.isAssignableFrom(runtimeType)) { 100224135Sdim return type.cast(runtime); 101224135Sdim } 102224135Sdim if (type.isAssignableFrom(configType)) { 103224135Sdim return type.cast(config); 104224135Sdim } 105224135Sdim return null; 106224135Sdim } 107224135Sdim} 108224135Sdim